KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > validator > TestValidWhen


1 /*
2  * $Id: TestValidWhen.java 54929 2004-10-16 16:38:42Z germuska $
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
19 package org.apache.struts.validator;
20
21 import java.io.StringReader JavaDoc;
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25 import org.apache.struts.validator.validwhen.ValidWhenLexer;
26 import org.apache.struts.validator.validwhen.ValidWhenParser;
27 import org.apache.commons.validator.util.ValidatorUtils;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.logging.Log;
30
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     private static Log log = LogFactory.getLog(TestValidWhen.class);
39
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 JavaDoc 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 JavaDoc[] theArgs) {
57         junit.awtui.TestRunner.main(
58             new String JavaDoc[] {TestValidWhen.class.getName()});
59     }
60
61     /**
62      * @return a test suite (<code>TestSuite</code>) that includes all methods
63      * starting with "test"
64      */

65     public static Test suite() {
66         // All methods starting with "test" will be executed in the test suite.
67
return new TestSuite(TestValidWhen.class);
68     }
69
70     public void setUp() {
71         testBean = new PojoBean(123, 789);
72         testBean.setStringValue1("ABC");
73         testBean.setStringValue2(null);
74         testBean.setBeans(new PojoBean[] {new PojoBean(11, 12),
75                                           new PojoBean(21, 22),
76                                           new PojoBean(31, 42),
77                                           new PojoBean(41, 52),
78                                           new PojoBean(51, 62)});
79         testBean.setMapped("testKey", "mappedValue");
80     }
81
82     public void tearDown() {
83         testBean = null;
84     }
85     
86     /**
87      * Test Operators.
88      */

89     public void testProperty() {
90
91         // *this*
92
doParse("(*this* == 123)", testBean , 0, "intValue1", true);
93
94         // Named property
95
doParse("(intValue2 == 789)", testBean , 0, "intValue1", true);
96
97         // Indexed Property
98
doParse("(beans[].intValue1 == 21)", testBean , 1, "intValue1", true);
99         doParse("(beans[1].intValue1 == 21)", testBean , 4, "intValue1", true);
100
101         // Mapped Property - *** NOT SUPPORTED ***
102
// doParse("(mapped(mappedValue) == 'testKey')", testBean , 0, "mappedValue", true);
103

104
105     }
106     
107     /**
108      * Test Operators.
109      */

110     public void testOperators() {
111
112         // Equal
113
doParse("(*this* == 123)", testBean , 0, "intValue1", true);
114
115         // Not Equal
116
doParse("(*this* != 456)", testBean , 0, "intValue1", true);
117
118         // Less Than
119
doParse("(*this* < 456)", testBean , 0, "intValue1", true);
120
121         // Greater Than
122
doParse("(*this* > 100)", testBean , 0, "intValue1", true);
123
124         // Less Than Equal
125
doParse("(*this* <= 123)", testBean , 0, "intValue1", true);
126
127         // Greater Than Equal
128
doParse("(*this* >= 123)", testBean , 0, "intValue1", true);
129
130     }
131
132     /**
133      * Test String values.
134      */

135     public void testString() {
136
137          doParse("(*this* != '--')", "XX", 0, "stringValue1", true);
138          doParse("(*this* == '--')", "--", 0, "stringValue1", true);
139
140
141          String JavaDoc testValue = "dsgUOLMdLsdL";
142
143          // single quote
144
doParse("(*this* == '" + testValue + "')", testValue, 0, "stringValue1", true);
145
146          // double quote
147
doParse("(*this* == \"" + testValue + "\")", testValue, 0, "stringValue1", true);
148     }
149     
150     /**
151      * Test Numeric values.
152      */

153     public void testNumeric() {
154
155         // Test Zero
156
PojoBean numberBean = new PojoBean(0, -50);
157         doParse("(intValue1 == 0)", numberBean, 0, "intValue1", true);
158         doParse("(intValue2 != 0)", numberBean, 0, "intValue2", true);
159         doParse("(integerValue1 == 0)", numberBean, 0, "integerValue1", true);
160         doParse("(integerValue2 != 0)", numberBean, 0, "integerValue2", true);
161
162         // int
163
doParse("(intValue1 == 123)", testBean , 0, "intValue1", true);
164
165         // Integer
166
doParse("(integerValue1 == 123)", testBean , 0, "integerValue1", true);
167
168         // Negative Numbers
169
doParse("((intValue2 < -10) and (intValue2 > -60))", numberBean, 0, "intValue2", true);
170         doParse("((integerValue2 < -10) and (integerValue2 > -60))", numberBean, 0, "integerValue2", true);
171
172         // Hex
173
doParse("(integerValue1 == 0x7B)", testBean , 0, "integerValue1", true);
174
175         // Octal
176
doParse("(integerValue1 == 0173)", testBean , 0, "integerValue1", true);
177
178         // Test 'String' numbers
179
PojoBean stringBean = new PojoBean("11", "2");
180         doParse("(stringValue1 > stringValue2)", stringBean , 0, "stringValue1", true);
181         doParse("(stringValue1 < stringValue2)", stringBean , 0, "stringValue1", false);
182
183     }
184     
185     /**
186      * Test Null.
187      */

