1   /*
2    * $Id: RequiredIfTest.java 329871 2005-10-31 17:50:55Z niallp $
3    * $Rev: 329871 $
4    * $Date: 2005-10-31 17:50:55 +0000 (Mon, 31 Oct 2005) $
5    *
6    * ====================================================================
7    * Copyright 2001-2005 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  
23  package org.apache.commons.validator;
24  
25  import java.io.IOException;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.xml.sax.SAXException;
31                                                            
32  /***                                                       
33   * Performs Validation Test.
34   */                                                       
35  public class RequiredIfTest extends TestCommon {
36     
37     /***
38      * The key used to retrieve the set of validation 
39      * rules from the xml file.
40      */
41     protected static String FORM_KEY = "nameForm";   
42  
43     /***
44      * The key used to retrieve the validator action.
45      */
46     protected static String ACTION = "requiredif";
47  
48     public RequiredIfTest(String name) {                  
49         super(name);                                      
50     }                                                     
51  
52     /***
53      * Start the tests.
54      *
55      * @param theArgs the arguments. Not used
56      */
57     public static void main(String[] theArgs) {
58         junit.awtui.TestRunner.main(new String[] {RequiredIfTest.class.getName()});
59     }
60  
61     /***
62      * @return a test suite (<code>TestSuite</code>) that includes all methods
63      *         starting with "test"
64      */
65     public static Test suite() {
66         // All methods starting with "test" will be executed in the test suite.
67         return new TestSuite(RequiredIfTest.class);
68     }
69  
70     /***
71      * Load <code>ValidatorResources</code> from 
72      * validator-requiredif.xml.
73      */
74     protected void setUp() throws IOException, SAXException {
75        // Load resources
76        loadResources("RequiredIfTest-config.xml");
77     }
78  
79     protected void tearDown() {
80     }
81  
82     /***
83      * With nothing provided, we should pass since the fields only fail on
84      * null if the other field is non-blank.
85      */
86     public void testRequired() throws ValidatorException {
87        // Create bean to run test on.
88        NameBean name = new NameBean();
89        
90        // Construct validator based on the loaded resources 
91        // and the form key
92        Validator validator = new Validator(resources, FORM_KEY);
93        // add the name bean to the validator as a resource 
94        // for the validations to be performed on.
95        validator.setParameter(Validator.BEAN_PARAM, name);
96  
97        // Get results of the validation.
98        ValidatorResults results = null;
99        
100       // throws ValidatorException, 
101       // but we aren't catching for testing 
102       // since no validation methods we use 
103       // throw this
104       results = validator.validate();
105       
106       assertNotNull("Results are null.", results);
107       
108       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
109       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
110       
111       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
112       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
113       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
114       
115       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
116       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
117       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
118    }
119 
120    /***
121     * Tests the required validation for first name if it is blank.
122     */
123    public void testRequiredFirstNameBlank() throws ValidatorException {
124       // Create bean to run test on.
125       NameBean name = new NameBean();
126       name.setFirstName("");
127       name.setLastName("Test");
128       
129       // Construct validator based on the loaded resources 
130       // and the form key
131       Validator validator = new Validator(resources, FORM_KEY);
132       // add the name bean to the validator as a resource 
133       // for the validations to be performed on.
134       validator.setParameter(Validator.BEAN_PARAM, name);
135 
136       // Get results of the validation.
137       ValidatorResults results = null;
138       
139       results = validator.validate();
140       
141       assertNotNull("Results are null.", results);
142       
143       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
144       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
145       
146       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
147       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
148       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
149       
150       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
151       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
152       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
153    }
154 
155    /***
156     * Tests the required validation for last name.
157     */
158    public void testRequiredFirstName() throws ValidatorException {
159       // Create bean to run test on.
160       NameBean name = new NameBean();
161       name.setFirstName("Test");
162       name.setLastName("Test");
163       
164       // Construct validator based on the loaded resources 
165       // and the form key
166       Validator validator = new Validator(resources, FORM_KEY);
167       // add the name bean to the validator as a resource 
168       // for the validations to be performed on.
169       validator.setParameter(Validator.BEAN_PARAM, name);
170 
171       // Get results of the validation.
172       ValidatorResults results = null;
173       
174       results = validator.validate();
175       
176       assertNotNull("Results are null.", results);
177       
178       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
179       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
180       
181       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
182       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
183       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
184       
185       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
186       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
187       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
188    }
189 
190    /***
191     * Tests the required validation for last name if it is blank.
192     */
193    public void testRequiredLastNameBlank() throws ValidatorException {
194       // Create bean to run test on.
195       NameBean name = new NameBean();
196       name.setFirstName("Joe");
197       name.setLastName("");
198       
199       // Construct validator based on the loaded resources 
200       // and the form key
201       Validator validator = new Validator(resources, FORM_KEY);
202       // add the name bean to the validator as a resource 
203       // for the validations to be performed on.
204       validator.setParameter(Validator.BEAN_PARAM, name);
205 
206       // Get results of the validation.
207       ValidatorResults results = null;
208       
209       results = validator.validate();
210       
211       assertNotNull("Results are null.", results);
212       
213       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
214       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
215       
216       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
217       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
218       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
219       
220       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
221       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
222       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
223    }
224 
225    /***
226     * Tests the required validation for last name.
227     */
228    public void testRequiredLastName() throws ValidatorException {
229       // Create bean to run test on.
230       NameBean name = new NameBean();
231       name.setFirstName("Joe");
232       name.setLastName("Smith");
233       
234       // Construct validator based on the loaded resources 
235       // and the form key
236       Validator validator = new Validator(resources, FORM_KEY);
237       // add the name bean to the validator as a resource 
238       // for the validations to be performed on.
239       validator.setParameter(Validator.BEAN_PARAM, name);
240 
241       // Get results of the validation.
242       ValidatorResults results = null;
243       
244       results = validator.validate();
245       
246       assertNotNull("Results are null.", results);
247       
248       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
249       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
250       
251       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
252       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
253       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
254       
255       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
256       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
257       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
258 
259    }
260    
261 }