1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.validator;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.validator.util.ValidatorUtils;
30 import org.apache.struts.validator.validwhen.ValidWhenLexer;
31 import org.apache.struts.validator.validwhen.ValidWhenParser;
32
33 import java.io.StringReader;
34
35
36
37
38 public class TestValidWhen extends TestCase {
39
40
41
42 private static Log log = LogFactory.getLog(TestValidWhen.class);
43 protected PojoBean testBean;
44
45
46
47
48
49
50 public TestValidWhen(String theName) {
51 super(theName);
52 }
53
54
55
56
57
58
59 public static void main(String[] theArgs) {
60 junit.awtui.TestRunner.main(new String[] { TestValidWhen.class.getName() });
61 }
62
63
64
65
66
67 public static Test suite() {
68
69 return new TestSuite(TestValidWhen.class);
70 }
71
72 public void setUp() {
73 testBean = new PojoBean(123, 789);
74 testBean.setStringValue1("ABC");
75 testBean.setStringValue2(null);
76 testBean.setBeans(new PojoBean[] {
77 new PojoBean(11, 12), new PojoBean(21, 22), new PojoBean(31, 42),
78 new PojoBean(41, 52), new PojoBean(51, 62)
79 });
80 testBean.setMapped("testKey", "mappedValue");
81 }
82
83 public void tearDown() {
84 testBean = null;
85 }
86
87
88
89
90 public void testProperty() {
91
92 doParse("(*this* == 123)", testBean, 0, "intValue1", true);
93
94
95 doParse("(intValue2 == 789)", testBean, 0, "intValue1", true);
96
97
98 doParse("(beans[].intValue1 == 21)", testBean, 1, "intValue1", true);
99 doParse("(beans[1].intValue1 == 21)", testBean, 4, "intValue1", true);
100
101
102
103 }
104
105
106
107
108 public void testOperators() {
109
110 doParse("(*this* == 123)", testBean, 0, "intValue1", true);
111
112
113 doParse("(*this* != 456)", testBean, 0, "intValue1", true);
114
115
116 doParse("(*this* < 456)", testBean, 0, "intValue1", true);
117
118
119 doParse("(*this* > 100)", testBean, 0, "intValue1", true);
120
121
122 doParse("(*this* <= 123)", testBean, 0, "intValue1", true);
123
124
125 doParse("(*this* >= 123)", testBean, 0, "intValue1", true);
126 }
127
128
129
130
131 public void testString() {
132 doParse("(*this* != '--')", "XX", 0, "stringValue1", true);
133 doParse("(*this* == '--')", "--", 0, "stringValue1", true);
134
135 String testValue = "dsgUOLMdLsdL";
136
137
138 doParse("(*this* == '" + testValue + "')", testValue, 0,
139 "stringValue1", true);
140
141
142 doParse("(*this* == \"" + testValue + "\")", testValue, 0,
143 "stringValue1", true);
144
145
146 doParse("(*this* == \":\")", ":", 0,
147 "stringValue1", true);
148 doParse("(*this* == \"foo:bar\")", "foo:bar", 0,
149 "stringValue1", true);
150 }
151
152
153
154
155 public void testNumeric() {
156
157 PojoBean numberBean = new PojoBean(0, -50);
158
159 doParse("(intValue1 == 0)", numberBean, 0, "intValue1", true);
160 doParse("(intValue2 != 0)", numberBean, 0, "intValue2", true);
161 doParse("(integerValue1 == 0)", numberBean, 0, "integerValue1", true);
162 doParse("(integerValue2 != 0)", numberBean, 0, "integerValue2", true);
163
164
165 doParse("(intValue1 == 123)", testBean, 0, "intValue1", true);
166
167
168 doParse("(integerValue1 == 123)", testBean, 0, "integerValue1", true);
169
170
171 doParse("((intValue2 < -10) and (intValue2 > -60))", numberBean, 0,
172 "intValue2", true);
173 doParse("((integerValue2 < -10) and (integerValue2 > -60))",
174 numberBean, 0, "integerValue2", true);
175
176
177 doParse("(integerValue1 == 0x7B)", testBean, 0, "integerValue1", true);
178
179
180 doParse("(integerValue1 == 0173)", testBean, 0, "integerValue1", true);
181
182
183 PojoBean stringBean = new PojoBean("11", "2");
184
185 doParse("(stringValue1 > stringValue2)", stringBean, 0, "stringValue1",
186 true);
187 doParse("(stringValue1 < stringValue2)", stringBean, 0, "stringValue1",
188 false);
189 }
190
191
192
193
194 public void testNull() {
195
196 doParse("(*this* != null)", testBean, 0, "stringValue1", true);
197
198
199 doParse("(*this* == null)", testBean, 0, "stringValue2", true);
200
201
202 testBean.setStringValue2("");
203 doParse("(*this* == null)", testBean, 0, "stringValue2", true);
204
205
206 testBean.setStringValue2(" ");
207 doParse("(*this* == null)", testBean, 0, "stringValue2", true);
208 }
209
210
211
212
213 public void testJoined() {
214
215 doParse("((*this* == 'ABC') or (stringValue2 == null))", testBean, 0,
216 "stringValue1", true);
217 doParse("((*this* != 'ABC') or (stringValue2 == null))", testBean, 0,
218 "stringValue1", true);
219 doParse("((*this* == 'ABC') or (stringValue2 != null))", testBean, 0,
220 "stringValue1", true);
221 doParse("((*this* != 'ABC') or (stringValue2 != null))", testBean, 0,
222 "stringValue1", false);
223
224
225 doParse("((*this* == 'ABC') and (stringValue2 == null))", testBean, 0,
226 "stringValue1", true);
227 doParse("((*this* != 'ABC') and (stringValue2 == null))", testBean, 0,
228 "stringValue1", false);
229 doParse("((*this* == 'ABC') and (stringValue2 != null))", testBean, 0,
230 "stringValue1", false);
231 doParse("((*this* != 'ABC') and (stringValue2 != null))", testBean, 0,
232 "stringValue1", false);
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246 private void doParse(String test, Object bean, int index, String property,
247 boolean expected) {
248 boolean result = false;
249
250 try {
251 result = doParse(test, bean, index, property);
252 } catch (Exception ex) {
253 log.error("Parsing " + test + " for property '" + property + "'", ex);
254 fail("Parsing " + test + " threw " + ex);
255 }
256
257 if (expected) {
258 assertTrue(test + " didn't return TRUE for " + property, result);
259 } else {
260 assertFalse(test + " didn't return FALSE for " + property, result);
261 }
262 }
263
264
265
266
267
268
269
270
271
272
273 private void doParseFail(String test, Object bean, int index,
274 String property) {
275 try {
276 boolean result = doParse(test, bean, index, property);
277
278 fail("Parsing " + test + " didn't throw exception as expected "
279 + result);
280 } catch (Exception expected) {
281
282 }
283 }
284
285
286
287
288
289
290
291
292
293 private boolean doParse(String test, Object bean, int index, String property)
294 throws Exception {
295 if (bean == null) {
296 throw new NullPointerException("Bean is null for property '"
297 + property + "'");
298 }
299
300 String value =
301 String.class.isInstance(bean) ? (String) bean
302 : ValidatorUtils.getValueAsString(bean,
303 property);
304
305 ValidWhenLexer lexer = new ValidWhenLexer(new StringReader(test));
306
307 ValidWhenParser parser = new ValidWhenParser(lexer);
308
309 parser.setForm(bean);
310 parser.setIndex(index);
311 parser.setValue(value);
312
313 parser.expression();
314
315 return parser.getResult();
316 }
317 }