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