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