1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 28 * 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions are met: 33 * 34 * * Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 37 * * Redistributions in binary form must reproduce the above copyright notice, 38 * this list of conditions and the following disclaimer in the documentation 39 * and/or other materials provided with the distribution. 40 * 41 * * Neither the name of JSR-310 nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 package tck.java.time.chrono; 58 59 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 60 import static java.time.temporal.ChronoField.ERA; 61 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 62 import static java.time.temporal.ChronoField.YEAR; 63 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 64 import static org.testng.Assert.assertEquals; 65 import static org.testng.Assert.assertFalse; 66 import static org.testng.Assert.assertTrue; 67 import static org.testng.Assert.fail; 68 69 import java.time.Clock; 70 import java.time.DateTimeException; 71 import java.time.LocalDate; 72 import java.time.LocalDateTime; 73 import java.time.Month; 74 import java.time.Period; 75 import java.time.Year; 76 import java.time.ZoneId; 77 import java.time.ZoneOffset; 78 import java.time.chrono.ChronoLocalDate; 79 import java.time.chrono.Chronology; 80 import java.time.chrono.Era; 81 import java.time.chrono.IsoChronology; 82 import java.time.chrono.JapaneseChronology; 83 import java.time.chrono.JapaneseDate; 84 import java.time.chrono.JapaneseEra; 85 import java.time.chrono.MinguoChronology; 86 import java.time.chrono.MinguoDate; 87 import java.time.temporal.ChronoUnit; 88 import java.time.temporal.TemporalAdjuster; 89 import java.util.List; 90 import java.util.Locale; 91 92 import org.testng.Assert; 93 import org.testng.annotations.DataProvider; 94 import org.testng.annotations.Test; 95 96 /** 97 * Test. 98 */ 99 @Test 100 public class TCKJapaneseChronology { 101 private static final int YDIFF_HEISEI = 1988; 102 private static final int YDIFF_MEIJI = 1867; 103 private static final int YDIFF_SHOWA = 1925; 104 private static final int YDIFF_TAISHO = 1911; 105 106 //----------------------------------------------------------------------- 107 // Chronology.of(String) 108 //----------------------------------------------------------------------- 109 @Test 110 public void test_chrono_byName() { 111 Chronology c = JapaneseChronology.INSTANCE; 112 Chronology test = Chronology.of("Japanese"); 113 Assert.assertNotNull(test, "The Japanese calendar could not be found byName"); 114 Assert.assertEquals(test.getId(), "Japanese", "ID mismatch"); 115 Assert.assertEquals(test.getCalendarType(), "japanese", "Type mismatch"); 116 Assert.assertEquals(test, c); 117 } 118 119 //----------------------------------------------------------------------- 120 // Chronology.ofLocale(Locale) 121 //----------------------------------------------------------------------- 122 @Test 123 public void test_chrono_byLocale_fullTag_japaneseCalendarFromJapan() { 124 Chronology test = Chronology.ofLocale(Locale.forLanguageTag("ja-JP-u-ca-japanese")); 125 Assert.assertEquals(test.getId(), "Japanese"); 126 Assert.assertEquals(test, JapaneseChronology.INSTANCE); 127 } 128 129 @Test 130 public void test_chrono_byLocale_fullTag_japaneseCalendarFromElsewhere() { 131 Chronology test = Chronology.ofLocale(Locale.forLanguageTag("en-US-u-ca-japanese")); 132 Assert.assertEquals(test.getId(), "Japanese"); 133 Assert.assertEquals(test, JapaneseChronology.INSTANCE); 134 } 135 136 @Test 137 public void test_chrono_byLocale_oldJP_noVariant() { 138 Chronology test = Chronology.ofLocale(new Locale("ja", "JP")); 139 Assert.assertEquals(test.getId(), "ISO"); 140 Assert.assertEquals(test, IsoChronology.INSTANCE); 141 } 142 143 @Test 144 public void test_chrono_byLocale_oldJP_variant() { 145 Chronology test = Chronology.ofLocale(new Locale("ja", "JP", "JP")); 146 Assert.assertEquals(test.getId(), "Japanese"); 147 Assert.assertEquals(test, JapaneseChronology.INSTANCE); 148 } 149 150 @Test 151 public void test_chrono_byLocale_iso() { 152 Assert.assertEquals(Chronology.ofLocale(new Locale("ja", "JP")).getId(), "ISO"); 153 Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("ja-JP")).getId(), "ISO"); 154 Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("ja-JP-JP")).getId(), "ISO"); 155 } 156 157 //----------------------------------------------------------------------- 158 // creation, toLocalDate() 159 //----------------------------------------------------------------------- 160 @DataProvider(name="samples") 161 Object[][] data_samples() { 162 return new Object[][] { 163 {JapaneseChronology.INSTANCE.date(1, 1, 1), LocalDate.of(1, 1, 1)}, 164 {JapaneseChronology.INSTANCE.date(1, 1, 2), LocalDate.of(1, 1, 2)}, 165 {JapaneseChronology.INSTANCE.date(1, 1, 3), LocalDate.of(1, 1, 3)}, 166 167 {JapaneseChronology.INSTANCE.date(2, 1, 1), LocalDate.of(2, 1, 1)}, 168 {JapaneseChronology.INSTANCE.date(3, 1, 1), LocalDate.of(3, 1, 1)}, 169 {JapaneseChronology.INSTANCE.date(3, 12, 6), LocalDate.of(3, 12, 6)}, 170 {JapaneseChronology.INSTANCE.date(4, 1, 1), LocalDate.of(4, 1, 1)}, 171 {JapaneseChronology.INSTANCE.date(4, 7, 3), LocalDate.of(4, 7, 3)}, 172 {JapaneseChronology.INSTANCE.date(4, 7, 4), LocalDate.of(4, 7, 4)}, 173 {JapaneseChronology.INSTANCE.date(5, 1, 1), LocalDate.of(5, 1, 1)}, 174 {JapaneseChronology.INSTANCE.date(1662, 3, 3), LocalDate.of(1662, 3, 3)}, 175 {JapaneseChronology.INSTANCE.date(1728, 10, 28), LocalDate.of(1728, 10, 28)}, 176 {JapaneseChronology.INSTANCE.date(1728, 10, 29), LocalDate.of(1728, 10, 29)}, 177 178 {JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 1996 - YDIFF_HEISEI, 2, 29), LocalDate.of(1996, 2, 29)}, 179 {JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 2000 - YDIFF_HEISEI, 2, 29), LocalDate.of(2000, 2, 29)}, 180 {JapaneseChronology.INSTANCE.date(JapaneseEra.MEIJI, 1868 - YDIFF_MEIJI, 2, 29), LocalDate.of(1868, 2, 29)}, 181 {JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 1928 - YDIFF_SHOWA, 12, 25), LocalDate.of(1928, 12, 25)}, 182 {JapaneseChronology.INSTANCE.date(JapaneseEra.TAISHO, 1912 - YDIFF_TAISHO, 7, 30), LocalDate.of(1912, 7, 30)}, 183 184 {JapaneseChronology.INSTANCE.dateYearDay(1996, 60), LocalDate.of(1996, 2, 29)}, 185 {JapaneseChronology.INSTANCE.dateYearDay(1868, 60), LocalDate.of(1868, 2, 29)}, 186 {JapaneseChronology.INSTANCE.dateYearDay(1928, 60), LocalDate.of(1928, 2, 29)}, 187 {JapaneseChronology.INSTANCE.dateYearDay(1912, 60), LocalDate.of(1912, 2, 29)}, 188 189 {JapaneseDate.ofYearDay(1996, 60), LocalDate.of(1996, 2, 29)}, 190 {JapaneseDate.ofYearDay(1868, 60), LocalDate.of(1868, 2, 29)}, 191 {JapaneseDate.ofYearDay(1928, 60), LocalDate.of(1928, 2, 29)}, 192 {JapaneseDate.ofYearDay(1912, 60), LocalDate.of(1912, 2, 29)}, 193 194 {JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.HEISEI, 1996 - YDIFF_HEISEI, 60), LocalDate.of(1996, 2, 29)}, 195 {JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.MEIJI, 1868 - YDIFF_MEIJI, 60), LocalDate.of(1868, 2, 29)}, 196 {JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.SHOWA, 1928 - YDIFF_SHOWA, 60), LocalDate.of(1928, 2, 29)}, 197 // {JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.TAISHO, 1916 - YDIFF_TAISHO, 60), LocalDate.of(1912, 2, 29)}, 198 }; 199 } 200 201 @Test(dataProvider="samples") 202 public void test_toLocalDate(JapaneseDate jdate, LocalDate iso) { 203 assertEquals(LocalDate.from(jdate), iso); 204 } 205 206 @Test(dataProvider="samples") 207 public void test_fromCalendrical(JapaneseDate jdate, LocalDate iso) { 208 assertEquals(JapaneseChronology.INSTANCE.date(iso), jdate); 209 } 210 211 @Test 212 public void test_dateNow(){ 213 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now()) ; 214 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now(ZoneId.systemDefault())) ; 215 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now(Clock.systemDefaultZone())) ; 216 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now(Clock.systemDefaultZone().getZone())) ; 217 218 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseChronology.INSTANCE.dateNow(ZoneId.systemDefault())) ; 219 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseChronology.INSTANCE.dateNow(Clock.systemDefaultZone())) ; 220 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseChronology.INSTANCE.dateNow(Clock.systemDefaultZone().getZone())) ; 221 222 ZoneId zoneId = ZoneId.of("Europe/Paris"); 223 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseChronology.INSTANCE.dateNow(Clock.system(zoneId))) ; 224 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseChronology.INSTANCE.dateNow(Clock.system(zoneId).getZone())) ; 225 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseDate.now(Clock.system(zoneId))) ; 226 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseDate.now(Clock.system(zoneId).getZone())) ; 227 228 assertEquals(JapaneseChronology.INSTANCE.dateNow(ZoneId.of(ZoneOffset.UTC.getId())), JapaneseChronology.INSTANCE.dateNow(Clock.systemUTC())) ; 229 } 230 231 @DataProvider(name="badDates") 232 Object[][] data_badDates() { 233 return new Object[][] { 234 {1728, 0, 0}, 235 236 {1728, -1, 1}, 237 {1728, 0, 1}, 238 {1728, 14, 1}, 239 {1728, 15, 1}, 240 241 {1728, 1, -1}, 242 {1728, 1, 0}, 243 {1728, 1, 32}, 244 245 {1728, 12, -1}, 246 {1728, 12, 0}, 247 {1728, 12, 32}, 248 249 {1725, 2, 29}, 250 {500, 2, 29}, 251 {2100, 2, 29}, 252 }; 253 } 254 255 @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class) 256 public void test_badDates(int year, int month, int dom) { 257 JapaneseChronology.INSTANCE.date(year, month, dom); 258 } 259 260 //----------------------------------------------------------------------- 261 // prolepticYear() and is LeapYear() 262 //----------------------------------------------------------------------- 263 @DataProvider(name="prolepticYear") 264 Object[][] data_prolepticYear() { 265 return new Object[][] { 266 {2, JapaneseEra.HEISEI, 1, 1 + YDIFF_HEISEI, false}, 267 {2, JapaneseEra.HEISEI, 100, 100 + YDIFF_HEISEI, true}, 268 269 {-1, JapaneseEra.MEIJI, 1, 1 + YDIFF_MEIJI, true}, 270 {-1, JapaneseEra.MEIJI, 4, 4 + YDIFF_MEIJI, false}, 271 272 {1, JapaneseEra.SHOWA, 1, 1 + YDIFF_SHOWA, false}, 273 {1, JapaneseEra.SHOWA, 7, 7 + YDIFF_SHOWA, true}, 274 275 {0, JapaneseEra.TAISHO, 1, 1 + YDIFF_TAISHO, true}, 276 {0, JapaneseEra.TAISHO, 4, 4 + YDIFF_TAISHO, false}, 277 }; 278 } 279 280 @Test(dataProvider="prolepticYear") 281 public void test_prolepticYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { 282 Era eraObj = JapaneseChronology.INSTANCE.eraOf(eraValue) ; 283 assertTrue(JapaneseChronology.INSTANCE.eras().contains(eraObj)); 284 assertEquals(eraObj, era); 285 assertEquals(JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear); 286 assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear) ; 287 assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear).isLeap()) ; 288 } 289 290 @DataProvider(name="prolepticYearError") 291 Object[][] data_prolepticYearError() { 292 return new Object[][] { 293 {JapaneseEra.MEIJI, 100}, 294 {JapaneseEra.MEIJI, 0}, 295 {JapaneseEra.MEIJI, -10}, 296 297 {JapaneseEra.SHOWA, 100}, 298 {JapaneseEra.SHOWA, 0}, 299 {JapaneseEra.SHOWA, -10}, 300 301 {JapaneseEra.TAISHO, 100}, 302 {JapaneseEra.TAISHO, 0}, 303 {JapaneseEra.TAISHO, -10}, 304 }; 305 } 306 307 @Test(dataProvider="prolepticYearError", expectedExceptions=DateTimeException.class) 308 public void test_prolepticYearError(Era era, int yearOfEra) { 309 JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra); 310 } 311 312 //----------------------------------------------------------------------- 313 // Bad Era for Chronology.date(era,...) and Chronology.prolepticYear(Era,...) 314 //----------------------------------------------------------------------- 315 @Test 316 public void test_InvalidEras() { 317 // Verify that the eras from every other Chronology are invalid 318 for (Chronology chrono : Chronology.getAvailableChronologies()) { 319 if (chrono instanceof JapaneseChronology) { 320 continue; 321 } 322 List<Era> eras = chrono.eras(); 323 for (Era era : eras) { 324 try { 325 ChronoLocalDate date = JapaneseChronology.INSTANCE.date(era, 1, 1, 1); 326 fail("JapaneseChronology.date did not throw ClassCastException for Era: " + era); 327 } catch (ClassCastException cex) { 328 ; // ignore expected exception 329 } 330 331 try { 332 @SuppressWarnings("unused") 333 JapaneseDate jdate = JapaneseDate.of(era, 1, 1, 1); 334 fail("JapaneseDate.of did not throw ClassCastException for Era: " + era); 335 } catch (ClassCastException cex) { 336 ; // ignore expected exception 337 } 338 339 try { 340 @SuppressWarnings("unused") 341 int year = JapaneseChronology.INSTANCE.prolepticYear(era, 1); 342 fail("JapaneseChronology.prolepticYear did not throw ClassCastException for Era: " + era); 343 } catch (ClassCastException cex) { 344 ; // ignore expected exception 345 } 346 347 } 348 } 349 } 350 351 //----------------------------------------------------------------------- 352 // get(TemporalField) 353 //----------------------------------------------------------------------- 354 @Test 355 public void test_getLong() { 356 JapaneseDate base = JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 63, 6, 30); 357 assertEquals(base.getLong(ERA), JapaneseEra.SHOWA.getValue()); 358 assertEquals(base.getLong(YEAR), 1988L); 359 assertEquals(base.getLong(YEAR_OF_ERA), 63L); 360 assertEquals(base.getLong(MONTH_OF_YEAR), 6L); 361 assertEquals(base.getLong(DAY_OF_MONTH), 30L); 362 } 363 364 //----------------------------------------------------------------------- 365 // with(TemporalField, long) 366 //----------------------------------------------------------------------- 367 @Test 368 public void test_with_TemporalField_long() { 369 JapaneseDate base = JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 63, 6, 30); 370 JapaneseDate test = base.with(YEAR, 1987); 371 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 62, 6, 30)); 372 373 test = test.with(YEAR_OF_ERA, 2); 374 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 2, 6, 30)); 375 376 test = test.with(ERA, JapaneseEra.HEISEI.getValue()); 377 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 2, 6, 30)); 378 379 test = test.with(MONTH_OF_YEAR, 3); 380 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 2, 3, 30)); 381 382 test = test.with(DAY_OF_MONTH, 4); 383 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 2, 3, 4)); 384 } 385 386 //----------------------------------------------------------------------- 387 // with(WithAdjuster) 388 //----------------------------------------------------------------------- 389 @Test 390 public void test_adjust1() { 391 JapaneseDate base = JapaneseChronology.INSTANCE.date(1728, 10, 29); 392 JapaneseDate test = base.with(TemporalAdjuster.lastDayOfMonth()); 393 assertEquals(test, JapaneseChronology.INSTANCE.date(1728, 10, 31)); 394 } 395 396 @Test 397 public void test_adjust2() { 398 JapaneseDate base = JapaneseChronology.INSTANCE.date(1728, 12, 2); 399 JapaneseDate test = base.with(TemporalAdjuster.lastDayOfMonth()); 400 assertEquals(test, JapaneseChronology.INSTANCE.date(1728, 12, 31)); 401 } 402 403 //----------------------------------------------------------------------- 404 // JapaneseDate.with(Local*) 405 //----------------------------------------------------------------------- 406 @Test 407 public void test_adjust_toLocalDate() { 408 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1726, 1, 4); 409 JapaneseDate test = jdate.with(LocalDate.of(2012, 7, 6)); 410 assertEquals(test, JapaneseChronology.INSTANCE.date(2012, 7, 6)); 411 } 412 413 @Test(expectedExceptions=DateTimeException.class) 414 public void test_adjust_toMonth() { 415 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1726, 1, 4); 416 jdate.with(Month.APRIL); 417 } 418 419 //----------------------------------------------------------------------- 420 // LocalDate.with(JapaneseDate) 421 //----------------------------------------------------------------------- 422 @Test 423 public void test_LocalDate_adjustToJapaneseDate() { 424 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1728, 10, 29); 425 LocalDate test = LocalDate.MIN.with(jdate); 426 assertEquals(test, LocalDate.of(1728, 10, 29)); 427 } 428 429 @Test 430 public void test_LocalDateTime_adjustToJapaneseDate() { 431 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1728, 10, 29); 432 LocalDateTime test = LocalDateTime.MIN.with(jdate); 433 assertEquals(test, LocalDateTime.of(1728, 10, 29, 0, 0)); 434 } 435 436 //----------------------------------------------------------------------- 437 // Check Japanese Eras 438 //----------------------------------------------------------------------- 439 @DataProvider(name="japaneseEras") 440 Object[][] data_japanseseEras() { 441 return new Object[][] { 442 { JapaneseEra.SEIREKI, -999, "Seireki"}, 443 { JapaneseEra.MEIJI, -1, "Meiji"}, 444 { JapaneseEra.TAISHO, 0, "Taisho"}, 445 { JapaneseEra.SHOWA, 1, "Showa"}, 446 { JapaneseEra.HEISEI, 2, "Heisei"}, 447 }; 448 } 449 450 @Test(dataProvider="japaneseEras") 451 public void test_Japanese_Eras(Era era, int eraValue, String name) { 452 assertEquals(era.getValue(), eraValue, "EraValue"); 453 assertEquals(era.toString(), name, "Era Name"); 454 assertEquals(era, JapaneseChronology.INSTANCE.eraOf(eraValue), "JapaneseChronology.eraOf()"); 455 List<Era> eras = JapaneseChronology.INSTANCE.eras(); 456 assertTrue(eras.contains(era), "Era is not present in JapaneseChronology.INSTANCE.eras()"); 457 } 458 459 @Test 460 public void test_Japanese_badEras() { 461 int badEras[] = {-1000, -998, -997, -2, 3, 4, 1000}; 462 for (int badEra : badEras) { 463 try { 464 Era era = JapaneseChronology.INSTANCE.eraOf(badEra); 465 fail("JapaneseChronology.eraOf returned " + era + " + for invalid eraValue " + badEra); 466 } catch (DateTimeException ex) { 467 // ignore expected exception 468 } 469 } 470 } 471 472 @Test(dataProvider="japaneseEras") 473 public void test_JapaneseEra_singletons(Era expectedEra, int eraValue, String name) { 474 JapaneseEra actualEra = JapaneseEra.valueOf(name); 475 assertEquals(actualEra, expectedEra, "JapaneseEra.valueOf(name)"); 476 477 actualEra = JapaneseEra.of(eraValue); 478 assertEquals(actualEra, expectedEra, "JapaneseEra.of(value)"); 479 480 String string = actualEra.toString(); 481 assertEquals(string, name, "JapaneseEra.toString()"); 482 } 483 484 @Test 485 public void test_JapaneseEra_values() { 486 JapaneseEra[] actualEras = JapaneseEra.values(); 487 Object[][] erasInfo = data_japanseseEras(); 488 assertEquals(actualEras.length, erasInfo.length, "Wrong number of Eras"); 489 490 for (int i = 0; i < erasInfo.length; i++) { 491 Object[] eraInfo = erasInfo[i]; 492 assertEquals(actualEras[i], eraInfo[0], "Singleton mismatch"); 493 } 494 } 495 496 @Test 497 public void test_JapaneseChronology_eras() { 498 List<Era> actualEras = JapaneseChronology.INSTANCE.eras(); 499 Object[][] erasInfo = data_japanseseEras(); 500 assertEquals(actualEras.size(), erasInfo.length, "Wrong number of Eras"); 501 502 for (int i = 0; i < erasInfo.length; i++) { 503 Object[] eraInfo = erasInfo[i]; 504 assertEquals(actualEras.get(i), eraInfo[0], "Singleton mismatch"); 505 } 506 } 507 508 //----------------------------------------------------------------------- 509 // PeriodUntil() 510 //----------------------------------------------------------------------- 511 @Test 512 public void test_periodUntilDate() { 513 JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1); 514 JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2); 515 Period period = mdate1.periodUntil(mdate2); 516 assertEquals(period, Period.of(1, 1, 1)); 517 } 518 519 @Test 520 public void test_periodUntilUnit() { 521 JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1); 522 JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2); 523 long months = mdate1.periodUntil(mdate2, ChronoUnit.MONTHS); 524 assertEquals(months, 13); 525 } 526 527 @Test 528 public void test_periodUntilDiffChrono() { 529 JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1); 530 JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2); 531 MinguoDate ldate2 = MinguoChronology.INSTANCE.date(mdate2); 532 Period period = mdate1.periodUntil(ldate2); 533 assertEquals(period, Period.of(1, 1, 1)); 534 } 535 536 //----------------------------------------------------------------------- 537 // toString() 538 //----------------------------------------------------------------------- 539 @DataProvider(name="toString") 540 Object[][] data_toString() { 541 return new Object[][] { 542 {JapaneseChronology.INSTANCE.date(0001, 1, 1), "Japanese 0001-01-01"}, 543 {JapaneseChronology.INSTANCE.date(1728, 10, 28), "Japanese 1728-10-28"}, 544 {JapaneseChronology.INSTANCE.date(1728, 10, 29), "Japanese 1728-10-29"}, 545 {JapaneseChronology.INSTANCE.date(1727, 12, 5), "Japanese 1727-12-05"}, 546 {JapaneseChronology.INSTANCE.date(1727, 12, 6), "Japanese 1727-12-06"}, 547 {JapaneseChronology.INSTANCE.date(1868, 9, 8), "Japanese Meiji 1-09-08"}, 548 {JapaneseChronology.INSTANCE.date(1912, 7, 29), "Japanese Meiji 45-07-29"}, 549 {JapaneseChronology.INSTANCE.date(1912, 7, 30), "Japanese Taisho 1-07-30"}, 550 {JapaneseChronology.INSTANCE.date(1926, 12, 24), "Japanese Taisho 15-12-24"}, 551 {JapaneseChronology.INSTANCE.date(1926, 12, 25), "Japanese Showa 1-12-25"}, 552 {JapaneseChronology.INSTANCE.date(1989, 1, 7), "Japanese Showa 64-01-07"}, 553 {JapaneseChronology.INSTANCE.date(1989, 1, 8), "Japanese Heisei 1-01-08"}, 554 {JapaneseChronology.INSTANCE.date(2012, 12, 6), "Japanese Heisei 24-12-06"}, 555 }; 556 } 557 558 @Test(dataProvider="toString") 559 public void test_toString(JapaneseDate jdate, String expected) { 560 assertEquals(jdate.toString(), expected); 561 } 562 563 //----------------------------------------------------------------------- 564 // equals() 565 //----------------------------------------------------------------------- 566 @Test 567 public void test_equals_true() { 568 assertTrue(JapaneseChronology.INSTANCE.equals(JapaneseChronology.INSTANCE)); 569 } 570 571 @Test 572 public void test_equals_false() { 573 assertFalse(JapaneseChronology.INSTANCE.equals(IsoChronology.INSTANCE)); 574 } 575 576 }