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 junit.framework.Test;
22  import junit.framework.TestSuite;
23  
24  import org.xml.sax.SAXException;
25  
26                                                            
27  /***                                                       
28   * Performs Validation Test.
29   *
30   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
31   */
32  public class RequiredNameTest extends TestCommon {
33     
34     /***
35      * The key used to retrieve the set of validation 
36      * rules from the xml file.
37      */
38     protected static String FORM_KEY = "nameForm";   
39  
40     /***
41      * The key used to retrieve the validator action.
42      */
43     protected static String ACTION = "required";
44  
45     public RequiredNameTest(String name) {                  
46         super(name);                                      
47     }                                                     
48  
49     /***
50      * Start the tests.
51      *
52      * @param theArgs the arguments. Not used
53      */
54     public static void main(String[] theArgs) {
55         junit.awtui.TestRunner.main(new String[] {RequiredNameTest.class.getName()});
56     }
57  
58     /***
59      * @return a test suite (<code>TestSuite</code>) that includes all methods
60      *         starting with "test"
61      */
62     public static Test suite() {
63         // All methods starting with "test" will be executed in the test suite.
64         return new TestSuite(RequiredNameTest.class);
65     }
66  
67     /***
68      * Load <code>ValidatorResources</code> from 
69      * validator-name-required.xml.
70      */
71     protected void setUp() throws IOException, SAXException {
72        // Load resources
73        loadResources("RequiredNameTest-config.xml");
74     }
75  
76     protected void tearDown() {
77     }
78  
79     /***
80      * Tests the required validation failure.
81      */
82     public void testRequired() throws ValidatorException {
83        // Create bean to run test on.
84        NameBean name = new NameBean();
85        
86        // Construct validator based on the loaded resources 
87        // and the form key
88        Validator validator = new Validator(resources, FORM_KEY);
89        // add the name bean to the validator as a resource 
90        // for the validations to be performed on.
91        validator.setParameter(Validator.BEAN_PARAM, name);
92  
93        // Get results of the validation.
94        ValidatorResults results = null;
95        
96        // throws ValidatorException, 
97        // but we aren't catching for testing 
98        // since no validation methods we use 
99        // throw this
100       results = validator.validate();
101       
102       assertNotNull("Results are null.", results);
103       
104       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
105       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
106       
107       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
108       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
109       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
110       
111       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
112       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
113       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
114    }
115 
116    /***
117     * Tests the required validation for first name if it is blank.
118     */
119    public void testRequiredFirstNameBlank() throws ValidatorException {
120       // Create bean to run test on.
121       NameBean name = new NameBean();
122       name.setFirstName("");
123       
124       // Construct validator based on the loaded resources 
125       // and the form key
126       Validator validator = new Validator(resources, FORM_KEY);
127       // add the name bean to the validator as a resource 
128       // for the validations to be performed on.
129       validator.setParameter(Validator.BEAN_PARAM, name);
130 
131       // Get results of the validation.
132       ValidatorResults results = null;
133       
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.
152     */
153    public void testRequiredFirstName() throws ValidatorException {
154       // Create bean to run test on.
155       NameBean name = new NameBean();
156       name.setFirstName("Joe");
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 passed.", 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 last name if it is blank.
186     */
187    public void testRequiredLastNameBlank() throws ValidatorException {
188       // Create bean to run test on.
189       NameBean name = new NameBean();
190       name.setLastName("");
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 failed.", !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.
220     */
221    public void testRequiredLastName() throws ValidatorException {
222       // Create bean to run test on.
223       NameBean name = new NameBean();
224       name.setLastName("Smith");
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 passed.", lastNameResult.isValid(ACTION));
250 
251    }
252 
253    /***
254     * Tests the required validation for first and last name.
255     */
256    public void testRequiredName() throws ValidatorException {
257       // Create bean to run test on.
258       NameBean name = new NameBean();
259       name.setFirstName("Joe");
260       name.setLastName("Smith");
261       
262       // Construct validator based on the loaded resources 
263       // and the form key
264       Validator validator = new Validator(resources, FORM_KEY);
265       // add the name bean to the validator as a resource 
266       // for the validations to be performed on.
267       validator.setParameter(Validator.BEAN_PARAM, name);
268 
269       // Get results of the validation.
270       ValidatorResults results = null;
271       
272       results = validator.validate();
273       
274       assertNotNull("Results are null.", results);
275       
276       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
277       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
278       
279       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
280       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
281       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
282       
283       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
284       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
285       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
286    }
287    
288 }