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 static org.junit.jupiter.api.Assertions.assertNotNull;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import java.io.IOException;
23 import java.util.Locale;
24
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.xml.sax.SAXException;
28
29
30
31
32 public class DateTest extends AbstractCommonTest {
33
34
35
36
37 protected String FORM_KEY = "dateForm";
38
39
40
41
42 protected String ACTION = "date";
43
44
45
46
47 @BeforeEach
48 protected void setUp() throws IOException, SAXException {
49
50 loadResources("DateTest-config.xml");
51 }
52
53
54
55
56 @Test
57 public void testInvalidDate() throws ValidatorException {
58
59 final ValueBean info = new ValueBean();
60 info.setValue("12/01as/2005");
61 valueTest(info, false);
62 }
63
64
65
66
67 @Test
68 public void testValidDate() throws ValidatorException {
69
70 final ValueBean info = new ValueBean();
71 info.setValue("12/01/2005");
72 valueTest(info, true);
73 }
74
75
76
77
78
79
80
81 protected void valueTest(final Object info, final boolean passed) throws ValidatorException {
82
83
84 final Validator validator = new Validator(resources, FORM_KEY);
85
86
87 validator.setParameter(Validator.BEAN_PARAM, info);
88 validator.setParameter(Validator.LOCALE_PARAM, Locale.US);
89
90
91
92
93
94
95 final ValidatorResults results = validator.validate();
96
97 assertNotNull(results, "Results are null.");
98
99 final ValidatorResult result = results.getValidatorResult("value");
100
101 assertNotNull(result, () -> ACTION + " value ValidatorResult should not be null.");
102 assertTrue(result.containsAction(ACTION), () -> ACTION + " value ValidatorResult should contain the '" + ACTION + "' action.");
103 assertTrue(passed ? result.isValid(ACTION) : !result.isValid(ACTION),
104 () -> ACTION + " value ValidatorResult for the '" + ACTION + "' action should have " + (passed ? "passed" : "failed") + ".");
105 }
106
107 }