2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.calendar;
10 import java.util.Locale;
12 import com.ibm.icu.dev.test.TestFmwk;
13 import com.ibm.icu.impl.LocaleUtility;
14 import com.ibm.icu.util.Calendar;
15 import com.ibm.icu.util.EasterHoliday;
16 import com.ibm.icu.util.GregorianCalendar;
17 import com.ibm.icu.util.Holiday;
18 import com.ibm.icu.util.RangeDateRule;
19 import com.ibm.icu.util.SimpleDateRule;
20 import com.ibm.icu.util.SimpleHoliday;
21 import com.ibm.icu.util.ULocale;
24 * Tests for the <code>Holiday</code> class.
26 public class HolidayTest extends TestFmwk {
27 public static void main(String args[]) throws Exception {
28 new HolidayTest().run(args);
30 protected void init()throws Exception{
32 cal = new GregorianCalendar(1, 0, 1);
33 longTimeAgo = cal.getTime();
38 static Date longTimeAgo;
40 static long awhile = 3600L * 24 * 28; // 28 days
42 public void TestAPI() {
45 Holiday[] holidays = Holiday.getHolidays();
46 exerciseHolidays(holidays, Locale.getDefault());
50 // getHolidays(Locale)
51 String[] localeNames =
77 for (int i = 0; i < localeNames.length; ++i) {
78 Locale locale = LocaleUtility.getLocaleFromName(localeNames[i]);
79 Holiday[] holidays = Holiday.getHolidays(locale);
80 exerciseHolidays(holidays, locale);
85 void exerciseHolidays(Holiday[] holidays, Locale locale) {
86 for (int i = 0; i < holidays.length; ++i) {
87 exerciseHoliday(holidays[i], locale);
91 void exerciseHoliday(Holiday h, Locale locale) {
92 logln("holiday: " + h.getDisplayName());
93 logln("holiday in " + locale + ": " + h.getDisplayName(locale));
95 Date first = h.firstAfter(longTimeAgo);
96 logln("firstAfter: " + longTimeAgo + " is " + first);
100 first.setTime(first.getTime() + awhile);
102 Date second = h.firstBetween(first, now);
103 logln("firstBetween: " + first + " and " + now + " is " + second);
104 if (second == null) {
108 logln("is on " + first + ": " + h.isOn(first));
109 logln("is on " + now + ": " + h.isOn(now));
116 + h.isBetween(first, now));
123 + h.isBetween(first, second));
125 // logln("rule: " + h.getRule().toString());
127 // h.setRule(h.getRule());
130 public void TestCoverage(){
131 Holiday[] h = { new EasterHoliday("Ram's Easter"),
132 new SimpleHoliday(2, 29, 0, "Leap year", 1900, 2100)};
133 exerciseHolidays(h, Locale.getDefault());
135 RangeDateRule rdr = new RangeDateRule();
136 rdr.add(new SimpleDateRule(7, 10));
137 Date mbd = getDate(1953, Calendar.JULY, 10);
138 Date dbd = getDate(1958, Calendar.AUGUST, 15);
139 Date nbd = getDate(1990, Calendar.DECEMBER, 17);
140 Date abd = getDate(1992, Calendar.SEPTEMBER, 16);
141 Date xbd = getDate(1976, Calendar.JULY, 4);
142 Date ybd = getDate(2003, Calendar.DECEMBER, 8);
143 rdr.add(new SimpleDateRule(Calendar.JULY, 10, Calendar.MONDAY, false));
144 rdr.add(dbd, new SimpleDateRule(Calendar.AUGUST, 15, Calendar.WEDNESDAY, true));
146 rdr.add(nbd, new SimpleDateRule(Calendar.DECEMBER, 17, Calendar.MONDAY, false));
149 logln("first after " + mbd + " is " + rdr.firstAfter(mbd));
150 logln("first between " + mbd + " and " + dbd + " is " + rdr.firstBetween(mbd, dbd));
151 logln("first between " + dbd + " and " + nbd + " is " + rdr.firstBetween(dbd, nbd));
152 logln("first between " + nbd + " and " + abd + " is " + rdr.firstBetween(nbd, abd));
153 logln("first between " + abd + " and " + xbd + " is " + rdr.firstBetween(abd, xbd));
154 logln("first between " + abd + " and " + null + " is " + rdr.firstBetween(abd, null));
155 logln("first between " + xbd + " and " + null + " is " + rdr.firstBetween(xbd, null));
158 logln("The rule in the holiday: " + h[1].getRule());
159 exerciseHoliday(h[1], Locale.getDefault());
161 logln("Set the new rule to the SimpleHoliday ...");
162 if (!rdr.equals(h[1].getRule())) {
163 errln("FAIL: getRule and setRule not matched.");
165 exerciseHoliday(h[1], Locale.getDefault());
168 public void TestIsOn() {
170 SimpleHoliday sh = new SimpleHoliday(Calendar.AUGUST, 15, "Doug's Day", 1958, 2058);
172 Calendar gcal = new GregorianCalendar();
174 gcal.set(Calendar.YEAR, 2000);
175 gcal.set(Calendar.MONTH, Calendar.AUGUST);
176 gcal.set(Calendar.DAY_OF_MONTH, 15);
178 Date d0 = gcal.getTime();
179 gcal.add(Calendar.SECOND, 1);
180 Date d1 = gcal.getTime();
181 gcal.add(Calendar.SECOND, -2);
182 Date d2 = gcal.getTime();
183 gcal.add(Calendar.DAY_OF_MONTH, 1);
184 Date d3 = gcal.getTime();
185 gcal.add(Calendar.SECOND, 1);
186 Date d4 = gcal.getTime();
187 gcal.add(Calendar.SECOND, -2);
188 gcal.set(Calendar.YEAR, 1957);
189 Date d5 = gcal.getTime();
190 gcal.set(Calendar.YEAR, 1958);
191 Date d6 = gcal.getTime();
192 gcal.set(Calendar.YEAR, 2058);
193 Date d7 = gcal.getTime();
194 gcal.set(Calendar.YEAR, 2059);
195 Date d8 = gcal.getTime();
197 Date[] dates = { d0, d1, d2, d3, d4, d5, d6, d7, d8 };
198 boolean[] isOns = { true, true, false, true, false, false, true, true, false };
199 for (int i = 0; i < dates.length; ++i) {
201 logln("\ndate: " + d);
202 boolean isOn = sh.isOn(d);
203 logln("isOnDate: " + isOn);
204 if (isOn != isOns[i]) {
205 errln("date: " + d + " should be on Doug's Day!");
207 Date h = sh.firstAfter(d);
208 logln("firstAfter: " + h);
212 public void TestDisplayName() {
213 Holiday[] holidays = Holiday.getHolidays(ULocale.US);
214 for (int i = 0; i < holidays.length; ++i) {
215 Holiday h = holidays[i];
216 // only need to test one
217 // if the display names differ, we're using our data. We know these names
218 // should differ for this holiday (not all will).
219 if ("Christmas".equals(h.getDisplayName(ULocale.US))) {
220 if ("Christmas".equals(h.getDisplayName(ULocale.GERMANY))) {
221 errln("Using default name for holidays");