]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/calendar/HolidayTest.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / calendar / HolidayTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.calendar;
8
9 import java.util.Date;
10 import java.util.Locale;
11
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;
22
23 /**
24  * Tests for the <code>Holiday</code> class.
25  */
26 public class HolidayTest extends TestFmwk {
27     public static void main(String args[]) throws Exception {
28         new HolidayTest().run(args);
29     }
30     protected void init()throws Exception{
31         if(cal==null){
32             cal = new GregorianCalendar(1, 0, 1);
33             longTimeAgo = cal.getTime();
34             now = new Date();
35         }
36     }
37     static  Calendar cal;
38     static  Date longTimeAgo;
39     static  Date now;
40     static  long awhile = 3600L * 24 * 28; // 28 days
41
42     public void TestAPI() {
43         {
44             // getHolidays
45             Holiday[] holidays = Holiday.getHolidays();
46             exerciseHolidays(holidays, Locale.getDefault());
47         }
48
49         {
50             // getHolidays(Locale)
51             String[] localeNames =
52             {
53                 "en_US",
54                 "da",
55                 "da_DK",
56                 "de",
57                 "de_AT",
58                 "de_DE",
59                 "el",
60                 "el_GR",
61                 "en",
62                 "en_CA",
63                 "en_GB",
64                 "es",
65                 "es_MX",
66                 "fr",
67                 "fr_CA",
68                 "fr_FR",
69                 "it",
70                 "it_IT",
71                 "iw",
72                 "iw_IL",
73                 "ja",
74                 "ja_JP",
75             };
76
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);
81             }
82         }
83     }
84
85     void exerciseHolidays(Holiday[] holidays, Locale locale) {
86         for (int i = 0; i < holidays.length; ++i) {
87             exerciseHoliday(holidays[i], locale);
88         }
89     }
90
91     void exerciseHoliday(Holiday h, Locale locale) {
92         logln("holiday: " + h.getDisplayName());
93         logln("holiday in " + locale + ": " + h.getDisplayName(locale));
94
95         Date first = h.firstAfter(longTimeAgo);
96         logln("firstAfter: " + longTimeAgo + " is " + first);
97         if (first == null) {
98             first = longTimeAgo;
99         }
100         first.setTime(first.getTime() + awhile);
101
102         Date second = h.firstBetween(first, now);
103         logln("firstBetween: " + first + " and " + now + " is " + second);
104         if (second == null) {
105             second = now;
106         }
107
108         logln("is on " + first + ": " + h.isOn(first));
109         logln("is on " + now + ": " + h.isOn(now));
110         logln(
111               "is between "
112               + first
113               + " and "
114               + now
115               + ": "
116               + h.isBetween(first, now));
117         logln(
118               "is between "
119               + first
120               + " and "
121               + second
122               + ": "
123               + h.isBetween(first, second));
124
125         //        logln("rule: " + h.getRule().toString());
126
127         //        h.setRule(h.getRule());
128     }
129     
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());
134
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));
145         rdr.add(xbd, null);
146         rdr.add(nbd, new SimpleDateRule(Calendar.DECEMBER, 17, Calendar.MONDAY, false));
147         rdr.add(ybd, null);
148
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));
156         
157         //getRule, setRule
158         logln("The rule in the holiday: " + h[1].getRule());
159         exerciseHoliday(h[1], Locale.getDefault());
160         h[1].setRule(rdr);
161         logln("Set the new rule to the SimpleHoliday ...");
162         if (!rdr.equals(h[1].getRule())) {
163             errln("FAIL: getRule and setRule not matched.");
164         }
165         exerciseHoliday(h[1], Locale.getDefault());
166     }
167
168     public void TestIsOn() {
169         // jb 1901
170         SimpleHoliday sh = new SimpleHoliday(Calendar.AUGUST, 15, "Doug's Day", 1958, 2058);
171         
172         Calendar gcal = new GregorianCalendar();
173         gcal.clear();
174         gcal.set(Calendar.YEAR, 2000);
175         gcal.set(Calendar.MONTH, Calendar.AUGUST);
176         gcal.set(Calendar.DAY_OF_MONTH, 15);
177         
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();
196
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) {
200             Date d = dates[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!");
206             }
207             Date h = sh.firstAfter(d);
208             logln("firstAfter: " + h);
209         }
210     }
211     
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");
222                 }
223             }
224         }
225     }
226 }