1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator.routines;
18
19 import junit.framework.TestCase;
20 import java.util.Date;
21 import java.util.Calendar;
22 import java.util.Locale;
23 import java.util.TimeZone;
24
25 /***
26 * Base Calendar Test Case.
27 *
28 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
29 */
30 public class BaseCalendarValidatorTest extends TestCase {
31
32 protected AbstractCalendarValidator validator;
33
34 protected static final TimeZone GMT = TimeZone.getTimeZone("GMT");
35 protected static final TimeZone EST = TimeZone.getTimeZone("EST");
36 protected static final TimeZone EET = TimeZone.getTimeZone("EET");
37 protected static final TimeZone UTC = TimeZone.getTimeZone("UTC");
38
39 protected String[] patternValid = new String[] {
40 "2005-01-01"
41 ,"2005-12-31"
42 ,"2004-02-29"
43 ,"2005-04-30"
44 ,"05-12-31"
45 ,"2005-1-1"
46 ,"05-1-1"};
47 protected String[] localeValid = new String[] {
48 "01/01/2005"
49 ,"12/31/2005"
50 ,"02/29/2004"
51 ,"04/30/2005"
52 ,"12/31/05"
53 ,"1/1/2005"
54 ,"1/1/05"};
55 protected Date[] patternExpect = new Date[] {
56 createDate(null, 20050101, 0)
57 ,createDate(null, 20051231, 0)
58 ,createDate(null, 20040229, 0)
59 ,createDate(null, 20050430, 0)
60 ,createDate(null, 20051231, 0)
61 ,createDate(null, 20050101, 0)
62 ,createDate(null, 20050101, 0)};
63 protected String[] patternInvalid = new String[] {
64 "2005-00-01"
65 ,"2005-01-00"
66 ,"2005-13-03"
67 ,"2005-04-31"
68 ,"2005-03-32"
69 ,"2005-02-29"
70 ,"200X-01-01"
71 ,"2005-0X-01"
72 ,"2005-01-0X"
73 ,"01/01/2005"
74 ,"2005-01"
75 ,"2005--01"
76 ,"2005-01-"};
77 protected String[] localeInvalid = new String[] {
78 "01/00/2005"
79 ,"00/01/2005"
80 ,"13/01/2005"
81 ,"04/31/2005"
82 ,"03/32/2005"
83 ,"02/29/2005"
84 ,"01/01/200X"
85 ,"01/0X/2005"
86 ,"0X/01/2005"
87 ,"01-01-2005"
88 ,"01/2005"
89
90 ,"01//2005"}; // invalid pattern
91
92 /***
93 * Constructor
94 * @param name test name
95 */
96 public BaseCalendarValidatorTest(String name) {
97 super(name);
98 }
99
100 /***
101 * Set Up.
102 * @throws Exception
103 */
104 protected void setUp() throws Exception {
105 super.setUp();
106 }
107
108 /***
109 * Tear down
110 * @throws Exception
111 */
112 protected void tearDown() throws Exception {
113 super.tearDown();
114 validator = null;
115 }
116
117 /***
118 * Test Valid Dates with "pattern" validation
119 */
120 public void testPatternValid() {
121 for (int i = 0; i < patternValid.length; i++) {
122 String text = i + " value=[" +patternValid[i]+"] failed ";
123 Object date = validator.parse(patternValid[i], "yy-MM-dd", null, null);
124 assertNotNull("validateObj() " + text + date, date);
125 assertTrue("isValid() " + text, validator.isValid(patternValid[i], "yy-MM-dd"));
126 if (date instanceof Calendar) {
127 date = ((Calendar)date).getTime();
128 }
129 assertEquals("compare " + text, patternExpect[i], date);
130 }
131 }
132
133 /***
134 * Test Invalid Dates with "pattern" validation
135 */
136 public void testPatternInvalid() {
137 for (int i = 0; i < patternInvalid.length; i++) {
138 String text = i + " value=[" +patternInvalid[i]+"] passed ";
139 Object date = validator.parse(patternInvalid[i], "yy-MM-dd", null, null);
140 assertNull("validateObj() " + text + date, date);
141 assertFalse("isValid() " + text, validator.isValid(patternInvalid[i], "yy-MM-dd"));
142 }
143 }
144
145 /***
146 * Test Valid Dates with "locale" validation
147 */
148 public void testLocaleValid() {
149 for (int i = 0; i < localeValid.length; i++) {
150 String text = i + " value=[" +localeValid[i]+"] failed ";
151 Object date = validator.parse(localeValid[i], null, Locale.US, null);
152 assertNotNull("validateObj() " + text + date, date);
153 assertTrue("isValid() " + text, validator.isValid(localeValid[i], Locale.US));
154 if (date instanceof Calendar) {
155 date = ((Calendar)date).getTime();
156 }
157 assertEquals("compare " + text, patternExpect[i], date);
158 }
159 }
160
161 /***
162 * Test Invalid Dates with "locale" validation
163 */
164 public void testLocaleInvalid() {
165 for (int i = 0; i < localeInvalid.length; i++) {
166 String text = i + " value=[" +localeInvalid[i]+"] passed ";
167 Object date = validator.parse(localeInvalid[i], null, Locale.US, null);
168 assertNull("validateObj() " + text + date, date);
169 assertFalse("isValid() " + text, validator.isValid(localeInvalid[i], Locale.US));
170 }
171 }
172
173 /***
174 * Test Invalid Dates with "locale" validation
175 */
176 public void testFormat() {
177
178
179 Object test = validator.parse("2005-11-28", "yyyy-MM-dd", null, null);
180 assertNotNull("Test Date ", test);
181 assertEquals("Format pattern", "28.11.05", validator.format(test, "dd.MM.yy"));
182 assertEquals("Format locale", "11/28/05", validator.format(test, Locale.US));
183 }
184
185 /***
186 * Create a calendar instance for a specified time zone, date and time.
187 *
188 * @param zone The time zone
189 * @param date The date in yyyyMMdd format
190 * @param time the time in HH:mm:ss format
191 * @return the new Calendar instance.
192 */
193 protected static Calendar createCalendar(TimeZone zone, int date, int time) {
194 Calendar calendar = zone == null ? Calendar.getInstance()
195 : Calendar.getInstance(zone);
196 int year = ((date / 10000) * 10000);
197 int mth = ((date / 100) * 100) - year;
198 int day = date - (year + mth);
199 int hour = ((time / 10000) * 10000);
200 int min = ((time / 100) * 100) - hour;
201 int sec = time - (hour + min);
202 calendar.set(Calendar.YEAR, (year / 10000));
203 calendar.set(Calendar.MONTH, ((mth / 100) - 1));
204 calendar.set(Calendar.DATE, day);
205 calendar.set(Calendar.HOUR_OF_DAY, (hour / 10000));
206 calendar.set(Calendar.MINUTE, (min / 100));
207 calendar.set(Calendar.SECOND, sec);
208 calendar.set(Calendar.MILLISECOND, 0);
209 return calendar;
210 }
211
212 /***
213 * Create a date instance for a specified time zone, date and time.
214 *
215 * @param zone The time zone
216 * @param date The date in yyyyMMdd format
217 * @param time the time in HH:mm:ss format
218 * @return the new Date instance.
219 */
220 protected static Date createDate(TimeZone zone, int date, int time) {
221 Calendar calendar = createCalendar(zone, date, time);
222 return calendar.getTime();
223 }
224
225 }