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