1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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"); // 0 offset
35      protected static final TimeZone EST = TimeZone.getTimeZone("EST"); // - 5 hours
36      protected static final TimeZone EET = TimeZone.getTimeZone("EET"); // + 2 hours
37      protected static final TimeZone UTC = TimeZone.getTimeZone("UTC"); // + 2 hours
38  
39      protected String[] patternValid = new String[] {
40                         "2005-01-01" 
41                        ,"2005-12-31"
42                        ,"2004-02-29"    // valid leap
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"    // valid leap
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"  // zero month
65                          ,"2005-01-00"  // zero day 
66                          ,"2005-13-03"  // month invalid
67                          ,"2005-04-31"  // invalid day 
68                          ,"2005-03-32"  // invalid day 
69                          ,"2005-02-29"  // invalid leap
70                          ,"200X-01-01"  // invalid char
71                          ,"2005-0X-01"  // invalid char
72                          ,"2005-01-0X"  // invalid char
73                          ,"01/01/2005"  // invalid pattern
74                          ,"2005-01"     // invalid pattern
75                          ,"2005--01"    // invalid pattern
76                          ,"2005-01-"};  // invalid pattern
77      protected String[] localeInvalid = new String[] {
78                           "01/00/2005"  // zero month
79                          ,"00/01/2005"  // zero day 
80                          ,"13/01/2005"  // month invalid
81                          ,"04/31/2005"  // invalid day 
82                          ,"03/32/2005"  // invalid day 
83                          ,"02/29/2005"  // invalid leap
84                          ,"01/01/200X"  // invalid char
85                          ,"01/0X/2005"  // invalid char
86                          ,"0X/01/2005"  // invalid char
87                          ,"01-01-2005"  // invalid pattern
88                          ,"01/2005"     // invalid pattern
89         // --------      ,"/01/2005"    ---- passes on some JDK
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         // Create a Date or Calendar
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 }