1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // All methods starting with "test" will be executed in the test suite.
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  		// Load resources
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        // Create bean to run test on.
115        NameBean name = new NameBean();
116 
117        // Construct validator based on the loaded resources
118        // and the form key
119        Validator validator = new Validator(resources, FORM_KEY);
120        // add the name bean to the validator as a resource
121        // for the validations to be performed on.
122        validator.setParameter(Validator.BEAN_PARAM, name);
123 
124        // Get results of the validation.
125        ValidatorResults results = null;
126 
127        // throws ValidatorException,
128        // but we aren't catching for testing
129        // since no validation methods we use
130        // throw this
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        // Create bean to run test on.
152        NameBean name = new NameBean();
153        name.setFirstName("");
154 
155        // Construct validator based on the loaded resources
156        // and the form key
157        Validator validator = new Validator(resources, FORM_KEY);
158        // add the name bean to the validator as a resource
159        // for the validations to be performed on.
160        validator.setParameter(Validator.BEAN_PARAM, name);
161 
162        // Get results of the validation.
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        // Create bean to run test on.
186        NameBean name = new NameBean();
187        name.setFirstName("Joe");
188 
189        // Construct validator based on the loaded resources
190        // and the form key
191        Validator validator = new Validator(resources, FORM_KEY);
192        // add the name bean to the validator as a resource
193        // for the validations to be performed on.
194        validator.setParameter(Validator.BEAN_PARAM, name);
195 
196        // Get results of the validation.
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        // Create bean to run test on.
220        NameBean name = new NameBean();
221        name.setLastName("");
222 
223        // Construct validator based on the loaded resources
224        // and the form key
225        Validator validator = new Validator(resources, FORM_KEY);
226        // add the name bean to the validator as a resource
227        // for the validations to be performed on.
228        validator.setParameter(Validator.BEAN_PARAM, name);
229 
230        // Get results of the validation.
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        // Create bean to run test on.
254        NameBean name = new NameBean();
255        name.setLastName("Smith");
256 
257        // Construct validator based on the loaded resources
258        // and the form key
259        Validator validator = new Validator(resources, FORM_KEY);
260        // add the name bean to the validator as a resource
261        // for the validations to be performed on.
262        validator.setParameter(Validator.BEAN_PARAM, name);
263 
264        // Get results of the validation.
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        // Create bean to run test on.
289        NameBean name = new NameBean();
290        name.setFirstName("Joe");
291        name.setLastName("Smith");
292 
293        // Construct validator based on the loaded resources
294        // and the form key
295        Validator validator = new Validator(resources, FORM_KEY);
296        // add the name bean to the validator as a resource
297        // for the validations to be performed on.
298        validator.setParameter(Validator.BEAN_PARAM, name);
299 
300        // Get results of the validation.
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        // Create bean to run test on.
327        NameBean name = new NameBean();
328        name.setLastName("Smith");
329 
330        // Construct validator based on the loaded resources
331        // and the form key
332        Validator validator = new Validator(resources, FORM_KEY2);
333        // add the name bean to the validator as a resource
334        // for the validations to be performed on.
335        validator.setParameter(Validator.BEAN_PARAM, name);
336 
337        // Get results of the validation.
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        //get the first field
369        Field fieldFirstName = (Field)form.getFields().get(0);
370        //get the second field
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 //     get the second field
376        fieldLastName = (Field)form2.getFields().get(0);
377         //get the first field
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 }