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