001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2006, 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.util.Calendar;
015: import com.ibm.icu.util.JapaneseCalendar;
016: import com.ibm.icu.util.TimeZone;
017: import java.text.ParsePosition;
018: import com.ibm.icu.text.SimpleDateFormat;
019: import com.ibm.icu.util.ULocale;
020:
021: /**
022: * Tests for the <code>JapaneseCalendar</code> class.
023: */
024: public class JapaneseTest extends CalendarTest {
025: public static void main(String args[]) throws Exception {
026: new JapaneseTest().run(args);
027: }
028:
029: public void TestCoverage() {
030: {
031: // new JapaneseCalendar(TimeZone)
032: JapaneseCalendar cal = new JapaneseCalendar(TimeZone
033: .getDefault());
034: if (cal == null) {
035: errln("could not create JapaneseCalendar with TimeZone");
036: }
037: }
038:
039: {
040: // new JapaneseCalendar(ULocale)
041: JapaneseCalendar cal = new JapaneseCalendar(ULocale
042: .getDefault());
043: if (cal == null) {
044: errln("could not create JapaneseCalendar with ULocale");
045: }
046: }
047:
048: {
049: // new JapaneseCalendar(TimeZone, ULocale)
050: JapaneseCalendar cal = new JapaneseCalendar(TimeZone
051: .getDefault(), ULocale.getDefault());
052: if (cal == null) {
053: errln("could not create JapaneseCalendar with TimeZone ULocale");
054: }
055: }
056:
057: {
058: // new JapaneseCalendar(Locale)
059: JapaneseCalendar cal = new JapaneseCalendar(Locale
060: .getDefault());
061: if (cal == null) {
062: errln("could not create JapaneseCalendar with Locale");
063: }
064: }
065:
066: {
067: // new JapaneseCalendar(TimeZone, Locale)
068: JapaneseCalendar cal = new JapaneseCalendar(TimeZone
069: .getDefault(), Locale.getDefault());
070: if (cal == null) {
071: errln("could not create JapaneseCalendar with TimeZone Locale");
072: }
073: }
074:
075: {
076: // new JapaneseCalendar(Date)
077: JapaneseCalendar cal = new JapaneseCalendar(new Date());
078: if (cal == null) {
079: errln("could not create JapaneseCalendar with Date");
080: }
081: }
082:
083: {
084: // new JapaneseCalendar(int year, int month, int date)
085: JapaneseCalendar cal = new JapaneseCalendar(1868,
086: Calendar.JANUARY, 1);
087: if (cal == null) {
088: errln("could not create JapaneseCalendar with year,month,date");
089: }
090: }
091:
092: {
093: // new JapaneseCalendar(int era, int year, int month, int date)
094: JapaneseCalendar cal = new JapaneseCalendar(
095: JapaneseCalendar.MEIJI, 43, Calendar.JANUARY, 1);
096: if (cal == null) {
097: errln("could not create JapaneseCalendar with era,year,month,date");
098: }
099: }
100:
101: {
102: // new JapaneseCalendar(int year, int month, int date, int hour, int minute, int second)
103: JapaneseCalendar cal = new JapaneseCalendar(1868,
104: Calendar.JANUARY, 1, 1, 1, 1);
105: if (cal == null) {
106: errln("could not create JapaneseCalendar with year,month,date,hour,min,second");
107: }
108: }
109:
110: {
111: // limits
112: JapaneseCalendar cal = new JapaneseCalendar();
113: DateFormat fmt = cal.getDateTimeFormat(DateFormat.FULL,
114: DateFormat.FULL, Locale.ENGLISH);
115:
116: cal.set(Calendar.ERA, JapaneseCalendar.MEIJI);
117: logln("date: " + cal.getTime());
118: logln("min era: " + cal.getMinimum(Calendar.ERA));
119: logln("min year: " + cal.getMinimum(Calendar.YEAR));
120: cal.set(Calendar.YEAR, cal.getActualMaximum(Calendar.YEAR));
121: logln("date: " + fmt.format(cal.getTime()));
122: cal.add(Calendar.YEAR, 1);
123: logln("date: " + fmt.format(cal.getTime()));
124: }
125:
126: {
127: // data
128: JapaneseCalendar cal = new JapaneseCalendar(1868,
129: Calendar.JANUARY, 1);
130: Date time = cal.getTime();
131:
132: String[] calendarLocales = { "en", "ja_JP" };
133:
134: String[] formatLocales = { "en", "ja" };
135: for (int i = 0; i < calendarLocales.length; ++i) {
136: String calLocName = calendarLocales[i];
137: Locale calLocale = LocaleUtility
138: .getLocaleFromName(calLocName);
139: cal = new JapaneseCalendar(calLocale);
140:
141: for (int j = 0; j < formatLocales.length; ++j) {
142: String locName = formatLocales[j];
143: Locale formatLocale = LocaleUtility
144: .getLocaleFromName(locName);
145: DateFormat format = DateFormat.getDateTimeInstance(
146: cal, DateFormat.FULL, DateFormat.FULL,
147: formatLocale);
148: logln(calLocName + "/" + locName + " --> "
149: + format.format(time));
150: }
151: }
152: }
153: }
154:
155: public void Test3860() {
156: ULocale loc = new ULocale("ja_JP@calendar=japanese");
157: Calendar cal = new JapaneseCalendar(loc);
158: DateFormat enjformat = cal.getDateTimeFormat(0, 0, new ULocale(
159: "en_JP@calendar=japanese"));
160: DateFormat format = cal.getDateTimeFormat(0, 0, loc);
161: ((SimpleDateFormat) format).applyPattern("y.M.d"); // Note: just 'y' doesn't work here.
162: ParsePosition pos = new ParsePosition(0);
163: Date aDate = format.parse("1.1.9", pos); // after the start of heisei accession. Jan 1, 1H wouldn't work because it is actually showa 64
164: String inEn = enjformat.format(aDate);
165:
166: cal.clear();
167: cal.setTime(aDate);
168: int gotYear = cal.get(Calendar.YEAR);
169: int gotEra = cal.get(Calendar.ERA);
170:
171: int expectYear = 1;
172: int expectEra = JapaneseCalendar.CURRENT_ERA;
173:
174: if ((gotYear != expectYear) || (gotEra != expectEra)) {
175: errln("Expected year " + expectYear + ", era " + expectEra
176: + ", but got year " + gotYear + " and era "
177: + gotEra + ", == " + inEn);
178: } else {
179: logln("Got year " + gotYear + " and era " + gotEra
180: + ", == " + inEn);
181: }
182: }
183:
184: public void Test5345parse() {
185: // Test parse with incomplete information
186: DateFormat fmt2 = DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US);
187: JapaneseCalendar c = new JapaneseCalendar(
188: TimeZone.getDefault(), new ULocale("en_US"));
189: SimpleDateFormat fmt = (SimpleDateFormat) c.getDateTimeFormat(
190: 1, 1, new ULocale("en_US@calendar=japanese"));
191: fmt.applyPattern("G y");
192: logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
193: //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
194: long aDateLong = -3197120400000L + 3600000L; // compensate for DST
195: Date aDate = new Date(aDateLong); //08 Sept 1868
196: logln("aDate: " + aDate.toString() + ", from " + aDateLong);
197: String str;
198: str = fmt2.format(aDate);
199: logln("Test Date: " + str);
200: str = fmt.format(aDate);
201: logln("as Japanese Calendar: " + str);
202: String expected = "Meiji 1";
203: if (!str.equals(expected)) {
204: errln("FAIL: Expected " + expected + " but got " + str);
205: }
206: Date otherDate;
207: try {
208: otherDate = fmt.parse(expected);
209: if (!otherDate.equals(aDate)) {
210: String str3;
211: // ParsePosition pp;
212: Date dd = fmt.parse(expected);
213: str3 = fmt.format(otherDate);
214: long oLong = otherDate.getTime();
215: long aLong = otherDate.getTime();
216:
217: errln("FAIL: Parse incorrect of " + expected
218: + ": wanted " + aDate + " (" + aLong
219: + "), but got " + " " + otherDate + " ("
220: + oLong + ") = " + str3 + " not "
221: + dd.toString());
222:
223: } else {
224: logln("Parsed OK: " + expected);
225: }
226: } catch (java.text.ParseException pe) {
227: errln("FAIL: ParseException: " + pe.toString());
228: pe.printStackTrace();
229: }
230: }
231:
232: private void checkExpected(Calendar c, int expected[]) {
233: final String[] FIELD_NAME = { "ERA", "YEAR", "MONTH",
234: "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH",
235: "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH",
236: "AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND",
237: "MILLISECOND", "ZONE_OFFSET", "DST_OFFSET", "YEAR_WOY",
238: "DOW_LOCAL", "EXTENDED_YEAR", "JULIAN_DAY",
239: "MILLISECONDS_IN_DAY", };
240:
241: for (int i = 0; i < expected.length; i += 2) {
242: int fieldNum = expected[i + 0];
243: int expectedVal = expected[i + 1];
244: int actualVal = c.get(fieldNum);
245:
246: if (expectedVal == actualVal) {
247: logln(FIELD_NAME[fieldNum] + ": " + actualVal);
248: } else {
249: errln("FAIL: " + FIELD_NAME[fieldNum] + ": expected "
250: + expectedVal + " got " + actualVal);
251: }
252: }
253: }
254:
255: public void Test5345calendar() {
256: logln("** testIncompleteCalendar()");
257: // Test calendar with incomplete information
258: JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault());
259: logln("test clear");
260: c.clear();
261:
262: int expected0[] = { Calendar.ERA, 0, Calendar.YEAR, -643 };
263: checkExpected(c, expected0);
264:
265: logln("test setting era");
266: c.clear();
267: c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
268:
269: int expectedA[] = { Calendar.ERA, JapaneseCalendar.MEIJI };
270: checkExpected(c, expectedA);
271:
272: logln("test setting era and year and month and date");
273: c.clear();
274: c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
275: c.set(Calendar.YEAR, 1);
276: c.set(Calendar.MONTH, Calendar.JANUARY);
277: c.set(Calendar.DATE, 1);
278:
279: int expectedC[] = { Calendar.ERA, JapaneseCalendar.MEIJI - 1 };
280: checkExpected(c, expectedC);
281:
282: logln("test setting year and month and date THEN era");
283: c.clear();
284: c.set(Calendar.YEAR, 1);
285: c.set(Calendar.MONTH, Calendar.JANUARY);
286: c.set(Calendar.DATE, 1);
287: c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
288:
289: checkExpected(c, expectedC);
290:
291: logln("test setting era and year");
292: c.clear();
293: c.set(Calendar.YEAR, 1);
294: c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
295:
296: int expectedB[] = { Calendar.ERA, JapaneseCalendar.MEIJI,
297: Calendar.YEAR, 1 };
298: checkExpected(c, expectedB);
299:
300: }
301:
302: }
|