    1   /*
    2    * $Id: CalendarValidatorTest.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.text.Format;
    24  import java.text.DateFormat;
    25  import java.util.Calendar;
    26  import java.util.Date;
    27  import java.util.Locale;
    28  import java.util.TimeZone;
    29
    30  /***
    31   * Test Case for CalendarValidator.
    32   *
    33   * @version $Revision: 371174 $ $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
    34   */
    35  public class CalendarValidatorTest extends BaseCalendarValidatorTest {
    36
    37      private static final int DATE_2005_11_23 = 20051123;
    38      private static final int TIME_12_03_45   = 120345;
    39
    40      private CalendarValidator calValidator;
    41
    42      /***
    43       * Main
    44       * @param args arguments
    45       */
    46      public static void main(String[] args) {
    47          junit.textui.TestRunner.run(CalendarValidatorTest.class);
    48      }
    49
    50      /***
    51       * Constructor
    52       * @param name test name
    53       */
    54      public CalendarValidatorTest(String name) {
    55          super(name);
    56      }
    57
    58      protected void setUp() throws Exception {
    59          super.setUp();
    60          calValidator = new CalendarValidator();
    61          validator         = calValidator;
    62      }
    63
    64      /***
    65       * Test CalendarValidator validate Methods
    66       */
    67      public void testCalendarValidatorMethods() {
    68          Locale.setDefault(Locale.US);
    69          Locale locale     = Locale.GERMAN;
    70          String pattern    = "yyyy-MM-dd";
    71          String patternVal = "2005-12-31";
    72          String germanVal     = "31 Dez 2005";
    73          String germanPattern = "dd MMM yyyy";
    74          String localeVal  = "31.12.2005";
    75          String defaultVal = "12/31/05";
    76          String XXXX    = "XXXX";
    77          Date expected = createCalendar(null, 20051231, 0).getTime();
    78          assertEquals("validate(A) default", expected, CalendarValidator.getInstance().validate(defaultVal).getTime());
    79          assertEquals("validate(A) locale ", expected, CalendarValidator.getInstance().validate(localeVal, locale).getTime());
    80          assertEquals("validate(A) pattern", expected, CalendarValidator.getInstance().validate(patternVal, pattern).getTime());
    81          assertEquals("validate(A) both",    expected, CalendarValidator.getInstance().validate(germanVal, germanPattern, Locale.GERMAN).getTime());
    82
    83          assertTrue("isValid(A) default", CalendarValidator.getInstance().isValid(defaultVal));
    84          assertTrue("isValid(A) locale ", CalendarValidator.getInstance().isValid(localeVal, locale));
    85          assertTrue("isValid(A) pattern", CalendarValidator.getInstance().isValid(patternVal, pattern));
    86          assertTrue("isValid(A) both",    CalendarValidator.getInstance().isValid(germanVal, germanPattern, Locale.GERMAN));
    87
    88          assertNull("validate(B) default", CalendarValidator.getInstance().validate(XXXX));
    89          assertNull("validate(B) locale ", CalendarValidator.getInstance().validate(XXXX, locale));
    90          assertNull("validate(B) pattern", CalendarValidator.getInstance().validate(XXXX, pattern));
    91          assertNull("validate(B) both",    CalendarValidator.getInstance().validate("31 Dec 2005", germanPattern, Locale.GERMAN));
    92
    93          assertFalse("isValid(B) default", CalendarValidator.getInstance().isValid(XXXX));
    94          assertFalse("isValid(B) locale ", CalendarValidator.getInstance().isValid(XXXX, locale));
    95          assertFalse("isValid(B) pattern", CalendarValidator.getInstance().isValid(XXXX, pattern));
    96          assertFalse("isValid(B) both",    CalendarValidator.getInstance().isValid("31 Dec 2005", germanPattern, Locale.GERMAN));
    97
    98          // Test Time Zone
    99          TimeZone zone = (TimeZone.getDefault().getRawOffset() == EET.getRawOffset() ? EST : EET);
    100         Date expectedZone = createCalendar(zone, 20051231, 0).getTime();
    101         assertFalse("default/EET same ", expected.getTime() == expectedZone.getTime());
    102
    103         assertEquals("validate(C) default", expectedZone, CalendarValidator.getInstance().validate(defaultVal, zone).getTime());
    104         assertEquals("validate(C) locale ", expectedZone, CalendarValidator.getInstance().validate(localeVal, locale, zone).getTime());
    105         assertEquals("validate(C) pattern", expectedZone, CalendarValidator.getInstance().validate(patternVal, pattern, zone).getTime());
    106         assertEquals("validate(C) both",    expectedZone, CalendarValidator.getInstance().validate(germanVal, germanPattern, Locale.GERMAN, zone).getTime());
    107     }
    108
    109     /***
    110      * Test compare date methods
    111      */
    112     public void testCompare() {
    113         int sameTime = 124522;
    114         int testDate = 20050823;
    115         Calendar diffHour    = createCalendar(GMT, testDate, 115922);    // same date, different time
    116         Calendar diffMin     = createCalendar(GMT, testDate, 124422);    // same date, different time
    117         Calendar diffSec     = createCalendar(GMT, testDate, 124521);    // same date, different time
    118
    119         Calendar value       = createCalendar(GMT, testDate, sameTime);   // test value
    120         Calendar cal20050824 = createCalendar(GMT, 20050824, sameTime);   // +1 day
    121         Calendar cal20050822 = createCalendar(GMT, 20050822, sameTime);   // -1 day
    122
    123         Calendar cal20050830 = createCalendar(GMT, 20050830, sameTime);   // +1 week
    124         Calendar cal20050816 = createCalendar(GMT, 20050816, sameTime);   // -1 week
    125
    126         Calendar cal20050901 = createCalendar(GMT, 20050901, sameTime);   // +1 month
    127         Calendar cal20050801 = createCalendar(GMT, 20050801, sameTime);   // same month
    128         Calendar cal20050731 = createCalendar(GMT, 20050731, sameTime);   // -1 month
    129
    130         Calendar cal20051101 = createCalendar(GMT, 20051101, sameTime);   // +1 quarter (Feb Start)
    131         Calendar cal20051001 = createCalendar(GMT, 20051001, sameTime);   // +1 quarter
    132         Calendar cal20050701 = createCalendar(GMT, 20050701, sameTime);   // same quarter
    133         Calendar cal20050630 = createCalendar(GMT, 20050630, sameTime);   // -1 quarter
    134
    135         Calendar cal20060101 = createCalendar(GMT, 20060101, sameTime);   // +1 year
    136         Calendar cal20050101 = createCalendar(GMT, 20050101, sameTime);   // same year
    137         Calendar cal20041231 = createCalendar(GMT, 20041231, sameTime);   // -1 year
    138
    139         assertEquals("hour GT", 1, calValidator.compare(value, diffHour, Calendar.HOUR_OF_DAY));
    140         assertEquals("hour EQ", 0, calValidator.compare(value, diffMin,  Calendar.HOUR_OF_DAY));
    141         assertEquals("mins GT", 1, calValidator.compare(value, diffMin,  Calendar.MINUTE));
    142         assertEquals("mins EQ", 0, calValidator.compare(value, diffSec,  Calendar.MINUTE));
    143         assertEquals("secs GT", 1, calValidator.compare(value, diffSec,  Calendar.SECOND));
    144
    145         assertEquals("date LT", -1, calValidator.compareDates(value, cal20050824)); // +1 day
    146         assertEquals("date EQ", 0,  calValidator.compareDates(value, diffHour));    // same day, diff hour
    147         assertEquals("date(B)", 0,  calValidator.compare(value, diffHour, Calendar.DAY_OF_YEAR));    // same day, diff hour
    148         assertEquals("date GT", 1,  calValidator.compareDates(value, cal20050822)); // -1 day
    149
    150         assertEquals("week LT", -1, calValidator.compareWeeks(value, cal20050830)); // +1 week
    151         assertEquals("week =1", 0,  calValidator.compareWeeks(value, cal20050824)); // +1 day
    152         assertEquals("week =2", 0,  calValidator.compareWeeks(value, cal20050822)); // same week
    153         assertEquals("week =3", 0,  calValidator.compare(value, cal20050822, Calendar.WEEK_OF_MONTH)); // same week
    154         assertEquals("week =4", 0,  calValidator.compareWeeks(value, cal20050822)); // -1 day
    155         assertEquals("week GT", 1,  calValidator.compareWeeks(value, cal20050816)); // -1 week
    156
    157         assertEquals("mnth LT", -1, calValidator.compareMonths(value, cal20050901)); // +1 month
    158         assertEquals("mnth =1", 0,  calValidator.compareMonths(value, cal20050830)); // +1 week
    159         assertEquals("mnth =2", 0,  calValidator.compareMonths(value, cal20050801)); // same month
    160         assertEquals("mnth =3", 0,  calValidator.compareMonths(value, cal20050816)); // -1 week
    161         assertEquals("mnth GT", 1,  calValidator.compareMonths(value, cal20050731)); // -1 month
    162
    163         assertEquals("qtrA <1", -1, calValidator.compareQuarters(value, cal20051101)); // +1 quarter (Feb)
    164         assertEquals("qtrA <2", -1, calValidator.compareQuarters(value, cal20051001)); // +1 quarter
    165         assertEquals("qtrA =1", 0,  calValidator.compareQuarters(value, cal20050901)); // +1 month
    166         assertEquals("qtrA =2", 0,  calValidator.compareQuarters(value, cal20050701)); // same quarter
    167         assertEquals("qtrA =3", 0,  calValidator.compareQuarters(value, cal20050731)); // -1 month
    168         assertEquals("qtrA GT", 1,  calValidator.compareQuarters(value, cal20050630)); // -1 quarter
    169
    170         // Change quarter 1 to start in Feb
    171         assertEquals("qtrB LT", -1, calValidator.compareQuarters(value, cal20051101, 2)); // +1 quarter (Feb)
    172         assertEquals("qtrB =1", 0,  calValidator.compareQuarters(value, cal20051001, 2));  // same quarter
    173         assertEquals("qtrB =2", 0,  calValidator.compareQuarters(value, cal20050901, 2)); // +1 month
    174         assertEquals("qtrB =3", 1,  calValidator.compareQuarters(value, cal20050701, 2)); // same quarter
    175         assertEquals("qtrB =4", 1,  calValidator.compareQuarters(value, cal20050731, 2)); // -1 month
    176         assertEquals("qtrB GT", 1,  calValidator.compareQuarters(value, cal20050630, 2)); // -1 quarter
    177
    178         assertEquals("year LT", -1, calValidator.compareYears(value, cal20060101)); // +1 year
    179         assertEquals("year EQ", 0,  calValidator.compareYears(value, cal20050101)); // same year
    180         assertEquals("year GT", 1,  calValidator.compareYears(value, cal20041231)); // -1 year
    181
    182         // invalid compare
    183         try {
    184             calValidator.compare(value, value,  -1);
    185             fail("Invalid Compare field - expected IllegalArgumentException to be thrown");
    186         } catch (IllegalArgumentException e) {
    187             assertEquals("check message", "Invalid field: -1", e.getMessage());
    188         }
    189     }
    190
    191     /***
    192      * Test Date/Time style Validator (there isn't an implementation for this)
    193      */
    194     public void testDateTimeStyle() {
    195         // Set the default Locale
    196         Locale origDefault = Locale.getDefault();
    197         Locale.setDefault(Locale.UK);
    198
    199         AbstractCalendarValidator dateTimeValidator =
    200                new AbstractCalendarValidator(true, DateFormat.SHORT, DateFormat.SHORT) {
    201             protected Object processParsedValue(Object value, Format formatter) {
    202                 return value;
    203             }
    204         };
    205         assertTrue("validate(A) default", dateTimeValidator.isValid("31/12/05 14:23"));
    206         assertTrue("validate(A) locale ", dateTimeValidator.isValid("12/31/05 2:23 PM", Locale.US));
    207
    208         // Restore the original default
    209         Locale.setDefault(origDefault);
    210     }
    211
    212     /***
    213      * Test format methods
    214      */
    215     public void testFormat() {
    216         // Set the default Locale
    217         Locale origDefault = Locale.getDefault();
    218         Locale.setDefault(Locale.UK);
    219
    220         Calendar cal20050101 = createCalendar(GMT, 20051231, 11500);
    221         assertNull("null", calValidator.format(null));
    222         assertEquals("default",  "31/12/05",         calValidator.format(cal20050101));
    223         assertEquals("locale",   "12/31/05",         calValidator.format(cal20050101, Locale.US));
    224         assertEquals("patternA", "2005-12-31 01:15", calValidator.format(cal20050101, "yyyy-MM-dd HH:mm"));
    225         assertEquals("patternB", "2005-12-31 GMT",   calValidator.format(cal20050101, "yyyy-MM-dd z"));
    226         assertEquals("both",     "31 Dez 2005",      calValidator.format(cal20050101, "dd MMM yyyy", Locale.GERMAN));
    227
    228         // EST Time Zone
    229         assertEquals("EST default",  "30/12/05",         calValidator.format(cal20050101, EST));
    230         assertEquals("EST locale",   "12/30/05",         calValidator.format(cal20050101, Locale.US, EST));
    231         assertEquals("EST patternA", "2005-12-30 20:15", calValidator.format(cal20050101, "yyyy-MM-dd HH:mm", EST));
    232         assertEquals("EST patternB", "2005-12-30 EST",   calValidator.format(cal20050101, "yyyy-MM-dd z", EST));
    233         assertEquals("EST both",     "30 Dez 2005",      calValidator.format(cal20050101, "dd MMM yyyy", Locale.GERMAN, EST));
    234
    235         // Restore the original default
    236         Locale.setDefault(origDefault);
    237     }
    238
    239     /***
    240      * Test adjustToTimeZone() method
    241      */
    242     public void testAdjustToTimeZone() {
    243
    244         Calendar calEST = createCalendar(EST, DATE_2005_11_23, TIME_12_03_45);
    245         Date dateEST = calEST.getTime();
    246
    247         Calendar calGMT = createCalendar(GMT, DATE_2005_11_23, TIME_12_03_45);
    248         Date dateGMT = calGMT.getTime();
    249
    250         Calendar calCET = createCalendar(EET, DATE_2005_11_23, TIME_12_03_45);
    251         Date dateCET = calCET.getTime();
    252
    253         // Check the dates don't match
    254         assertFalse("Check GMT != CET", dateGMT.getTime() == dateCET.getTime());
    255         assertFalse("Check GMT != EST", dateGMT.getTime() == dateEST.getTime());
    256         assertFalse("Check CET != EST", dateCET.getTime() == dateEST.getTime());
    257
    258         // EST to GMT and back
    259         CalendarValidator.adjustToTimeZone(calEST, GMT);
    260         assertEquals("EST to GMT", dateGMT, calEST.getTime());
    261         assertFalse("Check EST = GMT", dateEST == calEST.getTime());
    262         CalendarValidator.adjustToTimeZone(calEST, EST);
    263         assertEquals("back to EST", dateEST, calEST.getTime());
    264         assertFalse("Check EST != GMT", dateGMT == calEST.getTime());
    265
    266         // CET to GMT and back
    267         CalendarValidator.adjustToTimeZone(calCET, GMT);
    268         assertEquals("CET to GMT", dateGMT, calCET.getTime());
    269         assertFalse("Check CET = GMT", dateCET == calCET.getTime());
    270         CalendarValidator.adjustToTimeZone(calCET, EET);
    271         assertEquals("back to CET", dateCET, calCET.getTime());
    272         assertFalse("Check CET != GMT", dateGMT == calCET.getTime());
    273
    274         // Adjust to TimeZone with Same rules
    275         Calendar calUTC = createCalendar(UTC, DATE_2005_11_23, TIME_12_03_45);
    276         assertTrue("SAME: UTC = GMT",  UTC.hasSameRules(GMT));
    277         assertEquals("SAME: Check time (A)", calUTC.getTime(), calGMT.getTime());
    278         assertFalse("SAME: Check GMT(A)", GMT.equals(calUTC.getTimeZone()));
    279         assertTrue("SAME: Check UTC(A)",  UTC.equals(calUTC.getTimeZone()));
    280         CalendarValidator.adjustToTimeZone(calUTC, GMT);
    281         assertEquals("SAME: Check time (B)", calUTC.getTime(), calGMT.getTime());
    282         assertTrue("SAME: Check GMT(B)", GMT.equals(calUTC.getTimeZone()));
    283         assertFalse("SAME: Check UTC(B)",  UTC.equals(calUTC.getTimeZone()));
    284     }
    285
    286 }