1 package com.atlassian.plugin.web.descriptors;
2
3 import com.atlassian.plugin.Plugin;
4 import com.atlassian.plugin.PluginParseException;
5 import com.atlassian.plugin.web.Condition;
6 import com.atlassian.plugin.web.conditions.ConditionLoadingException;
7 import junit.framework.TestCase;
8 import org.dom4j.Document;
9 import org.dom4j.DocumentException;
10 import org.dom4j.DocumentHelper;
11 import org.mockito.Mockito;
12
13 public class TestConditionElementParser extends TestCase
14 {
15 private static final String TYPE_OR = "<conditions type=\"OR\">";
16 private static final String TYPE_AND = "<conditions type=\"AND\">";
17 private static final String TYPE_CLOSE = "</conditions>";
18 private static final String FALSE = "<condition class=\"com.atlassian.plugin.web.conditions.NeverDisplayCondition\" />";
19 private static final String TRUE = "<condition class=\"com.atlassian.plugin.web.conditions.AlwaysDisplayCondition\" />";
20 private static final String NOT_FALSE = "<condition class=\"com.atlassian.plugin.web.conditions.NeverDisplayCondition\" invert=\"true\" />";
21 private static final String NOT_TRUE = "<condition class=\"com.atlassian.plugin.web.conditions.AlwaysDisplayCondition\" invert=\"true\" />";
22
23 private final ConditionElementParser conditionElementParser = new ConditionElementParser(new ConditionElementParser.ConditionFactory()
24 {
25 public Condition create(String className, Plugin plugin) throws ConditionLoadingException
26 {
27 return new MockWebFragmentHelper().loadCondition(className, plugin);
28 }
29 });
30
31 public void testSimple() throws DocumentException, PluginParseException
32 {
33 assertConditions(TRUE, true);
34 assertConditions(FALSE, false);
35 assertConditions(NOT_TRUE, false);
36 assertConditions(NOT_FALSE, true);
37 }
38
39 public void testAnd() throws DocumentException, PluginParseException
40 {
41
42 assertConditions(TRUE + TRUE, true);
43 assertConditions(TRUE + FALSE, false);
44 assertConditions(FALSE + TRUE, false);
45 assertConditions(FALSE + FALSE, false);
46
47
48 assertConditions(TYPE_AND + TRUE + TRUE + TYPE_CLOSE, true);
49 assertConditions(TYPE_AND + TRUE + FALSE + TYPE_CLOSE, false);
50 assertConditions(TYPE_AND + FALSE + TRUE + TYPE_CLOSE, false);
51 assertConditions(TYPE_AND + FALSE + FALSE + TYPE_CLOSE, false);
52 }
53
54 public void testOr() throws DocumentException, PluginParseException
55 {
56 assertConditions(TYPE_OR + TRUE + TRUE + TYPE_CLOSE, true);
57 assertConditions(TYPE_OR + TRUE + FALSE + TYPE_CLOSE, true);
58 assertConditions(TYPE_OR + FALSE + TRUE + TYPE_CLOSE, true);
59 assertConditions(TYPE_OR + FALSE + FALSE + TYPE_CLOSE, false);
60 }
61
62
63 public void testNestedAnd() throws DocumentException, PluginParseException
64 {
65
66 assertConditions(TYPE_AND +
67 TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
68 TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
69 TYPE_CLOSE, true);
70
71
72 assertConditions(TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
73 TYPE_AND + TRUE + TRUE + TYPE_CLOSE, true);
74
75 assertConditions(TYPE_AND +
76 TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
77 TYPE_AND + FALSE + TRUE + TYPE_CLOSE +
78 TYPE_CLOSE, false);
79 }
80
81
82 public void testNestedOr() throws DocumentException, PluginParseException
83 {
84
85 assertConditions(TYPE_OR +
86 TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
87 TYPE_OR + FALSE + TRUE + TYPE_CLOSE +
88 TYPE_CLOSE, true);
89
90 assertConditions(TYPE_OR +
91 TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
92 TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
93 TYPE_CLOSE, false);
94 }
95
96 public void testNestedMix() throws DocumentException, PluginParseException
97 {
98
99 assertConditions(TYPE_AND +
100 TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
101 TYPE_OR + FALSE + TRUE + TYPE_CLOSE +
102 TYPE_CLOSE, false);
103
104 assertConditions(TYPE_AND +
105 TYPE_OR + TRUE + FALSE + TYPE_CLOSE +
106 TYPE_OR + FALSE + TRUE + TYPE_CLOSE +
107 TYPE_CLOSE, true);
108
109
110 assertConditions(TYPE_OR +
111 TYPE_AND + FALSE + FALSE + TYPE_CLOSE +
112 TYPE_AND + FALSE + TRUE + TYPE_CLOSE +
113 TYPE_CLOSE, false);
114
115 assertConditions(TYPE_OR +
116 TYPE_AND + FALSE + FALSE + TYPE_CLOSE +
117 TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
118 TYPE_CLOSE, true);
119 }
120
121 public void testComplex() throws DocumentException, PluginParseException
122 {
123 assertConditions(TRUE +
124 TYPE_OR +
125 TYPE_AND +
126 FALSE + FALSE +
127 TYPE_CLOSE +
128 FALSE +
129 TYPE_AND +
130 TRUE +
131 TYPE_OR +
132 FALSE + TRUE +
133 TYPE_CLOSE +
134 TYPE_CLOSE +
135 TYPE_CLOSE, true);
136 }
137
138 public void assertConditions(String conditionElement, boolean expectedResult) throws DocumentException, PluginParseException
139 {
140 String rootElement = "<root>" + conditionElement + "</root>";
141 Document document = DocumentHelper.parseText(rootElement);
142
143 Condition condition = conditionElementParser.makeConditions(Mockito.mock(Plugin.class), document.getRootElement(), AbstractWebFragmentModuleDescriptor.COMPOSITE_TYPE_AND);
144
145 assertEquals(expectedResult, condition.shouldDisplay(null));
146 }
147 }