    1   /*
    2    * $Id: DateValidatorTest.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 java.util.Date;
    24  import java.util.Locale;
    25  import java.util.TimeZone;
    26
    27  /***
    28   * Test Case for DateValidator.
    29   *
    30   * @version $Revision: 371174 $ $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
    31   */
    32  public class DateValidatorTest extends BaseCalendarValidatorTest {
    33
    34      private DateValidator dateValidator;
    35
    36      /***
    37       * Main
    38       * @param args arguments
    39       */
    40      public static void main(String[] args) {
    41          junit.textui.TestRunner.run(DateValidatorTest.class);
    42      }
    43
    44      /***
    45       * Constructor
    46       * @param name test name
    47       */
    48      public DateValidatorTest(String name) {
    49          super(name);
    50      }
    51
    52      /***
    53       * Set Up.
    54       * @throws Exception
    55       */
    56      protected void setUp() throws Exception {
    57          super.setUp();
    58          dateValidator = new DateValidator();
    59          validator         = dateValidator;
    60      }
    61
    62      /***
    63       * Test DateValidator validate Methods
    64       */
    65      public void testDateValidatorMethods() {
    66          Locale.setDefault(Locale.US);
    67          Locale locale     = Locale.GERMAN;
    68          String pattern    = "yyyy-MM-dd";
    69          String patternVal = "2005-12-31";
    70          String germanVal     = "31 Dez 2005";
    71          String germanPattern = "dd MMM yyyy";
    72          String localeVal  = "31.12.2005";
    73          String defaultVal = "12/31/05";
    74          String XXXX    = "XXXX";
    75          Date expected = createCalendar(null, 20051231, 0).getTime();
    76
    77          assertEquals("validate(A) default", expected, DateValidator.getInstance().validate(defaultVal));
    78          assertEquals("validate(A) locale ", expected, DateValidator.getInstance().validate(localeVal, locale));
    79          assertEquals("validate(A) pattern", expected, DateValidator.getInstance().validate(patternVal, pattern));
    80          assertEquals("validate(A) both",    expected, DateValidator.getInstance().validate(germanVal, germanPattern, Locale.GERMAN));
    81
    82          assertTrue("isValid(A) default", DateValidator.getInstance().isValid(defaultVal));
    83          assertTrue("isValid(A) locale ", DateValidator.getInstance().isValid(localeVal, locale));
    84          assertTrue("isValid(A) pattern", DateValidator.getInstance().isValid(patternVal, pattern));
    85          assertTrue("isValid(A) both",    DateValidator.getInstance().isValid(germanVal, germanPattern, Locale.GERMAN));
    86
    87          assertNull("validate(B) default", DateValidator.getInstance().validate(XXXX));
    88          assertNull("validate(B) locale ", DateValidator.getInstance().validate(XXXX, locale));
    89          assertNull("validate(B) pattern", DateValidator.getInstance().validate(XXXX, pattern));
    90          assertNull("validate(B) both",    DateValidator.getInstance().validate("31 Dec 2005", germanPattern, Locale.GERMAN));
    91
    92          assertFalse("isValid(B) default", DateValidator.getInstance().isValid(XXXX));
    93          assertFalse("isValid(B) locale ", DateValidator.getInstance().isValid(XXXX, locale));
    94          assertFalse("isValid(B) pattern", DateValidator.getInstance().isValid(XXXX, pattern));
    95          assertFalse("isValid(B) both",    DateValidator.getInstance().isValid("31 Dec 2005", germanPattern, Locale.GERMAN));
    96
    97          // Test Time Zone
    98          TimeZone zone = (TimeZone.getDefault().getRawOffset() == EET.getRawOffset() ? EST : EET);
    99          Date expectedZone = createCalendar(zone, 20051231, 0).getTime();
    100         assertFalse("default/zone same "+zone, expected.getTime() == expectedZone.getTime());
    101
    102         assertEquals("validate(C) default", expectedZone, DateValidator.getInstance().validate(defaultVal, zone));
    103         assertEquals("validate(C) locale ", expectedZone, DateValidator.getInstance().validate(localeVal, locale, zone));
    104         assertEquals("validate(C) pattern", expectedZone, DateValidator.getInstance().validate(patternVal, pattern, zone));
    105         assertEquals("validate(C) both",    expectedZone, DateValidator.getInstance().validate(germanVal, germanPattern, Locale.GERMAN, zone));
    106     }
    107
    108     /***
    109      * Test compare date methods
    110      */
    111     public void testCompare() {
    112         int sameTime  = 124522;
    113         int testDate = 20050823;
    114         Date diffHour    = createDate(GMT, testDate, 115922);    // same date, different time
    115
    116         Date value        = createDate(GMT, testDate, sameTime);   // test value
    117         Date date20050824 = createDate(GMT, 20050824, sameTime);   // +1 day
    118         Date date20050822 = createDate(GMT, 20050822, sameTime);   // -1 day
    119
    120         Date date20050830 = createDate(GMT, 20050830, sameTime);   // +1 week
    121         Date date20050816 = createDate(GMT, 20050816, sameTime);   // -1 week
    122
    123         Date date20050901 = createDate(GMT, 20050901, sameTime);   // +1 month
    124         Date date20050801 = createDate(GMT, 20050801, sameTime);   // same month
    125         Date date20050731 = createDate(GMT, 20050731, sameTime);   // -1 month
    126
    127         Date date20051101 = createDate(GMT, 20051101, sameTime);   // +1 quarter (Feb Start)
    128         Date date20051001 = createDate(GMT, 20051001, sameTime);   // +1 quarter
    129         Date date20050701 = createDate(GMT, 20050701, sameTime);   // same quarter
    130         Date date20050630 = createDate(GMT, 20050630, sameTime);   // -1 quarter
    131         Date date20050110 = createDate(GMT, 20050110, sameTime);   // Previous Year qtr (Fen start)
    132
    133         Date date20060101 = createDate(GMT, 20060101, sameTime);   // +1 year
    134         Date date20050101 = createDate(GMT, 20050101, sameTime);   // same year
    135         Date date20041231 = createDate(GMT, 20041231, sameTime);   // -1 year
    136
    137         assertEquals("date LT", -1, dateValidator.compareDates(value, date20050824, null)); // +1 day
    138         assertEquals("date EQ", 0,  dateValidator.compareDates(value, diffHour, null));    // same day, diff hour
    139         assertEquals("date GT", 1,  dateValidator.compareDates(value, date20050822, null)); // -1 day
    140
    141         assertEquals("week LT", -1, dateValidator.compareWeeks(value, date20050830, null)); // +1 week
    142         assertEquals("week =1", 0,  dateValidator.compareWeeks(value, date20050824, null)); // +1 day
    143         assertEquals("week =2", 0,  dateValidator.compareWeeks(value, date20050822, null)); // same week
    144         assertEquals("week =3", 0,  dateValidator.compareWeeks(value, date20050822, null)); // -1 day
    145         assertEquals("week GT", 1,  dateValidator.compareWeeks(value, date20050816, null)); // -1 week
    146
    147         assertEquals("mnth LT", -1, dateValidator.compareMonths(value, date20050901, null)); // +1 month
    148         assertEquals("mnth =1", 0,  dateValidator.compareMonths(value, date20050830, null)); // +1 week
    149         assertEquals("mnth =2", 0,  dateValidator.compareMonths(value, date20050801, null)); // same month
    150         assertEquals("mnth =3", 0,  dateValidator.compareMonths(value, date20050816, null)); // -1 week
    151         assertEquals("mnth GT", 1,  dateValidator.compareMonths(value, date20050731, null)); // -1 month
    152
    153         assertEquals("qtrA <1", -1, dateValidator.compareQuarters(value, date20051101, null)); // +1 quarter (Feb)
    154         assertEquals("qtrA <2", -1, dateValidator.compareQuarters(value, date20051001, null)); // +1 quarter
    155         assertEquals("qtrA =1", 0,  dateValidator.compareQuarters(value, date20050901, null)); // +1 month
    156         assertEquals("qtrA =2", 0,  dateValidator.compareQuarters(value, date20050701, null)); // same quarter
    157         assertEquals("qtrA =3", 0,  dateValidator.compareQuarters(value, date20050731, null)); // -1 month
    158         assertEquals("qtrA GT", 1,  dateValidator.compareQuarters(value, date20050630, null)); // -1 quarter
    159
    160         // Change quarter 1 to start in Feb
    161         assertEquals("qtrB LT", -1, dateValidator.compareQuarters(value, date20051101, null, 2)); // +1 quarter (Feb)
    162         assertEquals("qtrB =1", 0,  dateValidator.compareQuarters(value, date20051001, null, 2));  // same quarter
    163         assertEquals("qtrB =2", 0,  dateValidator.compareQuarters(value, date20050901, null, 2)); // +1 month
    164         assertEquals("qtrB =3", 1,  dateValidator.compareQuarters(value, date20050701, null, 2)); // same quarter
    165         assertEquals("qtrB =4", 1,  dateValidator.compareQuarters(value, date20050731, null, 2)); // -1 month
    166         assertEquals("qtrB GT", 1,  dateValidator.compareQuarters(value, date20050630, null, 2)); // -1 quarter
    167         assertEquals("qtrB prev", 1,  dateValidator.compareQuarters(value, date20050110, null, 2)); // Jan Prev year qtr
    168
    169         assertEquals("year LT", -1, dateValidator.compareYears(value, date20060101, null)); // +1 year
    170         assertEquals("year EQ", 0,  dateValidator.compareYears(value, date20050101, null)); // same year
    171         assertEquals("year GT", 1,  dateValidator.compareYears(value, date20041231, null)); // -1 year
    172
    173         // Compare using alternative TimeZone
    174         Date sameDayTwoAm    = createDate(GMT, testDate, 20000);
    175         assertEquals("date LT", -1, dateValidator.compareDates(value, date20050824, EST)); // +1 day
    176         assertEquals("date EQ", 0,  dateValidator.compareDates(value, diffHour, EST));    // same day, diff hour
    177         assertEquals("date EQ", 1,  dateValidator.compareDates(value, sameDayTwoAm, EST));    // same day, diff hour
    178         assertEquals("date GT", 1,  dateValidator.compareDates(value, date20050822, EST)); // -1 day
    179     }
    180 }