001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import java.io.IOException;
020: import java.util.*;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.xml.sax.SAXException;
026:
027: /**
028: * Abstracts date unit tests methods.
029: *
030: * @version $Revision$ $Date$
031: */
032: public class DateTest extends TestCommon {
033:
034: /**
035: * The key used to retrieve the set of validation
036: * rules from the xml file.
037: */
038: protected String FORM_KEY = "dateForm";
039:
040: /**
041: * The key used to retrieve the validator action.
042: */
043: protected String ACTION = "date";
044:
045: public DateTest(String name) {
046: super (name);
047: }
048:
049: /**
050: * Start the tests.
051: *
052: * @param theArgs the arguments. Not used
053: */
054: public static void main(String[] theArgs) {
055: junit.awtui.TestRunner.main(new String[] { DateTest.class
056: .getName() });
057: }
058:
059: /**
060: * Load <code>ValidatorResources</code> from
061: * validator-numeric.xml.
062: */
063: protected void setUp() throws IOException, SAXException {
064: // Load resources
065: loadResources("DateTest-config.xml");
066: }
067:
068: protected void tearDown() {
069: }
070:
071: /**
072: * @return a test suite (<code>TestSuite</code>) that includes all methods
073: * starting with "test"
074: */
075: public static Test suite() {
076: // All methods starting with "test" will be executed in the test suite.
077: return new TestSuite(DateTest.class);
078: }
079:
080: /**
081: * Tests the date validation.
082: */
083: public void testValidDate() throws ValidatorException {
084: // Create bean to run test on.
085: ValueBean info = new ValueBean();
086: info.setValue("12/01/2005");
087: valueTest(info, true);
088: }
089:
090: /**
091: * Tests the date validation.
092: */
093: public void testInvalidDate() throws ValidatorException {
094: // Create bean to run test on.
095: ValueBean info = new ValueBean();
096: info.setValue("12/01as/2005");
097: valueTest(info, false);
098: }
099:
100: /**
101: * Utlity class to run a test on a value.
102: *
103: * @param info Value to run test on.
104: * @param passed Whether or not the test is expected to pass.
105: */
106: protected void valueTest(Object info, boolean passed)
107: 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
130: + " value ValidatorResult should not be null.", result);
131: assertTrue(ACTION
132: + " value ValidatorResult should contain the '"
133: + ACTION + "' action.", result.containsAction(ACTION));
134: assertTrue(ACTION + " value ValidatorResult for the '" + ACTION
135: + "' action should have "
136: + (passed ? "passed" : "failed") + ".",
137: (passed ? result.isValid(ACTION) : !result
138: .isValid(ACTION)));
139: }
140:
141: }
|