1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import java.io.InputStream;
20
21 import junit.framework.TestCase;
22
23
24
25
26
27
28
29
30
31 public class ExtensionTest extends TestCase {
32
33
34
35
36
37 protected static String FORM_KEY = "nameForm";
38
39
40
41
42
43 protected static String FORM_KEY2 = "nameForm2";
44
45
46
47
48
49 protected static String CHECK_MSG_KEY = "nameForm.lastname.displayname";
50
51
52
53
54 protected static String ACTION = "required";
55
56
57
58
59 private ValidatorResources resources = null;
60
61
62
63
64
65 public ExtensionTest(String arg0) {
66 super(arg0);
67 }
68
69
70
71
72
73 @Override
74 protected void setUp() throws Exception {
75
76 InputStream in = null;
77
78 try {
79 in = this.getClass().getResourceAsStream("ExtensionTest-config.xml");
80 resources = new ValidatorResources(in);
81 } finally {
82 if (in != null) {
83 in.close();
84 }
85 }
86 }
87
88 @Override
89 protected void tearDown() {
90 }
91
92
93
94
95 public void testRequired() throws ValidatorException {
96
97 NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
98
99
100
101 Validator validator = new Validator(resources, FORM_KEY);
102
103
104 validator.setParameter(Validator.BEAN_PARAM, name);
105
106
107 ValidatorResults results = null;
108
109
110
111
112
113 results = validator.validate();
114
115 assertNotNull("Results are null.", results);
116
117 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
118 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
119
120 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
121 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
122 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
123
124 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
125 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
126 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
127 }
128
129
130
131
132 public void testRequiredFirstNameBlank() throws ValidatorException {
133
134 NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
135 name.setFirstName("");
136
137
138
139 Validator validator = new Validator(resources, FORM_KEY);
140
141
142 validator.setParameter(Validator.BEAN_PARAM, name);
143
144
145 ValidatorResults results = null;
146
147 results = validator.validate();
148
149 assertNotNull("Results are null.", results);
150
151 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
152 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
153
154 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
155 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
156 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
157
158 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
159 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
160 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
161 }
162
163
164
165
166 public void testRequiredFirstName() throws ValidatorException {
167
168 NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
169 name.setFirstName("Joe");
170
171
172
173 Validator validator = new Validator(resources, FORM_KEY);
174
175
176 validator.setParameter(Validator.BEAN_PARAM, name);
177
178
179 ValidatorResults results = null;
180
181 results = validator.validate();
182
183 assertNotNull("Results are null.", results);
184
185 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
186 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
187
188 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
189 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
190 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
191
192 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
193 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
194 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
195 }
196
197
198
199
200 public void testRequiredLastNameBlank() throws ValidatorException {
201
202 NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
203 name.setLastName("");
204
205
206
207 Validator validator = new Validator(resources, FORM_KEY);
208
209
210 validator.setParameter(Validator.BEAN_PARAM, name);
211
212
213 ValidatorResults results = null;
214
215 results = validator.validate();
216
217 assertNotNull("Results are null.", results);
218
219 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
220 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
221
222 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
223 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
224 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
225
226 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
227 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
228 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
229 }
230
231
232
233
234 public void testRequiredLastName() throws ValidatorException {
235
236 NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
237 name.setLastName("Smith");
238
239
240
241 Validator validator = new Validator(resources, FORM_KEY);
242
243
244 validator.setParameter(Validator.BEAN_PARAM, name);
245
246
247 ValidatorResults results = null;
248
249 results = validator.validate();
250
251 assertNotNull("Results are null.", results);
252
253 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
254 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
255
256 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
257 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
258 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
259
260 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
261 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
262 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
263
264 }
265
266
267
268
269 public void testRequiredName() throws ValidatorException {
270
271 NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
272 name.setFirstName("Joe");
273 name.setLastName("Smith");
274
275
276
277 Validator validator = new Validator(resources, FORM_KEY);
278
279
280 validator.setParameter(Validator.BEAN_PARAM, name);
281
282
283 ValidatorResults results = null;
284
285 results = validator.validate();
286
287 assertNotNull("Results are null.", results);
288
289 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
290 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
291
292 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
293 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
294 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
295
296 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
297 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
298 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
299 }
300
301
302
303
304
305
306 public void testOverrideRule() throws ValidatorException {
307
308
309 NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
310 name.setLastName("Smith");
311
312
313
314 Validator validator = new Validator(resources, FORM_KEY2);
315
316
317 validator.setParameter(Validator.BEAN_PARAM, name);
318
319
320 ValidatorResults results = null;
321
322 results = validator.validate();
323
324 assertNotNull("Results are null.", results);
325
326 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
327 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
328 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
329 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have '" + CHECK_MSG_KEY + " as a key.", firstNameResult.field.getArg(0).getKey().equals(CHECK_MSG_KEY));
330
331 assertNull("Last Name ValidatorResult should be null.", lastNameResult);
332 }
333
334
335
336
337
338
339 public void testOrder() {
340
341 Form form = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY);
342 Form form2 = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY2);
343
344 assertNotNull(FORM_KEY + " is null.", form);
345 assertTrue("There should only be 2 fields in " + FORM_KEY, form.getFields().size() == 2);
346
347 assertNotNull(FORM_KEY2 + " is null.", form2);
348 assertTrue("There should only be 2 fields in " + FORM_KEY2, form2.getFields().size() == 2);
349
350
351 Field fieldFirstName = form.getFields().get(0);
352
353 Field fieldLastName = form.getFields().get(1);
354 assertTrue("firstName in " + FORM_KEY + " should be the first in the list", fieldFirstName.getKey().equals("firstName"));
355 assertTrue("lastName in " + FORM_KEY + " should be the first in the list", fieldLastName.getKey().equals("lastName"));
356
357
358 fieldLastName = form2.getFields().get(0);
359
360 fieldFirstName = form2.getFields().get(1);
361 assertTrue("firstName in " + FORM_KEY2 + " should be the first in the list", fieldFirstName.getKey().equals("firstName"));
362 assertTrue("lastName in " + FORM_KEY2 + " should be the first in the list", fieldLastName.getKey().equals("lastName"));
363
364 }
365 }