1   /*
2    * $Id: ValidatorResultsTest.java 384724 2006-03-10 07:55:23Z niallp $
3    * $Rev: 384724 $
4    * $Date: 2006-03-10 07:55:23 +0000 (Fri, 10 Mar 2006) $
5    *
6    * ====================================================================
7    * Copyright 2006 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  import java.util.Iterator;
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.xml.sax.SAXException;
31  
32  /***
33   * Test ValidatorResults.
34   */
35  public class ValidatorResultsTest extends TestCommon {
36  
37     private static final String FORM_KEY = "nameForm";
38     private static final String firstNameField  = "firstName";
39     private static final String middleNameField = "middleName";
40     private static final String lastNameField   = "lastName";
41  
42     private String firstName;
43     private String middleName;
44     private String lastName;
45  
46     /***
47      * Constructor.
48      */
49     public ValidatorResultsTest(String name) {
50         super(name);
51     }
52  
53     /***
54      * Start the tests.
55      *
56      * @param theArgs the arguments. Not used
57      */
58     public static void main(String[] theArgs) {
59         junit.awtui.TestRunner.main(new String[] {ValidatorResultsTest.class.getName()});
60     }
61  
62     /***
63      * @return a test suite (<code>TestSuite</code>) that includes all methods
64      *         starting with "test"
65      */
66     public static Test suite() {
67         // All methods starting with "test" will be executed in the test suite.
68         return new TestSuite(ValidatorResultsTest.class);
69     }
70  
71     /***
72      * Load <code>ValidatorResources</code> from
73      * ValidatorResultsTest-config.xml.
74      */
75     protected void setUp() throws IOException, SAXException {
76        // Load resources
77        loadResources("ValidatorResultsTest-config.xml");
78  
79        // initialize values
80        firstName  = "foo";
81        middleName = "123";
82        lastName   = "456";
83  
84     }
85  
86     protected void tearDown() {
87     }
88  
89     /***
90      * Test all validations ran and passed.
91      */
92     public void testAllValid() throws ValidatorException {
93  
94        // Create bean to run test on.
95        NameBean bean = createNameBean();
96  
97        // Validate.
98        ValidatorResults results = validate(bean);
99  
100       // Check results
101       checkValidatorResult(results, firstNameField,  "required", true);
102       checkValidatorResult(results, middleNameField, "required", true);
103       checkValidatorResult(results, middleNameField, "int",      true);
104       checkValidatorResult(results, middleNameField, "positive", true);
105       checkValidatorResult(results, lastNameField,   "required", true);
106       checkValidatorResult(results, lastNameField,   "int",      true);
107 
108    }
109 
110    /***
111     * Test some validations failed and some didn't run.
112     */
113    public void testErrors() throws ValidatorException {
114 
115       middleName = "XXX";
116       lastName = null;
117 
118       // Create bean to run test on.
119       NameBean bean = createNameBean();
120 
121       // Validate.
122       ValidatorResults results = validate(bean);
123 
124       // Check results
125       checkValidatorResult(results, firstNameField,  "required", true);
126       checkValidatorResult(results, middleNameField, "required", true);
127       checkValidatorResult(results, middleNameField, "int",      false);
128       checkNotRun(results, middleNameField, "positive");
129       checkValidatorResult(results, lastNameField,   "required", false);
130       checkNotRun(results, lastNameField,   "int");
131 
132    }
133 
134    /***
135     * Check a validator has not been run for a field and the result.
136     */
137    private void checkNotRun(ValidatorResults results, String field, String action) {
138       ValidatorResult result = results.getValidatorResult(field);
139       assertNotNull(field + " result",  result);
140       assertFalse(field + "[" + action + "] run", result.containsAction(action));
141       // System.out.println(field + "[" + action + "] not run");
142    }
143 
144    /***
145     * Check a validator has run for a field and the result.
146     */
147    private void checkValidatorResult(ValidatorResults results, String field, String action, boolean expected) {
148       ValidatorResult result = results.getValidatorResult(field);
149       // System.out.println(field + "[" + action + "]=" + result.isValid(action));
150       assertNotNull(field + " result",  result);
151       assertTrue(field + "[" + action + "] not run", result.containsAction(action));
152       assertEquals(field + "[" + action + "] result", expected, result.isValid(action));
153    }
154 
155    /***
156     * Create a NameBean.
157     */
158    private NameBean createNameBean() {
159       NameBean name = new NameBean();
160       name.setFirstName(firstName);
161       name.setMiddleName(middleName);
162       name.setLastName(lastName);
163       return name;
164    }
165 
166    /***
167     * Validate results.
168     */
169    private ValidatorResults validate(Object bean) throws ValidatorException  {
170 
171       // Construct validator based on the loaded resources
172       // and the form key
173       Validator validator = new Validator(resources, FORM_KEY);
174 
175       // add the name bean to the validator as a resource
176       // for the validations to be performed on.
177       validator.setParameter(Validator.BEAN_PARAM, bean);
178 
179       // Get results of the validation.
180       ValidatorResults results = validator.validate();
181 
182       return results;
183 
184    }
185 
186 }