View Javadoc

1   /*
2    * $Id: TestValidWhen.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.validator;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.commons.validator.util.ValidatorUtils;
27  import org.apache.struts.validator.validwhen.ValidWhenLexer;
28  import org.apache.struts.validator.validwhen.ValidWhenParser;
29  
30  import java.io.StringReader;
31  
32  /***
33   * Unit tests for the ValidWhen Parser/Lexer.
34   */
35  public class TestValidWhen extends TestCase {
36      /***
37       * All logging goes through this logger
38       */
39      private static Log log = LogFactory.getLog(TestValidWhen.class);
40      protected PojoBean testBean;
41  
42      /***
43       * Defines the testcase name for JUnit.
44       *
45       * @param theName the testcase's name.
46       */
47      public TestValidWhen(String theName) {
48          super(theName);
49      }
50  
51      /***
52       * Start the tests.
53       *
54       * @param theArgs the arguments. Not used
55       */
56      public static void main(String[] theArgs) {
57          junit.awtui.TestRunner.main(new String[] { TestValidWhen.class.getName() });
58      }
59  
60      /***
61       * @return a test suite (<code>TestSuite</code>) that includes all methods
62       *         starting with "test"
63       */
64      public static Test suite() {
65          // All methods starting with "test" will be executed in the test suite.
66          return new TestSuite(TestValidWhen.class);
67      }
68  
69      public void setUp() {
70          testBean = new PojoBean(123, 789);
71          testBean.setStringValue1("ABC");
72          testBean.setStringValue2(null);
73          testBean.setBeans(new PojoBean[] {
74                  new PojoBean(11, 12), new PojoBean(21, 22), new PojoBean(31, 42),
75                  new PojoBean(41, 52), new PojoBean(51, 62)
76              });
77          testBean.setMapped("testKey", "mappedValue");
78      }
79  
80      public void tearDown() {
81          testBean = null;
82      }
83  
84      /***
85       * Test Operators.
86       */
87      public void testProperty() {
88          // *this*
89          doParse("(*this* == 123)", testBean, 0, "intValue1", true);
90  
91          // Named property
92          doParse("(intValue2 == 789)", testBean, 0, "intValue1", true);
93  
94          // Indexed Property
95          doParse("(beans[].intValue1 == 21)", testBean, 1, "intValue1", true);
96          doParse("(beans[1].intValue1 == 21)", testBean, 4, "intValue1", true);
97  
98          // Mapped Property - *** NOT SUPPORTED ***
99          // doParse("(mapped(mappedValue) == 'testKey')", testBean , 0, "mappedValue", true);
100     }
101 
102     /***
103      * Test Operators.
104      */
105     public void testOperators() {
106         // Equal
107         doParse("(*this* == 123)", testBean, 0, "intValue1", true);
108 
109         // Not Equal
110         doParse("(*this* != 456)", testBean, 0, "intValue1", true);
111 
112         // Less Than
113         doParse("(*this* < 456)", testBean, 0, "intValue1", true);
114 
115         // Greater Than
116         doParse("(*this* > 100)", testBean, 0, "intValue1", true);
117 
118         // Less Than Equal
119         doParse("(*this* <= 123)", testBean, 0, "intValue1", true);
120 
121         // Greater Than Equal
122         doParse("(*this* >= 123)", testBean, 0, "intValue1", true);
123     }
124 
125     /***
126      * Test String values.
127      */
128     public void testString() {
129         doParse("(*this* != '--')", "XX", 0, "stringValue1", true);
130         doParse("(*this* == '--')", "--", 0, "stringValue1", true);
131 
132         String testValue = "dsgUOLMdLsdL";
133 
134         // single quote
135         doParse("(*this* == '" + testValue + "')", testValue, 0,
136             "stringValue1", true);
137 
138         // double quote
139         doParse("(*this* == \"" + testValue + "\")", testValue, 0,
140             "stringValue1", true);
141     }
142 
143     /***
144      * Test Numeric values.
145      */
146     public void testNumeric() {
147         // Test Zero
148         PojoBean numberBean = new PojoBean(0, -50);
149 
150         doParse("(intValue1 == 0)", numberBean, 0, "intValue1", true);
151         doParse("(intValue2 != 0)", numberBean, 0, "intValue2", true);
152         doParse("(integerValue1 == 0)", numberBean, 0, "integerValue1", true);
153         doParse("(integerValue2 != 0)", numberBean, 0, "integerValue2", true);
154 
155         // int
156         doParse("(intValue1 == 123)", testBean, 0, "intValue1", true);
157 
158         // Integer
159         doParse("(integerValue1 == 123)", testBean, 0, "integerValue1", true);
160 
161         // Negative Numbers
162         doParse("((intValue2 < -10)     and (intValue2 > -60))", numberBean, 0,
163             "intValue2", true);
164         doParse("((integerValue2 < -10) and (integerValue2 > -60))",
165             numberBean, 0, "integerValue2", true);
166 
167         // Hex
168         doParse("(integerValue1 == 0x7B)", testBean, 0, "integerValue1", true);
169 
170         // Octal
171         doParse("(integerValue1 == 0173)", testBean, 0, "integerValue1", true);
172 
173         // Test 'String' numbers
174         PojoBean stringBean = new PojoBean("11", "2");
175 
176         doParse("(stringValue1 > stringValue2)", stringBean, 0, "stringValue1",
177             true);
178         doParse("(stringValue1 < stringValue2)", stringBean, 0, "stringValue1",
179             false);
180     }
181 
182     /***
183      * Test Null.
184      */
185     public void testNull() {
186         // Not Null
187         doParse("(*this* != null)", testBean, 0, "stringValue1", true);
188 
189         // Null
190         doParse("(*this* == null)", testBean, 0, "stringValue2", true);
191     }
192 
193     /***
194      * Test Joined expressions ('and' or 'or')
195      */
196     public void testJoined() {
197         // Join with 'or'
198         doParse("((*this* == 'ABC') or (stringValue2 == null))", testBean, 0,
199             "stringValue1", true);
200         doParse("((*this* != 'ABC') or (stringValue2 == null))", testBean, 0,
201             "stringValue1", true);
202         doParse("((*this* == 'ABC') or (stringValue2 != null))", testBean, 0,
203             "stringValue1", true);
204         doParse("((*this* != 'ABC') or (stringValue2 != null))", testBean, 0,
205             "stringValue1", false);
206 
207         // Join with 'and'
208         doParse("((*this* == 'ABC') and (stringValue2 == null))", testBean, 0,
209             "stringValue1", true);
210         doParse("((*this* != 'ABC') and (stringValue2 == null))", testBean, 0,
211             "stringValue1", false);
212         doParse("((*this* == 'ABC') and (stringValue2 != null))", testBean, 0,
213             "stringValue1", false);
214         doParse("((*this* != 'ABC') and (stringValue2 != null))", testBean, 0,
215             "stringValue1", false);
216     }
217 
218     /***
219      * Parse the expression and check that the expected result (either true or
220      * false) occurs - fail if an exception is thrown opr the wrong result
221      * occurs.
222      *
223      * @param test     Test expression
224      * @param bean     Test Bean
225      * @param index    index value
226      * @param property Bean property
227      * @param expected Expected Result
228      */
229     private void doParse(String test, Object bean, int index, String property,
230         boolean expected) {
231         boolean result = false;
232 
233         try {
234             result = doParse(test, bean, index, property);
235         } catch (Exception ex) {
236             log.error("Parsing " + test + " for property '" + property + "'", ex);
237             fail("Parsing " + test + " threw " + ex);
238         }
239 
240         if (expected) {
241             assertTrue(test + " didn't return TRUE for " + property, result);
242         } else {
243             assertFalse(test + " didn't return FALSE for " + property, result);
244         }
245     }
246 
247     /***
248      * Parse the expression and check that an Exception is throw. Failes if no
249      * expection is thrown.
250      *
251      * @param test     Test expression
252      * @param bean     Test Bean
253      * @param index    index value
254      * @param property Bean property
255      */
256     private void doParseFail(String test, Object bean, int index,
257         String property) {
258         try {
259             boolean result = doParse(test, bean, index, property);
260 
261             fail("Parsing " + test + " didn't throw exception as expected "
262                 + result);
263         } catch (Exception expected) {
264             // ignore exception - expected result
265         }
266     }
267 
268     /***
269      * Parse the expression returning the result
270      *
271      * @param test     Test expression
272      * @param bean     Test Bean
273      * @param index    index value
274      * @param property Bean property
275      */
276     private boolean doParse(String test, Object bean, int index, String property)
277         throws Exception {
278         if (bean == null) {
279             throw new NullPointerException("Bean is null for property '"
280                 + property + "'");
281         }
282 
283         String value =
284             String.class.isInstance(bean) ? (String) bean
285                                           : ValidatorUtils.getValueAsString(bean,
286                 property);
287 
288         ValidWhenLexer lexer = new ValidWhenLexer(new StringReader(test));
289 
290         ValidWhenParser parser = new ValidWhenParser(lexer);
291 
292         parser.setForm(bean);
293         parser.setIndex(index);
294         parser.setValue(value);
295 
296         parser.expression();
297 
298         return parser.getResult();
299     }
300 }