001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.calendar;
008:
009: import java.util.Date;
010: import java.util.Locale;
011:
012: import com.ibm.icu.dev.test.TestFmwk;
013:
014: import com.ibm.icu.impl.LocaleUtility;
015: import com.ibm.icu.util.Calendar;
016: import com.ibm.icu.util.EasterHoliday;
017: import com.ibm.icu.util.GregorianCalendar;
018: import com.ibm.icu.util.Holiday;
019: import com.ibm.icu.util.RangeDateRule;
020: import com.ibm.icu.util.SimpleDateRule;
021: import com.ibm.icu.util.SimpleHoliday;
022:
023: /**
024: * Tests for the <code>Holiday</code> class.
025: */
026: public class HolidayTest extends TestFmwk {
027: public static void main(String args[]) throws Exception {
028: new HolidayTest().run(args);
029: }
030:
031: protected void init() throws Exception {
032: if (cal == null) {
033: cal = new GregorianCalendar(1, 0, 1);
034: longTimeAgo = cal.getTime();
035: now = new Date();
036: }
037: }
038:
039: static Calendar cal;
040: static Date longTimeAgo;
041: static Date now;
042: static long awhile = 3600L * 24 * 28; // 28 days
043:
044: public void TestAPI() {
045: {
046: // getHolidays
047: Holiday[] holidays = Holiday.getHolidays();
048: exerciseHolidays(holidays, Locale.getDefault());
049: }
050:
051: {
052: // getHolidays(Locale)
053: String[] localeNames = { "en_US", "da", "da_DK", "de",
054: "de_AT", "de_DE", "el", "el_GR", "en", "en_CA",
055: "en_GB", "es", "es_MX", "fr", "fr_CA", "fr_FR",
056: "it", "it_IT", "iw", "iw_IL", "ja", "ja_JP", };
057:
058: for (int i = 0; i < localeNames.length; ++i) {
059: Locale locale = LocaleUtility
060: .getLocaleFromName(localeNames[i]);
061: Holiday[] holidays = Holiday.getHolidays(locale);
062: exerciseHolidays(holidays, locale);
063: }
064: }
065: }
066:
067: void exerciseHolidays(Holiday[] holidays, Locale locale) {
068: for (int i = 0; i < holidays.length; ++i) {
069: exerciseHoliday(holidays[i], locale);
070: }
071: }
072:
073: void exerciseHoliday(Holiday h, Locale locale) {
074: logln("holiday: " + h.getDisplayName());
075: logln("holiday in " + locale + ": " + h.getDisplayName(locale));
076:
077: Date first = h.firstAfter(longTimeAgo);
078: logln("firstAfter: " + longTimeAgo + " is " + first);
079: if (first == null) {
080: first = longTimeAgo;
081: }
082: first.setTime(first.getTime() + awhile);
083:
084: Date second = h.firstBetween(first, now);
085: logln("firstBetween: " + first + " and " + now + " is "
086: + second);
087: if (second == null) {
088: second = now;
089: }
090:
091: logln("is on " + first + ": " + h.isOn(first));
092: logln("is on " + now + ": " + h.isOn(now));
093: logln("is between " + first + " and " + now + ": "
094: + h.isBetween(first, now));
095: logln("is between " + first + " and " + second + ": "
096: + h.isBetween(first, second));
097:
098: // logln("rule: " + h.getRule().toString());
099:
100: // h.setRule(h.getRule());
101: }
102:
103: public void TestCoverage() {
104: Holiday[] h = { new EasterHoliday("Ram's Easter"),
105: new SimpleHoliday(2, 29, 0, "Leap year", 1900, 2100) };
106: exerciseHolidays(h, Locale.getDefault());
107:
108: RangeDateRule rdr = new RangeDateRule();
109: rdr.add(new SimpleDateRule(7, 10));
110: Date mbd = getDate(1953, Calendar.JULY, 10);
111: Date dbd = getDate(1958, Calendar.AUGUST, 15);
112: Date nbd = getDate(1990, Calendar.DECEMBER, 17);
113: Date abd = getDate(1992, Calendar.SEPTEMBER, 16);
114: Date xbd = getDate(1976, Calendar.JULY, 4);
115: Date ybd = getDate(2003, Calendar.DECEMBER, 8);
116: rdr.add(new SimpleDateRule(Calendar.JULY, 10, Calendar.MONDAY,
117: false));
118: rdr.add(dbd, new SimpleDateRule(Calendar.AUGUST, 15,
119: Calendar.WEDNESDAY, true));
120: rdr.add(xbd, null);
121: rdr.add(nbd, new SimpleDateRule(Calendar.DECEMBER, 17,
122: Calendar.MONDAY, false));
123: rdr.add(ybd, null);
124:
125: logln("first after " + mbd + " is " + rdr.firstAfter(mbd));
126: logln("first between " + mbd + " and " + dbd + " is "
127: + rdr.firstBetween(mbd, dbd));
128: logln("first between " + dbd + " and " + nbd + " is "
129: + rdr.firstBetween(dbd, nbd));
130: logln("first between " + nbd + " and " + abd + " is "
131: + rdr.firstBetween(nbd, abd));
132: logln("first between " + abd + " and " + xbd + " is "
133: + rdr.firstBetween(abd, xbd));
134: logln("first between " + abd + " and " + null + " is "
135: + rdr.firstBetween(abd, null));
136: logln("first between " + xbd + " and " + null + " is "
137: + rdr.firstBetween(xbd, null));
138:
139: //getRule, setRule
140: logln("The rule in the holiday: " + h[1].getRule());
141: exerciseHoliday(h[1], Locale.getDefault());
142: h[1].setRule(rdr);
143: logln("Set the new rule to the SimpleHoliday ...");
144: if (!rdr.equals(h[1].getRule())) {
145: errln("FAIL: getRule and setRule not matched.");
146: }
147: exerciseHoliday(h[1], Locale.getDefault());
148: }
149:
150: public void TestIsOn() {
151: // jb 1901
152: SimpleHoliday sh = new SimpleHoliday(Calendar.AUGUST, 15,
153: "Doug's Day", 1958, 2058);
154:
155: Calendar cal = new GregorianCalendar();
156: cal.clear();
157: cal.set(Calendar.YEAR, 2000);
158: cal.set(Calendar.MONTH, Calendar.AUGUST);
159: cal.set(Calendar.DAY_OF_MONTH, 15);
160:
161: Date d0 = cal.getTime();
162: cal.add(Calendar.SECOND, 1);
163: Date d1 = cal.getTime();
164: cal.add(Calendar.SECOND, -2);
165: Date d2 = cal.getTime();
166: cal.add(Calendar.DAY_OF_MONTH, 1);
167: Date d3 = cal.getTime();
168: cal.add(Calendar.SECOND, 1);
169: Date d4 = cal.getTime();
170: cal.add(Calendar.SECOND, -2);
171: cal.set(Calendar.YEAR, 1957);
172: Date d5 = cal.getTime();
173: cal.set(Calendar.YEAR, 1958);
174: Date d6 = cal.getTime();
175: cal.set(Calendar.YEAR, 2058);
176: Date d7 = cal.getTime();
177: cal.set(Calendar.YEAR, 2059);
178: Date d8 = cal.getTime();
179:
180: Date[] dates = { d0, d1, d2, d3, d4, d5, d6, d7, d8 };
181: boolean[] isOns = { true, true, false, true, false, false,
182: true, true, false };
183: for (int i = 0; i < dates.length; ++i) {
184: Date d = dates[i];
185: logln("\ndate: " + d);
186: boolean isOn = sh.isOn(d);
187: logln("isOnDate: " + isOn);
188: if (isOn != isOns[i]) {
189: errln("date: " + d + " should be on Doug's Day!");
190: }
191: Date h = sh.firstAfter(d);
192: logln("firstAfter: " + h);
193: }
194: }
195: }
|