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