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