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.IOException;
20  
21  import org.xml.sax.SAXException;
22  
23                                                            
24  /**                                                       
25   * Performs Validation Test.
26   *
27   * @version $Revision$
28   */
29  public class RequiredNameTest extends AbstractCommonTest {
30     
31     /**
32      * The key used to retrieve the set of validation 
33      * rules from the xml file.
34      */
35     protected static String FORM_KEY = "nameForm";   
36  
37     /**
38      * The key used to retrieve the validator action.
39      */
40     protected static String ACTION = "required";
41  
42     public RequiredNameTest(String name) {                  
43         super(name);                                      
44     }                                                     
45  
46     /**
47      * Load <code>ValidatorResources</code> from 
48      * validator-name-required.xml.
49      */
50     @Override
51  protected void setUp() throws IOException, SAXException {
52        // Load resources
53        loadResources("RequiredNameTest-config.xml");
54     }
55  
56     @Override
57  protected void tearDown() {
58     }
59  
60     /**
61      * Tests the required validation failure.
62      */
63     public void testRequired() throws ValidatorException {
64        // Create bean to run test on.
65        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
66        
67        // Construct validator based on the loaded resources 
68        // and the form key
69        Validator validator = new Validator(resources, FORM_KEY);
70        // add the name bean to the validator as a resource 
71        // for the validations to be performed on.
72        validator.setParameter(Validator.BEAN_PARAM, name);
73  
74        // Get results of the validation.
75        ValidatorResults results = null;
76        
77        // throws ValidatorException, 
78        // but we aren't catching for testing 
79        // since no validation methods we use 
80        // throw this
81        results = validator.validate();
82        
83        assertNotNull("Results are null.", results);
84        
85        ValidatorResult firstNameResult = results.getValidatorResult("firstName");
86        ValidatorResult lastNameResult = results.getValidatorResult("lastName");
87        
88        assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
89        assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
90        assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
91        
92        assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
93        assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
94        assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
95     }
96  
97     /**
98      * Tests the required validation for first name if it is blank.
99      */
100    public void testRequiredFirstNameBlank() throws ValidatorException {
101       // Create bean to run test on.
102       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
103       name.setFirstName("");
104       
105       // Construct validator based on the loaded resources 
106       // and the form key
107       Validator validator = new Validator(resources, FORM_KEY);
108       // add the name bean to the validator as a resource 
109       // for the validations to be performed on.
110       validator.setParameter(Validator.BEAN_PARAM, name);
111 
112       // Get results of the validation.
113       ValidatorResults results = null;
114       
115       results = validator.validate();
116       
117       assertNotNull("Results are null.", results);
118       
119       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
120       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
121       
122       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
123       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
124       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
125       
126       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
127       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
128       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
129    }
130 
131    /**
132     * Tests the required validation for first name.
133     */
134    public void testRequiredFirstName() throws ValidatorException {
135       // Create bean to run test on.
136       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
137       name.setFirstName("Joe");
138       
139       // Construct validator based on the loaded resources 
140       // and the form key
141       Validator validator = new Validator(resources, FORM_KEY);
142       // add the name bean to the validator as a resource 
143       // for the validations to be performed on.
144       validator.setParameter(Validator.BEAN_PARAM, name);
145 
146       // Get results of the validation.
147       ValidatorResults results = null;
148       
149       results = validator.validate();
150       
151       assertNotNull("Results are null.", results);
152       
153       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
154       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
155       
156       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
157       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
158       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
159       
160       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
161       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
162       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
163    }
164 
165    /**
166     * Tests the required validation for last name if it is blank.
167     */
168    public void testRequiredLastNameBlank() throws ValidatorException {
169       // Create bean to run test on.
170       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
171       name.setLastName("");
172       
173       // Construct validator based on the loaded resources 
174       // and the form key
175       Validator validator = new Validator(resources, FORM_KEY);
176       // add the name bean to the validator as a resource 
177       // for the validations to be performed on.
178       validator.setParameter(Validator.BEAN_PARAM, name);
179 
180       // Get results of the validation.
181       ValidatorResults results = null;
182       
183       results = validator.validate();
184       
185       assertNotNull("Results are null.", results);
186       
187       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
188       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
189       
190       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
191       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
192       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
193       
194       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
195       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
196       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
197    }
198 
199    /**
200     * Tests the required validation for last name.
201     */
202    public void testRequiredLastName() throws ValidatorException {
203       // Create bean to run test on.
204       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
205       name.setLastName("Smith");
206       
207       // Construct validator based on the loaded resources 
208       // and the form key
209       Validator validator = new Validator(resources, FORM_KEY);
210       // add the name bean to the validator as a resource 
211       // for the validations to be performed on.
212       validator.setParameter(Validator.BEAN_PARAM, name);
213 
214       // Get results of the validation.
215       ValidatorResults results = null;
216       
217       results = validator.validate();
218       
219       assertNotNull("Results are null.", results);
220       
221       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
222       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
223       
224       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
225       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
226       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
227       
228       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
229       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
230       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
231 
232    }
233 
234    /**
235     * Tests the required validation for first and last name.
236     */
237    public void testRequiredName() throws ValidatorException {
238       // Create bean to run test on.
239       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
240       name.setFirstName("Joe");
241       name.setLastName("Smith");
242       
243       // Construct validator based on the loaded resources 
244       // and the form key
245       Validator validator = new Validator(resources, FORM_KEY);
246       // add the name bean to the validator as a resource 
247       // for the validations to be performed on.
248       validator.setParameter(Validator.BEAN_PARAM, name);
249 
250       // Get results of the validation.
251       ValidatorResults results = null;
252       
253       results = validator.validate();
254       
255       assertNotNull("Results are null.", results);
256       
257       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
258       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
259       
260       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
261       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
262       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
263       
264       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
265       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
266       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
267    }
268    
269 }