188     public void testNull() {
189
190         // Not Null
191
doParse("(*this* != null)", testBean , 0, "stringValue1", true);
192
193         // Null
194
doParse("(*this* == null)", testBean , 0, "stringValue2", true);
195
196
197     }
198     
199     /**
200      * Test Joined expressions ('and' or 'or')
201      */

202     public void testJoined() {
203
204         // Join with 'or'
205
doParse("((*this* == 'ABC') or (stringValue2 == null))", testBean , 0, "stringValue1", true);
206         doParse("((*this* != 'ABC') or (stringValue2 == null))", testBean , 0, "stringValue1", true);
207         doParse("((*this* == 'ABC') or (stringValue2 != null))", testBean , 0, "stringValue1", true);
208         doParse("((*this* != 'ABC') or (stringValue2 != null))", testBean , 0, "stringValue1", false);
209
210         // Join with 'and'
211
doParse("((*this* == 'ABC') and (stringValue2 == null))", testBean , 0, "stringValue1", true);
212         doParse("((*this* != 'ABC') and (stringValue2 == null))", testBean , 0, "stringValue1", false);
213         doParse("((*this* == 'ABC') and (stringValue2 != null))", testBean , 0, "stringValue1", false);
214         doParse("((*this* != 'ABC') and (stringValue2 != null))", testBean , 0, "stringValue1", false);
215
216     }
217     
218
219     /**
220      * Parse the expression and check that the expected result
221      * (either true or false) occurs - fail if an exception is thrown
222      * opr the wrong result occurs.
223      *
224      * @param test Test expression
225      * @param bean Test Bean
226      * @param index index value
227      * @param property Bean property
228      * @param expected Expected Result
229      */

230     private void doParse(String JavaDoc test, Object JavaDoc bean, int index, String JavaDoc property, boolean expected) {
231         boolean result = false;
232         try {
233             result = doParse(test, bean, index, property);
234         } catch (Exception JavaDoc ex) {
235             log.error("Parsing " + test + " for property '"+property+"'", ex);
236             fail("Parsing " + test + " threw " + ex);
237         }
238         if (expected) {
239             assertTrue(test + " didn't return TRUE for " + property, result);
240         } else {
241             assertFalse(test + " didn't return FALSE for " + property, result);
242         }
243     }
244
245     /**
246      * Parse the expression and check that an Exception is throw.
247      * Failes if no expection is thrown.
248      *
249      * @param test Test expression
250      * @param bean Test Bean
251      * @param index index value
252      * @param property Bean property
253      */

254     private void doParseFail(String JavaDoc test, Object JavaDoc bean, int index, String JavaDoc property) {
255
256         try {
257             boolean result = doParse(test, bean, index, property);
258             fail("Parsing " + test + " didn't throw exception as expected " + result);
259         } catch (Exception JavaDoc expected) {
260             // ignore exception - expected result
261
}
262     }
263
264     /**
265      * Parse the expression returning the result
266      *
267      * @param test Test expression
268      * @param bean Test Bean
269      * @param index index value
270      * @param property Bean property
271      */

272     private boolean doParse(String JavaDoc test, Object JavaDoc bean, int index, String JavaDoc property) throws Exception JavaDoc {
273
274         if (bean == null) {
275             throw new NullPointerException JavaDoc("Bean is null for property '"+property+"'");
276         }
277
278         String JavaDoc value = String JavaDoc.class.isInstance(bean)
279                             ? (String JavaDoc)bean
280                             : ValidatorUtils.getValueAsString(bean, property);
281
282         ValidWhenLexer lexer = new ValidWhenLexer(new StringReader JavaDoc(test));
283
284         ValidWhenParser parser = new ValidWhenParser(lexer);
285         parser.setForm(bean);
286         parser.setIndex(index);
287         parser.setValue(value);
288
289         parser.expression();
290         return parser.getResult();
291
292     }
293
294 }
295
Popular Tags