1/* GENERATED SOURCE. DO NOT MODIFY. */ 2// © 2016 and later: Unicode, Inc. and others. 3// License & terms of use: http://www.unicode.org/copyright.html#License 4/* 5 ******************************************************************************* 6 * Copyright (C) 2002-2010, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10package android.icu.dev.test.calendar; 11 12import java.text.ParseException; 13import java.text.ParsePosition; 14import java.util.Date; 15import java.util.Locale; 16 17import org.junit.Test; 18 19import android.icu.impl.LocaleUtility; 20import android.icu.text.DateFormat; 21import android.icu.text.SimpleDateFormat; 22import android.icu.util.Calendar; 23import android.icu.util.JapaneseCalendar; 24import android.icu.util.TimeZone; 25import android.icu.util.ULocale; 26 27/** 28 * Tests for the <code>JapaneseCalendar</code> class. 29 */ 30public class JapaneseTest extends CalendarTestFmwk { 31 32 @Test 33 public void TestCoverage() { 34 { 35 // new JapaneseCalendar(TimeZone) 36 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault()); 37 if(cal == null){ 38 errln("could not create JapaneseCalendar with TimeZone"); 39 } 40 } 41 42 { 43 // new JapaneseCalendar(ULocale) 44 JapaneseCalendar cal = new JapaneseCalendar(ULocale.getDefault()); 45 if(cal == null){ 46 errln("could not create JapaneseCalendar with ULocale"); 47 } 48 } 49 50 { 51 // new JapaneseCalendar(TimeZone, ULocale) 52 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), ULocale.getDefault()); 53 if(cal == null){ 54 errln("could not create JapaneseCalendar with TimeZone ULocale"); 55 } 56 } 57 58 { 59 // new JapaneseCalendar(Locale) 60 JapaneseCalendar cal = new JapaneseCalendar(Locale.getDefault()); 61 if(cal == null){ 62 errln("could not create JapaneseCalendar with Locale"); 63 } 64 } 65 66 { 67 // new JapaneseCalendar(TimeZone, Locale) 68 JapaneseCalendar cal = new JapaneseCalendar(TimeZone.getDefault(), Locale.getDefault()); 69 if(cal == null){ 70 errln("could not create JapaneseCalendar with TimeZone Locale"); 71 } 72 } 73 74 { 75 // new JapaneseCalendar(Date) 76 JapaneseCalendar cal = new JapaneseCalendar(new Date()); 77 if(cal == null){ 78 errln("could not create JapaneseCalendar with Date"); 79 } 80 } 81 82 { 83 // new JapaneseCalendar(int year, int month, int date) 84 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1); 85 if(cal == null){ 86 errln("could not create JapaneseCalendar with year,month,date"); 87 } 88 } 89 90 { 91 // new JapaneseCalendar(int era, int year, int month, int date) 92 JapaneseCalendar cal = new JapaneseCalendar(JapaneseCalendar.MEIJI, 43, Calendar.JANUARY, 1); 93 if(cal == null){ 94 errln("could not create JapaneseCalendar with era,year,month,date"); 95 } 96 } 97 98 { 99 // new JapaneseCalendar(int year, int month, int date, int hour, int minute, int second) 100 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1, 1, 1, 1); 101 if(cal == null){ 102 errln("could not create JapaneseCalendar with year,month,date,hour,min,second"); 103 } 104 } 105 106 { 107 // limits 108 JapaneseCalendar cal = new JapaneseCalendar(); 109 DateFormat fmt = cal.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, Locale.ENGLISH); 110 111 cal.set(Calendar.ERA, JapaneseCalendar.MEIJI); 112 logln("date: " + cal.getTime()); 113 logln("min era: " + cal.getMinimum(Calendar.ERA)); 114 logln("min year: " + cal.getMinimum(Calendar.YEAR)); 115 cal.set(Calendar.YEAR, cal.getActualMaximum(Calendar.YEAR)); 116 logln("date: " + fmt.format(cal.getTime())); 117 cal.add(Calendar.YEAR, 1); 118 logln("date: " + fmt.format(cal.getTime())); 119 } 120 121 { 122 // data 123 JapaneseCalendar cal = new JapaneseCalendar(1868, Calendar.JANUARY, 1); 124 Date time = cal.getTime(); 125 126 String[] calendarLocales = { 127 "en", "ja_JP" 128 }; 129 130 String[] formatLocales = { 131 "en", "ja" 132 }; 133 for (int i = 0; i < calendarLocales.length; ++i) { 134 String calLocName = calendarLocales[i]; 135 Locale calLocale = LocaleUtility.getLocaleFromName(calLocName); 136 cal = new JapaneseCalendar(calLocale); 137 138 for (int j = 0; j < formatLocales.length; ++j) { 139 String locName = formatLocales[j]; 140 Locale formatLocale = LocaleUtility.getLocaleFromName(locName); 141 DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale); 142 logln(calLocName + "/" + locName + " --> " + format.format(time)); 143 } 144 } 145 } 146 } 147 148 @Test 149 public void Test3860() 150 { 151 ULocale loc = new ULocale("ja_JP@calendar=japanese"); 152 Calendar cal = new JapaneseCalendar(loc); 153 DateFormat enjformat = cal.getDateTimeFormat(0,0,new ULocale("en_JP@calendar=japanese")); 154 DateFormat format = cal.getDateTimeFormat(0,0,loc); 155 ((SimpleDateFormat)format).applyPattern("y.M.d"); // Note: just 'y' doesn't work here. 156 ParsePosition pos = new ParsePosition(0); 157 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 158 String inEn = enjformat.format(aDate); 159 160 cal.clear(); 161 cal.setTime(aDate); 162 int gotYear = cal.get(Calendar.YEAR); 163 int gotEra = cal.get(Calendar.ERA); 164 165 int expectYear = 1; 166 int expectEra = JapaneseCalendar.CURRENT_ERA; 167 168 if((gotYear != expectYear) || (gotEra != expectEra)) { 169 errln("Expected year " + expectYear + ", era " + expectEra +", but got year " + gotYear + " and era " + gotEra + ", == " + inEn); 170 } else { 171 logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn); 172 } 173 174 // Test parse with missing era (should default to current era, heisei) 175 // Test parse with incomplete information 176 logln("Testing parse w/ just year..."); 177 Calendar cal2 = new JapaneseCalendar(loc); 178 SimpleDateFormat fmt = new SimpleDateFormat("y", loc); 179 SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian")); 180 cal2.clear(); 181 String samplestr = "1"; 182 logln("Test Year: " + samplestr); 183 try { 184 aDate = fmt.parse(samplestr); 185 } catch (ParseException pe) { 186 errln("Error parsing " + samplestr); 187 } 188 ParsePosition pp = new ParsePosition(0); 189 fmt.parse(samplestr, cal2, pp); 190 logln("cal2 after 1 parse:"); 191 String str = fmt2.format(aDate); 192 logln("as Gregorian Calendar: " + str); 193 194 cal2.setTime(aDate); 195 gotYear = cal2.get(Calendar.YEAR); 196 gotEra = cal2.get(Calendar.ERA); 197 expectYear = 1; 198 expectEra = JapaneseCalendar.CURRENT_ERA; 199 if((gotYear != 1) || (gotEra != expectEra)) { 200 errln("parse "+ samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear + 201 " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")"); 202 } else { 203 logln(" year: " + gotYear + ", era: " + gotEra); 204 } 205 } 206 207 @Test 208 public void Test5345parse() { 209 // Test parse with incomplete information 210 DateFormat fmt2= DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US); 211 JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US")); 212 SimpleDateFormat fmt = (SimpleDateFormat)c.getDateTimeFormat(1,1,new ULocale("en_US@calendar=japanese")); 213 fmt.applyPattern("G y"); 214 logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE)); 215 //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese")); 216 long aDateLong = -3197117222000L; // 1868-09-08 00:00 Pacific Time (GMT-07:52:58) 217 if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) { 218 // Java time zone implementation does not support LMTs 219 aDateLong = -3197116800000L; // 1868-09-08 00:00 Pacific Time (GMT-08:00) 220 } 221 Date aDate = new Date(aDateLong); 222 logln("aDate: " + aDate.toString() +", from " + aDateLong); 223 String str; 224 str = fmt2.format(aDate); 225 logln("Test Date: " + str); 226 str = fmt.format(aDate); 227 logln("as Japanese Calendar: " + str); 228 String expected = "Meiji 1"; 229 if(!str.equals(expected)) { 230 errln("FAIL: Expected " + expected + " but got " + str); 231 } 232 Date otherDate; 233 try { 234 otherDate = fmt.parse(expected); 235 if(!otherDate.equals(aDate)) { 236 String str3; 237 // ParsePosition pp; 238 Date dd = fmt.parse(expected); 239 str3 = fmt.format(otherDate); 240 long oLong = otherDate.getTime(); 241 long aLong = otherDate.getTime(); 242 243 errln("FAIL: Parse incorrect of " + expected + ": wanted " + aDate + " ("+aLong+"), but got " + " " + 244 otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() ); 245 246 247 } else { 248 logln("Parsed OK: " + expected); 249 } 250 } catch(java.text.ParseException pe) { 251 errln("FAIL: ParseException: " + pe.toString()); 252 pe.printStackTrace(); 253 } 254 } 255 256 257 private void checkExpected(Calendar c, int expected[] ) { 258 final String[] FIELD_NAME = { 259 "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", 260 "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK", 261 "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY", 262 "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET", 263 "DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR", 264 "JULIAN_DAY", "MILLISECONDS_IN_DAY", 265 }; 266 267 for(int i= 0;i<expected.length;i += 2) { 268 int fieldNum = expected[i+0]; 269 int expectedVal = expected[i+1]; 270 int actualVal = c.get(fieldNum); 271 272 if(expectedVal == actualVal) { 273 logln(FIELD_NAME[fieldNum]+": "+ actualVal); 274 } else { 275 errln("FAIL: "+FIELD_NAME[fieldNum]+": expected "+ expectedVal + " got " + actualVal); 276 } 277 } 278 } 279 280 @Test 281 public void Test5345calendar() { 282 logln("** testIncompleteCalendar()"); 283 // Test calendar with incomplete information 284 JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault()); 285 logln("test clear"); 286 c.clear(); 287 288 // Showa 45 = Gregorian 1970 289 int expected0[] = { Calendar.ERA, 234, 290 Calendar.YEAR, 45 }; 291 checkExpected(c, expected0); 292 293 logln("test setting era"); 294 c.clear(); 295 c.set(Calendar.ERA, JapaneseCalendar.MEIJI); 296 297 298 int expectedA[] = { Calendar.ERA, JapaneseCalendar.MEIJI }; 299 checkExpected(c, expectedA); 300 301 302 logln("test setting era and year and month and date"); 303 c.clear(); 304 c.set(Calendar.ERA, JapaneseCalendar.MEIJI); 305 c.set(Calendar.YEAR, 1); 306 c.set(Calendar.MONTH, Calendar.JANUARY); 307 c.set(Calendar.DATE, 1); 308 309 310 int expectedC[] = { Calendar.ERA, JapaneseCalendar.MEIJI -1}; 311 checkExpected(c, expectedC); 312 313 314 logln("test setting year and month and date THEN era"); 315 c.clear(); 316 c.set(Calendar.YEAR, 1); 317 c.set(Calendar.MONTH, Calendar.JANUARY); 318 c.set(Calendar.DATE, 1); 319 c.set(Calendar.ERA, JapaneseCalendar.MEIJI); 320 321 322 checkExpected(c, expectedC); 323 324 325 logln("test setting era and year"); 326 c.clear(); 327 c.set(Calendar.YEAR, 1); 328 c.set(Calendar.ERA, JapaneseCalendar.MEIJI); 329 330 331 int expectedB[] = { Calendar.ERA, JapaneseCalendar.MEIJI, 332 Calendar.YEAR, 1 }; 333 checkExpected(c, expectedB); 334 335 } 336 337 @Test 338 public void TestJapaneseYear3282() { 339 Calendar c = Calendar.getInstance(ULocale.ENGLISH); 340 c.set(2003,Calendar.SEPTEMBER,25); 341 JapaneseCalendar jcal = new JapaneseCalendar(); 342 //jcal.setTime(new Date(1187906308151L)); alternate value 343 jcal.setTime(c.getTime()); 344 logln("Now is: " + jcal.getTime()); 345 c.setTime(jcal.getTime()); 346 int nowYear = c.get(Calendar.YEAR); 347 logln("Now year: "+nowYear); 348 SimpleDateFormat jdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(jcal, 349 SimpleDateFormat.DEFAULT, Locale.getDefault()); 350 jdf.applyPattern("G yy/MM/dd"); 351 String text = jdf.format(jcal.getTime()); 352 logln("Now is: " + text + " (in Japan)"); 353 try { 354 Date date = jdf.parse(text); 355 logln("But is this not the date?: " + date); 356 c.setTime(date); 357 int thenYear = c.get(Calendar.YEAR); 358 logln("Then year: "+thenYear); 359 if(thenYear != nowYear) { 360 errln("Nowyear "+nowYear +" is not thenyear "+thenYear); 361 } else { 362 logln("Nowyear "+nowYear +" == thenyear "+thenYear); 363 } 364 } catch (java.text.ParseException ex) { 365 ex.printStackTrace(); 366 } 367 } 368 369 /** 370 * Test limits of the Japanese calendar 371 */ 372 @Test 373 public void TestLimits() { 374 Calendar cal = Calendar.getInstance(); 375 cal.set(1988, Calendar.DECEMBER, 1); 376 JapaneseCalendar jcal = new JapaneseCalendar(); 377 doLimitsTest(jcal, null, cal.getTime()); 378 doTheoreticalLimitsTest(jcal, true); 379 } 380} 381 382