KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > ExtensionTest


1 /*
2  * $Id: ExtensionTest.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package org.apache.commons.validator;
23
24 import java.io.InputStream JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc CHECK_MSG_KEY = "nameForm.lastname.displayname";
55
56     /**
57      * The key used to retrieve the validator action.
58     */

59     protected static String JavaDoc 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 JavaDoc 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 JavaDoc[] theArgs) {
80         junit.awtui.TestRunner.main(new String JavaDoc[] {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         // All methods starting with "test" will be executed in the test suite.
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 JavaDoc {
97         // Load resources
98
InputStream JavaDoc in = null;
99
100         try {
101             in = this.getClass().getResourceAsStream("validator-extension.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        // Create bean to run test on.
118
NameBean name = new NameBean();
119
120        // Construct validator based on the loaded resources
121
// and the form key
122
Validator validator = new Validator(resources, FORM_KEY);
123        // add the name bean to the validator as a resource
124
// for the validations to be performed on.
125
validator.setParameter(Validator.BEAN_PARAM, name);
126
127        // Get results of the validation.
128
ValidatorResults results = null;
129
130        // throws ValidatorException,
131
// but we aren't catching for testing
132
// since no validation methods we use
133
// throw this
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        // Create bean to run test on.
155
NameBean name = new NameBean();
156        name.setFirstName("");
157
158        // Construct validator based on the loaded resources
159
// and the form key
160
Validator validator = new Validator(resources, FORM_KEY);
161        // add the name bean to the validator as a resource
162
// for the validations to be performed on.
163
validator.setParameter(Validator.BEAN_PARAM, name);
164
165        // Get results of the validation.
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        // Create bean to run test on.
189
NameBean name = new NameBean();
190        name.setFirstName("Joe");
191
192        // Construct validator based on the loaded resources
193
// and the form key
194
Validator validator = new Validator(resources, FORM_KEY);
195        // add the name bean to the validator as a resource
196
// for the validations to be performed on.
197
validator.setParameter(Validator.BEAN_PARAM, name);
198
199        // Get results of the validation.
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        // Create bean to run test on.
223
NameBean name = new NameBean();
224        name.setLastName("");
225
226        // Construct validator based on the loaded resources
227
// and the form key
228
Validator validator = new Validator(resources, FORM_KEY);
229        // add the name bean to the validator as a resource
230
// for the validations to be performed on.
231
validator.setParameter(Validator.BEAN_PARAM, name);
232
233        // Get results of the validation.
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        // Create bean to run test on.
257
NameBean name = new NameBean();
258        name.setLastName("Smith");
259
260        // Construct validator based on the loaded resources
261
// and the form key
262
Validator validator = new Validator(resources, FORM_KEY);
263        // add the name bean to the validator as a resource
264
// for the validations to be performed on.
265
validator.setParameter(Validator.BEAN_PARAM, name);
266
267        // Get results of the validation.
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        // Create bean to run test on.
292
NameBean name = new NameBean();
293        name.setFirstName("Joe");
294        name.setLastName("Smith");
295
296        // Construct validator based on the loaded resources
297
// and the form key
298
Validator validator = new Validator(resources, FORM_KEY);
299        // add the name bean to the validator as a resource
300
// for the validations to be performed on.
301
validator.setParameter(Validator.BEAN_PARAM, name);
302
303        // Get results of the validation.
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        // Create bean to run test on.
330
NameBean name = new NameBean();
331        name.setLastName("Smith");
332
333        // Construct validator based on the loaded resources
334
// and the form key
335
Validator validator = new Validator(resources, FORM_KEY2);
336        // add the name bean to the validator as a resource
337
// for the validations to be performed on.
338
validator.setParameter(Validator.BEAN_PARAM, name);
339
340        // Get results of the validation.
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        //get the first field
372
Field fieldFirstName = (Field)form.getFields().get(0);
373        //get the second field
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 // get the second field
379
fieldLastName = (Field)form2.getFields().get(0);
380         //get the first field
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 }
Popular Tags