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.DAY_OF_YEAR; 61 import static java.time.temporal.ChronoField.EPOCH_DAY; 62 import static java.time.temporal.ChronoField.ERA; 63 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 64 import static java.time.temporal.ChronoField.YEAR; 65 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 66 import static org.testng.Assert.assertEquals; 67 import static org.testng.Assert.assertFalse; 68 import static org.testng.Assert.assertNotEquals; 69 import static org.testng.Assert.assertTrue; 70 import static org.testng.Assert.fail; 71 72 import java.time.Clock; 73 import java.time.DateTimeException; 74 import java.time.LocalDate; 75 import java.time.LocalDateTime; 76 import java.time.Month; 77 import java.time.Period; 78 import java.time.Year; 79 import java.time.ZoneId; 80 import java.time.ZoneOffset; 81 import java.time.chrono.ChronoLocalDate; 82 import java.time.chrono.ChronoPeriod; 83 import java.time.chrono.Chronology; 84 import java.time.chrono.Era; 85 import java.time.chrono.IsoChronology; 86 import java.time.chrono.JapaneseChronology; 87 import java.time.chrono.JapaneseDate; 88 import java.time.chrono.JapaneseEra; 89 import java.time.chrono.MinguoChronology; 90 import java.time.chrono.MinguoDate; 91 import java.time.chrono.ThaiBuddhistChronology; 92 import java.time.format.ResolverStyle; 93 import java.time.temporal.ChronoField; 94 import java.time.temporal.ChronoUnit; 95 import java.time.temporal.TemporalAdjusters; 96 import java.time.temporal.TemporalField; 97 import java.time.temporal.TemporalQueries; 98 import java.time.temporal.ValueRange; 99 100 import java.util.HashMap; 101 import java.util.List; 102 import java.util.Locale; 103 import java.util.Map; 104 105 import org.testng.Assert; 106 import org.testng.annotations.DataProvider; 107 import org.testng.annotations.Test; 108 109 /** 110 * Test. 111 */ 112 @Test 113 public class TCKJapaneseChronology { 114 private static final int YDIFF_HEISEI = 1988; 115 private static final int YDIFF_MEIJI = 1867; 116 private static final int YDIFF_SHOWA = 1925; 117 private static final int YDIFF_TAISHO = 1911; 118 119 //----------------------------------------------------------------------- 120 // Chronology.of(String) 121 //----------------------------------------------------------------------- 122 @Test 123 public void test_chrono_byName() { 124 Chronology c = JapaneseChronology.INSTANCE; 125 Chronology test = Chronology.of("Japanese"); 126 Assert.assertNotNull(test, "The Japanese calendar could not be found byName"); 127 Assert.assertEquals(test.getId(), "Japanese", "ID mismatch"); 128 Assert.assertEquals(test.getCalendarType(), "japanese", "Type mismatch"); 129 Assert.assertEquals(test, c); 130 } 131 132 //----------------------------------------------------------------------- 133 // Chronology.ofLocale(Locale) 134 //----------------------------------------------------------------------- 135 @Test 136 public void test_chrono_byLocale_fullTag_japaneseCalendarFromJapan() { 137 Chronology test = Chronology.ofLocale(Locale.forLanguageTag("ja-JP-u-ca-japanese")); 138 Assert.assertEquals(test.getId(), "Japanese"); 139 Assert.assertEquals(test, JapaneseChronology.INSTANCE); 140 } 141 142 @Test 143 public void test_chrono_byLocale_fullTag_japaneseCalendarFromElsewhere() { 144 Chronology test = Chronology.ofLocale(Locale.forLanguageTag("en-US-u-ca-japanese")); 145 Assert.assertEquals(test.getId(), "Japanese"); 146 Assert.assertEquals(test, JapaneseChronology.INSTANCE); 147 } 148 149 @Test 150 public void test_chrono_byLocale_oldJP_noVariant() { 151 Chronology test = Chronology.ofLocale(new Locale("ja", "JP")); 152 Assert.assertEquals(test.getId(), "ISO"); 153 Assert.assertEquals(test, IsoChronology.INSTANCE); 154 } 155 156 @Test 157 public void test_chrono_byLocale_oldJP_variant() { 158 Chronology test = Chronology.ofLocale(new Locale("ja", "JP", "JP")); 159 Assert.assertEquals(test.getId(), "Japanese"); 160 Assert.assertEquals(test, JapaneseChronology.INSTANCE); 161 } 162 163 @Test 164 public void test_chrono_byLocale_iso() { 165 Assert.assertEquals(Chronology.ofLocale(new Locale("ja", "JP")).getId(), "ISO"); 166 Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("ja-JP")).getId(), "ISO"); 167 Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("ja-JP-JP")).getId(), "ISO"); 168 } 169 170 //----------------------------------------------------------------------- 171 // creation and cross-checks 172 //----------------------------------------------------------------------- 173 @DataProvider(name="createByEra") 174 Object[][] data_createByEra() { 175 return new Object[][] { 176 {JapaneseEra.HEISEI, 1996 - YDIFF_HEISEI, 2, 29, 60, LocalDate.of(1996, 2, 29)}, 177 {JapaneseEra.HEISEI, 2000 - YDIFF_HEISEI, 2, 29, 60, LocalDate.of(2000, 2, 29)}, 178 {JapaneseEra.MEIJI, 1874 - YDIFF_MEIJI, 2, 28, 59, LocalDate.of(1874, 2, 28)}, 179 {JapaneseEra.SHOWA, 1928 - YDIFF_SHOWA, 12, 25, 360, LocalDate.of(1928, 12, 25)}, 180 {JapaneseEra.TAISHO, 1916 - YDIFF_TAISHO, 7, 30, 212, LocalDate.of(1916, 7, 30)}, 181 {JapaneseEra.MEIJI, 6, 1, 1, 1, LocalDate.of(1873, 1, 1)}, 182 {JapaneseEra.MEIJI, 45, 7, 29, 211, LocalDate.of(1912, 7, 29)}, 183 {JapaneseEra.TAISHO, 1, 7, 30, 1, LocalDate.of(1912, 7, 30)}, 184 {JapaneseEra.TAISHO, 15, 12, 24, 358, LocalDate.of(1926, 12, 24)}, 185 {JapaneseEra.SHOWA, 1, 12, 25, 1, LocalDate.of(1926, 12, 25)}, 186 {JapaneseEra.SHOWA, 64, 1, 7, 7, LocalDate.of(1989, 1, 7)}, 187 {JapaneseEra.HEISEI, 1, 1, 8, 1, LocalDate.of(1989, 1, 8)}, 188 }; 189 } 190 191 @Test(dataProvider="createByEra") 192 public void test_createEymd(JapaneseEra era, int yoe, int moy, int dom, int doy, LocalDate iso) { 193 JapaneseDate dateByChronoFactory = JapaneseChronology.INSTANCE.date(era, yoe, moy, dom); 194 JapaneseDate dateByDateFactory = JapaneseDate.of(era, yoe, moy, dom); 195 assertEquals(dateByChronoFactory, dateByDateFactory); 196 assertEquals(dateByChronoFactory.hashCode(), dateByDateFactory.hashCode()); 197 } 198 199 @Test(dataProvider="createByEra") 200 public void test_createEyd(JapaneseEra era, int yoe, int moy, int dom, int doy, LocalDate iso) { 201 JapaneseDate dateByChronoFactory = JapaneseChronology.INSTANCE.dateYearDay(era, yoe, doy); 202 JapaneseDate dateByDateFactory = JapaneseDate.of(era, yoe, moy, dom); 203 assertEquals(dateByChronoFactory, dateByDateFactory); 204 assertEquals(dateByChronoFactory.hashCode(), dateByDateFactory.hashCode()); 205 } 206 207 @Test(dataProvider="createByEra") 208 public void test_createByEra_isEqual(JapaneseEra era, int yoe, int moy, int dom, int doy, LocalDate iso) { 209 JapaneseDate test = JapaneseDate.of(era, yoe, moy, dom); 210 assertEquals(test.isEqual(iso), true); 211 assertEquals(iso.isEqual(test), true); 212 } 213 214 @Test(dataProvider="createByEra") 215 public void test_createByEra_chronologyTemporalFactory(JapaneseEra era, int yoe, int moy, int dom, int doy, LocalDate iso) { 216 JapaneseDate test = JapaneseDate.of(era, yoe, moy, dom); 217 assertEquals(IsoChronology.INSTANCE.date(test), iso); 218 assertEquals(JapaneseChronology.INSTANCE.date(iso), test); 219 } 220 221 @Test(dataProvider="createByEra") 222 public void test_createByEra_dateFrom(JapaneseEra era, int yoe, int moy, int dom, int doy, LocalDate iso) { 223 JapaneseDate test = JapaneseDate.of(era, yoe, moy, dom); 224 assertEquals(LocalDate.from(test), iso); 225 assertEquals(JapaneseDate.from(iso), test); 226 } 227 228 @Test(dataProvider="createByEra") 229 public void test_createByEra_query(JapaneseEra era, int yoe, int moy, int dom, int doy, LocalDate iso) { 230 JapaneseDate test = JapaneseDate.of(era, yoe, moy, dom); 231 assertEquals(test.query(TemporalQueries.localDate()), iso); 232 } 233 234 @Test(dataProvider="createByEra") 235 public void test_createByEra_epochDay(JapaneseEra era, int yoe, int moy, int dom, int doy, LocalDate iso) { 236 JapaneseDate test = JapaneseDate.of(era, yoe, moy, dom); 237 assertEquals(test.getLong(EPOCH_DAY), iso.getLong(EPOCH_DAY)); 238 assertEquals(test.toEpochDay(), iso.toEpochDay()); 239 } 240 241 //----------------------------------------------------------------------- 242 @DataProvider(name="createByProleptic") 243 Object[][] data_createByProleptic() { 244 return new Object[][] { 245 {1928, 2, 28, 59, LocalDate.of(1928, 2, 28)}, 246 {1928, 2, 29, 60, LocalDate.of(1928, 2, 29)}, 247 248 {1873, 9, 7, 250, LocalDate.of(1873, 9, 7)}, 249 {1873, 9, 8, 251, LocalDate.of(1873, 9, 8)}, 250 {1912, 7, 29, 211, LocalDate.of(1912, 7, 29)}, 251 {1912, 7, 30, 212, LocalDate.of(1912, 7, 30)}, 252 {1926, 12, 24, 358, LocalDate.of(1926, 12, 24)}, 253 {1926, 12, 25, 359, LocalDate.of(1926, 12, 25)}, 254 {1989, 1, 7, 7, LocalDate.of(1989, 1, 7)}, 255 {1989, 1, 8, 8, LocalDate.of(1989, 1, 8)}, 256 }; 257 } 258 259 @Test(dataProvider="createByProleptic") 260 public void test_createYmd(int y, int moy, int dom, int doy, LocalDate iso) { 261 JapaneseDate dateByChronoFactory = JapaneseChronology.INSTANCE.date(y, moy, dom); 262 JapaneseDate dateByDateFactory = JapaneseDate.of(y, moy, dom); 263 assertEquals(dateByChronoFactory, dateByDateFactory); 264 assertEquals(dateByChronoFactory.hashCode(), dateByDateFactory.hashCode()); 265 } 266 267 @Test(dataProvider="createByProleptic") 268 public void test_createYd(int y, int moy, int dom, int doy, LocalDate iso) { 269 JapaneseDate dateByChronoFactory = JapaneseChronology.INSTANCE.dateYearDay(y, doy); 270 JapaneseDate dateByDateFactory = JapaneseDate.of(y, moy, dom); 271 assertEquals(dateByChronoFactory, dateByDateFactory); 272 assertEquals(dateByChronoFactory.hashCode(), dateByDateFactory.hashCode()); 273 } 274 275 @Test(dataProvider="createByProleptic") 276 public void test_createByProleptic_isEqual(int y, int moy, int dom, int doy, LocalDate iso) { 277 JapaneseDate test = JapaneseDate.of(y, moy, dom); 278 assertEquals(test.isEqual(iso), true); 279 assertEquals(iso.isEqual(test), true); 280 } 281 282 @Test(dataProvider="createByProleptic") 283 public void test_createByProleptic_chronologyTemporalFactory(int y, int moy, int dom, int doy, LocalDate iso) { 284 JapaneseDate test = JapaneseDate.of(y, moy, dom); 285 assertEquals(IsoChronology.INSTANCE.date(test), iso); 286 assertEquals(JapaneseChronology.INSTANCE.date(iso), test); 287 } 288 289 @Test(dataProvider="createByProleptic") 290 public void test_createByProleptic_dateFrom(int y, int moy, int dom, int doy, LocalDate iso) { 291 JapaneseDate test = JapaneseDate.of(y, moy, dom); 292 assertEquals(LocalDate.from(test), iso); 293 assertEquals(JapaneseDate.from(iso), test); 294 } 295 296 @Test(dataProvider="createByProleptic") 297 public void test_createByProleptic_query(int y, int moy, int dom, int doy, LocalDate iso) { 298 JapaneseDate test = JapaneseDate.of(y, moy, dom); 299 assertEquals(test.query(TemporalQueries.localDate()), iso); 300 } 301 302 @Test(dataProvider="createByProleptic") 303 public void test_createByProleptic_epochDay(int y, int moy, int dom, int doy, LocalDate iso) { 304 JapaneseDate test = JapaneseDate.of(y, moy, dom); 305 assertEquals(test.getLong(EPOCH_DAY), iso.getLong(EPOCH_DAY)); 306 assertEquals(test.toEpochDay(), iso.toEpochDay()); 307 } 308 309 //----------------------------------------------------------------------- 310 @Test 311 public void test_dateNow(){ 312 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now()) ; 313 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now(ZoneId.systemDefault())) ; 314 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now(Clock.systemDefaultZone())) ; 315 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseDate.now(Clock.systemDefaultZone().getZone())) ; 316 317 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseChronology.INSTANCE.dateNow(ZoneId.systemDefault())) ; 318 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseChronology.INSTANCE.dateNow(Clock.systemDefaultZone())) ; 319 assertEquals(JapaneseChronology.INSTANCE.dateNow(), JapaneseChronology.INSTANCE.dateNow(Clock.systemDefaultZone().getZone())) ; 320 321 ZoneId zoneId = ZoneId.of("Europe/Paris"); 322 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseChronology.INSTANCE.dateNow(Clock.system(zoneId))) ; 323 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseChronology.INSTANCE.dateNow(Clock.system(zoneId).getZone())) ; 324 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseDate.now(Clock.system(zoneId))) ; 325 assertEquals(JapaneseChronology.INSTANCE.dateNow(zoneId), JapaneseDate.now(Clock.system(zoneId).getZone())) ; 326 327 assertEquals(JapaneseChronology.INSTANCE.dateNow(ZoneId.of(ZoneOffset.UTC.getId())), JapaneseChronology.INSTANCE.dateNow(Clock.systemUTC())) ; 328 } 329 330 //----------------------------------------------------------------------- 331 @DataProvider(name="badDates") 332 Object[][] data_badDates() { 333 return new Object[][] { 334 {1928, 0, 0}, 335 336 {1928, -1, 1}, 337 {1928, 0, 1}, 338 {1928, 14, 1}, 339 {1928, 15, 1}, 340 341 {1928, 1, -1}, 342 {1928, 1, 0}, 343 {1928, 1, 32}, 344 345 {1928, 12, -1}, 346 {1928, 12, 0}, 347 {1928, 12, 32}, 348 349 {1725, 2, 29}, 350 {500, 2, 29}, 351 {2100, 2, 29}, 352 353 {1872, 12, 31}, // Last day of MEIJI 5 354 }; 355 } 356 357 @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class) 358 public void test_badDates(int year, int month, int dom) { 359 JapaneseChronology.INSTANCE.date(year, month, dom); 360 } 361 362 //----------------------------------------------------------------------- 363 // prolepticYear() and is LeapYear() 364 //----------------------------------------------------------------------- 365 @DataProvider(name="prolepticYear") 366 Object[][] data_prolepticYear() { 367 return new Object[][] { 368 {2, JapaneseEra.HEISEI, 1, 1 + YDIFF_HEISEI, false}, 369 {2, JapaneseEra.HEISEI, 100, 100 + YDIFF_HEISEI, true}, 370 371 {-1, JapaneseEra.MEIJI, 9, 9 + YDIFF_MEIJI, true}, 372 {-1, JapaneseEra.MEIJI, 10, 10 + YDIFF_MEIJI, false}, 373 374 {1, JapaneseEra.SHOWA, 1, 1 + YDIFF_SHOWA, false}, 375 {1, JapaneseEra.SHOWA, 7, 7 + YDIFF_SHOWA, true}, 376 377 {0, JapaneseEra.TAISHO, 1, 1 + YDIFF_TAISHO, true}, 378 {0, JapaneseEra.TAISHO, 4, 4 + YDIFF_TAISHO, false}, 379 }; 380 } 381 382 @Test(dataProvider="prolepticYear") 383 public void test_prolepticYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { 384 Era eraObj = JapaneseChronology.INSTANCE.eraOf(eraValue); 385 assertTrue(JapaneseChronology.INSTANCE.eras().contains(eraObj)); 386 assertEquals(eraObj, era); 387 assertEquals(JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear); 388 } 389 390 @Test(dataProvider="prolepticYear") 391 public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { 392 assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear); 393 assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear).isLeap()); 394 395 JapaneseDate jdate = JapaneseDate.now(); 396 jdate = jdate.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2); 397 if (isLeapYear) { 398 assertEquals(jdate.lengthOfMonth(), 29); 399 } else { 400 assertEquals(jdate.lengthOfMonth(), 28); 401 } 402 } 403 404 @DataProvider(name="prolepticYearError") 405 Object[][] data_prolepticYearError() { 406 return new Object[][] { 407 {JapaneseEra.MEIJI, 100}, 408 {JapaneseEra.MEIJI, 0}, 409 {JapaneseEra.MEIJI, -10}, 410 411 {JapaneseEra.SHOWA, 100}, 412 {JapaneseEra.SHOWA, 0}, 413 {JapaneseEra.SHOWA, -10}, 414 415 {JapaneseEra.TAISHO, 100}, 416 {JapaneseEra.TAISHO, 0}, 417 {JapaneseEra.TAISHO, -10}, 418 }; 419 } 420 421 @Test(dataProvider="prolepticYearError", expectedExceptions=DateTimeException.class) 422 public void test_prolepticYearError(Era era, int yearOfEra) { 423 JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra); 424 } 425 426 //----------------------------------------------------------------------- 427 // Bad Era for Chronology.date(era,...) and Chronology.prolepticYear(Era,...) 428 //----------------------------------------------------------------------- 429 @Test 430 public void test_InvalidEras() { 431 // Verify that the eras from every other Chronology are invalid 432 for (Chronology chrono : Chronology.getAvailableChronologies()) { 433 if (chrono instanceof JapaneseChronology) { 434 continue; 435 } 436 List<Era> eras = chrono.eras(); 437 for (Era era : eras) { 438 try { 439 ChronoLocalDate date = JapaneseChronology.INSTANCE.date(era, 1, 1, 1); 440 fail("JapaneseChronology.date did not throw ClassCastException for Era: " + era); 441 } catch (ClassCastException cex) { 442 ; // ignore expected exception 443 } 444 try { 445 @SuppressWarnings("unused") 446 int year = JapaneseChronology.INSTANCE.prolepticYear(era, 1); 447 fail("JapaneseChronology.prolepticYear did not throw ClassCastException for Era: " + era); 448 } catch (ClassCastException cex) { 449 ; // ignore expected exception 450 } 451 452 } 453 } 454 } 455 456 //----------------------------------------------------------------------- 457 // get(TemporalField) 458 //----------------------------------------------------------------------- 459 @Test 460 public void test_getLong() { 461 JapaneseDate base = JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 63, 6, 30); 462 assertEquals(base.getLong(ERA), JapaneseEra.SHOWA.getValue()); 463 assertEquals(base.getLong(YEAR), 1988L); 464 assertEquals(base.getLong(YEAR_OF_ERA), 63L); 465 assertEquals(base.getLong(MONTH_OF_YEAR), 6L); 466 assertEquals(base.getLong(DAY_OF_MONTH), 30L); 467 } 468 469 //----------------------------------------------------------------------- 470 // with(TemporalField, long) 471 //----------------------------------------------------------------------- 472 @Test 473 public void test_with_TemporalField_long() { 474 JapaneseDate base = JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 63, 6, 30); 475 JapaneseDate test = base.with(YEAR, 1987); 476 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 62, 6, 30)); 477 478 test = test.with(YEAR_OF_ERA, 2); 479 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.SHOWA, 2, 6, 30)); 480 481 test = test.with(ERA, JapaneseEra.HEISEI.getValue()); 482 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 2, 6, 30)); 483 484 test = test.with(MONTH_OF_YEAR, 3); 485 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 2, 3, 30)); 486 487 test = test.with(DAY_OF_MONTH, 4); 488 assertEquals(test, JapaneseChronology.INSTANCE.date(JapaneseEra.HEISEI, 2, 3, 4)); 489 } 490 491 //----------------------------------------------------------------------- 492 // with(WithAdjuster) 493 //----------------------------------------------------------------------- 494 @Test 495 public void test_adjust1() { 496 JapaneseDate base = JapaneseChronology.INSTANCE.date(1928, 10, 29); 497 JapaneseDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 498 assertEquals(test, JapaneseChronology.INSTANCE.date(1928, 10, 31)); 499 } 500 501 @Test 502 public void test_adjust2() { 503 JapaneseDate base = JapaneseChronology.INSTANCE.date(1928, 12, 2); 504 JapaneseDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 505 assertEquals(test, JapaneseChronology.INSTANCE.date(1928, 12, 31)); 506 } 507 508 //----------------------------------------------------------------------- 509 // JapaneseDate.with(Local*) 510 //----------------------------------------------------------------------- 511 @Test 512 public void test_adjust_toLocalDate() { 513 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1926, 1, 4); 514 JapaneseDate test = jdate.with(LocalDate.of(2012, 7, 6)); 515 assertEquals(test, JapaneseChronology.INSTANCE.date(2012, 7, 6)); 516 } 517 518 @Test(expectedExceptions=DateTimeException.class) 519 public void test_adjust_toMonth() { 520 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1926, 1, 4); 521 jdate.with(Month.APRIL); 522 } 523 524 //----------------------------------------------------------------------- 525 // LocalDate.with(JapaneseDate) 526 //----------------------------------------------------------------------- 527 @Test 528 public void test_LocalDate_adjustToJapaneseDate() { 529 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1928, 10, 29); 530 LocalDate test = LocalDate.MIN.with(jdate); 531 assertEquals(test, LocalDate.of(1928, 10, 29)); 532 } 533 534 @Test 535 public void test_LocalDateTime_adjustToJapaneseDate() { 536 JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1928, 10, 29); 537 LocalDateTime test = LocalDateTime.MIN.with(jdate); 538 assertEquals(test, LocalDateTime.of(1928, 10, 29, 0, 0)); 539 } 540 541 //----------------------------------------------------------------------- 542 // Check Japanese Eras 543 //----------------------------------------------------------------------- 544 @DataProvider(name="japaneseEras") 545 Object[][] data_japanseseEras() { 546 return new Object[][] { 547 { JapaneseEra.MEIJI, -1, "Meiji"}, 548 { JapaneseEra.TAISHO, 0, "Taisho"}, 549 { JapaneseEra.SHOWA, 1, "Showa"}, 550 { JapaneseEra.HEISEI, 2, "Heisei"}, 551 }; 552 } 553 554 @Test(dataProvider="japaneseEras") 555 public void test_Japanese_Eras(Era era, int eraValue, String name) { 556 assertEquals(era.getValue(), eraValue, "EraValue"); 557 assertEquals(era.toString(), name, "Era Name"); 558 assertEquals(era, JapaneseChronology.INSTANCE.eraOf(eraValue), "JapaneseChronology.eraOf()"); 559 List<Era> eras = JapaneseChronology.INSTANCE.eras(); 560 assertTrue(eras.contains(era), "Era is not present in JapaneseChronology.INSTANCE.eras()"); 561 } 562 563 @Test 564 public void test_Japanese_badEras() { 565 int badEras[] = {-1000, -998, -997, -2, 3, 4, 1000}; 566 for (int badEra : badEras) { 567 try { 568 Era era = JapaneseChronology.INSTANCE.eraOf(badEra); 569 fail("JapaneseChronology.eraOf returned " + era + " + for invalid eraValue " + badEra); 570 } catch (DateTimeException ex) { 571 // ignore expected exception 572 } 573 } 574 } 575 576 @Test(dataProvider="japaneseEras") 577 public void test_JapaneseEra_singletons(Era expectedEra, int eraValue, String name) { 578 JapaneseEra actualEra = JapaneseEra.valueOf(name); 579 assertEquals(actualEra, expectedEra, "JapaneseEra.valueOf(name)"); 580 581 actualEra = JapaneseEra.of(eraValue); 582 assertEquals(actualEra, expectedEra, "JapaneseEra.of(value)"); 583 584 String string = actualEra.toString(); 585 assertEquals(string, name, "JapaneseEra.toString()"); 586 } 587 588 @Test 589 public void test_JapaneseEra_values() { 590 JapaneseEra[] actualEras = JapaneseEra.values(); 591 Object[][] erasInfo = data_japanseseEras(); 592 assertEquals(actualEras.length, erasInfo.length, "Wrong number of Eras"); 593 594 for (int i = 0; i < erasInfo.length; i++) { 595 Object[] eraInfo = erasInfo[i]; 596 assertEquals(actualEras[i], eraInfo[0], "Singleton mismatch"); 597 } 598 } 599 600 @Test 601 public void test_JapaneseChronology_eras() { 602 List<Era> actualEras = JapaneseChronology.INSTANCE.eras(); 603 Object[][] erasInfo = data_japanseseEras(); 604 assertEquals(actualEras.size(), erasInfo.length, "Wrong number of Eras"); 605 606 for (int i = 0; i < erasInfo.length; i++) { 607 Object[] eraInfo = erasInfo[i]; 608 assertEquals(actualEras.get(i), eraInfo[0], "Singleton mismatch"); 609 } 610 } 611 612 //----------------------------------------------------------------------- 613 // PeriodUntil() 614 //----------------------------------------------------------------------- 615 @Test 616 public void test_periodUntilDate() { 617 JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1); 618 JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2); 619 ChronoPeriod period = mdate1.until(mdate2); 620 assertEquals(period, JapaneseChronology.INSTANCE.period(1, 1, 1)); 621 } 622 623 @Test 624 public void test_periodUntilUnit() { 625 JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1); 626 JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2); 627 long months = mdate1.until(mdate2, ChronoUnit.MONTHS); 628 assertEquals(months, 13); 629 } 630 631 @Test 632 public void test_periodUntilDiffChrono() { 633 JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1); 634 JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2); 635 MinguoDate ldate2 = MinguoChronology.INSTANCE.date(mdate2); 636 ChronoPeriod period = mdate1.until(ldate2); 637 assertEquals(period, JapaneseChronology.INSTANCE.period(1, 1, 1)); 638 } 639 640 //----------------------------------------------------------------------- 641 // JapaneseChronology.dateYearDay, getDayOfYear 642 //----------------------------------------------------------------------- 643 @Test 644 public void test_getDayOfYear() { 645 // Test all the Eras 646 for (JapaneseEra era : JapaneseEra.values()) { 647 int firstYear = (era == JapaneseEra.MEIJI) ? 6 : 1; // Until Era supports range(YEAR_OF_ERA) 648 JapaneseDate hd1 = JapaneseChronology.INSTANCE.dateYearDay(era, firstYear, 1); 649 ValueRange range = hd1.range(DAY_OF_YEAR); 650 assertEquals(range.getMaximum(), hd1.lengthOfYear(), "lengthOfYear should match range.getMaximum()"); 651 652 for (int i = 1; i <= hd1.lengthOfYear(); i++) { 653 JapaneseDate hd = JapaneseChronology.INSTANCE.dateYearDay(era, firstYear, i); 654 int doy = hd.get(DAY_OF_YEAR); 655 assertEquals(doy, i, "get(DAY_OF_YEAR) incorrect for " + i + ", of date: " + hd); 656 } 657 } 658 } 659 660 @Test 661 public void test_withDayOfYear() { 662 JapaneseDate hd = JapaneseChronology.INSTANCE.dateYearDay(1990, 1); 663 for (int i = 1; i <= hd.lengthOfYear(); i++) { 664 JapaneseDate hd2 = hd.with(DAY_OF_YEAR, i); 665 int doy = hd2.get(DAY_OF_YEAR); 666 assertEquals(doy, i, "with(DAY_OF_YEAR) incorrect for " + i + " " + hd2); 667 } 668 } 669 670 //----------------------------------------------------------------------- 671 // toString() 672 //----------------------------------------------------------------------- 673 @DataProvider(name="toString") 674 Object[][] data_toString() { 675 return new Object[][] { 676 {JapaneseChronology.INSTANCE.date(1873, 12, 5), "Japanese Meiji 6-12-05"}, 677 {JapaneseChronology.INSTANCE.date(1873, 12, 6), "Japanese Meiji 6-12-06"}, 678 {JapaneseChronology.INSTANCE.date(1873, 9, 8), "Japanese Meiji 6-09-08"}, 679 {JapaneseChronology.INSTANCE.date(1912, 7, 29), "Japanese Meiji 45-07-29"}, 680 {JapaneseChronology.INSTANCE.date(1912, 7, 30), "Japanese Taisho 1-07-30"}, 681 {JapaneseChronology.INSTANCE.date(1926, 12, 24), "Japanese Taisho 15-12-24"}, 682 {JapaneseChronology.INSTANCE.date(1926, 12, 25), "Japanese Showa 1-12-25"}, 683 {JapaneseChronology.INSTANCE.date(1989, 1, 7), "Japanese Showa 64-01-07"}, 684 {JapaneseChronology.INSTANCE.date(1989, 1, 8), "Japanese Heisei 1-01-08"}, 685 {JapaneseChronology.INSTANCE.date(2012, 12, 6), "Japanese Heisei 24-12-06"}, 686 }; 687 } 688 689 @Test(dataProvider="toString") 690 public void test_toString(JapaneseDate jdate, String expected) { 691 assertEquals(jdate.toString(), expected); 692 } 693 694 //----------------------------------------------------------------------- 695 // equals() 696 //----------------------------------------------------------------------- 697 @Test 698 public void test_equals_true() { 699 assertTrue(JapaneseChronology.INSTANCE.equals(JapaneseChronology.INSTANCE)); 700 } 701 702 @Test 703 public void test_equals_false() { 704 assertFalse(JapaneseChronology.INSTANCE.equals(IsoChronology.INSTANCE)); 705 } 706 707 //----------------------------------------------------------------------- 708 //----------------------------------------------------------------------- 709 @DataProvider(name = "resolve_styleByEra") 710 Object[][] data_resolve_styleByEra() { 711 Object[][] result = new Object[ResolverStyle.values().length * JapaneseEra.values().length][]; 712 int i = 0; 713 for (ResolverStyle style : ResolverStyle.values()) { 714 for (JapaneseEra era : JapaneseEra.values()) { 715 result[i++] = new Object[] {style, era}; 716 } 717 } 718 return result; 719 } 720 721 @Test(dataProvider = "resolve_styleByEra") 722 public void test_resolve_yearOfEra_eraOnly_valid(ResolverStyle style, JapaneseEra era) { 723 Map<TemporalField, Long> fieldValues = new HashMap<>(); 724 fieldValues.put(ChronoField.ERA, (long) era.getValue()); 725 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 726 assertEquals(date, null); 727 assertEquals(fieldValues.get(ChronoField.ERA), (Long) (long) era.getValue()); 728 assertEquals(fieldValues.size(), 1); 729 } 730 731 @Test(dataProvider = "resolve_styleByEra") 732 public void test_resolve_yearOfEra_eraAndYearOfEraOnly_valid(ResolverStyle style, JapaneseEra era) { 733 Map<TemporalField, Long> fieldValues = new HashMap<>(); 734 fieldValues.put(ChronoField.ERA, (long) era.getValue()); 735 fieldValues.put(ChronoField.YEAR_OF_ERA, 1L); 736 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 737 assertEquals(date, null); 738 assertEquals(fieldValues.get(ChronoField.ERA), (Long) (long) era.getValue()); 739 assertEquals(fieldValues.get(ChronoField.YEAR_OF_ERA), (Long) 1L); 740 assertEquals(fieldValues.size(), 2); 741 } 742 743 @Test(dataProvider = "resolve_styleByEra") 744 public void test_resolve_yearOfEra_eraAndYearOnly_valid(ResolverStyle style, JapaneseEra era) { 745 Map<TemporalField, Long> fieldValues = new HashMap<>(); 746 fieldValues.put(ChronoField.ERA, (long) era.getValue()); 747 fieldValues.put(ChronoField.YEAR, 1L); 748 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 749 assertEquals(date, null); 750 assertEquals(fieldValues.get(ChronoField.ERA), (Long) (long) era.getValue()); 751 assertEquals(fieldValues.get(ChronoField.YEAR), (Long) 1L); 752 assertEquals(fieldValues.size(), 2); 753 } 754 755 @DataProvider(name = "resolve_styles") 756 Object[][] data_resolve_styles() { 757 Object[][] result = new Object[ResolverStyle.values().length][]; 758 int i = 0; 759 for (ResolverStyle style : ResolverStyle.values()) { 760 result[i++] = new Object[] {style}; 761 } 762 return result; 763 } 764 765 @Test(dataProvider = "resolve_styles") 766 public void test_resolve_yearOfEra_yearOfEraOnly_valid(ResolverStyle style) { 767 Map<TemporalField, Long> fieldValues = new HashMap<>(); 768 fieldValues.put(ChronoField.YEAR_OF_ERA, 1L); 769 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 770 assertEquals(date, null); 771 assertEquals(fieldValues.get(ChronoField.YEAR_OF_ERA), (Long) 1L); 772 assertEquals(fieldValues.size(), 1); 773 } 774 775 @Test(dataProvider = "resolve_styles") 776 public void test_resolve_yearOfEra_yearOfEraAndYearOnly_valid(ResolverStyle style) { 777 Map<TemporalField, Long> fieldValues = new HashMap<>(); 778 fieldValues.put(ChronoField.YEAR_OF_ERA, 1L); 779 fieldValues.put(ChronoField.YEAR, 2012L); 780 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 781 assertEquals(date, null); 782 assertEquals(fieldValues.get(ChronoField.YEAR_OF_ERA), (Long) 1L); 783 assertEquals(fieldValues.get(ChronoField.YEAR), (Long) 2012L); 784 assertEquals(fieldValues.size(), 2); 785 } 786 787 public void test_resolve_yearOfEra_eraOnly_invalidTooSmall() { 788 for (ResolverStyle style : ResolverStyle.values()) { 789 Map<TemporalField, Long> fieldValues = new HashMap<>(); 790 fieldValues.put(ChronoField.ERA, JapaneseEra.MEIJI.getValue() - 1L); 791 try { 792 JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 793 fail("Should have failed: " + style); 794 } catch (DateTimeException ex) { 795 // expected 796 } 797 } 798 } 799 800 public void test_resolve_yearOfEra_eraOnly_invalidTooLarge() { 801 for (ResolverStyle style : ResolverStyle.values()) { 802 Map<TemporalField, Long> fieldValues = new HashMap<>(); 803 fieldValues.put(ChronoField.ERA, JapaneseEra.values()[JapaneseEra.values().length - 1].getValue() + 1L); 804 try { 805 JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 806 fail("Should have failed: " + style); 807 } catch (DateTimeException ex) { 808 // expected 809 } 810 } 811 } 812 813 //----------------------------------------------------------------------- 814 //----------------------------------------------------------------------- 815 @DataProvider(name = "resolve_ymd") 816 Object[][] data_resolve_ymd() { 817 return new Object[][] { 818 {2012, 1, -365, date(2010, 12, 31), false, false}, 819 {2012, 1, -364, date(2011, 1, 1), false, false}, 820 {2012, 1, -31, date(2011, 11, 30), false, false}, 821 {2012, 1, -30, date(2011, 12, 1), false, false}, 822 {2012, 1, -12, date(2011, 12, 19), false, false}, 823 {2012, 1, 1, date(2012, 1, 1), true, true}, 824 {2012, 1, 59, date(2012, 2, 28), false, false}, 825 {2012, 1, 60, date(2012, 2, 29), false, false}, 826 {2012, 1, 61, date(2012, 3, 1), false, false}, 827 {2012, 1, 365, date(2012, 12, 30), false, false}, 828 {2012, 1, 366, date(2012, 12, 31), false, false}, 829 {2012, 1, 367, date(2013, 1, 1), false, false}, 830 {2012, 1, 367 + 364, date(2013, 12, 31), false, false}, 831 {2012, 1, 367 + 365, date(2014, 1, 1), false, false}, 832 833 {2012, 2, 1, date(2012, 2, 1), true, true}, 834 {2012, 2, 28, date(2012, 2, 28), true, true}, 835 {2012, 2, 29, date(2012, 2, 29), true, true}, 836 {2012, 2, 30, date(2012, 3, 1), date(2012, 2, 29), false}, 837 {2012, 2, 31, date(2012, 3, 2), date(2012, 2, 29), false}, 838 {2012, 2, 32, date(2012, 3, 3), false, false}, 839 840 {2012, -12, 1, date(2010, 12, 1), false, false}, 841 {2012, -11, 1, date(2011, 1, 1), false, false}, 842 {2012, -1, 1, date(2011, 11, 1), false, false}, 843 {2012, 0, 1, date(2011, 12, 1), false, false}, 844 {2012, 1, 1, date(2012, 1, 1), true, true}, 845 {2012, 12, 1, date(2012, 12, 1), true, true}, 846 {2012, 13, 1, date(2013, 1, 1), false, false}, 847 {2012, 24, 1, date(2013, 12, 1), false, false}, 848 {2012, 25, 1, date(2014, 1, 1), false, false}, 849 850 {2012, 6, -31, date(2012, 4, 30), false, false}, 851 {2012, 6, -30, date(2012, 5, 1), false, false}, 852 {2012, 6, -1, date(2012, 5, 30), false, false}, 853 {2012, 6, 0, date(2012, 5, 31), false, false}, 854 {2012, 6, 1, date(2012, 6, 1), true, true}, 855 {2012, 6, 30, date(2012, 6, 30), true, true}, 856 {2012, 6, 31, date(2012, 7, 1), date(2012, 6, 30), false}, 857 {2012, 6, 61, date(2012, 7, 31), false, false}, 858 {2012, 6, 62, date(2012, 8, 1), false, false}, 859 860 {2011, 2, 1, date(2011, 2, 1), true, true}, 861 {2011, 2, 28, date(2011, 2, 28), true, true}, 862 {2011, 2, 29, date(2011, 3, 1), date(2011, 2, 28), false}, 863 {2011, 2, 30, date(2011, 3, 2), date(2011, 2, 28), false}, 864 {2011, 2, 31, date(2011, 3, 3), date(2011, 2, 28), false}, 865 {2011, 2, 32, date(2011, 3, 4), false, false}, 866 }; 867 } 868 869 @Test(dataProvider = "resolve_ymd") 870 public void test_resolve_ymd_lenient(int y, int m, int d, JapaneseDate expected, Object smart, boolean strict) { 871 Map<TemporalField, Long> fieldValues = new HashMap<>(); 872 fieldValues.put(ChronoField.YEAR, (long) y); 873 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 874 fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); 875 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); 876 assertEquals(date, expected); 877 assertEquals(fieldValues.size(), 0); 878 } 879 880 @Test(dataProvider = "resolve_ymd") 881 public void test_resolve_ymd_smart(int y, int m, int d, JapaneseDate expected, Object smart, boolean strict) { 882 Map<TemporalField, Long> fieldValues = new HashMap<>(); 883 fieldValues.put(ChronoField.YEAR, (long) y); 884 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 885 fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); 886 if (Boolean.TRUE.equals(smart)) { 887 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 888 assertEquals(date, expected); 889 assertEquals(fieldValues.size(), 0); 890 } else if (smart instanceof JapaneseDate) { 891 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 892 assertEquals(date, smart); 893 } else { 894 try { 895 JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 896 fail("Should have failed"); 897 } catch (DateTimeException ex) { 898 // expected 899 } 900 } 901 } 902 903 @Test(dataProvider = "resolve_ymd") 904 public void test_resolve_ymd_strict(int y, int m, int d, JapaneseDate expected, Object smart, boolean strict) { 905 Map<TemporalField, Long> fieldValues = new HashMap<>(); 906 fieldValues.put(ChronoField.YEAR, (long) y); 907 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 908 fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); 909 if (strict) { 910 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 911 assertEquals(date, expected); 912 assertEquals(fieldValues.size(), 0); 913 } else { 914 try { 915 JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 916 fail("Should have failed"); 917 } catch (DateTimeException ex) { 918 // expected 919 } 920 } 921 } 922 923 //----------------------------------------------------------------------- 924 //----------------------------------------------------------------------- 925 @DataProvider(name = "resolve_yd") 926 Object[][] data_resolve_yd() { 927 return new Object[][] { 928 {2012, -365, date(2010, 12, 31), false, false}, 929 {2012, -364, date(2011, 1, 1), false, false}, 930 {2012, -31, date(2011, 11, 30), false, false}, 931 {2012, -30, date(2011, 12, 1), false, false}, 932 {2012, -12, date(2011, 12, 19), false, false}, 933 {2012, -1, date(2011, 12, 30), false, false}, 934 {2012, 0, date(2011, 12, 31), false, false}, 935 {2012, 1, date(2012, 1, 1), true, true}, 936 {2012, 2, date(2012, 1, 2), true, true}, 937 {2012, 31, date(2012, 1, 31), true, true}, 938 {2012, 32, date(2012, 2, 1), true, true}, 939 {2012, 59, date(2012, 2, 28), true, true}, 940 {2012, 60, date(2012, 2, 29), true, true}, 941 {2012, 61, date(2012, 3, 1), true, true}, 942 {2012, 365, date(2012, 12, 30), true, true}, 943 {2012, 366, date(2012, 12, 31), true, true}, 944 {2012, 367, date(2013, 1, 1), false, false}, 945 {2012, 367 + 364, date(2013, 12, 31), false, false}, 946 {2012, 367 + 365, date(2014, 1, 1), false, false}, 947 948 {2011, 59, date(2011, 2, 28), true, true}, 949 {2011, 60, date(2011, 3, 1), true, true}, 950 }; 951 } 952 953 @Test(dataProvider = "resolve_yd") 954 public void test_resolve_yd_lenient(int y, int d, JapaneseDate expected, boolean smart, boolean strict) { 955 Map<TemporalField, Long> fieldValues = new HashMap<>(); 956 fieldValues.put(ChronoField.YEAR, (long) y); 957 fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); 958 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); 959 assertEquals(date, expected); 960 assertEquals(fieldValues.size(), 0); 961 } 962 963 @Test(dataProvider = "resolve_yd") 964 public void test_resolve_yd_smart(int y, int d, JapaneseDate expected, boolean smart, boolean strict) { 965 Map<TemporalField, Long> fieldValues = new HashMap<>(); 966 fieldValues.put(ChronoField.YEAR, (long) y); 967 fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); 968 if (smart) { 969 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 970 assertEquals(date, expected); 971 assertEquals(fieldValues.size(), 0); 972 } else { 973 try { 974 JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 975 fail("Should have failed"); 976 } catch (DateTimeException ex) { 977 // expected 978 } 979 } 980 } 981 982 @Test(dataProvider = "resolve_yd") 983 public void test_resolve_yd_strict(int y, int d, JapaneseDate expected, boolean smart, boolean strict) { 984 Map<TemporalField, Long> fieldValues = new HashMap<>(); 985 fieldValues.put(ChronoField.YEAR, (long) y); 986 fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); 987 if (strict) { 988 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 989 assertEquals(date, expected); 990 assertEquals(fieldValues.size(), 0); 991 } else { 992 try { 993 JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 994 fail("Should have failed"); 995 } catch (DateTimeException ex) { 996 // expected 997 } 998 } 999 } 1000 1001 //----------------------------------------------------------------------- 1002 //----------------------------------------------------------------------- 1003 @DataProvider(name = "resolve_eymd") 1004 Object[][] data_resolve_eymd() { 1005 return new Object[][] { 1006 // lenient 1007 {ResolverStyle.LENIENT, JapaneseEra.HEISEI, 1, 1, 1, date(1989, 1, 1)}, // SHOWA, not HEISEI 1008 {ResolverStyle.LENIENT, JapaneseEra.HEISEI, 1, 1, 7, date(1989, 1, 7)}, // SHOWA, not HEISEI 1009 {ResolverStyle.LENIENT, JapaneseEra.HEISEI, 1, 1, 8, date(1989, 1, 8)}, 1010 {ResolverStyle.LENIENT, JapaneseEra.HEISEI, 1, 12, 31, date(1989, 12, 31)}, 1011 {ResolverStyle.LENIENT, JapaneseEra.HEISEI, 2, 1, 1, date(1990, 1, 1)}, 1012 1013 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 1, date(1989, 1, 1)}, 1014 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 7, date(1989, 1, 7)}, 1015 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 8, date(1989, 1, 8)}, // HEISEI, not SHOWA 1016 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 12, 31, date(1989, 12, 31)}, // HEISEI, not SHOWA 1017 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 65, 1, 1, date(1990, 1, 1)}, // HEISEI, not SHOWA 1018 1019 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, -366, date(1987, 12, 31)}, 1020 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, -365, date(1988, 1, 1)}, 1021 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, -31, date(1988, 11, 30)}, 1022 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, -30, date(1988, 12, 1)}, 1023 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 0, date(1988, 12, 31)}, 1024 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 1, date(1989, 1, 1)}, 1025 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 27, date(1989, 1, 27)}, 1026 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 28, date(1989, 1, 28)}, 1027 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 29, date(1989, 1, 29)}, 1028 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 30, date(1989, 1, 30)}, 1029 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 31, date(1989, 1, 31)}, 1030 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 32, date(1989, 2, 1)}, 1031 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 58, date(1989, 2, 27)}, 1032 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 59, date(1989, 2, 28)}, 1033 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 60, date(1989, 3, 1)}, 1034 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 365, date(1989, 12, 31)}, 1035 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 1, 366, date(1990, 1, 1)}, 1036 1037 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 1, 1, date(1988, 1, 1)}, 1038 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 1, 31, date(1988, 1, 31)}, 1039 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 1, 32, date(1988, 2, 1)}, 1040 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 1, 58, date(1988, 2, 27)}, 1041 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 1, 59, date(1988, 2, 28)}, 1042 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 1, 60, date(1988, 2, 29)}, 1043 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 1, 61, date(1988, 3, 1)}, 1044 1045 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 2, 1, date(1989, 2, 1)}, 1046 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 2, 28, date(1989, 2, 28)}, 1047 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 64, 2, 29, date(1989, 3, 1)}, 1048 1049 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 2, 1, date(1988, 2, 1)}, 1050 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 2, 28, date(1988, 2, 28)}, 1051 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 2, 29, date(1988, 2, 29)}, 1052 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 63, 2, 30, date(1988, 3, 1)}, 1053 1054 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 62, -11, 1, date(1986, 1, 1)}, 1055 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 62, -1, 1, date(1986, 11, 1)}, 1056 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 62, 0, 1, date(1986, 12, 1)}, 1057 {ResolverStyle.LENIENT, JapaneseEra.SHOWA, 62, 13, 1, date(1988, 1, 1)}, 1058 1059 // smart 1060 {ResolverStyle.SMART, JapaneseEra.HEISEI, 0, 1, 1, null}, 1061 {ResolverStyle.SMART, JapaneseEra.HEISEI, 1, 1, 1, date(1989, 1, 1)}, // SHOWA, not HEISEI 1062 {ResolverStyle.SMART, JapaneseEra.HEISEI, 1, 1, 7, date(1989, 1, 7)}, // SHOWA, not HEISEI 1063 {ResolverStyle.SMART, JapaneseEra.HEISEI, 1, 1, 8, date(1989, 1, 8)}, 1064 {ResolverStyle.SMART, JapaneseEra.HEISEI, 1, 12, 31, date(1989, 12, 31)}, 1065 {ResolverStyle.SMART, JapaneseEra.HEISEI, 2, 1, 1, date(1990, 1, 1)}, 1066 1067 {ResolverStyle.SMART, JapaneseEra.SHOWA, 64, 1, 1, date(1989, 1, 1)}, 1068 {ResolverStyle.SMART, JapaneseEra.SHOWA, 64, 1, 7, date(1989, 1, 7)}, 1069 {ResolverStyle.SMART, JapaneseEra.SHOWA, 64, 1, 8, date(1989, 1, 8)}, // HEISEI, not SHOWA 1070 {ResolverStyle.SMART, JapaneseEra.SHOWA, 64, 12, 31, date(1989, 12, 31)}, // HEISEI, not SHOWA 1071 {ResolverStyle.SMART, JapaneseEra.SHOWA, 65, 1, 1, null}, // HEISEI, not SHOWA 1072 1073 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 0, null}, 1074 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 1, date(1987, 1, 1)}, 1075 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 27, date(1987, 1, 27)}, 1076 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 28, date(1987, 1, 28)}, 1077 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 29, date(1987, 1, 29)}, 1078 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 30, date(1987, 1, 30)}, 1079 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 31, date(1987, 1, 31)}, 1080 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 1, 32, null}, 1081 1082 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 0, null}, 1083 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 1, date(1987, 2, 1)}, 1084 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 27, date(1987, 2, 27)}, 1085 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 28, date(1987, 2, 28)}, 1086 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 29, date(1987, 2, 28)}, 1087 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 30, date(1987, 2, 28)}, 1088 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 31, date(1987, 2, 28)}, 1089 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 2, 32, null}, 1090 1091 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 0, null}, 1092 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 1, date(1988, 2, 1)}, 1093 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 27, date(1988, 2, 27)}, 1094 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 28, date(1988, 2, 28)}, 1095 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 29, date(1988, 2, 29)}, 1096 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 30, date(1988, 2, 29)}, 1097 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 31, date(1988, 2, 29)}, 1098 {ResolverStyle.SMART, JapaneseEra.SHOWA, 63, 2, 32, null}, 1099 1100 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, -12, 1, null}, 1101 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, -1, 1, null}, 1102 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 0, 1, null}, 1103 {ResolverStyle.SMART, JapaneseEra.SHOWA, 62, 13, 1, null}, 1104 1105 // strict 1106 {ResolverStyle.STRICT, JapaneseEra.HEISEI, 0, 1, 1, null}, 1107 {ResolverStyle.STRICT, JapaneseEra.HEISEI, 1, 1, 1, null}, // SHOWA, not HEISEI 1108 {ResolverStyle.STRICT, JapaneseEra.HEISEI, 1, 1, 7, null}, // SHOWA, not HEISEI 1109 {ResolverStyle.STRICT, JapaneseEra.HEISEI, 1, 1, 8, date(1989, 1, 8)}, 1110 {ResolverStyle.STRICT, JapaneseEra.HEISEI, 1, 12, 31, date(1989, 12, 31)}, 1111 {ResolverStyle.STRICT, JapaneseEra.HEISEI, 2, 1, 1, date(1990, 1, 1)}, 1112 1113 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 64, 1, 1, date(1989, 1, 1)}, 1114 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 64, 1, 7, date(1989, 1, 7)}, 1115 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 64, 1, 8, null}, // HEISEI, not SHOWA 1116 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 64, 12, 31, null}, // HEISEI, not SHOWA 1117 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 65, 1, 1, null}, // HEISEI, not SHOWA 1118 1119 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 0, null}, 1120 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 1, date(1987, 1, 1)}, 1121 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 27, date(1987, 1, 27)}, 1122 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 28, date(1987, 1, 28)}, 1123 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 29, date(1987, 1, 29)}, 1124 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 30, date(1987, 1, 30)}, 1125 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 31, date(1987, 1, 31)}, 1126 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 1, 32, null}, 1127 1128 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 0, null}, 1129 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 1, date(1987, 2, 1)}, 1130 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 27, date(1987, 2, 27)}, 1131 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 28, date(1987, 2, 28)}, 1132 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 29, null}, 1133 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 30, null}, 1134 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 31, null}, 1135 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 2, 32, null}, 1136 1137 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 0, null}, 1138 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 1, date(1988, 2, 1)}, 1139 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 27, date(1988, 2, 27)}, 1140 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 28, date(1988, 2, 28)}, 1141 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 29, date(1988, 2, 29)}, 1142 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 30, null}, 1143 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 31, null}, 1144 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 63, 2, 32, null}, 1145 1146 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, -12, 1, null}, 1147 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, -1, 1, null}, 1148 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 0, 1, null}, 1149 {ResolverStyle.STRICT, JapaneseEra.SHOWA, 62, 13, 1, null}, 1150 }; 1151 } 1152 1153 @Test(dataProvider = "resolve_eymd") 1154 public void test_resolve_eymd(ResolverStyle style, JapaneseEra era, int yoe, int m, int d, JapaneseDate expected) { 1155 Map<TemporalField, Long> fieldValues = new HashMap<>(); 1156 fieldValues.put(ChronoField.ERA, (long) era.getValue()); 1157 fieldValues.put(ChronoField.YEAR_OF_ERA, (long) yoe); 1158 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 1159 fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); 1160 if (expected != null) { 1161 JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 1162 assertEquals(date, expected); 1163 assertEquals(fieldValues.size(), 0); 1164 } else { 1165 try { 1166 JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); 1167 fail("Should have failed"); 1168 } catch (DateTimeException ex) { 1169 // expected 1170 } 1171 } 1172 } 1173 1174 //----------------------------------------------------------------------- 1175 private static JapaneseDate date(int y, int m, int d) { 1176 return JapaneseDate.of(y, m, d); 1177 } 1178 1179 }