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