1 /* 2 * Copyright (c) 2012, 2020, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * This file is available under and governed by the GNU General Public 26 * License version 2 only, as published by the Free Software Foundation. 27 * However, the following notice accompanied the original version of this 28 * file: 29 * 30 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 31 * 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions are met: 36 * 37 * * Redistributions of source code must retain the above copyright notice, 38 * this list of conditions and the following disclaimer. 39 * 40 * * Redistributions in binary form must reproduce the above copyright notice, 41 * this list of conditions and the following disclaimer in the documentation 42 * and/or other materials provided with the distribution. 43 * 44 * * Neither the name of JSR-310 nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package tck.java.time; 61 62 import static java.time.temporal.ChronoField.ERA; 63 import static java.time.temporal.ChronoField.YEAR; 64 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 65 import static java.time.temporal.ChronoUnit.CENTURIES; 66 import static java.time.temporal.ChronoUnit.DAYS; 67 import static java.time.temporal.ChronoUnit.DECADES; 68 import static java.time.temporal.ChronoUnit.MILLENNIA; 69 import static java.time.temporal.ChronoUnit.MONTHS; 70 import static java.time.temporal.ChronoUnit.YEARS; 71 import static org.testng.Assert.assertEquals; 72 import static org.testng.Assert.assertTrue; 73 import static org.testng.Assert.fail; 74 75 import libcore.test.annotation.NonCts; 76 import libcore.test.reasons.NonCtsReasons; 77 78 import java.io.ByteArrayOutputStream; 79 import java.io.DataOutputStream; 80 import java.time.Clock; 81 import java.time.DateTimeException; 82 import java.time.Duration; 83 import java.time.Instant; 84 import java.time.LocalDate; 85 import java.time.LocalTime; 86 import java.time.Month; 87 import java.time.MonthDay; 88 import java.time.OffsetDateTime; 89 import java.time.Period; 90 import java.time.Year; 91 import java.time.YearMonth; 92 import java.time.ZoneId; 93 import java.time.ZoneOffset; 94 import java.time.chrono.IsoChronology; 95 import java.time.chrono.IsoEra; 96 import java.time.format.DateTimeFormatter; 97 import java.time.format.DateTimeParseException; 98 import java.time.temporal.ChronoField; 99 import java.time.temporal.ChronoUnit; 100 import java.time.temporal.JulianFields; 101 import java.time.temporal.Temporal; 102 import java.time.temporal.TemporalAccessor; 103 import java.time.temporal.TemporalAmount; 104 import java.time.temporal.TemporalField; 105 import java.time.temporal.TemporalQueries; 106 import java.time.temporal.TemporalQuery; 107 import java.time.temporal.TemporalUnit; 108 import java.time.temporal.UnsupportedTemporalTypeException; 109 import java.util.ArrayList; 110 import java.util.Arrays; 111 import java.util.List; 112 113 import org.testng.annotations.BeforeMethod; 114 import org.testng.annotations.DataProvider; 115 import org.testng.annotations.Test; 116 117 /** 118 * Test Year. 119 */ 120 @Test 121 public class TCKYear extends AbstractDateTimeTest { 122 123 private static final Year TEST_2008 = Year.of(2008); 124 125 @BeforeMethod setUp()126 public void setUp() { 127 } 128 129 //----------------------------------------------------------------------- 130 @Override samples()131 protected List<TemporalAccessor> samples() { 132 TemporalAccessor[] array = {TEST_2008, }; 133 return Arrays.asList(array); 134 } 135 136 @Override validFields()137 protected List<TemporalField> validFields() { 138 TemporalField[] array = { 139 YEAR_OF_ERA, 140 YEAR, 141 ERA, 142 }; 143 return Arrays.asList(array); 144 } 145 146 @Override invalidFields()147 protected List<TemporalField> invalidFields() { 148 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 149 list.removeAll(validFields()); 150 list.add(JulianFields.JULIAN_DAY); 151 list.add(JulianFields.MODIFIED_JULIAN_DAY); 152 list.add(JulianFields.RATA_DIE); 153 return list; 154 } 155 156 //----------------------------------------------------------------------- 157 // now() 158 //----------------------------------------------------------------------- 159 @Test now()160 public void now() { 161 Year expected = Year.now(Clock.systemDefaultZone()); 162 Year test = Year.now(); 163 for (int i = 0; i < 100; i++) { 164 if (expected.equals(test)) { 165 return; 166 } 167 expected = Year.now(Clock.systemDefaultZone()); 168 test = Year.now(); 169 } 170 assertEquals(test, expected); 171 } 172 173 //----------------------------------------------------------------------- 174 // now(ZoneId) 175 //----------------------------------------------------------------------- 176 @Test(expectedExceptions=NullPointerException.class) now_ZoneId_nullZoneId()177 public void now_ZoneId_nullZoneId() { 178 Year.now((ZoneId) null); 179 } 180 181 @Test now_ZoneId()182 public void now_ZoneId() { 183 ZoneId zone = ZoneId.of("UTC+01:02:03"); 184 Year expected = Year.now(Clock.system(zone)); 185 Year test = Year.now(zone); 186 for (int i = 0; i < 100; i++) { 187 if (expected.equals(test)) { 188 return; 189 } 190 expected = Year.now(Clock.system(zone)); 191 test = Year.now(zone); 192 } 193 assertEquals(test, expected); 194 } 195 196 //----------------------------------------------------------------------- 197 // now(Clock) 198 //----------------------------------------------------------------------- 199 @Test now_Clock()200 public void now_Clock() { 201 Instant instant = OffsetDateTime.of(LocalDate.of(2010, 12, 31), LocalTime.of(0, 0), ZoneOffset.UTC).toInstant(); 202 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 203 Year test = Year.now(clock); 204 assertEquals(test.getValue(), 2010); 205 } 206 207 @Test(expectedExceptions=NullPointerException.class) now_Clock_nullClock()208 public void now_Clock_nullClock() { 209 Year.now((Clock) null); 210 } 211 212 //----------------------------------------------------------------------- 213 @Test test_factory_int_singleton()214 public void test_factory_int_singleton() { 215 for (int i = -4; i <= 2104; i++) { 216 Year test = Year.of(i); 217 assertEquals(test.getValue(), i); 218 assertEquals(Year.of(i), test); 219 } 220 } 221 222 @Test(expectedExceptions=DateTimeException.class) test_factory_int_tooLow()223 public void test_factory_int_tooLow() { 224 Year.of(Year.MIN_VALUE - 1); 225 } 226 227 @Test(expectedExceptions=DateTimeException.class) test_factory_int_tooHigh()228 public void test_factory_int_tooHigh() { 229 Year.of(Year.MAX_VALUE + 1); 230 } 231 232 //----------------------------------------------------------------------- 233 @Test test_from_TemporalAccessor()234 public void test_from_TemporalAccessor() { 235 assertEquals(Year.from(LocalDate.of(2007, 7, 15)), Year.of(2007)); 236 } 237 238 @Test(expectedExceptions=DateTimeException.class) test_from_TemporalAccessor_invalid_noDerive()239 public void test_from_TemporalAccessor_invalid_noDerive() { 240 Year.from(LocalTime.of(12, 30)); 241 } 242 243 @Test(expectedExceptions=NullPointerException.class) test_from_TemporalAccessor_null()244 public void test_from_TemporalAccessor_null() { 245 Year.from((TemporalAccessor) null); 246 } 247 248 //----------------------------------------------------------------------- 249 // parse() 250 //----------------------------------------------------------------------- 251 @DataProvider(name="goodParseData") provider_goodParseData()252 Object[][] provider_goodParseData() { 253 return new Object[][] { 254 {"9999", Year.of(9999)}, 255 {"2000", Year.of(2000)}, 256 257 {"0", Year.of(0)}, 258 {"00", Year.of(0)}, 259 {"000", Year.of(0)}, 260 {"0000", Year.of(0)}, 261 {"00000", Year.of(0)}, 262 {"+00000", Year.of(0)}, 263 {"-0", Year.of(0)}, 264 {"-00", Year.of(0)}, 265 {"-000", Year.of(0)}, 266 {"-0000", Year.of(0)}, 267 {"-00000", Year.of(0)}, 268 {"1", Year.of(1)}, 269 {"01", Year.of(1)}, 270 {"001", Year.of(1)}, 271 {"0001", Year.of(1)}, 272 {"00001", Year.of(1)}, 273 {"+00001", Year.of(1)}, 274 {"-1", Year.of(-1)}, 275 {"-01", Year.of(-1)}, 276 {"-001", Year.of(-1)}, 277 {"-0001", Year.of(-1)}, 278 {"-00001", Year.of(-1)}, 279 280 {"+12345678", Year.of(12345678)}, 281 {"+123456", Year.of(123456)}, 282 {"-1234", Year.of(-1234)}, 283 {"-12345678", Year.of(-12345678)}, 284 285 {"+" + Year.MAX_VALUE, Year.of(Year.MAX_VALUE)}, 286 {"" + Year.MIN_VALUE, Year.of(Year.MIN_VALUE)}, 287 }; 288 } 289 290 @Test(dataProvider="goodParseData") factory_parse_success(String text, Year expected)291 public void factory_parse_success(String text, Year expected) { 292 Year year = Year.parse(text); 293 assertEquals(year, expected); 294 } 295 296 @DataProvider(name="badParseData") provider_badParseData()297 Object[][] provider_badParseData() { 298 return new Object[][] { 299 {"", 0}, 300 {"--01-0", 1}, 301 {"A01", 0}, 302 {"2009/12", 4}, 303 304 {"-0000-10", 5}, 305 {"-12345678901-10", 10}, 306 {"+1-10", 2}, 307 {"+12-10", 3}, 308 {"+123-10", 4}, 309 {"+1234-10", 5}, 310 {"12345-10", 5}, 311 {"+12345678901-10", 10}, 312 }; 313 } 314 315 @NonCts(bug = 286802267, reason = NonCtsReasons.NON_BREAKING_BEHAVIOR_FIX) 316 @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class) factory_parse_fail(String text, int pos)317 public void factory_parse_fail(String text, int pos) { 318 try { 319 Year.parse(text); 320 fail(String.format("Parse should have failed for %s at position %d", text, pos)); 321 } catch (DateTimeParseException ex) { 322 assertEquals(ex.getParsedString(), text); 323 assertEquals(ex.getErrorIndex(), pos); 324 throw ex; 325 } 326 } 327 328 @Test(expectedExceptions=NullPointerException.class) factory_parse_nullText()329 public void factory_parse_nullText() { 330 Year.parse(null); 331 } 332 333 //----------------------------------------------------------------------- 334 // parse(DateTimeFormatter) 335 //----------------------------------------------------------------------- 336 @Test factory_parse_formatter()337 public void factory_parse_formatter() { 338 DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); 339 Year test = Year.parse("2010", f); 340 assertEquals(test, Year.of(2010)); 341 } 342 343 @Test(expectedExceptions=NullPointerException.class) factory_parse_formatter_nullText()344 public void factory_parse_formatter_nullText() { 345 DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); 346 Year.parse((String) null, f); 347 } 348 349 @Test(expectedExceptions=NullPointerException.class) factory_parse_formatter_nullFormatter()350 public void factory_parse_formatter_nullFormatter() { 351 Year.parse("ANY", null); 352 } 353 354 //----------------------------------------------------------------------- 355 // isSupported(TemporalField) 356 //----------------------------------------------------------------------- 357 @Test test_isSupported_TemporalField()358 public void test_isSupported_TemporalField() { 359 assertEquals(TEST_2008.isSupported((TemporalField) null), false); 360 assertEquals(TEST_2008.isSupported(ChronoField.NANO_OF_SECOND), false); 361 assertEquals(TEST_2008.isSupported(ChronoField.NANO_OF_DAY), false); 362 assertEquals(TEST_2008.isSupported(ChronoField.MICRO_OF_SECOND), false); 363 assertEquals(TEST_2008.isSupported(ChronoField.MICRO_OF_DAY), false); 364 assertEquals(TEST_2008.isSupported(ChronoField.MILLI_OF_SECOND), false); 365 assertEquals(TEST_2008.isSupported(ChronoField.MILLI_OF_DAY), false); 366 assertEquals(TEST_2008.isSupported(ChronoField.SECOND_OF_MINUTE), false); 367 assertEquals(TEST_2008.isSupported(ChronoField.SECOND_OF_DAY), false); 368 assertEquals(TEST_2008.isSupported(ChronoField.MINUTE_OF_HOUR), false); 369 assertEquals(TEST_2008.isSupported(ChronoField.MINUTE_OF_DAY), false); 370 assertEquals(TEST_2008.isSupported(ChronoField.HOUR_OF_AMPM), false); 371 assertEquals(TEST_2008.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), false); 372 assertEquals(TEST_2008.isSupported(ChronoField.HOUR_OF_DAY), false); 373 assertEquals(TEST_2008.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), false); 374 assertEquals(TEST_2008.isSupported(ChronoField.AMPM_OF_DAY), false); 375 assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_WEEK), false); 376 assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), false); 377 assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), false); 378 assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_MONTH), false); 379 assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_YEAR), false); 380 assertEquals(TEST_2008.isSupported(ChronoField.EPOCH_DAY), false); 381 assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), false); 382 assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), false); 383 assertEquals(TEST_2008.isSupported(ChronoField.MONTH_OF_YEAR), false); 384 assertEquals(TEST_2008.isSupported(ChronoField.PROLEPTIC_MONTH), false); 385 assertEquals(TEST_2008.isSupported(ChronoField.YEAR), true); 386 assertEquals(TEST_2008.isSupported(ChronoField.YEAR_OF_ERA), true); 387 assertEquals(TEST_2008.isSupported(ChronoField.ERA), true); 388 assertEquals(TEST_2008.isSupported(ChronoField.INSTANT_SECONDS), false); 389 assertEquals(TEST_2008.isSupported(ChronoField.OFFSET_SECONDS), false); 390 } 391 392 //----------------------------------------------------------------------- 393 // isSupported(TemporalUnit) 394 //----------------------------------------------------------------------- 395 @Test test_isSupported_TemporalUnit()396 public void test_isSupported_TemporalUnit() { 397 assertEquals(TEST_2008.isSupported((TemporalUnit) null), false); 398 assertEquals(TEST_2008.isSupported(ChronoUnit.NANOS), false); 399 assertEquals(TEST_2008.isSupported(ChronoUnit.MICROS), false); 400 assertEquals(TEST_2008.isSupported(ChronoUnit.MILLIS), false); 401 assertEquals(TEST_2008.isSupported(ChronoUnit.SECONDS), false); 402 assertEquals(TEST_2008.isSupported(ChronoUnit.MINUTES), false); 403 assertEquals(TEST_2008.isSupported(ChronoUnit.HOURS), false); 404 assertEquals(TEST_2008.isSupported(ChronoUnit.HALF_DAYS), false); 405 assertEquals(TEST_2008.isSupported(ChronoUnit.DAYS), false); 406 assertEquals(TEST_2008.isSupported(ChronoUnit.WEEKS), false); 407 assertEquals(TEST_2008.isSupported(ChronoUnit.MONTHS), false); 408 assertEquals(TEST_2008.isSupported(ChronoUnit.YEARS), true); 409 assertEquals(TEST_2008.isSupported(ChronoUnit.DECADES), true); 410 assertEquals(TEST_2008.isSupported(ChronoUnit.CENTURIES), true); 411 assertEquals(TEST_2008.isSupported(ChronoUnit.MILLENNIA), true); 412 assertEquals(TEST_2008.isSupported(ChronoUnit.ERAS), true); 413 assertEquals(TEST_2008.isSupported(ChronoUnit.FOREVER), false); 414 } 415 416 //----------------------------------------------------------------------- 417 // get(TemporalField) 418 //----------------------------------------------------------------------- 419 @Test test_get_TemporalField()420 public void test_get_TemporalField() { 421 assertEquals(TEST_2008.get(ChronoField.YEAR), 2008); 422 assertEquals(TEST_2008.get(ChronoField.YEAR_OF_ERA), 2008); 423 assertEquals(TEST_2008.get(ChronoField.ERA), 1); 424 } 425 426 @Test test_getLong_TemporalField()427 public void test_getLong_TemporalField() { 428 assertEquals(TEST_2008.getLong(ChronoField.YEAR), 2008); 429 assertEquals(TEST_2008.getLong(ChronoField.YEAR_OF_ERA), 2008); 430 assertEquals(TEST_2008.getLong(ChronoField.ERA), 1); 431 } 432 433 //----------------------------------------------------------------------- 434 // query(TemporalQuery) 435 //----------------------------------------------------------------------- 436 @DataProvider(name="query") data_query()437 Object[][] data_query() { 438 return new Object[][] { 439 {TEST_2008, TemporalQueries.chronology(), IsoChronology.INSTANCE}, 440 {TEST_2008, TemporalQueries.zoneId(), null}, 441 {TEST_2008, TemporalQueries.precision(), ChronoUnit.YEARS}, 442 {TEST_2008, TemporalQueries.zone(), null}, 443 {TEST_2008, TemporalQueries.offset(), null}, 444 {TEST_2008, TemporalQueries.localDate(), null}, 445 {TEST_2008, TemporalQueries.localTime(), null}, 446 }; 447 } 448 449 @Test(dataProvider="query") test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected)450 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 451 assertEquals(temporal.query(query), expected); 452 } 453 454 @Test(dataProvider="query") test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected)455 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 456 assertEquals(query.queryFrom(temporal), expected); 457 } 458 459 @Test(expectedExceptions=NullPointerException.class) test_query_null()460 public void test_query_null() { 461 TEST_2008.query(null); 462 } 463 464 //----------------------------------------------------------------------- 465 // isLeap() 466 //----------------------------------------------------------------------- 467 @Test test_isLeap()468 public void test_isLeap() { 469 assertEquals(Year.of(1999).isLeap(), false); 470 assertEquals(Year.of(2000).isLeap(), true); 471 assertEquals(Year.of(2001).isLeap(), false); 472 473 assertEquals(Year.of(2007).isLeap(), false); 474 assertEquals(Year.of(2008).isLeap(), true); 475 assertEquals(Year.of(2009).isLeap(), false); 476 assertEquals(Year.of(2010).isLeap(), false); 477 assertEquals(Year.of(2011).isLeap(), false); 478 assertEquals(Year.of(2012).isLeap(), true); 479 480 assertEquals(Year.of(2095).isLeap(), false); 481 assertEquals(Year.of(2096).isLeap(), true); 482 assertEquals(Year.of(2097).isLeap(), false); 483 assertEquals(Year.of(2098).isLeap(), false); 484 assertEquals(Year.of(2099).isLeap(), false); 485 assertEquals(Year.of(2100).isLeap(), false); 486 assertEquals(Year.of(2101).isLeap(), false); 487 assertEquals(Year.of(2102).isLeap(), false); 488 assertEquals(Year.of(2103).isLeap(), false); 489 assertEquals(Year.of(2104).isLeap(), true); 490 assertEquals(Year.of(2105).isLeap(), false); 491 492 assertEquals(Year.of(-500).isLeap(), false); 493 assertEquals(Year.of(-400).isLeap(), true); 494 assertEquals(Year.of(-300).isLeap(), false); 495 assertEquals(Year.of(-200).isLeap(), false); 496 assertEquals(Year.of(-100).isLeap(), false); 497 assertEquals(Year.of(0).isLeap(), true); 498 assertEquals(Year.of(100).isLeap(), false); 499 assertEquals(Year.of(200).isLeap(), false); 500 assertEquals(Year.of(300).isLeap(), false); 501 assertEquals(Year.of(400).isLeap(), true); 502 assertEquals(Year.of(500).isLeap(), false); 503 } 504 505 //----------------------------------------------------------------------- 506 // plus(Period) 507 //----------------------------------------------------------------------- 508 @DataProvider(name="plusValid") data_plusValid()509 Object[][] data_plusValid() { 510 return new Object[][] { 511 {2012, Period.ofYears(0), 2012}, 512 {2012, Period.ofYears(1), 2013}, 513 {2012, Period.ofYears(2), 2014}, 514 {2012, Period.ofYears(-2), 2010}, 515 }; 516 } 517 518 @Test(dataProvider="plusValid") test_plusValid(int year, TemporalAmount amount, int expected)519 public void test_plusValid(int year, TemporalAmount amount, int expected) { 520 assertEquals(Year.of(year).plus(amount), Year.of(expected)); 521 } 522 523 @DataProvider(name="plusInvalidUnit") data_plusInvalidUnit()524 Object[][] data_plusInvalidUnit() { 525 return new Object[][] { 526 {Period.of(0, 1, 0)}, 527 {Period.of(0, 0, 1)}, 528 {Period.of(0, 1, 1)}, 529 {Period.of(1, 1, 1)}, 530 {Duration.ofDays(1)}, 531 {Duration.ofHours(1)}, 532 {Duration.ofMinutes(1)}, 533 {Duration.ofSeconds(1)}, 534 }; 535 } 536 537 @Test(dataProvider="plusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class) test_plusInvalidUnit(TemporalAmount amount)538 public void test_plusInvalidUnit(TemporalAmount amount) { 539 TEST_2008.plus(amount); 540 } 541 542 @Test(expectedExceptions=NullPointerException.class) test_plus_null()543 public void test_plus_null() { 544 TEST_2008.plus(null); 545 } 546 547 //----------------------------------------------------------------------- 548 // plusYears() 549 //----------------------------------------------------------------------- 550 @Test test_plusYears()551 public void test_plusYears() { 552 assertEquals(Year.of(2007).plusYears(-1), Year.of(2006)); 553 assertEquals(Year.of(2007).plusYears(0), Year.of(2007)); 554 assertEquals(Year.of(2007).plusYears(1), Year.of(2008)); 555 assertEquals(Year.of(2007).plusYears(2), Year.of(2009)); 556 557 assertEquals(Year.of(Year.MAX_VALUE - 1).plusYears(1), Year.of(Year.MAX_VALUE)); 558 assertEquals(Year.of(Year.MAX_VALUE).plusYears(0), Year.of(Year.MAX_VALUE)); 559 560 assertEquals(Year.of(Year.MIN_VALUE + 1).plusYears(-1), Year.of(Year.MIN_VALUE)); 561 assertEquals(Year.of(Year.MIN_VALUE).plusYears(0), Year.of(Year.MIN_VALUE)); 562 } 563 564 @Test test_plusYear_zero_equals()565 public void test_plusYear_zero_equals() { 566 Year base = Year.of(2007); 567 assertEquals(base.plusYears(0), base); 568 } 569 570 @Test test_plusYears_big()571 public void test_plusYears_big() { 572 long years = 20L + Year.MAX_VALUE; 573 assertEquals(Year.of(-40).plusYears(years), Year.of((int) (-40L + years))); 574 } 575 576 @Test(expectedExceptions=DateTimeException.class) test_plusYears_max()577 public void test_plusYears_max() { 578 Year.of(Year.MAX_VALUE).plusYears(1); 579 } 580 581 @Test(expectedExceptions=DateTimeException.class) test_plusYears_maxLots()582 public void test_plusYears_maxLots() { 583 Year.of(Year.MAX_VALUE).plusYears(1000); 584 } 585 586 @Test(expectedExceptions=DateTimeException.class) test_plusYears_min()587 public void test_plusYears_min() { 588 Year.of(Year.MIN_VALUE).plusYears(-1); 589 } 590 591 @Test(expectedExceptions=DateTimeException.class) test_plusYears_minLots()592 public void test_plusYears_minLots() { 593 Year.of(Year.MIN_VALUE).plusYears(-1000); 594 } 595 596 //----------------------------------------------------------------------- 597 // plus(long, TemporalUnit) 598 //----------------------------------------------------------------------- 599 @DataProvider(name="plus_long_TemporalUnit") data_plus_long_TemporalUnit()600 Object[][] data_plus_long_TemporalUnit() { 601 return new Object[][] { 602 {Year.of(1), 1, ChronoUnit.YEARS, Year.of(2), null}, 603 {Year.of(1), -12, ChronoUnit.YEARS, Year.of(-11), null}, 604 {Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null}, 605 {Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null}, 606 {Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null}, 607 {Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(-999999999), null}, 608 {Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(999999999), null}, 609 610 {Year.of(-1), 1, ChronoUnit.ERAS, Year.of(2), null}, 611 {Year.of(5), 1, ChronoUnit.CENTURIES, Year.of(105), null}, 612 {Year.of(5), 1, ChronoUnit.DECADES, Year.of(15), null}, 613 614 {Year.of(999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class}, 615 {Year.of(-999999999), -1, ChronoUnit.YEARS, null, DateTimeException.class}, 616 617 {Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class}, 618 {Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class}, 619 {Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class}, 620 }; 621 } 622 623 @Test(dataProvider="plus_long_TemporalUnit") test_plus_long_TemporalUnit(Year base, long amount, TemporalUnit unit, Year expectedYear, Class<?> expectedEx)624 public void test_plus_long_TemporalUnit(Year base, long amount, TemporalUnit unit, Year expectedYear, Class<?> expectedEx) { 625 if (expectedEx == null) { 626 assertEquals(base.plus(amount, unit), expectedYear); 627 } else { 628 try { 629 base.plus(amount, unit); 630 fail(); 631 } catch (Exception ex) { 632 assertTrue(expectedEx.isInstance(ex)); 633 } 634 } 635 } 636 637 //----------------------------------------------------------------------- 638 // minus(Period) 639 //----------------------------------------------------------------------- 640 @DataProvider(name="minusValid") data_minusValid()641 Object[][] data_minusValid() { 642 return new Object[][] { 643 {2012, Period.ofYears(0), 2012}, 644 {2012, Period.ofYears(1), 2011}, 645 {2012, Period.ofYears(2), 2010}, 646 {2012, Period.ofYears(-2), 2014}, 647 }; 648 } 649 650 @Test(dataProvider="minusValid") test_minusValid(int year, TemporalAmount amount, int expected)651 public void test_minusValid(int year, TemporalAmount amount, int expected) { 652 assertEquals(Year.of(year).minus(amount), Year.of(expected)); 653 } 654 655 @DataProvider(name="minusInvalidUnit") data_minusInvalidUnit()656 Object[][] data_minusInvalidUnit() { 657 return new Object[][] { 658 {Period.of(0, 1, 0)}, 659 {Period.of(0, 0, 1)}, 660 {Period.of(0, 1, 1)}, 661 {Period.of(1, 1, 1)}, 662 {Duration.ofDays(1)}, 663 {Duration.ofHours(1)}, 664 {Duration.ofMinutes(1)}, 665 {Duration.ofSeconds(1)}, 666 }; 667 } 668 669 @Test(dataProvider="minusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class) test_minusInvalidUnit(TemporalAmount amount)670 public void test_minusInvalidUnit(TemporalAmount amount) { 671 TEST_2008.minus(amount); 672 } 673 674 @Test(expectedExceptions=NullPointerException.class) test_minus_null()675 public void test_minus_null() { 676 TEST_2008.minus(null); 677 } 678 679 //----------------------------------------------------------------------- 680 // minusYears() 681 //----------------------------------------------------------------------- 682 @Test test_minusYears()683 public void test_minusYears() { 684 assertEquals(Year.of(2007).minusYears(-1), Year.of(2008)); 685 assertEquals(Year.of(2007).minusYears(0), Year.of(2007)); 686 assertEquals(Year.of(2007).minusYears(1), Year.of(2006)); 687 assertEquals(Year.of(2007).minusYears(2), Year.of(2005)); 688 689 assertEquals(Year.of(Year.MAX_VALUE - 1).minusYears(-1), Year.of(Year.MAX_VALUE)); 690 assertEquals(Year.of(Year.MAX_VALUE).minusYears(0), Year.of(Year.MAX_VALUE)); 691 692 assertEquals(Year.of(Year.MIN_VALUE + 1).minusYears(1), Year.of(Year.MIN_VALUE)); 693 assertEquals(Year.of(Year.MIN_VALUE).minusYears(0), Year.of(Year.MIN_VALUE)); 694 } 695 696 @Test test_minusYear_zero_equals()697 public void test_minusYear_zero_equals() { 698 Year base = Year.of(2007); 699 assertEquals(base.minusYears(0), base); 700 } 701 702 @Test test_minusYears_big()703 public void test_minusYears_big() { 704 long years = 20L + Year.MAX_VALUE; 705 assertEquals(Year.of(40).minusYears(years), Year.of((int) (40L - years))); 706 } 707 708 @Test(expectedExceptions=DateTimeException.class) test_minusYears_max()709 public void test_minusYears_max() { 710 Year.of(Year.MAX_VALUE).minusYears(-1); 711 } 712 713 @Test(expectedExceptions=DateTimeException.class) test_minusYears_maxLots()714 public void test_minusYears_maxLots() { 715 Year.of(Year.MAX_VALUE).minusYears(-1000); 716 } 717 718 @Test(expectedExceptions=DateTimeException.class) test_minusYears_min()719 public void test_minusYears_min() { 720 Year.of(Year.MIN_VALUE).minusYears(1); 721 } 722 723 @Test(expectedExceptions=DateTimeException.class) test_minusYears_minLots()724 public void test_minusYears_minLots() { 725 Year.of(Year.MIN_VALUE).minusYears(1000); 726 } 727 728 //----------------------------------------------------------------------- 729 // minus(long, TemporalUnit) 730 //----------------------------------------------------------------------- 731 @DataProvider(name="minus_long_TemporalUnit") data_minus_long_TemporalUnit()732 Object[][] data_minus_long_TemporalUnit() { 733 return new Object[][] { 734 {Year.of(1), 1, ChronoUnit.YEARS, Year.of(0), null}, 735 {Year.of(1), -12, ChronoUnit.YEARS, Year.of(13), null}, 736 {Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null}, 737 {Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null}, 738 {Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null}, 739 {Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(999999999), null}, 740 {Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(-999999999), null}, 741 742 {Year.of(999999999), 1, ChronoUnit.ERAS, Year.of(-999999999 + 1), null}, 743 {Year.of(105), 1, ChronoUnit.CENTURIES, Year.of(5), null}, 744 {Year.of(15), 1, ChronoUnit.DECADES, Year.of(5), null}, 745 746 {Year.of(-999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class}, 747 {Year.of(1), -999999999, ChronoUnit.YEARS, null, DateTimeException.class}, 748 749 {Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class}, 750 {Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class}, 751 {Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class}, 752 }; 753 } 754 755 @Test(dataProvider="minus_long_TemporalUnit") test_minus_long_TemporalUnit(Year base, long amount, TemporalUnit unit, Year expectedYear, Class<?> expectedEx)756 public void test_minus_long_TemporalUnit(Year base, long amount, TemporalUnit unit, Year expectedYear, Class<?> expectedEx) { 757 if (expectedEx == null) { 758 assertEquals(base.minus(amount, unit), expectedYear); 759 } else { 760 try { 761 Year result = base.minus(amount, unit); 762 fail(); 763 } catch (Exception ex) { 764 assertTrue(expectedEx.isInstance(ex)); 765 } 766 } 767 } 768 769 //----------------------------------------------------------------------- 770 // adjustInto() 771 //----------------------------------------------------------------------- 772 @Test test_adjustDate()773 public void test_adjustDate() { 774 LocalDate base = LocalDate.of(2007, 2, 12); 775 for (int i = -4; i <= 2104; i++) { 776 Temporal result = Year.of(i).adjustInto(base); 777 assertEquals(result, LocalDate.of(i, 2, 12)); 778 } 779 } 780 781 @Test test_adjustDate_resolve()782 public void test_adjustDate_resolve() { 783 Year test = Year.of(2011); 784 assertEquals(test.adjustInto(LocalDate.of(2012, 2, 29)), LocalDate.of(2011, 2, 28)); 785 } 786 787 @Test(expectedExceptions=NullPointerException.class) test_adjustDate_nullLocalDate()788 public void test_adjustDate_nullLocalDate() { 789 Year test = Year.of(1); 790 test.adjustInto((LocalDate) null); 791 } 792 793 //----------------------------------------------------------------------- 794 // with(TemporalAdjuster) 795 //----------------------------------------------------------------------- 796 @Test test_with_TemporalAdjuster()797 public void test_with_TemporalAdjuster() { 798 Year base = Year.of(-10); 799 for (int i = -4; i <= 2104; i++) { 800 Temporal result = base.with(Year.of(i)); 801 assertEquals(result, Year.of(i)); 802 } 803 } 804 805 @Test(expectedExceptions=DateTimeException.class) test_with_BadTemporalAdjuster()806 public void test_with_BadTemporalAdjuster() { 807 Year test = Year.of(1); 808 test.with(LocalTime.of(18, 1, 2)); 809 } 810 811 //----------------------------------------------------------------------- 812 // with(TemporalField, long) 813 //----------------------------------------------------------------------- 814 @Test test_with()815 public void test_with() { 816 Year base = Year.of(5); 817 Year result = base.with(ChronoField.ERA, 0); 818 assertEquals(result, base.with(IsoEra.of(0))); 819 820 int prolepticYear = IsoChronology.INSTANCE.prolepticYear(IsoEra.of(0), 5); 821 assertEquals(result.get(ChronoField.ERA), 0); 822 assertEquals(result.get(ChronoField.YEAR), prolepticYear); 823 assertEquals(result.get(ChronoField.YEAR_OF_ERA), 5); 824 825 result = base.with(ChronoField.YEAR, 10); 826 assertEquals(result.get(ChronoField.ERA), base.get(ChronoField.ERA)); 827 assertEquals(result.get(ChronoField.YEAR), 10); 828 assertEquals(result.get(ChronoField.YEAR_OF_ERA), 10); 829 830 result = base.with(ChronoField.YEAR_OF_ERA, 20); 831 assertEquals(result.get(ChronoField.ERA), base.get(ChronoField.ERA)); 832 assertEquals(result.get(ChronoField.YEAR), 20); 833 assertEquals(result.get(ChronoField.YEAR_OF_ERA), 20); 834 } 835 836 //----------------------------------------------------------------------- 837 // length() 838 //----------------------------------------------------------------------- 839 @Test test_length()840 public void test_length() { 841 assertEquals(Year.of(1999).length(), 365); 842 assertEquals(Year.of(2000).length(), 366); 843 assertEquals(Year.of(2001).length(), 365); 844 845 assertEquals(Year.of(2007).length(), 365); 846 assertEquals(Year.of(2008).length(), 366); 847 assertEquals(Year.of(2009).length(), 365); 848 assertEquals(Year.of(2010).length(), 365); 849 assertEquals(Year.of(2011).length(), 365); 850 assertEquals(Year.of(2012).length(), 366); 851 852 assertEquals(Year.of(2095).length(), 365); 853 assertEquals(Year.of(2096).length(), 366); 854 assertEquals(Year.of(2097).length(), 365); 855 assertEquals(Year.of(2098).length(), 365); 856 assertEquals(Year.of(2099).length(), 365); 857 assertEquals(Year.of(2100).length(), 365); 858 assertEquals(Year.of(2101).length(), 365); 859 assertEquals(Year.of(2102).length(), 365); 860 assertEquals(Year.of(2103).length(), 365); 861 assertEquals(Year.of(2104).length(), 366); 862 assertEquals(Year.of(2105).length(), 365); 863 864 assertEquals(Year.of(-500).length(), 365); 865 assertEquals(Year.of(-400).length(), 366); 866 assertEquals(Year.of(-300).length(), 365); 867 assertEquals(Year.of(-200).length(), 365); 868 assertEquals(Year.of(-100).length(), 365); 869 assertEquals(Year.of(0).length(), 366); 870 assertEquals(Year.of(100).length(), 365); 871 assertEquals(Year.of(200).length(), 365); 872 assertEquals(Year.of(300).length(), 365); 873 assertEquals(Year.of(400).length(), 366); 874 assertEquals(Year.of(500).length(), 365); 875 } 876 877 //----------------------------------------------------------------------- 878 // isValidMonthDay(MonthDay) 879 //----------------------------------------------------------------------- 880 @DataProvider(name="isValidMonthDay") data_isValidMonthDay()881 Object[][] data_isValidMonthDay() { 882 return new Object[][] { 883 {Year.of(2007), MonthDay.of(6, 30), true}, 884 {Year.of(2008), MonthDay.of(2, 28), true}, 885 {Year.of(2008), MonthDay.of(2, 29), true}, 886 {Year.of(2009), MonthDay.of(2, 28), true}, 887 {Year.of(2009), MonthDay.of(2, 29), false}, 888 {Year.of(2009), null, false}, 889 }; 890 } 891 892 @Test(dataProvider="isValidMonthDay") test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected)893 public void test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected) { 894 assertEquals(year.isValidMonthDay(monthDay), expected); 895 } 896 897 //----------------------------------------------------------------------- 898 // until(Temporal, TemporalUnit) 899 //----------------------------------------------------------------------- 900 @DataProvider(name="periodUntilUnit") data_periodUntilUnit()901 Object[][] data_periodUntilUnit() { 902 return new Object[][] { 903 {Year.of(2000), Year.of(-1), YEARS, -2001}, 904 {Year.of(2000), Year.of(0), YEARS, -2000}, 905 {Year.of(2000), Year.of(1), YEARS, -1999}, 906 {Year.of(2000), Year.of(1998), YEARS, -2}, 907 {Year.of(2000), Year.of(1999), YEARS, -1}, 908 {Year.of(2000), Year.of(2000), YEARS, 0}, 909 {Year.of(2000), Year.of(2001), YEARS, 1}, 910 {Year.of(2000), Year.of(2002), YEARS, 2}, 911 {Year.of(2000), Year.of(2246), YEARS, 246}, 912 913 {Year.of(2000), Year.of(-1), DECADES, -200}, 914 {Year.of(2000), Year.of(0), DECADES, -200}, 915 {Year.of(2000), Year.of(1), DECADES, -199}, 916 {Year.of(2000), Year.of(1989), DECADES, -1}, 917 {Year.of(2000), Year.of(1990), DECADES, -1}, 918 {Year.of(2000), Year.of(1991), DECADES, 0}, 919 {Year.of(2000), Year.of(2000), DECADES, 0}, 920 {Year.of(2000), Year.of(2009), DECADES, 0}, 921 {Year.of(2000), Year.of(2010), DECADES, 1}, 922 {Year.of(2000), Year.of(2011), DECADES, 1}, 923 924 {Year.of(2000), Year.of(-1), CENTURIES, -20}, 925 {Year.of(2000), Year.of(0), CENTURIES, -20}, 926 {Year.of(2000), Year.of(1), CENTURIES, -19}, 927 {Year.of(2000), Year.of(1899), CENTURIES, -1}, 928 {Year.of(2000), Year.of(1900), CENTURIES, -1}, 929 {Year.of(2000), Year.of(1901), CENTURIES, 0}, 930 {Year.of(2000), Year.of(2000), CENTURIES, 0}, 931 {Year.of(2000), Year.of(2099), CENTURIES, 0}, 932 {Year.of(2000), Year.of(2100), CENTURIES, 1}, 933 {Year.of(2000), Year.of(2101), CENTURIES, 1}, 934 935 {Year.of(2000), Year.of(-1), MILLENNIA, -2}, 936 {Year.of(2000), Year.of(0), MILLENNIA, -2}, 937 {Year.of(2000), Year.of(1), MILLENNIA, -1}, 938 {Year.of(2000), Year.of(999), MILLENNIA, -1}, 939 {Year.of(2000), Year.of(1000), MILLENNIA, -1}, 940 {Year.of(2000), Year.of(1001), MILLENNIA, 0}, 941 {Year.of(2000), Year.of(2000), MILLENNIA, 0}, 942 {Year.of(2000), Year.of(2999), MILLENNIA, 0}, 943 {Year.of(2000), Year.of(3000), MILLENNIA, 1}, 944 {Year.of(2000), Year.of(3001), MILLENNIA, 1}, 945 }; 946 } 947 948 @Test(dataProvider="periodUntilUnit") test_until_TemporalUnit(Year year1, Year year2, TemporalUnit unit, long expected)949 public void test_until_TemporalUnit(Year year1, Year year2, TemporalUnit unit, long expected) { 950 long amount = year1.until(year2, unit); 951 assertEquals(amount, expected); 952 } 953 954 @Test(dataProvider="periodUntilUnit") test_until_TemporalUnit_negated(Year year1, Year year2, TemporalUnit unit, long expected)955 public void test_until_TemporalUnit_negated(Year year1, Year year2, TemporalUnit unit, long expected) { 956 long amount = year2.until(year1, unit); 957 assertEquals(amount, -expected); 958 } 959 960 @Test(dataProvider="periodUntilUnit") test_until_TemporalUnit_between(Year year1, Year year2, TemporalUnit unit, long expected)961 public void test_until_TemporalUnit_between(Year year1, Year year2, TemporalUnit unit, long expected) { 962 long amount = unit.between(year1, year2); 963 assertEquals(amount, expected); 964 } 965 966 @Test test_until_convertedType()967 public void test_until_convertedType() { 968 Year start = Year.of(2010); 969 YearMonth end = start.plusYears(2).atMonth(Month.APRIL); 970 assertEquals(start.until(end, YEARS), 2); 971 } 972 973 @Test(expectedExceptions=DateTimeException.class) test_until_invalidType()974 public void test_until_invalidType() { 975 Year start = Year.of(2010); 976 start.until(LocalTime.of(11, 30), YEARS); 977 } 978 979 @Test(expectedExceptions = UnsupportedTemporalTypeException.class) test_until_TemporalUnit_unsupportedUnit()980 public void test_until_TemporalUnit_unsupportedUnit() { 981 TEST_2008.until(TEST_2008, MONTHS); 982 } 983 984 @Test(expectedExceptions = NullPointerException.class) test_until_TemporalUnit_nullEnd()985 public void test_until_TemporalUnit_nullEnd() { 986 TEST_2008.until(null, DAYS); 987 } 988 989 @Test(expectedExceptions = NullPointerException.class) test_until_TemporalUnit_nullUnit()990 public void test_until_TemporalUnit_nullUnit() { 991 TEST_2008.until(TEST_2008, null); 992 } 993 994 //----------------------------------------------------------------------- 995 // format(DateTimeFormatter) 996 //----------------------------------------------------------------------- 997 @Test test_format_formatter()998 public void test_format_formatter() { 999 DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); 1000 String t = Year.of(2010).format(f); 1001 assertEquals(t, "2010"); 1002 } 1003 1004 @Test(expectedExceptions=NullPointerException.class) test_format_formatter_null()1005 public void test_format_formatter_null() { 1006 Year.of(2010).format(null); 1007 } 1008 1009 //----------------------------------------------------------------------- 1010 // atMonth(Month) 1011 //----------------------------------------------------------------------- 1012 @Test test_atMonth()1013 public void test_atMonth() { 1014 Year test = Year.of(2008); 1015 assertEquals(test.atMonth(Month.JUNE), YearMonth.of(2008, 6)); 1016 } 1017 1018 @Test(expectedExceptions=NullPointerException.class) test_atMonth_nullMonth()1019 public void test_atMonth_nullMonth() { 1020 Year test = Year.of(2008); 1021 test.atMonth((Month) null); 1022 } 1023 1024 //----------------------------------------------------------------------- 1025 // atMonth(int) 1026 //----------------------------------------------------------------------- 1027 @Test test_atMonth_int()1028 public void test_atMonth_int() { 1029 Year test = Year.of(2008); 1030 assertEquals(test.atMonth(6), YearMonth.of(2008, 6)); 1031 } 1032 1033 @Test(expectedExceptions=DateTimeException.class) test_atMonth_int_invalidMonth()1034 public void test_atMonth_int_invalidMonth() { 1035 Year test = Year.of(2008); 1036 test.atMonth(13); 1037 } 1038 1039 //----------------------------------------------------------------------- 1040 // atMonthDay(MonthDay) 1041 //----------------------------------------------------------------------- 1042 @DataProvider(name="atMonthDay") data_atMonthDay()1043 Object[][] data_atMonthDay() { 1044 return new Object[][] { 1045 {Year.of(2008), MonthDay.of(6, 30), LocalDate.of(2008, 6, 30)}, 1046 {Year.of(2008), MonthDay.of(2, 29), LocalDate.of(2008, 2, 29)}, 1047 {Year.of(2009), MonthDay.of(2, 29), LocalDate.of(2009, 2, 28)}, 1048 }; 1049 } 1050 1051 @Test(dataProvider="atMonthDay") test_atMonthDay(Year year, MonthDay monthDay, LocalDate expected)1052 public void test_atMonthDay(Year year, MonthDay monthDay, LocalDate expected) { 1053 assertEquals(year.atMonthDay(monthDay), expected); 1054 } 1055 1056 @Test(expectedExceptions=NullPointerException.class) test_atMonthDay_nullMonthDay()1057 public void test_atMonthDay_nullMonthDay() { 1058 Year test = Year.of(2008); 1059 test.atMonthDay((MonthDay) null); 1060 } 1061 1062 //----------------------------------------------------------------------- 1063 // atDay(int) 1064 //----------------------------------------------------------------------- 1065 @Test test_atDay_notLeapYear()1066 public void test_atDay_notLeapYear() { 1067 Year test = Year.of(2007); 1068 LocalDate expected = LocalDate.of(2007, 1, 1); 1069 for (int i = 1; i <= 365; i++) { 1070 assertEquals(test.atDay(i), expected); 1071 expected = expected.plusDays(1); 1072 } 1073 } 1074 1075 @Test(expectedExceptions=DateTimeException.class) test_atDay_notLeapYear_day366()1076 public void test_atDay_notLeapYear_day366() { 1077 Year test = Year.of(2007); 1078 test.atDay(366); 1079 } 1080 1081 @Test test_atDay_leapYear()1082 public void test_atDay_leapYear() { 1083 Year test = Year.of(2008); 1084 LocalDate expected = LocalDate.of(2008, 1, 1); 1085 for (int i = 1; i <= 366; i++) { 1086 assertEquals(test.atDay(i), expected); 1087 expected = expected.plusDays(1); 1088 } 1089 } 1090 1091 @Test(expectedExceptions=DateTimeException.class) test_atDay_day0()1092 public void test_atDay_day0() { 1093 Year test = Year.of(2007); 1094 test.atDay(0); 1095 } 1096 1097 @Test(expectedExceptions=DateTimeException.class) test_atDay_day367()1098 public void test_atDay_day367() { 1099 Year test = Year.of(2007); 1100 test.atDay(367); 1101 } 1102 1103 //----------------------------------------------------------------------- 1104 // compareTo() 1105 //----------------------------------------------------------------------- 1106 @Test test_compareTo()1107 public void test_compareTo() { 1108 for (int i = -4; i <= 2104; i++) { 1109 Year a = Year.of(i); 1110 for (int j = -4; j <= 2104; j++) { 1111 Year b = Year.of(j); 1112 if (i < j) { 1113 assertEquals(a.compareTo(b) < 0, true); 1114 assertEquals(b.compareTo(a) > 0, true); 1115 assertEquals(a.isAfter(b), false); 1116 assertEquals(a.isBefore(b), true); 1117 assertEquals(b.isAfter(a), true); 1118 assertEquals(b.isBefore(a), false); 1119 } else if (i > j) { 1120 assertEquals(a.compareTo(b) > 0, true); 1121 assertEquals(b.compareTo(a) < 0, true); 1122 assertEquals(a.isAfter(b), true); 1123 assertEquals(a.isBefore(b), false); 1124 assertEquals(b.isAfter(a), false); 1125 assertEquals(b.isBefore(a), true); 1126 } else { 1127 assertEquals(a.compareTo(b), 0); 1128 assertEquals(b.compareTo(a), 0); 1129 assertEquals(a.isAfter(b), false); 1130 assertEquals(a.isBefore(b), false); 1131 assertEquals(b.isAfter(a), false); 1132 assertEquals(b.isBefore(a), false); 1133 } 1134 } 1135 } 1136 } 1137 1138 @Test(expectedExceptions=NullPointerException.class) 1139 public void test_compareTo_nullYear() { 1140 Year doy = null; 1141 Year test = Year.of(1); 1142 test.compareTo(doy); 1143 } 1144 1145 //----------------------------------------------------------------------- 1146 // equals() / hashCode() 1147 //----------------------------------------------------------------------- 1148 @Test 1149 public void test_equals() { 1150 for (int i = -4; i <= 2104; i++) { 1151 Year a = Year.of(i); 1152 for (int j = -4; j <= 2104; j++) { 1153 Year b = Year.of(j); 1154 assertEquals(a.equals(b), i == j); 1155 assertEquals(a.hashCode() == b.hashCode(), i == j); 1156 } 1157 } 1158 } 1159 1160 @Test 1161 public void test_equals_same() { 1162 Year test = Year.of(2011); 1163 assertEquals(test.equals(test), true); 1164 } 1165 1166 @Test 1167 public void test_equals_nullYear() { 1168 Year doy = null; 1169 Year test = Year.of(1); 1170 assertEquals(test.equals(doy), false); 1171 } 1172 1173 @Test 1174 public void test_equals_incorrectType() { 1175 Year test = Year.of(1); 1176 assertEquals(test.equals("Incorrect type"), false); 1177 } 1178 1179 //----------------------------------------------------------------------- 1180 // toString() 1181 //----------------------------------------------------------------------- 1182 @Test 1183 public void test_toString() { 1184 for (int i = -4; i <= 2104; i++) { 1185 Year a = Year.of(i); 1186 assertEquals(a.toString(), "" + i); 1187 } 1188 } 1189 1190 } 1191