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   * Test ValidatorResults.
25   *
26   * @version $Revision$
27   */
28  public class ValidatorResultsTest extends AbstractCommonTest {
29  
30     private static final String FORM_KEY = "nameForm";
31     private static final String firstNameField  = "firstName";
32     private static final String middleNameField = "middleName";
33     private static final String lastNameField   = "lastName";
34  
35     private String firstName;
36     private String middleName;
37     private String lastName;
38  
39     /**
40      * Constructor.
41      */
42     public ValidatorResultsTest(String name) {
43         super(name);
44     }
45  
46     /**
47      * Load <code>ValidatorResources</code> from
48      * ValidatorResultsTest-config.xml.
49      */
50     @Override
51  protected void setUp() throws IOException, SAXException {
52        // Load resources
53        loadResources("ValidatorResultsTest-config.xml");
54  
55        // initialize values
56        firstName  = "foo";
57        middleName = "123";
58        lastName   = "456";
59  
60     }
61  
62     @Override
63  protected void tearDown() {
64     }
65  
66     /**
67      * Test all validations ran and passed.
68      */
69     public void testAllValid() throws ValidatorException {
70  
71        // Create bean to run test on.
72        NameBean bean = createNameBean();
73  
74        // Validate.
75        ValidatorResults results = validate(bean);
76  
77        // Check results
78        checkValidatorResult(results, firstNameField,  "required", true);
79        checkValidatorResult(results, middleNameField, "required", true);
80        checkValidatorResult(results, middleNameField, "int",      true);
81        checkValidatorResult(results, middleNameField, "positive", true);
82        checkValidatorResult(results, lastNameField,   "required", true);
83        checkValidatorResult(results, lastNameField,   "int",      true);
84  
85     }
86  
87     /**
88      * Test some validations failed and some didn't run.
89      */
90     public void testErrors() throws ValidatorException {
91  
92        middleName = "XXX";
93        lastName = null;
94  
95        // Create bean to run test on.
96        NameBean bean = createNameBean();
97  
98        // Validate.
99        ValidatorResults results = validate(bean);
100 
101       // Check results
102       checkValidatorResult(results, firstNameField,  "required", true);
103       checkValidatorResult(results, middleNameField, "required", true);
104       checkValidatorResult(results, middleNameField, "int",      false);
105       checkNotRun(results, middleNameField, "positive");
106       checkValidatorResult(results, lastNameField,   "required", false);
107       checkNotRun(results, lastNameField,   "int");
108 
109    }
110 
111    /**
112     * Check a validator has not been run for a field and the result.
113     */
114    private void checkNotRun(ValidatorResults results, String field, String action) {
115       ValidatorResult result = results.getValidatorResult(field);
116       assertNotNull(field + " result",  result);
117       assertFalse(field + "[" + action + "] run", result.containsAction(action));
118       // System.out.println(field + "[" + action + "] not run");
119    }
120 
121    /**
122     * Check a validator has run for a field and the result.
123     */
124    private void checkValidatorResult(ValidatorResults results, String field, String action, boolean expected) {
125       ValidatorResult result = results.getValidatorResult(field);
126       // System.out.println(field + "[" + action + "]=" + result.isValid(action));
127       assertNotNull(field + " result",  result);
128       assertTrue(field + "[" + action + "] not run", result.containsAction(action));
129       assertEquals(field + "[" + action + "] result", expected, result.isValid(action));
130    }
131 
132    /**
133     * Create a NameBean.
134     */
135    private NameBean createNameBean() {
136       NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
137       name.setFirstName(firstName);
138       name.setMiddleName(middleName);
139       name.setLastName(lastName);
140       return name;
141    }
142 
143    /**
144     * Validate results.
145     */
146    private ValidatorResults validate(Object bean) throws ValidatorException  {
147 
148       // Construct validator based on the loaded resources
149       // and the form key
150       Validator validator = new Validator(resources, FORM_KEY);
151 
152       // add the name bean to the validator as a resource
153       // for the validations to be performed on.
154       validator.setParameter(Validator.BEAN_PARAM, bean);
155 
156       // Get results of the validation.
157       ValidatorResults results = validator.validate();
158 
159       return results;
160 
161    }
162 
163 }