1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
77 return new TestSuite(DateTest.class);
78 }
79
80 /***
81 * Tests the date validation.
82 */
83 public void testValidDate() throws ValidatorException {
84
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
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
109
110 Validator validator = new Validator(resources, FORM_KEY);
111
112
113 validator.setParameter(Validator.BEAN_PARAM, info);
114 validator.setParameter(Validator.LOCALE_PARAM, Locale.US);
115
116
117 ValidatorResults results = null;
118
119
120
121
122
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 }