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 org.testng.Assert.assertEquals; 60 import static org.testng.Assert.assertFalse; 61 import static org.testng.Assert.assertNotEquals; 62 import static org.testng.Assert.assertTrue; 63 import static org.testng.Assert.fail; 64 65 import java.time.Clock; 66 import java.time.DateTimeException; 67 import java.time.LocalDate; 68 import java.time.LocalDateTime; 69 import java.time.LocalTime; 70 import java.time.Month; 71 import java.time.OffsetDateTime; 72 import java.time.Year; 73 import java.time.ZoneId; 74 import java.time.ZoneOffset; 75 import java.time.ZonedDateTime; 76 import java.time.chrono.ChronoLocalDate; 77 import java.time.chrono.ChronoLocalDateTime; 78 import java.time.chrono.ChronoPeriod; 79 import java.time.chrono.ChronoZonedDateTime; 80 import java.time.chrono.Chronology; 81 import java.time.chrono.Era; 82 import java.time.chrono.IsoChronology; 83 import java.time.chrono.JapaneseDate; 84 import java.time.chrono.MinguoChronology; 85 import java.time.chrono.MinguoEra; 86 import java.time.chrono.MinguoDate; 87 import java.time.chrono.ThaiBuddhistChronology; 88 import java.time.chrono.ThaiBuddhistDate; 89 import java.time.format.ResolverStyle; 90 import java.time.temporal.ChronoField; 91 import java.time.temporal.ChronoUnit; 92 import java.time.temporal.TemporalAccessor; 93 import java.time.temporal.TemporalAdjusters; 94 import java.time.temporal.TemporalField; 95 import java.util.HashMap; 96 import java.util.List; 97 import java.util.Map; 98 99 import org.testng.Assert; 100 import org.testng.annotations.DataProvider; 101 import org.testng.annotations.Test; 102 103 /** 104 * Test. 105 */ 106 @Test 107 public class TCKMinguoChronology { 108 109 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2); 110 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 111 private static final int YDIFF = 1911; 112 //----------------------------------------------------------------------- 113 // Chronology.ofName("Minguo") Lookup by name 114 //----------------------------------------------------------------------- 115 @Test test_chrono_byName()116 public void test_chrono_byName() { 117 Chronology c = MinguoChronology.INSTANCE; 118 Chronology test = Chronology.of("Minguo"); 119 Assert.assertNotNull(test, "The Minguo calendar could not be found byName"); 120 Assert.assertEquals(test.getId(), "Minguo", "ID mismatch"); 121 Assert.assertEquals(test.getCalendarType(), "roc", "Type mismatch"); 122 Assert.assertEquals(test, c); 123 } 124 125 //----------------------------------------------------------------------- 126 // creation, toLocalDate() 127 //----------------------------------------------------------------------- 128 @DataProvider(name="samples") data_samples()129 Object[][] data_samples() { 130 return new Object[][] { 131 {MinguoChronology.INSTANCE.date(1, 1, 1), LocalDate.of(1 + YDIFF, 1, 1)}, 132 {MinguoChronology.INSTANCE.date(1, 1, 2), LocalDate.of(1 + YDIFF, 1, 2)}, 133 {MinguoChronology.INSTANCE.date(1, 1, 3), LocalDate.of(1 + YDIFF, 1, 3)}, 134 135 {MinguoChronology.INSTANCE.date(2, 1, 1), LocalDate.of(2 + YDIFF, 1, 1)}, 136 {MinguoChronology.INSTANCE.date(3, 1, 1), LocalDate.of(3 + YDIFF, 1, 1)}, 137 {MinguoChronology.INSTANCE.date(3, 12, 6), LocalDate.of(3 + YDIFF, 12, 6)}, 138 {MinguoChronology.INSTANCE.date(4, 1, 1), LocalDate.of(4 + YDIFF, 1, 1)}, 139 {MinguoChronology.INSTANCE.date(4, 7, 3), LocalDate.of(4 + YDIFF, 7, 3)}, 140 {MinguoChronology.INSTANCE.date(4, 7, 4), LocalDate.of(4 + YDIFF, 7, 4)}, 141 {MinguoChronology.INSTANCE.date(5, 1, 1), LocalDate.of(5 + YDIFF, 1, 1)}, 142 {MinguoChronology.INSTANCE.date(100, 3, 3), LocalDate.of(100 + YDIFF, 3, 3)}, 143 {MinguoChronology.INSTANCE.date(101, 10, 28), LocalDate.of(101 + YDIFF, 10, 28)}, 144 {MinguoChronology.INSTANCE.date(101, 10, 29), LocalDate.of(101 + YDIFF, 10, 29)}, 145 146 {MinguoChronology.INSTANCE.dateYearDay(1916 - YDIFF, 60), LocalDate.of(1916, 2, 29)}, 147 {MinguoChronology.INSTANCE.dateYearDay(1908 - YDIFF, 60), LocalDate.of(1908, 2, 29)}, 148 {MinguoChronology.INSTANCE.dateYearDay(2000 - YDIFF, 60), LocalDate.of(2000, 2, 29)}, 149 {MinguoChronology.INSTANCE.dateYearDay(2400 - YDIFF, 60), LocalDate.of(2400, 2, 29)}, 150 151 {MinguoChronology.INSTANCE.dateYearDay(MinguoEra.ROC, 1916 - YDIFF, 60), LocalDate.of(1916, 2, 29)}, 152 {MinguoChronology.INSTANCE.dateYearDay(MinguoEra.BEFORE_ROC, 4, 60), LocalDate.of(1908, 2, 29)}, 153 {MinguoChronology.INSTANCE.dateYearDay(MinguoEra.ROC, 2000 - YDIFF, 60), LocalDate.of(2000, 2, 29)}, 154 {MinguoChronology.INSTANCE.dateYearDay(MinguoEra.ROC, 2400 - YDIFF, 60), LocalDate.of(2400, 2, 29)}, 155 156 {MinguoChronology.INSTANCE.date(MinguoEra.ROC, 1916 - YDIFF, 2, 29 ), LocalDate.of(1916, 2, 29)}, 157 {MinguoChronology.INSTANCE.date(MinguoEra.BEFORE_ROC, 4, 2, 29), LocalDate.of(1908, 2, 29)}, 158 {MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2000 - YDIFF, 2, 29), LocalDate.of(2000, 2, 29)}, 159 {MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2400 - YDIFF, 2, 29), LocalDate.of(2400, 2, 29)}, 160 }; 161 } 162 163 @Test(dataProvider="samples") test_toLocalDate(MinguoDate minguo, LocalDate iso)164 public void test_toLocalDate(MinguoDate minguo, LocalDate iso) { 165 assertEquals(LocalDate.from(minguo), iso); 166 } 167 168 @Test(dataProvider="samples") test_fromCalendrical(MinguoDate minguo, LocalDate iso)169 public void test_fromCalendrical(MinguoDate minguo, LocalDate iso) { 170 assertEquals(MinguoChronology.INSTANCE.date(iso), minguo); 171 assertEquals(MinguoDate.from(iso), minguo); 172 } 173 174 @Test(dataProvider="samples") test_isEqual(MinguoDate minguo, LocalDate iso)175 public void test_isEqual(MinguoDate minguo, LocalDate iso) { 176 assertTrue(minguo.isEqual(iso)); 177 } 178 179 @Test(dataProvider="samples") test_date_equals(MinguoDate minguo, LocalDate iso)180 public void test_date_equals(MinguoDate minguo, LocalDate iso) { 181 assertFalse(minguo.equals(iso)); 182 assertNotEquals(minguo.hashCode(), iso.hashCode()); 183 } 184 185 @Test test_dateNow()186 public void test_dateNow(){ 187 assertEquals(MinguoChronology.INSTANCE.dateNow(), MinguoDate.now()) ; 188 assertEquals(MinguoChronology.INSTANCE.dateNow(), MinguoDate.now(ZoneId.systemDefault())) ; 189 assertEquals(MinguoChronology.INSTANCE.dateNow(), MinguoDate.now(Clock.systemDefaultZone())) ; 190 assertEquals(MinguoChronology.INSTANCE.dateNow(), MinguoDate.now(Clock.systemDefaultZone().getZone())) ; 191 192 assertEquals(MinguoChronology.INSTANCE.dateNow(), MinguoChronology.INSTANCE.dateNow(ZoneId.systemDefault())) ; 193 assertEquals(MinguoChronology.INSTANCE.dateNow(), MinguoChronology.INSTANCE.dateNow(Clock.systemDefaultZone())) ; 194 assertEquals(MinguoChronology.INSTANCE.dateNow(), MinguoChronology.INSTANCE.dateNow(Clock.systemDefaultZone().getZone())) ; 195 196 ZoneId zoneId = ZoneId.of("Europe/Paris"); 197 assertEquals(MinguoChronology.INSTANCE.dateNow(zoneId), MinguoChronology.INSTANCE.dateNow(Clock.system(zoneId))) ; 198 assertEquals(MinguoChronology.INSTANCE.dateNow(zoneId), MinguoChronology.INSTANCE.dateNow(Clock.system(zoneId).getZone())) ; 199 assertEquals(MinguoChronology.INSTANCE.dateNow(zoneId), MinguoDate.now(Clock.system(zoneId))) ; 200 assertEquals(MinguoChronology.INSTANCE.dateNow(zoneId), MinguoDate.now(Clock.system(zoneId).getZone())) ; 201 202 assertEquals(MinguoChronology.INSTANCE.dateNow(ZoneId.of(ZoneOffset.UTC.getId())), MinguoChronology.INSTANCE.dateNow(Clock.systemUTC())) ; 203 } 204 205 @SuppressWarnings("unused") 206 @Test(dataProvider="samples") test_MinguoDate(MinguoDate minguoDate, LocalDate iso)207 public void test_MinguoDate(MinguoDate minguoDate, LocalDate iso) { 208 MinguoDate hd = minguoDate; 209 ChronoLocalDateTime<MinguoDate> hdt = hd.atTime(LocalTime.NOON); 210 ZoneOffset zo = ZoneOffset.ofHours(1); 211 ChronoZonedDateTime<MinguoDate> hzdt = hdt.atZone(zo); 212 hdt = hdt.plus(1, ChronoUnit.YEARS); 213 hdt = hdt.plus(1, ChronoUnit.MONTHS); 214 hdt = hdt.plus(1, ChronoUnit.DAYS); 215 hdt = hdt.plus(1, ChronoUnit.HOURS); 216 hdt = hdt.plus(1, ChronoUnit.MINUTES); 217 hdt = hdt.plus(1, ChronoUnit.SECONDS); 218 hdt = hdt.plus(1, ChronoUnit.NANOS); 219 ChronoLocalDateTime<MinguoDate> a2 = hzdt.toLocalDateTime(); 220 MinguoDate a3 = a2.toLocalDate(); 221 MinguoDate a5 = hzdt.toLocalDate(); 222 //System.out.printf(" d: %s, dt: %s; odt: %s; zodt: %s; a4: %s%n", date, hdt, hodt, hzdt, a5); 223 } 224 225 @Test() test_MinguoChrono()226 public void test_MinguoChrono() { 227 MinguoDate h1 = MinguoChronology.INSTANCE.date(MinguoEra.ROC, 1, 2, 3); 228 MinguoDate h2 = h1; 229 ChronoLocalDateTime<MinguoDate> h3 = h2.atTime(LocalTime.NOON); 230 @SuppressWarnings("unused") 231 ChronoZonedDateTime<MinguoDate> h4 = h3.atZone(ZoneOffset.UTC); 232 } 233 234 @DataProvider(name="badDates") data_badDates()235 Object[][] data_badDates() { 236 return new Object[][] { 237 {1912, 0, 0}, 238 239 {1912, -1, 1}, 240 {1912, 0, 1}, 241 {1912, 14, 1}, 242 {1912, 15, 1}, 243 244 {1912, 1, -1}, 245 {1912, 1, 0}, 246 {1912, 1, 32}, 247 {1912, 2, 29}, 248 {1912, 2, 30}, 249 250 {1912, 12, -1}, 251 {1912, 12, 0}, 252 {1912, 12, 32}, 253 254 {1907 - YDIFF, 2, 29}, 255 {100 - YDIFF, 2, 29}, 256 {2100 - YDIFF, 2, 29}, 257 {2101 - YDIFF, 2, 29}, 258 }; 259 } 260 261 @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class) test_badDates(int year, int month, int dom)262 public void test_badDates(int year, int month, int dom) { 263 MinguoChronology.INSTANCE.date(year, month, dom); 264 } 265 266 //----------------------------------------------------------------------- 267 // prolepticYear() and is LeapYear() 268 //----------------------------------------------------------------------- 269 @DataProvider(name="prolepticYear") data_prolepticYear()270 Object[][] data_prolepticYear() { 271 return new Object[][] { 272 {1, MinguoEra.ROC, 1912 - YDIFF, 1912 - YDIFF, true}, 273 {1, MinguoEra.ROC, 1916 - YDIFF, 1916 - YDIFF, true}, 274 {1, MinguoEra.ROC, 1914 - YDIFF, 1914 - YDIFF, false}, 275 {1, MinguoEra.ROC, 2000 - YDIFF, 2000 - YDIFF, true}, 276 {1, MinguoEra.ROC, 2100 - YDIFF, 2100 - YDIFF, false}, 277 {1, MinguoEra.ROC, 0, 0, false}, 278 {1, MinguoEra.ROC, 1908 - YDIFF, 1908 - YDIFF, true}, 279 {1, MinguoEra.ROC, 1900 - YDIFF, 1900 - YDIFF, false}, 280 {1, MinguoEra.ROC, 1600 - YDIFF, 1600 - YDIFF, true}, 281 282 {0, MinguoEra.BEFORE_ROC, YDIFF - 1911, 1912 - YDIFF, true}, 283 {0, MinguoEra.BEFORE_ROC, YDIFF - 1915, 1916 - YDIFF, true}, 284 {0, MinguoEra.BEFORE_ROC, YDIFF - 1913, 1914 - YDIFF, false}, 285 {0, MinguoEra.BEFORE_ROC, YDIFF - 1999, 2000 - YDIFF, true}, 286 {0, MinguoEra.BEFORE_ROC, YDIFF - 2099, 2100 - YDIFF, false}, 287 {0, MinguoEra.BEFORE_ROC, 1, 0, false}, 288 {0, MinguoEra.BEFORE_ROC, YDIFF - 1907, 1908 - YDIFF, true}, 289 {0, MinguoEra.BEFORE_ROC, YDIFF - 1899, 1900 - YDIFF, false}, 290 {0, MinguoEra.BEFORE_ROC, YDIFF - 1599, 1600 - YDIFF, true}, 291 292 }; 293 } 294 295 @Test(dataProvider="prolepticYear") test_prolepticYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear)296 public void test_prolepticYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { 297 Era eraObj = MinguoChronology.INSTANCE.eraOf(eraValue); 298 assertTrue(MinguoChronology.INSTANCE.eras().contains(eraObj)); 299 assertEquals(eraObj, era); 300 assertEquals(MinguoChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear); 301 } 302 303 @Test(dataProvider="prolepticYear") test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear)304 public void test_isLeapYear(int eraValue, Era era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) { 305 assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear); 306 assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap()); 307 308 MinguoDate minguo = MinguoDate.now(); 309 minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2); 310 if (isLeapYear) { 311 assertEquals(minguo.lengthOfMonth(), 29); 312 } else { 313 assertEquals(minguo.lengthOfMonth(), 28); 314 } 315 } 316 317 //----------------------------------------------------------------------- 318 // Bad Era for Chronology.date(era,...) and Chronology.prolepticYear(Era,...) 319 //----------------------------------------------------------------------- 320 @Test test_InvalidEras()321 public void test_InvalidEras() { 322 // Verify that the eras from every other Chronology are invalid 323 for (Chronology chrono : Chronology.getAvailableChronologies()) { 324 if (chrono instanceof MinguoChronology) { 325 continue; 326 } 327 List<Era> eras = chrono.eras(); 328 for (Era era : eras) { 329 try { 330 ChronoLocalDate date = MinguoChronology.INSTANCE.date(era, 1, 1, 1); 331 fail("MinguoChronology.date did not throw ClassCastException for Era: " + era); 332 } catch (ClassCastException cex) { 333 ; // ignore expected exception 334 } 335 336 /* Test for missing MinguoDate.of(Era, y, m, d) method. 337 try { 338 @SuppressWarnings("unused") 339 MinguoDate jdate = MinguoDate.of(era, 1, 1, 1); 340 fail("MinguoDate.of did not throw ClassCastException for Era: " + era); 341 } catch (ClassCastException cex) { 342 ; // ignore expected exception 343 } 344 */ 345 346 try { 347 @SuppressWarnings("unused") 348 int year = MinguoChronology.INSTANCE.prolepticYear(era, 1); 349 fail("MinguoChronology.prolepticYear did not throw ClassCastException for Era: " + era); 350 } catch (ClassCastException cex) { 351 ; // ignore expected exception 352 } 353 } 354 } 355 } 356 357 //----------------------------------------------------------------------- 358 // with(DateTimeAdjuster) 359 //----------------------------------------------------------------------- 360 @Test test_adjust1()361 public void test_adjust1() { 362 MinguoDate base = MinguoChronology.INSTANCE.date(2012, 10, 29); 363 MinguoDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 364 assertEquals(test, MinguoChronology.INSTANCE.date(2012, 10, 31)); 365 } 366 367 @Test test_adjust2()368 public void test_adjust2() { 369 MinguoDate base = MinguoChronology.INSTANCE.date(1728, 12, 2); 370 MinguoDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 371 assertEquals(test, MinguoChronology.INSTANCE.date(1728, 12, 31)); 372 } 373 374 //----------------------------------------------------------------------- 375 // MinguoDate.with(Local*) 376 //----------------------------------------------------------------------- 377 @Test test_adjust_toLocalDate()378 public void test_adjust_toLocalDate() { 379 MinguoDate minguo = MinguoChronology.INSTANCE.date(99, 1, 4); 380 MinguoDate test = minguo.with(LocalDate.of(2012, 7, 6)); 381 assertEquals(test, MinguoChronology.INSTANCE.date(101, 7, 6)); 382 } 383 384 @Test(expectedExceptions=DateTimeException.class) test_adjust_toMonth()385 public void test_adjust_toMonth() { 386 MinguoDate minguo = MinguoChronology.INSTANCE.date(1726, 1, 4); 387 minguo.with(Month.APRIL); 388 } 389 390 //----------------------------------------------------------------------- 391 // LocalDate.with(MinguoDate) 392 //----------------------------------------------------------------------- 393 @Test test_LocalDate_adjustToMinguoDate()394 public void test_LocalDate_adjustToMinguoDate() { 395 MinguoDate minguo = MinguoChronology.INSTANCE.date(101, 10, 29); 396 LocalDate test = LocalDate.MIN.with(minguo); 397 assertEquals(test, LocalDate.of(2012, 10, 29)); 398 } 399 400 @Test test_LocalDateTime_adjustToMinguoDate()401 public void test_LocalDateTime_adjustToMinguoDate() { 402 MinguoDate minguo = MinguoChronology.INSTANCE.date(101, 10, 29); 403 LocalDateTime test = LocalDateTime.MIN.with(minguo); 404 assertEquals(test, LocalDateTime.of(2012, 10, 29, 0, 0)); 405 } 406 407 //----------------------------------------------------------------------- 408 // localDateTime() 409 //----------------------------------------------------------------------- 410 @DataProvider(name="localDateTime") data_localDateTime()411 Object[][] data_localDateTime() { 412 return new Object[][] { 413 {LocalDateTime.of(2012, 2, 29, 2, 7), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29), LocalTime.of(2, 7), null}, 414 {ZonedDateTime.of(2012, 2, 29, 2, 7, 1, 1, ZONE_PARIS), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29), LocalTime.of(2, 7, 1, 1), null}, 415 {OffsetDateTime.of(2012, 2, 29, 2, 7, 1, 1, OFFSET_PTWO), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29), LocalTime.of(2, 7, 1, 1), null}, 416 417 {JapaneseDate.of(2012, 2, 29), null, null, DateTimeException.class}, 418 {ThaiBuddhistDate.of(2012 + 543, 2, 29), null, null, DateTimeException.class}, 419 {LocalDate.of(2012, 2, 29), null, null, DateTimeException.class}, 420 {LocalTime.of(20, 30, 29, 0), null, null, DateTimeException.class}, 421 }; 422 } 423 424 @Test(dataProvider="localDateTime") test_localDateTime(TemporalAccessor accessor, MinguoDate expectedDate, LocalTime expectedTime, Class<?> expectedEx)425 public void test_localDateTime(TemporalAccessor accessor, MinguoDate expectedDate, LocalTime expectedTime, Class<?> expectedEx) { 426 if (expectedEx == null) { 427 ChronoLocalDateTime<MinguoDate> result = MinguoChronology.INSTANCE.localDateTime(accessor); 428 assertEquals(result.toLocalDate(), expectedDate); 429 assertEquals(MinguoDate.from(accessor), expectedDate); 430 assertEquals(result.toLocalTime(), expectedTime); 431 } else { 432 try { 433 ChronoLocalDateTime<MinguoDate> result = MinguoChronology.INSTANCE.localDateTime(accessor); 434 fail(); 435 } catch (Exception ex) { 436 assertTrue(expectedEx.isInstance(ex)); 437 } 438 } 439 } 440 441 //----------------------------------------------------------------------- 442 // zonedDateTime(TemporalAccessor) 443 //----------------------------------------------------------------------- 444 @DataProvider(name="zonedDateTime") data_zonedDateTime()445 Object[][] data_zonedDateTime() { 446 return new Object[][] { 447 {ZonedDateTime.of(2012, 2, 29, 2, 7, 1, 1, ZONE_PARIS), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29), LocalTime.of(2, 7, 1, 1), null}, 448 {OffsetDateTime.of(2012, 2, 29, 2, 7, 1, 1, OFFSET_PTWO), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29), LocalTime.of(2, 7, 1, 1), null}, 449 450 {LocalDateTime.of(2012, 2, 29, 2, 7), null, null, DateTimeException.class}, 451 {JapaneseDate.of(2012, 2, 29), null, null, DateTimeException.class}, 452 {ThaiBuddhistDate.of(2012 + 543, 2, 29), null, null, DateTimeException.class}, 453 {LocalDate.of(2012, 2, 29), null, null, DateTimeException.class}, 454 {LocalTime.of(20, 30, 29, 0), null, null, DateTimeException.class}, 455 }; 456 } 457 458 @Test(dataProvider="zonedDateTime") test_zonedDateTime(TemporalAccessor accessor, MinguoDate expectedDate, LocalTime expectedTime, Class<?> expectedEx)459 public void test_zonedDateTime(TemporalAccessor accessor, MinguoDate expectedDate, LocalTime expectedTime, Class<?> expectedEx) { 460 if (expectedEx == null) { 461 ChronoZonedDateTime<MinguoDate> result = MinguoChronology.INSTANCE.zonedDateTime(accessor); 462 assertEquals(result.toLocalDate(), expectedDate); 463 assertEquals(MinguoDate.from(accessor), expectedDate); 464 assertEquals(result.toLocalTime(), expectedTime); 465 466 } else { 467 try { 468 ChronoZonedDateTime<MinguoDate> result = MinguoChronology.INSTANCE.zonedDateTime(accessor); 469 fail(); 470 } catch (Exception ex) { 471 assertTrue(expectedEx.isInstance(ex)); 472 } 473 } 474 } 475 476 //----------------------------------------------------------------------- 477 // zonedDateTime(Instant, ZoneId ) 478 //----------------------------------------------------------------------- 479 @Test test_Instant_zonedDateTime()480 public void test_Instant_zonedDateTime() { 481 OffsetDateTime offsetDateTime = OffsetDateTime.of(2012, 2, 29, 2, 7, 1, 1, OFFSET_PTWO); 482 ZonedDateTime zonedDateTime = ZonedDateTime.of(2012, 2, 29, 2, 7, 1, 1, ZONE_PARIS); 483 484 ChronoZonedDateTime<MinguoDate> result = MinguoChronology.INSTANCE.zonedDateTime(offsetDateTime.toInstant(), offsetDateTime.getOffset()); 485 assertEquals(result.toLocalDate(), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29)); 486 assertEquals(result.toLocalTime(), LocalTime.of(2, 7, 1, 1)); 487 488 result = MinguoChronology.INSTANCE.zonedDateTime(zonedDateTime.toInstant(), zonedDateTime.getOffset()); 489 assertEquals(result.toLocalDate(), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29)); 490 assertEquals(result.toLocalTime(), LocalTime.of(2, 7, 1, 1)); 491 } 492 493 //----------------------------------------------------------------------- 494 // PeriodUntil() 495 //----------------------------------------------------------------------- 496 @Test test_periodUntilDate()497 public void test_periodUntilDate() { 498 MinguoDate mdate1 = MinguoDate.of(1970, 1, 1); 499 MinguoDate mdate2 = MinguoDate.of(1971, 2, 2); 500 ChronoPeriod period = mdate1.until(mdate2); 501 assertEquals(period, MinguoChronology.INSTANCE.period(1, 1, 1)); 502 } 503 504 @Test test_periodUntilUnit()505 public void test_periodUntilUnit() { 506 MinguoDate mdate1 = MinguoDate.of(1970, 1, 1); 507 MinguoDate mdate2 = MinguoDate.of(1971, 2, 2); 508 long months = mdate1.until(mdate2, ChronoUnit.MONTHS); 509 assertEquals(months, 13); 510 } 511 512 @Test test_periodUntilDiffChrono()513 public void test_periodUntilDiffChrono() { 514 MinguoDate mdate1 = MinguoDate.of(1970, 1, 1); 515 MinguoDate mdate2 = MinguoDate.of(1971, 2, 2); 516 ThaiBuddhistDate ldate2 = ThaiBuddhistChronology.INSTANCE.date(mdate2); 517 ChronoPeriod period = mdate1.until(ldate2); 518 assertEquals(period, MinguoChronology.INSTANCE.period(1, 1, 1)); 519 } 520 521 //----------------------------------------------------------------------- 522 // toString() 523 //----------------------------------------------------------------------- 524 @DataProvider(name="toString") data_toString()525 Object[][] data_toString() { 526 return new Object[][] { 527 {MinguoChronology.INSTANCE.date(1, 1, 1), "Minguo ROC 1-01-01"}, 528 {MinguoChronology.INSTANCE.date(1728, 10, 28), "Minguo ROC 1728-10-28"}, 529 {MinguoChronology.INSTANCE.date(1728, 10, 29), "Minguo ROC 1728-10-29"}, 530 {MinguoChronology.INSTANCE.date(1727, 12, 5), "Minguo ROC 1727-12-05"}, 531 {MinguoChronology.INSTANCE.date(1727, 12, 6), "Minguo ROC 1727-12-06"}, 532 }; 533 } 534 535 @Test(dataProvider="toString") test_toString(MinguoDate minguo, String expected)536 public void test_toString(MinguoDate minguo, String expected) { 537 assertEquals(minguo.toString(), expected); 538 } 539 540 //----------------------------------------------------------------------- 541 // equals() 542 //----------------------------------------------------------------------- 543 @Test test_equals_true()544 public void test_equals_true() { 545 assertTrue(MinguoChronology.INSTANCE.equals(MinguoChronology.INSTANCE)); 546 } 547 548 @Test test_equals_false()549 public void test_equals_false() { 550 assertFalse(MinguoChronology.INSTANCE.equals(IsoChronology.INSTANCE)); 551 } 552 553 //----------------------------------------------------------------------- 554 //----------------------------------------------------------------------- 555 @DataProvider(name = "resolve_yearOfEra") data_resolve_yearOfEra()556 Object[][] data_resolve_yearOfEra() { 557 return new Object[][] { 558 // era only 559 {ResolverStyle.STRICT, -1, null, null, null, null}, 560 {ResolverStyle.SMART, -1, null, null, null, null}, 561 {ResolverStyle.LENIENT, -1, null, null, null, null}, 562 563 {ResolverStyle.STRICT, 0, null, null, ChronoField.ERA, 0}, 564 {ResolverStyle.SMART, 0, null, null, ChronoField.ERA, 0}, 565 {ResolverStyle.LENIENT, 0, null, null, ChronoField.ERA, 0}, 566 567 {ResolverStyle.STRICT, 1, null, null, ChronoField.ERA, 1}, 568 {ResolverStyle.SMART, 1, null, null, ChronoField.ERA, 1}, 569 {ResolverStyle.LENIENT, 1, null, null, ChronoField.ERA, 1}, 570 571 {ResolverStyle.STRICT, 2, null, null, null, null}, 572 {ResolverStyle.SMART, 2, null, null, null, null}, 573 {ResolverStyle.LENIENT, 2, null, null, null, null}, 574 575 // era and year-of-era 576 {ResolverStyle.STRICT, -1, 2012, null, null, null}, 577 {ResolverStyle.SMART, -1, 2012, null, null, null}, 578 {ResolverStyle.LENIENT, -1, 2012, null, null, null}, 579 580 {ResolverStyle.STRICT, 0, 2012, null, ChronoField.YEAR, -2011}, 581 {ResolverStyle.SMART, 0, 2012, null, ChronoField.YEAR, -2011}, 582 {ResolverStyle.LENIENT, 0, 2012, null, ChronoField.YEAR, -2011}, 583 584 {ResolverStyle.STRICT, 1, 2012, null, ChronoField.YEAR, 2012}, 585 {ResolverStyle.SMART, 1, 2012, null, ChronoField.YEAR, 2012}, 586 {ResolverStyle.LENIENT, 1, 2012, null, ChronoField.YEAR, 2012}, 587 588 {ResolverStyle.STRICT, 2, 2012, null, null, null}, 589 {ResolverStyle.SMART, 2, 2012, null, null, null}, 590 {ResolverStyle.LENIENT, 2, 2012, null, null, null}, 591 592 // year-of-era only 593 {ResolverStyle.STRICT, null, 2012, null, ChronoField.YEAR_OF_ERA, 2012}, 594 {ResolverStyle.SMART, null, 2012, null, ChronoField.YEAR, 2012}, 595 {ResolverStyle.LENIENT, null, 2012, null, ChronoField.YEAR, 2012}, 596 597 {ResolverStyle.STRICT, null, Integer.MAX_VALUE, null, null, null}, 598 {ResolverStyle.SMART, null, Integer.MAX_VALUE, null, null, null}, 599 {ResolverStyle.LENIENT, null, Integer.MAX_VALUE, null, ChronoField.YEAR, Integer.MAX_VALUE}, 600 601 // year-of-era and year 602 {ResolverStyle.STRICT, null, 2012, 2012, ChronoField.YEAR, 2012}, 603 {ResolverStyle.SMART, null, 2012, 2012, ChronoField.YEAR, 2012}, 604 {ResolverStyle.LENIENT, null, 2012, 2012, ChronoField.YEAR, 2012}, 605 606 {ResolverStyle.STRICT, null, 2012, -2011, ChronoField.YEAR, -2011}, 607 {ResolverStyle.SMART, null, 2012, -2011, ChronoField.YEAR, -2011}, 608 {ResolverStyle.LENIENT, null, 2012, -2011, ChronoField.YEAR, -2011}, 609 610 {ResolverStyle.STRICT, null, 2012, 2013, null, null}, 611 {ResolverStyle.SMART, null, 2012, 2013, null, null}, 612 {ResolverStyle.LENIENT, null, 2012, 2013, null, null}, 613 614 {ResolverStyle.STRICT, null, 2012, -2013, null, null}, 615 {ResolverStyle.SMART, null, 2012, -2013, null, null}, 616 {ResolverStyle.LENIENT, null, 2012, -2013, null, null}, 617 }; 618 } 619 620 @Test(dataProvider = "resolve_yearOfEra") test_resolve_yearOfEra(ResolverStyle style, Integer e, Integer yoe, Integer y, ChronoField field, Integer expected)621 public void test_resolve_yearOfEra(ResolverStyle style, Integer e, Integer yoe, Integer y, ChronoField field, Integer expected) { 622 Map<TemporalField, Long> fieldValues = new HashMap<>(); 623 if (e != null) { 624 fieldValues.put(ChronoField.ERA, (long) e); 625 } 626 if (yoe != null) { 627 fieldValues.put(ChronoField.YEAR_OF_ERA, (long) yoe); 628 } 629 if (y != null) { 630 fieldValues.put(ChronoField.YEAR, (long) y); 631 } 632 if (field != null) { 633 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, style); 634 assertEquals(date, null); 635 assertEquals(fieldValues.get(field), (Long) expected.longValue()); 636 assertEquals(fieldValues.size(), 1); 637 } else { 638 try { 639 MinguoChronology.INSTANCE.resolveDate(fieldValues, style); 640 fail("Should have failed"); 641 } catch (DateTimeException ex) { 642 // expected 643 } 644 } 645 } 646 647 //----------------------------------------------------------------------- 648 //----------------------------------------------------------------------- 649 @DataProvider(name = "resolve_ymd") data_resolve_ymd()650 Object[][] data_resolve_ymd() { 651 return new Object[][] { 652 {2012 - YDIFF, 1, -365, date(2010 - YDIFF, 12, 31), false, false}, 653 {2012 - YDIFF, 1, -364, date(2011 - YDIFF, 1, 1), false, false}, 654 {2012 - YDIFF, 1, -31, date(2011 - YDIFF, 11, 30), false, false}, 655 {2012 - YDIFF, 1, -30, date(2011 - YDIFF, 12, 1), false, false}, 656 {2012 - YDIFF, 1, -12, date(2011 - YDIFF, 12, 19), false, false}, 657 {2012 - YDIFF, 1, 1, date(2012 - YDIFF, 1, 1), true, true}, 658 {2012 - YDIFF, 1, 27, date(2012 - YDIFF, 1, 27), true, true}, 659 {2012 - YDIFF, 1, 28, date(2012 - YDIFF, 1, 28), true, true}, 660 {2012 - YDIFF, 1, 29, date(2012 - YDIFF, 1, 29), true, true}, 661 {2012 - YDIFF, 1, 30, date(2012 - YDIFF, 1, 30), true, true}, 662 {2012 - YDIFF, 1, 31, date(2012 - YDIFF, 1, 31), true, true}, 663 {2012 - YDIFF, 1, 59, date(2012 - YDIFF, 2, 28), false, false}, 664 {2012 - YDIFF, 1, 60, date(2012 - YDIFF, 2, 29), false, false}, 665 {2012 - YDIFF, 1, 61, date(2012 - YDIFF, 3, 1), false, false}, 666 {2012 - YDIFF, 1, 365, date(2012 - YDIFF, 12, 30), false, false}, 667 {2012 - YDIFF, 1, 366, date(2012 - YDIFF, 12, 31), false, false}, 668 {2012 - YDIFF, 1, 367, date(2013 - YDIFF, 1, 1), false, false}, 669 {2012 - YDIFF, 1, 367 + 364, date(2013 - YDIFF, 12, 31), false, false}, 670 {2012 - YDIFF, 1, 367 + 365, date(2014 - YDIFF, 1, 1), false, false}, 671 672 {2012 - YDIFF, 2, 1, date(2012 - YDIFF, 2, 1), true, true}, 673 {2012 - YDIFF, 2, 28, date(2012 - YDIFF, 2, 28), true, true}, 674 {2012 - YDIFF, 2, 29, date(2012 - YDIFF, 2, 29), true, true}, 675 {2012 - YDIFF, 2, 30, date(2012 - YDIFF, 3, 1), date(2012 - YDIFF, 2, 29), false}, 676 {2012 - YDIFF, 2, 31, date(2012 - YDIFF, 3, 2), date(2012 - YDIFF, 2, 29), false}, 677 {2012 - YDIFF, 2, 32, date(2012 - YDIFF, 3, 3), false, false}, 678 679 {2012 - YDIFF, -12, 1, date(2010 - YDIFF, 12, 1), false, false}, 680 {2012 - YDIFF, -11, 1, date(2011 - YDIFF, 1, 1), false, false}, 681 {2012 - YDIFF, -1, 1, date(2011 - YDIFF, 11, 1), false, false}, 682 {2012 - YDIFF, 0, 1, date(2011 - YDIFF, 12, 1), false, false}, 683 {2012 - YDIFF, 1, 1, date(2012 - YDIFF, 1, 1), true, true}, 684 {2012 - YDIFF, 12, 1, date(2012 - YDIFF, 12, 1), true, true}, 685 {2012 - YDIFF, 13, 1, date(2013 - YDIFF, 1, 1), false, false}, 686 {2012 - YDIFF, 24, 1, date(2013 - YDIFF, 12, 1), false, false}, 687 {2012 - YDIFF, 25, 1, date(2014 - YDIFF, 1, 1), false, false}, 688 689 {2012 - YDIFF, 6, -31, date(2012 - YDIFF, 4, 30), false, false}, 690 {2012 - YDIFF, 6, -30, date(2012 - YDIFF, 5, 1), false, false}, 691 {2012 - YDIFF, 6, -1, date(2012 - YDIFF, 5, 30), false, false}, 692 {2012 - YDIFF, 6, 0, date(2012 - YDIFF, 5, 31), false, false}, 693 {2012 - YDIFF, 6, 1, date(2012 - YDIFF, 6, 1), true, true}, 694 {2012 - YDIFF, 6, 30, date(2012 - YDIFF, 6, 30), true, true}, 695 {2012 - YDIFF, 6, 31, date(2012 - YDIFF, 7, 1), date(2012 - YDIFF, 6, 30), false}, 696 {2012 - YDIFF, 6, 61, date(2012 - YDIFF, 7, 31), false, false}, 697 {2012 - YDIFF, 6, 62, date(2012 - YDIFF, 8, 1), false, false}, 698 699 {2011 - YDIFF, 2, 1, date(2011 - YDIFF, 2, 1), true, true}, 700 {2011 - YDIFF, 2, 28, date(2011 - YDIFF, 2, 28), true, true}, 701 {2011 - YDIFF, 2, 29, date(2011 - YDIFF, 3, 1), date(2011 - YDIFF, 2, 28), false}, 702 {2011 - YDIFF, 2, 30, date(2011 - YDIFF, 3, 2), date(2011 - YDIFF, 2, 28), false}, 703 {2011 - YDIFF, 2, 31, date(2011 - YDIFF, 3, 3), date(2011 - YDIFF, 2, 28), false}, 704 {2011 - YDIFF, 2, 32, date(2011 - YDIFF, 3, 4), false, false}, 705 }; 706 } 707 708 @Test(dataProvider = "resolve_ymd") test_resolve_ymd_lenient(int y, int m, int d, MinguoDate expected, Object smart, boolean strict)709 public void test_resolve_ymd_lenient(int y, int m, int d, MinguoDate expected, Object smart, boolean strict) { 710 Map<TemporalField, Long> fieldValues = new HashMap<>(); 711 fieldValues.put(ChronoField.YEAR, (long) y); 712 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 713 fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); 714 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); 715 assertEquals(date, expected); 716 assertEquals(fieldValues.size(), 0); 717 } 718 719 @Test(dataProvider = "resolve_ymd") test_resolve_ymd_smart(int y, int m, int d, MinguoDate expected, Object smart, boolean strict)720 public void test_resolve_ymd_smart(int y, int m, int d, MinguoDate expected, Object smart, boolean strict) { 721 Map<TemporalField, Long> fieldValues = new HashMap<>(); 722 fieldValues.put(ChronoField.YEAR, (long) y); 723 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 724 fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); 725 if (Boolean.TRUE.equals(smart)) { 726 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 727 assertEquals(date, expected); 728 assertEquals(fieldValues.size(), 0); 729 } else if (smart instanceof MinguoDate) { 730 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 731 assertEquals(date, smart); 732 } else { 733 try { 734 MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 735 fail("Should have failed"); 736 } catch (DateTimeException ex) { 737 // expected 738 } 739 } 740 } 741 742 @Test(dataProvider = "resolve_ymd") test_resolve_ymd_strict(int y, int m, int d, MinguoDate expected, Object smart, boolean strict)743 public void test_resolve_ymd_strict(int y, int m, int d, MinguoDate expected, Object smart, boolean strict) { 744 Map<TemporalField, Long> fieldValues = new HashMap<>(); 745 fieldValues.put(ChronoField.YEAR, (long) y); 746 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 747 fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); 748 if (strict) { 749 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 750 assertEquals(date, expected); 751 assertEquals(fieldValues.size(), 0); 752 } else { 753 try { 754 MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 755 fail("Should have failed"); 756 } catch (DateTimeException ex) { 757 // expected 758 } 759 } 760 } 761 762 //----------------------------------------------------------------------- 763 //----------------------------------------------------------------------- 764 @DataProvider(name = "resolve_yd") data_resolve_yd()765 Object[][] data_resolve_yd() { 766 return new Object[][] { 767 {2012 - YDIFF, -365, date(2010 - YDIFF, 12, 31), false, false}, 768 {2012 - YDIFF, -364, date(2011 - YDIFF, 1, 1), false, false}, 769 {2012 - YDIFF, -31, date(2011 - YDIFF, 11, 30), false, false}, 770 {2012 - YDIFF, -30, date(2011 - YDIFF, 12, 1), false, false}, 771 {2012 - YDIFF, -12, date(2011 - YDIFF, 12, 19), false, false}, 772 {2012 - YDIFF, -1, date(2011 - YDIFF, 12, 30), false, false}, 773 {2012 - YDIFF, 0, date(2011 - YDIFF, 12, 31), false, false}, 774 {2012 - YDIFF, 1, date(2012 - YDIFF, 1, 1), true, true}, 775 {2012 - YDIFF, 2, date(2012 - YDIFF, 1, 2), true, true}, 776 {2012 - YDIFF, 31, date(2012 - YDIFF, 1, 31), true, true}, 777 {2012 - YDIFF, 32, date(2012 - YDIFF, 2, 1), true, true}, 778 {2012 - YDIFF, 59, date(2012 - YDIFF, 2, 28), true, true}, 779 {2012 - YDIFF, 60, date(2012 - YDIFF, 2, 29), true, true}, 780 {2012 - YDIFF, 61, date(2012 - YDIFF, 3, 1), true, true}, 781 {2012 - YDIFF, 365, date(2012 - YDIFF, 12, 30), true, true}, 782 {2012 - YDIFF, 366, date(2012 - YDIFF, 12, 31), true, true}, 783 {2012 - YDIFF, 367, date(2013 - YDIFF, 1, 1), false, false}, 784 {2012 - YDIFF, 367 + 364, date(2013 - YDIFF, 12, 31), false, false}, 785 {2012 - YDIFF, 367 + 365, date(2014 - YDIFF, 1, 1), false, false}, 786 787 {2011 - YDIFF, 59, date(2011 - YDIFF, 2, 28), true, true}, 788 {2011 - YDIFF, 60, date(2011 - YDIFF, 3, 1), true, true}, 789 }; 790 } 791 792 @Test(dataProvider = "resolve_yd") test_resolve_yd_lenient(int y, int d, MinguoDate expected, boolean smart, boolean strict)793 public void test_resolve_yd_lenient(int y, int d, MinguoDate expected, boolean smart, boolean strict) { 794 Map<TemporalField, Long> fieldValues = new HashMap<>(); 795 fieldValues.put(ChronoField.YEAR, (long) y); 796 fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); 797 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); 798 assertEquals(date, expected); 799 assertEquals(fieldValues.size(), 0); 800 } 801 802 @Test(dataProvider = "resolve_yd") test_resolve_yd_smart(int y, int d, MinguoDate expected, boolean smart, boolean strict)803 public void test_resolve_yd_smart(int y, int d, MinguoDate expected, boolean smart, boolean strict) { 804 Map<TemporalField, Long> fieldValues = new HashMap<>(); 805 fieldValues.put(ChronoField.YEAR, (long) y); 806 fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); 807 if (smart) { 808 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 809 assertEquals(date, expected); 810 assertEquals(fieldValues.size(), 0); 811 } else { 812 try { 813 MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 814 fail("Should have failed"); 815 } catch (DateTimeException ex) { 816 // expected 817 } 818 } 819 } 820 821 @Test(dataProvider = "resolve_yd") test_resolve_yd_strict(int y, int d, MinguoDate expected, boolean smart, boolean strict)822 public void test_resolve_yd_strict(int y, int d, MinguoDate expected, boolean smart, boolean strict) { 823 Map<TemporalField, Long> fieldValues = new HashMap<>(); 824 fieldValues.put(ChronoField.YEAR, (long) y); 825 fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); 826 if (strict) { 827 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 828 assertEquals(date, expected); 829 assertEquals(fieldValues.size(), 0); 830 } else { 831 try { 832 MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 833 fail("Should have failed"); 834 } catch (DateTimeException ex) { 835 // expected 836 } 837 } 838 } 839 840 //----------------------------------------------------------------------- 841 //----------------------------------------------------------------------- 842 @DataProvider(name = "resolve_ymaa") data_resolve_ymaa()843 Object[][] data_resolve_ymaa() { 844 return new Object[][] { 845 {2012 - YDIFF, 1, 1, -365, date(2010 - YDIFF, 12, 31), false, false}, 846 {2012 - YDIFF, 1, 1, -364, date(2011 - YDIFF, 1, 1), false, false}, 847 {2012 - YDIFF, 1, 1, -31, date(2011 - YDIFF, 11, 30), false, false}, 848 {2012 - YDIFF, 1, 1, -30, date(2011 - YDIFF, 12, 1), false, false}, 849 {2012 - YDIFF, 1, 1, -12, date(2011 - YDIFF, 12, 19), false, false}, 850 {2012 - YDIFF, 1, 1, 1, date(2012 - YDIFF, 1, 1), true, true}, 851 {2012 - YDIFF, 1, 1, 59, date(2012 - YDIFF, 2, 28), false, false}, 852 {2012 - YDIFF, 1, 1, 60, date(2012 - YDIFF, 2, 29), false, false}, 853 {2012 - YDIFF, 1, 1, 61, date(2012 - YDIFF, 3, 1), false, false}, 854 {2012 - YDIFF, 1, 1, 365, date(2012 - YDIFF, 12, 30), false, false}, 855 {2012 - YDIFF, 1, 1, 366, date(2012 - YDIFF, 12, 31), false, false}, 856 {2012 - YDIFF, 1, 1, 367, date(2013 - YDIFF, 1, 1), false, false}, 857 {2012 - YDIFF, 1, 1, 367 + 364, date(2013 - YDIFF, 12, 31), false, false}, 858 {2012 - YDIFF, 1, 1, 367 + 365, date(2014 - YDIFF, 1, 1), false, false}, 859 860 {2012 - YDIFF, 2, 0, 1, date(2012 - YDIFF, 1, 25), false, false}, 861 {2012 - YDIFF, 2, 0, 7, date(2012 - YDIFF, 1, 31), false, false}, 862 {2012 - YDIFF, 2, 1, 1, date(2012 - YDIFF, 2, 1), true, true}, 863 {2012 - YDIFF, 2, 1, 7, date(2012 - YDIFF, 2, 7), true, true}, 864 {2012 - YDIFF, 2, 2, 1, date(2012 - YDIFF, 2, 8), true, true}, 865 {2012 - YDIFF, 2, 2, 7, date(2012 - YDIFF, 2, 14), true, true}, 866 {2012 - YDIFF, 2, 3, 1, date(2012 - YDIFF, 2, 15), true, true}, 867 {2012 - YDIFF, 2, 3, 7, date(2012 - YDIFF, 2, 21), true, true}, 868 {2012 - YDIFF, 2, 4, 1, date(2012 - YDIFF, 2, 22), true, true}, 869 {2012 - YDIFF, 2, 4, 7, date(2012 - YDIFF, 2, 28), true, true}, 870 {2012 - YDIFF, 2, 5, 1, date(2012 - YDIFF, 2, 29), true, true}, 871 {2012 - YDIFF, 2, 5, 2, date(2012 - YDIFF, 3, 1), true, false}, 872 {2012 - YDIFF, 2, 5, 7, date(2012 - YDIFF, 3, 6), true, false}, 873 {2012 - YDIFF, 2, 6, 1, date(2012 - YDIFF, 3, 7), false, false}, 874 {2012 - YDIFF, 2, 6, 7, date(2012 - YDIFF, 3, 13), false, false}, 875 876 {2012 - YDIFF, 12, 1, 1, date(2012 - YDIFF, 12, 1), true, true}, 877 {2012 - YDIFF, 12, 5, 1, date(2012 - YDIFF, 12, 29), true, true}, 878 {2012 - YDIFF, 12, 5, 2, date(2012 - YDIFF, 12, 30), true, true}, 879 {2012 - YDIFF, 12, 5, 3, date(2012 - YDIFF, 12, 31), true, true}, 880 {2012 - YDIFF, 12, 5, 4, date(2013 - YDIFF, 1, 1), true, false}, 881 {2012 - YDIFF, 12, 5, 7, date(2013 - YDIFF, 1, 4), true, false}, 882 883 {2012 - YDIFF, -12, 1, 1, date(2010 - YDIFF, 12, 1), false, false}, 884 {2012 - YDIFF, -11, 1, 1, date(2011 - YDIFF, 1, 1), false, false}, 885 {2012 - YDIFF, -1, 1, 1, date(2011 - YDIFF, 11, 1), false, false}, 886 {2012 - YDIFF, 0, 1, 1, date(2011 - YDIFF, 12, 1), false, false}, 887 {2012 - YDIFF, 1, 1, 1, date(2012 - YDIFF, 1, 1), true, true}, 888 {2012 - YDIFF, 12, 1, 1, date(2012 - YDIFF, 12, 1), true, true}, 889 {2012 - YDIFF, 13, 1, 1, date(2013 - YDIFF, 1, 1), false, false}, 890 {2012 - YDIFF, 24, 1, 1, date(2013 - YDIFF, 12, 1), false, false}, 891 {2012 - YDIFF, 25, 1, 1, date(2014 - YDIFF, 1, 1), false, false}, 892 893 {2011 - YDIFF, 2, 1, 1, date(2011 - YDIFF, 2, 1), true, true}, 894 {2011 - YDIFF, 2, 4, 7, date(2011 - YDIFF, 2, 28), true, true}, 895 {2011 - YDIFF, 2, 5, 1, date(2011 - YDIFF, 3, 1), true, false}, 896 }; 897 } 898 899 @Test(dataProvider = "resolve_ymaa") test_resolve_ymaa_lenient(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict)900 public void test_resolve_ymaa_lenient(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict) { 901 Map<TemporalField, Long> fieldValues = new HashMap<>(); 902 fieldValues.put(ChronoField.YEAR, (long) y); 903 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 904 fieldValues.put(ChronoField.ALIGNED_WEEK_OF_MONTH, (long) w); 905 fieldValues.put(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, (long) d); 906 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); 907 assertEquals(date, expected); 908 assertEquals(fieldValues.size(), 0); 909 } 910 911 @Test(dataProvider = "resolve_ymaa") test_resolve_ymaa_smart(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict)912 public void test_resolve_ymaa_smart(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict) { 913 Map<TemporalField, Long> fieldValues = new HashMap<>(); 914 fieldValues.put(ChronoField.YEAR, (long) y); 915 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 916 fieldValues.put(ChronoField.ALIGNED_WEEK_OF_MONTH, (long) w); 917 fieldValues.put(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, (long) d); 918 if (smart) { 919 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 920 assertEquals(date, expected); 921 assertEquals(fieldValues.size(), 0); 922 } else { 923 try { 924 MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); 925 fail("Should have failed"); 926 } catch (DateTimeException ex) { 927 // expected 928 } 929 } 930 } 931 932 @Test(dataProvider = "resolve_ymaa") test_resolve_ymaa_strict(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict)933 public void test_resolve_ymaa_strict(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict) { 934 Map<TemporalField, Long> fieldValues = new HashMap<>(); 935 fieldValues.put(ChronoField.YEAR, (long) y); 936 fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); 937 fieldValues.put(ChronoField.ALIGNED_WEEK_OF_MONTH, (long) w); 938 fieldValues.put(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, (long) d); 939 if (strict) { 940 MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 941 assertEquals(date, expected); 942 assertEquals(fieldValues.size(), 0); 943 } else { 944 try { 945 MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); 946 fail("Should have failed"); 947 } catch (DateTimeException ex) { 948 // expected 949 } 950 } 951 } 952 953 //----------------------------------------------------------------------- date(int y, int m, int d)954 private static MinguoDate date(int y, int m, int d) { 955 return MinguoDate.of(y, m, d); 956 } 957 958 } 959