001: /*
002: *******************************************************************************
003: * Copyright (C) 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.impl.LocaleUtility;
013: import com.ibm.icu.text.DateFormat;
014: import com.ibm.icu.text.SimpleDateFormat;
015: import com.ibm.icu.util.Calendar;
016: import com.ibm.icu.util.GregorianCalendar;
017: import com.ibm.icu.util.EthiopicCalendar;
018: import com.ibm.icu.util.TimeZone;
019: import com.ibm.icu.util.ULocale;
020:
021: /**
022: * Tests for the <code>EthiopicCalendar</code> class.
023: */
024: public class EthiopicTest extends CalendarTest {
025: public static void main(String args[]) throws Exception {
026: new EthiopicTest().run(args);
027: }
028:
029: /** Constants to save typing. */
030: public static final int MESKEREM = EthiopicCalendar.MESKEREM;
031: public static final int TEKEMT = EthiopicCalendar.TEKEMT;
032: public static final int HEDAR = EthiopicCalendar.HEDAR;
033: public static final int TAHSAS = EthiopicCalendar.TAHSAS;
034: public static final int TER = EthiopicCalendar.TER;
035: public static final int YEKATIT = EthiopicCalendar.YEKATIT;
036: public static final int MEGABIT = EthiopicCalendar.MEGABIT;
037: public static final int MIAZIA = EthiopicCalendar.MIAZIA;
038: public static final int GENBOT = EthiopicCalendar.GENBOT;
039: public static final int SENE = EthiopicCalendar.SENE;
040: public static final int HAMLE = EthiopicCalendar.HAMLE;
041: public static final int NEHASSE = EthiopicCalendar.NEHASSE;
042: public static final int PAGUMEN = EthiopicCalendar.PAGUMEN;
043:
044: /* DY[20050507] I don't know what this is for yet:
045: public void TestRoll() {
046: int[][] tests = new int[][] {
047: // input roll by output
048: // year month day field amount year month day
049:
050: { 0001, QIDAH, 2, MONTH, 1, 0001, HIJJAH, 2 }, // non-leap years
051: { 0001, QIDAH, 2, MONTH, 2, 0001, MUHARRAM, 2 },
052: { 0001, QIDAH, 2, MONTH, -1, 0001, SHAWWAL, 2 },
053: { 0001, MUHARRAM, 2, MONTH, 12, 0001, MUHARRAM, 2 },
054: { 0001, MUHARRAM, 2, MONTH, 13, 0001, SAFAR, 2 },
055:
056: { 0001, HIJJAH, 1, DATE, 30, 0001, HIJJAH, 2 }, // 29-day month
057: { 0002, HIJJAH, 1, DATE, 31, 0002, HIJJAH, 2 }, // 30-day month
058:
059: // Try some rolls that require other fields to be adjusted
060: { 0001, MUHARRAM, 30, MONTH, 1, 0001, SAFAR, 29 },
061: { 0002, HIJJAH, 30, YEAR, -1, 0001, HIJJAH, 29 },
062: };
063:
064: EthiopicCalendar cal = newCivil();
065:
066: doRollAdd(ROLL, cal, tests);
067: }
068: */
069:
070: /* Test dates from:
071: * "The Amharic Letters of Emperor Theodore of Ethiopia to Queen Victoria and
072: * Her Special Envoy", David Appleyard, Girma Selasse Asfaw, Oxford University Press,
073: * June 1 1979, ISBN: 0856726605, Longwood Pr Ltd
074: *
075: * Ethiopic Gregorian JD
076: * 20/02/1855 29/10/1862 2401443
077: * 29/10/1857 05/07/1865 2402423
078: * 22/05/1858 29/01/1866 2402631
079: * 10/08/1858 17/04/1866 2402709
080: * 28/04/1859 05/01/1867 2402972
081: * 05/05/1860 13/01/1868 2403345
082: *
083: * --------------------------------------------------
084: *
085: * From the Calendrica applet: http://emr.cs.iit.edu/home/reingold/calendar-book/Calendrica.html
086: *
087: * Ethiopic Gregorian JD
088: * 07/05/-8 01/01/0000 1721060
089: * 08/05/-7 01/01/0001 1721426
090: * 06/13/-1 27/08/0007 1723855
091: *
092: * 01/01/0000 28/08/0007 1723856
093: * 01/01/0001 27/08/0008 1724221
094: * 01/01/0002 27/08/0009 1724586
095: * 01/01/0003 27/08/0010 1724951
096: * 01/01/0004 28/08/0011 1724537
097: * 05/13/0000 26/08/0008 1724220
098: * 05/13/0001 26/08/0009 1724585
099: * 05/13/0002 26/08/0010 1724950
100: * 05/13/0003 26/08/0011 1725315
101: * 06/13/0003 27/08/0011 1725316 first ethiopian leap year
102: * 05/13/0004 26/08/0012 1725561
103: *
104: * 06/02/1575 13/10/1582 2299159
105: * 07/02/1575 14/10/1582 2299160 Julian 04/10/1582
106: * 08/02/1575 15/10/1582 2299161
107: * 09/02/1575 16/10/1582 2299162
108: *
109: * 23/04/1892 01/01/1900 2415021
110: * 23/04/1997 01/01/2005 2453372
111: * 05/13/2000 10/09/2008 2454720
112: */
113:
114: /** A huge list of test cases to make sure that computeTime and computeFields
115: * work properly for a wide range of data in the civil calendar.
116: */
117: public void TestCases() {
118: final TestCase[] tests = {
119: //
120: // The months in this table are 1-based rather than 0-based,
121: // because it's easier to edit that way.
122: // Ethiopic
123: // Julian Day Era Year Month Day WkDay Hour Min Sec
124: //
125: // Dates from "Emporer Theodore..."
126:
127: new TestCase(2401442.5, 1, 1855, 2, 20, WED, 0, 0, 0), // Gregorian: 29/10/1862
128: new TestCase(2402422.5, 1, 1857, 10, 29, WED, 0, 0, 0), // Gregorian: 05/07/1865
129: new TestCase(2402630.5, 1, 1858, 5, 22, MON, 0, 0, 0), // Gregorian: 29/01/1866
130: new TestCase(2402708.5, 1, 1858, 8, 10, TUE, 0, 0, 0), // Gregorian: 17/04/1866
131: new TestCase(2402971.5, 1, 1859, 4, 28, SAT, 0, 0, 0), // Gregorian: 05/01/1867
132: new TestCase(2403344.5, 1, 1860, 5, 5, MON, 0, 0, 0), // Gregorian: 13/01/1868
133:
134: // Miscellaneous:
135: /* Skip these tests until JD bug fixed in the Gregorian calendar:
136: * http://www.jtcsv.com/cgibin/icu-bugs/incoming?id=4406;page=2;user=guest
137: */
138: new TestCase(1721059.5, 0, -8, 5, 7, SAT, 0, 0, 0), // Gregorian: 01/01/0000
139: new TestCase(1721425.5, 0, -7, 5, 8, MON, 0, 0, 0), // Gregorian: 01/01/0001
140: new TestCase(1723854.5, 0, -1, 13, 6, MON, 0, 0, 0), // Gregorian: 27/08/0007
141:
142: new TestCase(1723855.5, 1, 0, 1, 1, TUE, 0, 0, 0), // Gregorian: 28/08/0007
143: new TestCase(1724220.5, 1, 1, 1, 1, WED, 0, 0, 0), // Gregorian: 27/08/0008
144: new TestCase(1724585.5, 1, 2, 1, 1, THU, 0, 0, 0), // Gregorian: 27/08/0009
145: new TestCase(1724950.5, 1, 3, 1, 1, FRI, 0, 0, 0), // Gregorian: 27/08/0010
146:
147: // new TestCase(1724536.5, 1, 4, 1, 1, SUN, 0, 0, 0), // Gregorian: 28/08/0011
148: new TestCase(1725316.5, 1, 4, 1, 1, SUN, 0, 0, 0), // Gregorian: 28/08/0011 - dlf
149: new TestCase(1724219.5, 1, 0, 13, 5, TUE, 0, 0, 0), // Gregorian: 26/08/0008
150: new TestCase(1724584.5, 1, 1, 13, 5, WED, 0, 0, 0), // Gregorian: 26/08/0009
151: new TestCase(1724949.5, 1, 2, 13, 5, THU, 0, 0, 0), // Gregorian: 26/08/0010
152: new TestCase(1725314.5, 1, 3, 13, 5, FRI, 0, 0, 0), // Gregorian: 26/08/0011
153: new TestCase(1725315.5, 1, 3, 13, 6, SAT, 0, 0, 0), // Gregorian: 27/08/0011 - first ethiopic leap year
154: // new TestCase(1725560.5, 1, 4, 13, 5, SUN, 0, 0, 0), // Gregorian: 26/08/0012 - dlf
155: new TestCase(1725680.5, 1, 4, 13, 5, SUN, 0, 0, 0), // Gregorian: 26/08/0012
156: new TestCase(2299158.5, 1, 1575, 2, 6, WED, 0, 0, 0), // Gregorian: 13/10/1582
157: new TestCase(2299159.5, 1, 1575, 2, 7, THU, 0, 0, 0), // Gregorian: 14/10/1582 Julian 04/10/1582
158:
159: new TestCase(2299160.5, 1, 1575, 2, 8, FRI, 0, 0, 0), // Gregorian: 15/10/1582
160: new TestCase(2299161.5, 1, 1575, 2, 9, SAT, 0, 0, 0), // Gregorian: 16/10/1582
161:
162: new TestCase(2415020.5, 1, 1892, 4, 23, MON, 0, 0, 0), // Gregorian: 01/01/1900
163: new TestCase(2453371.5, 1, 1997, 4, 23, SAT, 0, 0, 0), // Gregorian: 01/01/2005
164: new TestCase(2454719.5, 1, 2000, 13, 5, WED, 0, 0, 0), // Gregorian: 10/09/2008
165: };
166:
167: EthiopicCalendar testCalendar = new EthiopicCalendar();
168: testCalendar.setLenient(true);
169: doTestCases(tests, testCalendar);
170: }
171:
172: // basic check to see that we print out eras ok
173: // eventually should modify to use locale strings and formatter appropriate to coptic calendar
174: public void TestEraStart() {
175: EthiopicCalendar cal = new EthiopicCalendar(0, 0, 1);
176: SimpleDateFormat fmt = new SimpleDateFormat(
177: "EEE MMM dd, yyyy GG");
178: assertEquals("Ethiopic Date", "Tue Jan 01, 0000 AD", fmt
179: .format(cal));
180:
181: // The gregorian calendar gets off by two days when
182: // the date gets low, unless the gregorian changeover is set to
183: // very early. The funny thing is, it's ok for dates in the year
184: // 283, but not in the year 7, and it claims to be ok until the year 4.
185: // should track down when the dates start to differ...
186:
187: GregorianCalendar gc = new GregorianCalendar();
188: gc.setGregorianChange(new Date(Long.MIN_VALUE)); // act like proleptic Gregorian
189: gc.setTime(cal.getTime());
190: assertEquals("Gregorian Date", "Tue Aug 28, 0007 AD", fmt
191: .format(gc));
192: }
193:
194: public void TestBasic() {
195: EthiopicCalendar cal = new EthiopicCalendar();
196: cal.clear();
197: cal.set(1000, 0, 30);
198: logln("1000/0/30-> " + cal.get(YEAR) + "/" + cal.get(MONTH)
199: + "/" + cal.get(DATE));
200: cal.clear();
201: cal.set(1, 0, 30);
202: logln("1/0/30 -> " + cal.get(YEAR) + "/" + cal.get(MONTH) + "/"
203: + cal.get(DATE));
204: }
205:
206: public void TestJD() {
207: int jd = EthiopicCalendar.EthiopicToJD(1567, 8, 9);
208: Integer[] l = EthiopicCalendar.getDateFromJD(jd);
209: if (l[0].intValue() == 1567 && l[1].intValue() == 8
210: && l[2].intValue() == 9) {
211: logln("EthiopicCalendar.getDateFromJD tested");
212: } else {
213: errln("EthiopicCalendar.getDateFromJD failed");
214: }
215: }
216:
217: public void TestCoverage() {
218:
219: {
220: // new EthiopicCalendar(TimeZone)
221: EthiopicCalendar cal = new EthiopicCalendar(TimeZone
222: .getDefault());
223: if (cal == null) {
224: errln("could not create EthiopicCalendar with TimeZone");
225: }
226: }
227:
228: {
229: // new EthiopicCalendar(ULocale)
230: EthiopicCalendar cal = new EthiopicCalendar(ULocale
231: .getDefault());
232: if (cal == null) {
233: errln("could not create EthiopicCalendar with ULocale");
234: }
235: }
236:
237: {
238: // new EthiopicCalendar(Locale)
239: EthiopicCalendar cal = new EthiopicCalendar(Locale
240: .getDefault());
241: if (cal == null) {
242: errln("could not create EthiopicCalendar with Locale");
243: }
244: }
245:
246: {
247: // new EthiopicCalendar(TimeZone, Locale)
248: EthiopicCalendar cal = new EthiopicCalendar(TimeZone
249: .getDefault(), Locale.getDefault());
250: if (cal == null) {
251: errln("could not create EthiopicCalendar with TimeZone,Locale");
252: }
253: }
254:
255: {
256: // new EthiopicCalendar(TimeZone, ULocale)
257: EthiopicCalendar cal = new EthiopicCalendar(TimeZone
258: .getDefault(), ULocale.getDefault());
259: if (cal == null) {
260: errln("could not create EthiopicCalendar with TimeZone,ULocale");
261: }
262: }
263:
264: {
265: // new EthiopicCalendar(Date)
266: EthiopicCalendar cal = new EthiopicCalendar(new Date());
267: if (cal == null) {
268: errln("could not create EthiopicCalendar with Date");
269: }
270: }
271:
272: {
273: // new EthiopicCalendar(int year, int month, int date)
274: EthiopicCalendar cal = new EthiopicCalendar(1997,
275: EthiopicCalendar.MESKEREM, 1);
276: if (cal == null) {
277: errln("could not create EthiopicCalendar with year,month,date");
278: }
279: }
280:
281: {
282: // new EthiopicCalendar(int year, int month, int date, int hour, int minute, int second)
283: EthiopicCalendar cal = new EthiopicCalendar(1997,
284: EthiopicCalendar.MESKEREM, 1, 1, 1, 1);
285: if (cal == null) {
286: errln("could not create EthiopicCalendar with year,month,date,hour,minute,second");
287: }
288: }
289:
290: {
291: // setCivil/isCivil
292: // operations on non-civil calendar
293: EthiopicCalendar cal = newAmeteAlemEraCalendar();
294: cal.setAmeteAlemEra(false);
295: if (cal.isAmeteAlemEra()) {
296: errln("EthiopicCalendar calendar is old system");
297: }
298:
299: Date now = new Date();
300: cal.setTime(now);
301:
302: Date then = cal.getTime();
303: if (!now.equals(then)) {
304: errln("get/set time failed with non-civil EthiopicCalendar calendar");
305: }
306:
307: logln(then.toString());
308:
309: cal.add(Calendar.MONTH, 1);
310: cal.add(Calendar.DAY_OF_MONTH, 1);
311: cal.add(Calendar.YEAR, 1);
312:
313: logln(cal.getTime().toString());
314: }
315:
316: {
317: // data
318: EthiopicCalendar cal = new EthiopicCalendar(1997,
319: EthiopicCalendar.MESKEREM, 1);
320: Date time = cal.getTime();
321:
322: String[] calendarLocales = { "am_ET", "gez_ET", "ti_ET" };
323:
324: String[] formatLocales = { "en", "am", "gez", "ti" };
325: for (int i = 0; i < calendarLocales.length; ++i) {
326: String calLocName = calendarLocales[i];
327: Locale calLocale = LocaleUtility
328: .getLocaleFromName(calLocName);
329: cal = new EthiopicCalendar(calLocale);
330:
331: for (int j = 0; j < formatLocales.length; ++j) {
332: String locName = formatLocales[j];
333: Locale formatLocale = LocaleUtility
334: .getLocaleFromName(locName);
335: DateFormat format = DateFormat.getDateTimeInstance(
336: cal, DateFormat.FULL, DateFormat.FULL,
337: formatLocale);
338: logln(calLocName + "/" + locName + " --> "
339: + format.format(time));
340: }
341: }
342: }
343: }
344:
345: private static EthiopicCalendar newAmeteAlemEraCalendar() {
346: EthiopicCalendar alemawiCalendar = new EthiopicCalendar();
347: alemawiCalendar.setAmeteAlemEra(true);
348: return alemawiCalendar;
349: }
350: }
|