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 MultipleTest 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 = "required";
40  
41  
42  
43     public MultipleTest(String name) {
44         super(name);
45     }
46  
47     /**
48      * Load <code>ValidatorResources</code> from
49      * validator-multipletest.xml.
50      */
51     @Override
52  protected void setUp() throws IOException, SAXException {
53        // Load resources
54        loadResources("MultipleTests-config.xml");
55     }
56  
57     @Override
58  protected void tearDown() {
59     }
60  
61     /**
62      * With nothing provided, we should fail both because both are required.
63      */
64     public void testBothBlank() throws ValidatorException {
65        // Create bean to run test on.
66        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
67  
68        // Construct validator based on the loaded resources
69        // and the form key
70        Validator validator = new Validator(resources, FORM_KEY);
71        // add the name bean to the validator as a resource
72        // for the validations to be performed on.
73        validator.setParameter(Validator.BEAN_PARAM, name);
74  
75        // Get results of the validation.
76        ValidatorResults results = null;
77  
78        // throws ValidatorException,
79        // but we aren't catching for testing
80        // since no validation methods we use
81        // throw this
82        results = validator.validate();
83  
84        assertNotNull("Results are null.", results);
85  
86        ValidatorResult firstNameResult = results.getValidatorResult("firstName");
87        ValidatorResult lastNameResult = results.getValidatorResult("lastName");
88  
89        assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
90        assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
91        assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
92  
93        assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
94        assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
95        assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
96        assertTrue("Last Name ValidatorResults should not contain the 'int' action.", !lastNameResult.containsAction("int"));
97     }
98  
99     /**
100     * If the first name fails required, and the second test fails int, we should get two errors.
101     */
102    public void testRequiredFirstNameBlankLastNameShort() throws ValidatorException {
103       // Create bean to run test on.
104       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
105       name.setFirstName("");
106       name.setLastName("Test");
107 
108       // Construct validator based on the loaded resources
109       // and the form key
110       Validator validator = new Validator(resources, FORM_KEY);
111       // add the name bean to the validator as a resource
112       // for the validations to be performed on.
113       validator.setParameter(Validator.BEAN_PARAM, name);
114 
115       // Get results of the validation.
116       ValidatorResults results = null;
117 
118       results = validator.validate();
119 
120       assertNotNull("Results are null.", results);
121 
122       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
123       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
124 
125       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
126       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
127       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
128 
129       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
130       assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int"));
131       assertTrue("Last Name ValidatorResult for the 'int' action should have failed.", !lastNameResult.isValid("int"));
132    }
133 
134    /**
135     * If the first name is there, and the last name fails int, we should get one error.
136     */
137    public void testRequiredLastNameShort() throws ValidatorException {
138       // Create bean to run test on.
139       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
140       name.setFirstName("Test");
141       name.setLastName("Test");
142 
143       // Construct validator based on the loaded resources
144       // and the form key
145       Validator validator = new Validator(resources, FORM_KEY);
146       // add the name bean to the validator as a resource
147       // for the validations to be performed on.
148       validator.setParameter(Validator.BEAN_PARAM, name);
149 
150       // Get results of the validation.
151       ValidatorResults results = null;
152 
153       results = validator.validate();
154 
155       assertNotNull("Results are null.", results);
156 
157       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
158       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
159 
160       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
161       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
162       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
163 
164       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
165       assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int"));
166       assertTrue("Last Name ValidatorResult for the 'int' action should have failed.", !lastNameResult.isValid("int"));
167    }
168 
169    /**
170     * If first name is ok and last name is ok and is an int, no errors.
171     */
172    public void testRequiredLastNameLong() throws ValidatorException {
173       // Create bean to run test on.
174       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
175       name.setFirstName("Joe");
176       name.setLastName("12345678");
177 
178       // Construct validator based on the loaded resources
179       // and the form key
180       Validator validator = new Validator(resources, FORM_KEY);
181       // add the name bean to the validator as a resource
182       // for the validations to be performed on.
183       validator.setParameter(Validator.BEAN_PARAM, name);
184 
185       // Get results of the validation.
186       ValidatorResults results = null;
187 
188       results = validator.validate();
189 
190       assertNotNull("Results are null.", results);
191 
192       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
193       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
194 
195       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
196       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
197       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
198 
199       assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
200       assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int"));
201       assertTrue("Last Name ValidatorResult for the 'int' action should have passed.", lastNameResult.isValid("int"));
202    }
203 
204    /**
205     * If middle name is not there, then the required dependent test should fail.
206     * No other tests should run
207     *
208     * @throws ValidatorException
209     */
210    public void testFailingFirstDependentValidator() throws ValidatorException {
211        // Create bean to run test on.
212        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
213 
214        // Construct validator based on the loaded resources
215        // and the form key
216        Validator validator = new Validator(resources, FORM_KEY);
217        // add the name bean to the validator as a resource
218        // for the validations to be performed on.
219        validator.setParameter(Validator.BEAN_PARAM, name);
220 
221        // Get results of the validation.
222        ValidatorResults results = null;
223 
224        results = validator.validate();
225 
226        assertNotNull("Results are null.", results);
227 
228        ValidatorResult middleNameResult = results.getValidatorResult("middleName");
229 
230        assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
231 
232        assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
233        assertTrue("Middle Name ValidatorResult for the 'required' action should have failed", !middleNameResult.isValid("required"));
234 
235        assertTrue("Middle Name ValidatorResult should not contain the 'int' action.", !middleNameResult.containsAction("int"));
236 
237        assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive"));
238    }
239 
240    /**
241     * If middle name is there but not int, then the required dependent test
242     * should pass, but the int dependent test should fail. No other tests should
243     * run.
244     *
245     * @throws ValidatorException
246     */
247    public void testFailingNextDependentValidator() throws ValidatorException {
248        // Create bean to run test on.
249        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
250        name.setMiddleName("TEST");
251 
252        // Construct validator based on the loaded resources
253        // and the form key
254        Validator validator = new Validator(resources, FORM_KEY);
255        // add the name bean to the validator as a resource
256        // for the validations to be performed on.
257        validator.setParameter(Validator.BEAN_PARAM, name);
258 
259        // Get results of the validation.
260        ValidatorResults results = null;
261 
262        results = validator.validate();
263 
264        assertNotNull("Results are null.", results);
265 
266        ValidatorResult middleNameResult = results.getValidatorResult("middleName");
267 
268        assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
269 
270        assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
271        assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
272 
273        assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
274        assertTrue("Middle Name ValidatorResult for the 'int' action should have failed", !middleNameResult.isValid("int"));
275 
276        assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive"));
277    }
278 
279    /**
280     * If middle name is there and a negative int, then the required and int
281     * dependent tests should pass, but the positive test should fail.
282     *
283     * @throws ValidatorException
284     */
285    public void testPassingDependentsFailingMain() throws ValidatorException {
286        // Create bean to run test on.
287        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
288        name.setMiddleName("-2534");
289 
290        // Construct validator based on the loaded resources
291        // and the form key
292        Validator validator = new Validator(resources, FORM_KEY);
293        // add the name bean to the validator as a resource
294        // for the validations to be performed on.
295        validator.setParameter(Validator.BEAN_PARAM, name);
296 
297        // Get results of the validation.
298        ValidatorResults results = null;
299 
300        results = validator.validate();
301 
302        assertNotNull("Results are null.", results);
303 
304        ValidatorResult middleNameResult = results.getValidatorResult("middleName");
305 
306        assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
307 
308        assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
309        assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
310 
311        assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
312        assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int"));
313 
314        assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive"));
315        assertTrue("Middle Name ValidatorResult for the 'positive' action should have failed", !middleNameResult.isValid("positive"));
316    }
317 
318    /**
319     * If middle name is there and a positve int, then the required and int
320     * dependent tests should pass, and the positive test should pass.
321     *
322     * @throws ValidatorException
323     */
324    public void testPassingDependentsPassingMain() throws ValidatorException {
325        // Create bean to run test on.
326        NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
327        name.setMiddleName("2534");
328 
329        // Construct validator based on the loaded resources
330        // and the form key
331        Validator validator = new Validator(resources, FORM_KEY);
332        // add the name bean to the validator as a resource
333        // for the validations to be performed on.
334        validator.setParameter(Validator.BEAN_PARAM, name);
335 
336        // Get results of the validation.
337        ValidatorResults results = null;
338 
339        results = validator.validate();
340 
341        assertNotNull("Results are null.", results);
342 
343        ValidatorResult middleNameResult = results.getValidatorResult("middleName");
344 
345        assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
346 
347        assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
348        assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
349 
350        assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
351        assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int"));
352 
353        assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive"));
354        assertTrue("Middle Name ValidatorResult for the 'positive' action should have passed", middleNameResult.isValid("positive"));
355    }
356 }