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   * Performs Validation Test.
25   *
26   * @version $Revision$
27   */
28  public class RequiredIfTest extends AbstractCommonTest {
29     
30     /**
31      * The key used to retrieve the set of validation 
32      * rules from the xml file.
33      */
34     protected static String FORM_KEY = "nameForm";   
35  
36     /**
37      * The key used to retrieve the validator action.
38      */
39     protected static String ACTION = "requiredif";
40  
41     public RequiredIfTest(String name) {                  
42         super(name);                                      
43     }                                                     
44  
45     /**
46      * Load <code>ValidatorResources</code> from 
47      * validator-requiredif.xml.
48      */
49     @Override
50  protected void setUp() throws IOException, SAXException {
51        // Load resources
52        loadResources("RequiredIfTest-config.xml");
53     }
54  
55     @Override
56  protected void tearDown() {
57     }
58  
59     /**
60      * With nothing provided, we should pass since the fields only fail on
61      * null if the other field is non-blank.
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 passed.", firstNameResult.isValid(ACTION));
91        
92        assertNotNull("Last 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 passed.", 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       name.setLastName("Test");
105       
106       // Construct validator based on the loaded resources 
107       // and the form key
108       Validator validator = new Validator(resources, FORM_KEY);
109       // add the name bean to the validator as a resource 
110       // for the validations to be performed on.
111       validator.setParameter(Validator.BEAN_PARAM, name);
112 
113       // Get results of the validation.
114       ValidatorResults results = null;
115       
116       results = validator.validate();
117       
118       assertNotNull("Results are null.", results);
119       
120       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
121       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
122       
123       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
124       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
125       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
126       
127       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
128       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
129       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
130    }
131 
132    /**
133     * Tests the required validation for last name.
134     */
135    public void testRequiredFirstName() throws ValidatorException {
136       // Create bean to run test on.
137       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
138       name.setFirstName("Test");
139       name.setLastName("Test");
140       
141       // Construct validator based on the loaded resources 
142       // and the form key
143       Validator validator = new Validator(resources, FORM_KEY);
144       // add the name bean to the validator as a resource 
145       // for the validations to be performed on.
146       validator.setParameter(Validator.BEAN_PARAM, name);
147 
148       // Get results of the validation.
149       ValidatorResults results = null;
150       
151       results = validator.validate();
152       
153       assertNotNull("Results are null.", results);
154       
155       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
156       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
157       
158       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
159       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
160       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
161       
162       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
163       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
164       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
165    }
166 
167    /**
168     * Tests the required validation for last name if it is blank.
169     */
170    public void testRequiredLastNameBlank() throws ValidatorException {
171       // Create bean to run test on.
172       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
173       name.setFirstName("Joe");
174       name.setLastName("");
175       
176       // Construct validator based on the loaded resources 
177       // and the form key
178       Validator validator = new Validator(resources, FORM_KEY);
179       // add the name bean to the validator as a resource 
180       // for the validations to be performed on.
181       validator.setParameter(Validator.BEAN_PARAM, name);
182 
183       // Get results of the validation.
184       ValidatorResults results = null;
185       
186       results = validator.validate();
187       
188       assertNotNull("Results are null.", results);
189       
190       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
191       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
192       
193       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
194       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
195       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
196       
197       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
198       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
199       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
200    }
201 
202    /**
203     * Tests the required validation for last name.
204     */
205    public void testRequiredLastName() throws ValidatorException {
206       // Create bean to run test on.
207       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
208       name.setFirstName("Joe");
209       name.setLastName("Smith");
210       
211       // Construct validator based on the loaded resources 
212       // and the form key
213       Validator validator = new Validator(resources, FORM_KEY);
214       // add the name bean to the validator as a resource 
215       // for the validations to be performed on.
216       validator.setParameter(Validator.BEAN_PARAM, name);
217 
218       // Get results of the validation.
219       ValidatorResults results = null;
220       
221       results = validator.validate();
222       
223       assertNotNull("Results are null.", results);
224       
225       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
226       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
227       
228       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
229       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
230       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
231       
232       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
233       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
234       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
235 
236    }
237    
238 }