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