View Javadoc

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); //true
34          assertConditions(FALSE, false); //false
35          assertConditions(NOT_TRUE, false); //NOT true = false
36          assertConditions(NOT_FALSE, true); //NOT false = true
37      }
38  
39      public void testAnd() throws DocumentException, PluginParseException
40      {
41          //using the condition elements only (by default they are grouped with AND operator)
42          assertConditions(TRUE + TRUE, true);//true AND true = true
43          assertConditions(TRUE + FALSE, false);//true AND false = false
44          assertConditions(FALSE + TRUE, false);//false AND true = false
45          assertConditions(FALSE + FALSE, false);//false AND false = false
46  
47          //using the conditions element and explicitly specifying the AND operator
48          assertConditions(TYPE_AND + TRUE + TRUE + TYPE_CLOSE, true);//true AND true = true
49          assertConditions(TYPE_AND + TRUE + FALSE + TYPE_CLOSE, false);//true AND false = false
50          assertConditions(TYPE_AND + FALSE + TRUE + TYPE_CLOSE, false);//false AND true = false
51          assertConditions(TYPE_AND + FALSE + FALSE + TYPE_CLOSE, false);//false AND false = false
52      }
53  
54      public void testOr() throws DocumentException, PluginParseException
55      {
56          assertConditions(TYPE_OR + TRUE + TRUE + TYPE_CLOSE, true);//true OR true = true
57          assertConditions(TYPE_OR + TRUE + FALSE + TYPE_CLOSE, true);//true OR false = true
58          assertConditions(TYPE_OR + FALSE + TRUE + TYPE_CLOSE, true);//false OR true = true
59          assertConditions(TYPE_OR + FALSE + FALSE + TYPE_CLOSE, false);//false OR false = false
60      }
61  
62      //test nested AND's - normally all nested AND's should be a single AND
63      public void testNestedAnd() throws DocumentException, PluginParseException
64      {
65          //nested AND'ed conditions
66          assertConditions(TYPE_AND +
67                              TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
68                              TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
69                           TYPE_CLOSE, true);//(true AND true) AND (true AND true) = true
70  
71          //same as above but without explicit AND
72          assertConditions(TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
73                           TYPE_AND + TRUE + TRUE + TYPE_CLOSE, true);//(true AND true) AND (true AND true) = true
74  
75          assertConditions(TYPE_AND +
76                              TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
77                              TYPE_AND + FALSE + TRUE + TYPE_CLOSE +
78                           TYPE_CLOSE, false);//(true AND true) AND (false AND true) = false
79      }
80  
81      //test nested OR's - normally all nested OR's should be a single OR
82      public void testNestedOr() throws DocumentException, PluginParseException
83      {
84          //nested OR'ed conditions
85          assertConditions(TYPE_OR +
86                              TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
87                              TYPE_OR + FALSE + TRUE + TYPE_CLOSE +
88                           TYPE_CLOSE, true);//(false OR false) OR (false OR true) = true
89  
90          assertConditions(TYPE_OR +
91                              TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
92                              TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
93                           TYPE_CLOSE, false);//(false OR false) OR (false OR false) = false
94      }
95  
96      public void testNestedMix() throws DocumentException, PluginParseException
97      {
98          //AND with nested OR conditions
99          assertConditions(TYPE_AND +
100                             TYPE_OR + FALSE + FALSE + TYPE_CLOSE +
101                             TYPE_OR + FALSE + TRUE + TYPE_CLOSE +
102                          TYPE_CLOSE, false);//(false OR false) AND (false OR true) = false
103 
104         assertConditions(TYPE_AND +
105                             TYPE_OR + TRUE + FALSE + TYPE_CLOSE +
106                             TYPE_OR + FALSE + TRUE + TYPE_CLOSE +
107                          TYPE_CLOSE, true);//(true OR false) AND (false OR true) = true
108 
109         //OR with nested AND conditions
110         assertConditions(TYPE_OR +
111                             TYPE_AND + FALSE + FALSE + TYPE_CLOSE +
112                             TYPE_AND + FALSE + TRUE + TYPE_CLOSE +
113                          TYPE_CLOSE, false);//(false AND false) OR (false AND true) = false
114 
115         assertConditions(TYPE_OR +
116                             TYPE_AND + FALSE + FALSE + TYPE_CLOSE +
117                             TYPE_AND + TRUE + TRUE + TYPE_CLOSE +
118                          TYPE_CLOSE, true);//(false AND false) OR (true AND true) = 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);//true AND ((false AND false) OR false OR (true AND (false OR true))) = 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 }