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  import java.util.Locale;
21  
22  import org.xml.sax.SAXException;
23  
24  /**
25   * Abstracts date unit tests methods.
26   *
27   * @version $Revision$
28   */
29  public class DateTest extends AbstractCommonTest {
30      
31      /**
32       * The key used to retrieve the set of validation
33       * rules from the xml file.
34       */
35      protected String FORM_KEY = "dateForm";
36      
37      /**
38       * The key used to retrieve the validator action.
39       */
40      protected String ACTION = "date";
41  
42  
43      public DateTest(String name) {
44          super(name);
45      }
46      
47      /**
48       * Load <code>ValidatorResources</code> from 
49       * validator-numeric.xml.
50       */
51      @Override
52      protected void setUp() throws IOException, SAXException {
53          // Load resources
54          loadResources("DateTest-config.xml");
55      }
56  
57      /**
58       * Tests the date validation.
59       */
60      public void testValidDate() throws ValidatorException {
61          // Create bean to run test on.
62          ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
63          info.setValue("12/01/2005");
64          valueTest(info, true);
65      }
66  
67      /**
68       * Tests the date validation.
69       */
70      public void testInvalidDate() throws ValidatorException {
71          // Create bean to run test on.
72          ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
73          info.setValue("12/01as/2005");
74          valueTest(info, false);
75      }
76  
77      
78      /**
79       * Utlity class to run a test on a value.
80       *
81       * @param    info    Value to run test on.
82       * @param    passed    Whether or not the test is expected to pass.
83       */
84      protected void valueTest(Object info, boolean passed) throws ValidatorException {
85          // Construct validator based on the loaded resources
86          // and the form key
87          Validator validator = new Validator(resources, FORM_KEY);
88          // add the name bean to the validator as a resource
89          // for the validations to be performed on.
90          validator.setParameter(Validator.BEAN_PARAM, info);
91          validator.setParameter(Validator.LOCALE_PARAM, Locale.US);
92  
93          // Get results of the validation.
94          ValidatorResults results = null;
95  
96          // throws ValidatorException,
97          // but we aren't catching for testing
98          // since no validation methods we use
99          // throw this
100         results = validator.validate();
101 
102         assertNotNull("Results are null.", results);
103 
104         ValidatorResult result = results.getValidatorResult("value");
105 
106         assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
107         assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.", result.containsAction(ACTION));
108         assertTrue(ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
109     }
110 
111 
112 }