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.text.DateFormat;
25 import java.text.ParseException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.Locale;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 import org.apache.commons.validator.util.ValidatorUtils;
36
37 /***
38 * Performs Validation Test.
39 */
40 public class ValidatorTest extends TestCase {
41
42 public ValidatorTest(String name) {
43 super(name);
44 }
45
46 /***
47 * Start the tests.
48 *
49 * @param theArgs the arguments. Not used
50 */
51 public static void main(String[] theArgs) {
52 junit.awtui.TestRunner.main(new String[] {ValidatorTest.class.getName()});
53 }
54
55 /***
56 * @return a test suite (<code>TestSuite</code>) that includes all methods
57 * starting with "test"
58 */
59 public static Test suite() {
60
61 return new TestSuite(ValidatorTest.class);
62 }
63
64 protected void setUp() {
65 }
66
67 protected void tearDown() {
68 }
69
70 /***
71 * Verify that one value generates an error and the other passes. The validation
72 * method being tested returns an object (<code>null</code> will be considered an error).
73 */
74 public void testManualObject() {
75
76 String property = "date";
77
78 String action = "date";
79 ValidatorResources resources = setupDateResources(property, action);
80
81 TestBean bean = new TestBean();
82 bean.setDate("2/3/1999");
83
84 Validator validator = new Validator(resources, "testForm");
85 validator.setParameter(Validator.BEAN_PARAM, bean);
86
87 try {
88 ValidatorResults results = validator.validate();
89
90 assertNotNull("Results are null.", results);
91
92 ValidatorResult result = results.getValidatorResult(property);
93
94 assertNotNull("Results are null.", results);
95
96 assertTrue("ValidatorResult does not contain '" + action + "' validator result.", result.containsAction(action));
97
98 assertTrue("Validation of the date formatting has failed.", result.isValid(action));
99 } catch (Exception e) {
100 fail("An exception was thrown while calling Validator.validate()");
101 }
102
103 bean.setDate("2/30/1999");
104
105 try {
106 ValidatorResults results = validator.validate();
107
108 assertNotNull("Results are null.", results);
109
110 ValidatorResult result = results.getValidatorResult(property);
111
112 assertNotNull("Results are null.", results);
113
114 assertTrue("ValidatorResult does not contain '" + action + "' validator result.", result.containsAction(action));
115
116 assertTrue("Validation of the date formatting has passed when it should have failed.", !result.isValid(action));
117 } catch (Exception e) {
118 fail("An exception was thrown while calling Validator.validate()");
119 }
120
121 }
122
123 public void testOnlyReturnErrors() throws ValidatorException {
124
125 String property = "date";
126
127 String action = "date";
128 ValidatorResources resources = setupDateResources(property, action);
129
130 TestBean bean = new TestBean();
131 bean.setDate("2/3/1999");
132
133 Validator validator = new Validator(resources, "testForm");
134 validator.setParameter(Validator.BEAN_PARAM, bean);
135
136 ValidatorResults results = validator.validate();
137
138 assertNotNull(results);
139
140
141 assertTrue(results.getPropertyNames().contains(property));
142
143
144 validator.setOnlyReturnErrors(true);
145 results = validator.validate();
146 assertFalse(results.getPropertyNames().contains(property));
147 }
148
149 public void testOnlyValidateField() throws ValidatorException {
150
151 String property = "date";
152
153 String action = "date";
154 ValidatorResources resources = setupDateResources(property, action);
155
156 TestBean bean = new TestBean();
157 bean.setDate("2/3/1999");
158
159 Validator validator = new Validator(resources, "testForm", property);
160 validator.setParameter(Validator.BEAN_PARAM, bean);
161
162 ValidatorResults results = validator.validate();
163
164 assertNotNull(results);
165
166
167 assertTrue(results.getPropertyNames().contains(property));
168 }
169
170
171 private ValidatorResources setupDateResources(String property, String action) {
172
173 ValidatorResources resources = new ValidatorResources();
174
175 ValidatorAction va = new ValidatorAction();
176 va.setName(action);
177 va.setClassname("org.apache.commons.validator.ValidatorTest");
178 va.setMethod("formatDate");
179 va.setMethodParams("java.lang.Object,org.apache.commons.validator.Field");
180
181 FormSet fs = new FormSet();
182 Form form = new Form();
183 form.setName("testForm");
184 Field field = new Field();
185 field.setProperty(property);
186 field.setDepends(action);
187 form.addField(field);
188 fs.addForm(form);
189
190 resources.addValidatorAction(va);
191 resources.addFormSet(fs);
192 resources.process();
193
194 return resources;
195 }
196
197 /***
198 * Verify that one value generates an error and the other passes. The validation
199 * method being tested returns a <code>boolean</code> value.
200 */
201 public void testManualBoolean() {
202 ValidatorResources resources = new ValidatorResources();
203
204 ValidatorAction va = new ValidatorAction();
205 va.setName("capLetter");
206 va.setClassname("org.apache.commons.validator.ValidatorTest");
207 va.setMethod("isCapLetter");
208 va.setMethodParams("java.lang.Object,org.apache.commons.validator.Field,java.util.List");
209
210 FormSet fs = new FormSet();
211 Form form = new Form();
212 form.setName("testForm");
213 Field field = new Field();
214 field.setProperty("letter");
215 field.setDepends("capLetter");
216 form.addField(field);
217 fs.addForm(form);
218
219 resources.addValidatorAction(va);
220 resources.addFormSet(fs);
221 resources.process();
222
223 List l = new ArrayList();
224
225 TestBean bean = new TestBean();
226 bean.setLetter("A");
227
228 Validator validator = new Validator(resources, "testForm");
229 validator.setParameter(Validator.BEAN_PARAM, bean);
230 validator.setParameter("java.util.List", l);
231
232 try {
233 validator.validate();
234 } catch (Exception e) {
235 fail("An exception was thrown while calling Validator.validate()");
236 }
237
238 assertEquals("Validation of the letter 'A'.", 0, l.size());
239
240 l.clear();
241 bean.setLetter("AA");
242
243 try {
244 validator.validate();
245 } catch (Exception e) {
246 fail("An exception was thrown while calling Validator.validate()");
247 }
248
249 assertEquals("Validation of the letter 'AA'.", 1, l.size());
250 }
251
252 /***
253 * Checks if the field is one upper case letter between 'A' and 'Z'.
254 */
255 public static boolean isCapLetter(Object bean, Field field, List l) {
256 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
257
258 if (value != null && value.length() == 1) {
259 if (value.charAt(0) >= 'A' && value.charAt(0) <= 'Z') {
260 return true;
261 } else {
262 l.add("Error");
263 return false;
264 }
265 } else {
266 l.add("Error");
267 return false;
268 }
269 }
270
271 /***
272 * Formats a <code>String</code> to a <code>Date</code>.
273 * The <code>Validator</code> will interpret a <code>null</code>
274 * as validation having failed.
275 */
276 public static Date formatDate(Object bean, Field field) {
277 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
278 Date date = null;
279
280 try {
281 DateFormat formatter = null;
282 formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
283
284 formatter.setLenient(false);
285
286 date = formatter.parse(value);
287 } catch (ParseException e) {
288 System.out.println("ValidatorTest.formatDate() - " + e.getMessage());
289 }
290
291 return date;
292 }
293
294 public class TestBean {
295 private String letter = null;
296 private String date = null;
297
298 public String getLetter() {
299 return letter;
300 }
301
302 public void setLetter(String letter) {
303 this.letter = letter;
304 }
305
306 public String getDate() {
307 return date;
308 }
309
310 public void setDate(String date) {
311 this.date = date;
312 }
313 }
314
315 }