View Javadoc
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.TestCase;
22  
23  /**
24   * <p>Performs tests for extension in form definitions. Performs the same tests
25   * RequiredNameTest does but with an equivalent validation definition with extension
26   * definitions (validator-extension.xml), plus an extra check on overriding rules and
27   * another one checking it mantains correct order when extending.</p>
28   *
29   * @version $Revision$
30   */
31  public class ExtensionTest extends TestCase {
32  
33      /**
34       * The key used to retrieve the set of validation
35       * rules from the xml file.
36      */
37      protected static String FORM_KEY = "nameForm";
38  
39      /**
40       * The key used to retrieve the set of validation
41       * rules from the xml file.
42      */
43      protected static String FORM_KEY2 = "nameForm2";
44  
45      /**
46       * The key used to retrieve the set of validation
47       * rules from the xml file.
48      */
49      protected static String CHECK_MSG_KEY = "nameForm.lastname.displayname";
50  
51      /**
52       * The key used to retrieve the validator action.
53      */
54      protected static String ACTION = "required";
55  
56      /**
57       * Resources used for validation tests.
58      */
59      private ValidatorResources resources = null;
60  
61      /**
62       * Constructor de ExtensionTest.
63       * @param arg0
64       */
65      public ExtensionTest(String arg0) {
66          super(arg0);
67      }
68  
69      /**
70       * Load <code>ValidatorResources</code> from
71       * validator-extension.xml.
72      */
73      @Override
74      protected void setUp() throws Exception {
75          // Load resources
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       * Tests the required validation failure.
94      */
95      public void testRequired() throws ValidatorException {
96         // Create bean to run test on.
97         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
98  
99         // Construct validator based on the loaded resources
100        // and the form key
101        Validator validator = new Validator(resources, FORM_KEY);
102        // add the name bean to the validator as a resource
103        // for the validations to be performed on.
104        validator.setParameter(Validator.BEAN_PARAM, name);
105 
106        // Get results of the validation.
107        ValidatorResults results = null;
108 
109        // throws ValidatorException,
110        // but we aren't catching for testing
111        // since no validation methods we use
112        // throw this
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      * Tests the required validation for first name if it is blank.
131     */
132     public void testRequiredFirstNameBlank() throws ValidatorException {
133        // Create bean to run test on.
134        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
135        name.setFirstName("");
136 
137        // Construct validator based on the loaded resources
138        // and the form key
139        Validator validator = new Validator(resources, FORM_KEY);
140        // add the name bean to the validator as a resource
141        // for the validations to be performed on.
142        validator.setParameter(Validator.BEAN_PARAM, name);
143 
144        // Get results of the validation.
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      * Tests the required validation for first name.
165     */
166     public void testRequiredFirstName() throws ValidatorException {
167        // Create bean to run test on.
168        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
169        name.setFirstName("Joe");
170 
171        // Construct validator based on the loaded resources
172        // and the form key
173        Validator validator = new Validator(resources, FORM_KEY);
174        // add the name bean to the validator as a resource
175        // for the validations to be performed on.
176        validator.setParameter(Validator.BEAN_PARAM, name);
177 
178        // Get results of the validation.
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      * Tests the required validation for last name if it is blank.
199     */
200     public void testRequiredLastNameBlank() throws ValidatorException {
201        // Create bean to run test on.
202        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
203        name.setLastName("");
204 
205        // Construct validator based on the loaded resources
206        // and the form key
207        Validator validator = new Validator(resources, FORM_KEY);
208        // add the name bean to the validator as a resource
209        // for the validations to be performed on.
210        validator.setParameter(Validator.BEAN_PARAM, name);
211 
212        // Get results of the validation.
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      * Tests the required validation for last name.
233     */
234     public void testRequiredLastName() throws ValidatorException {
235        // Create bean to run test on.
236        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
237        name.setLastName("Smith");
238 
239        // Construct validator based on the loaded resources
240        // and the form key
241        Validator validator = new Validator(resources, FORM_KEY);
242        // add the name bean to the validator as a resource
243        // for the validations to be performed on.
244        validator.setParameter(Validator.BEAN_PARAM, name);
245 
246        // Get results of the validation.
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      * Tests the required validation for first and last name.
268     */
269     public void testRequiredName() throws ValidatorException {
270        // Create bean to run test on.
271        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
272        name.setFirstName("Joe");
273        name.setLastName("Smith");
274 
275        // Construct validator based on the loaded resources
276        // and the form key
277        Validator validator = new Validator(resources, FORM_KEY);
278        // add the name bean to the validator as a resource
279        // for the validations to be performed on.
280        validator.setParameter(Validator.BEAN_PARAM, name);
281 
282        // Get results of the validation.
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      * Tests if we can override a rule. We "can" override a rule if the message shown
304      * when the firstName required test fails and the lastName test is null.
305     */
306     public void testOverrideRule() throws ValidatorException {
307 
308        // Create bean to run test on.
309        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
310        name.setLastName("Smith");
311 
312        // Construct validator based on the loaded resources
313        // and the form key
314        Validator validator = new Validator(resources, FORM_KEY2);
315        // add the name bean to the validator as a resource
316        // for the validations to be performed on.
317        validator.setParameter(Validator.BEAN_PARAM, name);
318 
319        // Get results of the validation.
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      * Tests if the order is mantained when extending a form. Parent form fields should
337      * preceed self form fields, except if we override the rules.
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        //get the first field
351        Field fieldFirstName = form.getFields().get(0);
352        //get the second field
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 //     get the second field
358        fieldLastName = form2.getFields().get(0);
359         //get the first field
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 }