    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.*;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestSuite;
    24
    25  import org.xml.sax.SAXException;
    26
    27  /***
    28   * Abstracts date unit tests methods.
    29   *
    30   * @version $Revision$ $Date$
    31   */
    32  public class DateTest extends TestCommon {
    33
    34      /***
    35       * The key used to retrieve the set of validation
    36       * rules from the xml file.
    37       */
    38      protected String FORM_KEY = "dateForm";
    39
    40      /***
    41       * The key used to retrieve the validator action.
    42       */
    43      protected String ACTION = "date";
    44
    45
    46      public DateTest(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[]{DateTest.class.getName()});
    57      }
    58
    59      /***
    60       * Load <code>ValidatorResources</code> from
    61       * validator-numeric.xml.
    62       */
    63      protected void setUp() throws IOException, SAXException {
    64          // Load resources
    65          loadResources("DateTest-config.xml");
    66      }
    67
    68      protected void tearDown() {
    69      }
    70
    71      /***
    72       * @return a test suite (<code>TestSuite</code>) that includes all methods
    73       *         starting with "test"
    74       */
    75      public static Test suite() {
    76          // All methods starting with "test" will be executed in the test suite.
    77          return new TestSuite(DateTest.class);
    78      }
    79
    80      /***
    81       * Tests the date validation.
    82       */
    83      public void testValidDate() throws ValidatorException {
    84          // Create bean to run test on.
    85          ValueBean info = new ValueBean();
    86          info.setValue("12/01/2005");
    87          valueTest(info, true);
    88      }
    89
    90      /***
    91       * Tests the date validation.
    92       */
    93      public void testInvalidDate() throws ValidatorException {
    94          // Create bean to run test on.
    95          ValueBean info = new ValueBean();
    96          info.setValue("12/01as/2005");
    97          valueTest(info, false);
    98      }
    99
    100
    101     /***
    102      * Utlity class to run a test on a value.
    103      *
    104      * @param	info	Value to run test on.
    105      * @param	passed	Whether or not the test is expected to pass.
    106      */
    107     protected void valueTest(Object info, boolean passed) throws ValidatorException {
    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, info);
    114         validator.setParameter(Validator.LOCALE_PARAM, Locale.US);
    115
    116         // Get results of the validation.
    117         ValidatorResults results = null;
    118
    119         // throws ValidatorException,
    120         // but we aren't catching for testing
    121         // since no validation methods we use
    122         // throw this
    123         results = validator.validate();
    124
    125         assertNotNull("Results are null.", results);
    126
    127         ValidatorResult result = results.getValidatorResult("value");
    128
    129         assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
    130         assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
    131         assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
    132     }
    133
    134
    135 }