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