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. 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.HOURS; 69 import static java.time.temporal.ChronoUnit.MILLENNIA; 70 import static java.time.temporal.ChronoUnit.MONTHS; 71 import static java.time.temporal.ChronoUnit.WEEKS; 72 import static java.time.temporal.ChronoUnit.YEARS; 73 import static org.testng.Assert.assertEquals; 74 import static org.testng.Assert.fail; 75 76 import java.io.ByteArrayOutputStream; 77 import java.io.DataOutputStream; 78 import java.time.Clock; 79 import java.time.DateTimeException; 80 import java.time.Duration; 81 import java.time.Instant; 82 import java.time.LocalDate; 83 import java.time.LocalTime; 84 import java.time.Month; 85 import java.time.MonthDay; 86 import java.time.OffsetDateTime; 87 import java.time.Period; 88 import java.time.Year; 89 import java.time.YearMonth; 90 import java.time.ZoneId; 91 import java.time.ZoneOffset; 92 import java.time.chrono.IsoChronology; 93 import java.time.format.DateTimeFormatter; 94 import java.time.format.DateTimeParseException; 95 import java.time.temporal.ChronoField; 96 import java.time.temporal.ChronoUnit; 97 import java.time.temporal.JulianFields; 98 import java.time.temporal.Temporal; 99 import java.time.temporal.TemporalAccessor; 100 import java.time.temporal.TemporalAmount; 101 import java.time.temporal.TemporalField; 102 import java.time.temporal.TemporalQuery; 103 import java.time.temporal.TemporalUnit; 104 import java.time.temporal.UnsupportedTemporalTypeException; 105 import java.util.ArrayList; 106 import java.util.Arrays; 107 import java.util.List; 108 109 import org.testng.annotations.BeforeMethod; 110 import org.testng.annotations.DataProvider; 111 import org.testng.annotations.Test; 112 113 /** 114 * Test Year. 115 */ 116 @Test 117 public class TCKYear extends AbstractDateTimeTest { 118 119 private static final Year TEST_2008 = Year.of(2008); 120 121 @BeforeMethod 122 public void setUp() { 123 } 124 125 //----------------------------------------------------------------------- 126 @Override 127 protected List<TemporalAccessor> samples() { 128 TemporalAccessor[] array = {TEST_2008, }; 129 return Arrays.asList(array); 130 } 131 132 @Override 133 protected List<TemporalField> validFields() { 134 TemporalField[] array = { 135 YEAR_OF_ERA, 136 YEAR, 137 ERA, 138 }; 139 return Arrays.asList(array); 140 } 141 142 @Override 143 protected List<TemporalField> invalidFields() { 144 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 145 list.removeAll(validFields()); 146 list.add(JulianFields.JULIAN_DAY); 147 list.add(JulianFields.MODIFIED_JULIAN_DAY); 148 list.add(JulianFields.RATA_DIE); 149 return list; 150 } 151 152 //----------------------------------------------------------------------- 153 @Test 154 public void test_serialization() throws Exception { 155 assertSerializable(Year.of(2)); 156 assertSerializable(Year.of(0)); 157 assertSerializable(Year.of(-2)); 158 } 159 160 @Test 161 public void test_serialization_format() throws Exception { 162 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 163 try (DataOutputStream dos = new DataOutputStream(baos) ) { 164 dos.writeByte(11); // java.time.temporal.Ser.YEAR_TYPE 165 dos.writeInt(2012); 166 } 167 byte[] bytes = baos.toByteArray(); 168 assertSerializedBySer(Year.of(2012), bytes); 169 } 170 171 //----------------------------------------------------------------------- 172 // now() 173 //----------------------------------------------------------------------- 174 @Test 175 public void now() { 176 Year expected = Year.now(Clock.systemDefaultZone()); 177 Year test = Year.now(); 178 for (int i = 0; i < 100; i++) { 179 if (expected.equals(test)) { 180 return; 181 } 182 expected = Year.now(Clock.systemDefaultZone()); 183 test = Year.now(); 184 } 185 assertEquals(test, expected); 186 } 187 188 //----------------------------------------------------------------------- 189 // now(ZoneId) 190 //----------------------------------------------------------------------- 191 @Test(expectedExceptions=NullPointerException.class) 192 public void now_ZoneId_nullZoneId() { 193 Year.now((ZoneId) null); 194 } 195 196 @Test 197 public void now_ZoneId() { 198 ZoneId zone = ZoneId.of("UTC+01:02:03"); 199 Year expected = Year.now(Clock.system(zone)); 200 Year test = Year.now(zone); 201 for (int i = 0; i < 100; i++) { 202 if (expected.equals(test)) { 203 return; 204 } 205 expected = Year.now(Clock.system(zone)); 206 test = Year.now(zone); 207 } 208 assertEquals(test, expected); 209 } 210 211 //----------------------------------------------------------------------- 212 // now(Clock) 213 //----------------------------------------------------------------------- 214 @Test 215 public void now_Clock() { 216 Instant instant = OffsetDateTime.of(LocalDate.of(2010, 12, 31), LocalTime.of(0, 0), ZoneOffset.UTC).toInstant(); 217 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 218 Year test = Year.now(clock); 219 assertEquals(test.getValue(), 2010); 220 } 221 222 @Test(expectedExceptions=NullPointerException.class) 223 public void now_Clock_nullClock() { 224 Year.now((Clock) null); 225 } 226 227 //----------------------------------------------------------------------- 228 @Test 229 public void test_factory_int_singleton() { 230 for (int i = -4; i <= 2104; i++) { 231 Year test = Year.of(i); 232 assertEquals(test.getValue(), i); 233 assertEquals(Year.of(i), test); 234 } 235 } 236 237 @Test(expectedExceptions=DateTimeException.class) 238 public void test_factory_int_tooLow() { 239 Year.of(Year.MIN_VALUE - 1); 240 } 241 242 @Test(expectedExceptions=DateTimeException.class) 243 public void test_factory_int_tooHigh() { 244 Year.of(Year.MAX_VALUE + 1); 245 } 246 247 //----------------------------------------------------------------------- 248 @Test 249 public void test_factory_CalendricalObject() { 250 assertEquals(Year.from(LocalDate.of(2007, 7, 15)), Year.of(2007)); 251 } 252 253 @Test(expectedExceptions=DateTimeException.class) 254 public void test_factory_CalendricalObject_invalid_noDerive() { 255 Year.from(LocalTime.of(12, 30)); 256 } 257 258 @Test(expectedExceptions=NullPointerException.class) 259 public void test_factory_CalendricalObject_null() { 260 Year.from((TemporalAccessor) null); 261 } 262 263 //----------------------------------------------------------------------- 264 // parse() 265 //----------------------------------------------------------------------- 266 @DataProvider(name="goodParseData") 267 Object[][] provider_goodParseData() { 268 return new Object[][] { 269 {"0000", Year.of(0)}, 270 {"9999", Year.of(9999)}, 271 {"2000", Year.of(2000)}, 272 273 {"+12345678", Year.of(12345678)}, 274 {"+123456", Year.of(123456)}, 275 {"-1234", Year.of(-1234)}, 276 {"-12345678", Year.of(-12345678)}, 277 278 {"+" + Year.MAX_VALUE, Year.of(Year.MAX_VALUE)}, 279 {"" + Year.MIN_VALUE, Year.of(Year.MIN_VALUE)}, 280 }; 281 } 282 283 @Test(dataProvider="goodParseData") 284 public void factory_parse_success(String text, Year expected) { 285 Year year = Year.parse(text); 286 assertEquals(year, expected); 287 } 288 289 @DataProvider(name="badParseData") 290 Object[][] provider_badParseData() { 291 return new Object[][] { 292 {"", 0}, 293 {"-00", 1}, 294 {"--01-0", 1}, 295 {"A01", 0}, 296 {"200", 0}, 297 {"2009/12", 4}, 298 299 {"-0000-10", 0}, 300 {"-12345678901-10", 11}, 301 {"+1-10", 1}, 302 {"+12-10", 1}, 303 {"+123-10", 1}, 304 {"+1234-10", 0}, 305 {"12345-10", 0}, 306 {"+12345678901-10", 11}, 307 }; 308 } 309 310 @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class) 311 public void factory_parse_fail(String text, int pos) { 312 try { 313 Year.parse(text); 314 fail(String.format("Parse should have failed for %s at position %d", text, pos)); 315 } catch (DateTimeParseException ex) { 316 assertEquals(ex.getParsedString(), text); 317 assertEquals(ex.getErrorIndex(), pos); 318 throw ex; 319 } 320 } 321 322 @Test(expectedExceptions=NullPointerException.class) 323 public void factory_parse_nullText() { 324 Year.parse(null); 325 } 326 327 //----------------------------------------------------------------------- 328 // parse(DateTimeFormatter) 329 //----------------------------------------------------------------------- 330 @Test 331 public void factory_parse_formatter() { 332 DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); 333 Year test = Year.parse("2010", f); 334 assertEquals(test, Year.of(2010)); 335 } 336 337 @Test(expectedExceptions=NullPointerException.class) 338 public void factory_parse_formatter_nullText() { 339 DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); 340 Year.parse((String) null, f); 341 } 342 343 @Test(expectedExceptions=NullPointerException.class) 344 public void factory_parse_formatter_nullFormatter() { 345 Year.parse("ANY", null); 346 } 347 348 //----------------------------------------------------------------------- 349 // get(TemporalField) 350 //----------------------------------------------------------------------- 351 @Test 352 public void test_get_TemporalField() { 353 assertEquals(TEST_2008.get(ChronoField.YEAR), 2008); 354 assertEquals(TEST_2008.get(ChronoField.YEAR_OF_ERA), 2008); 355 assertEquals(TEST_2008.get(ChronoField.ERA), 1); 356 } 357 358 @Test 359 public void test_getLong_TemporalField() { 360 assertEquals(TEST_2008.getLong(ChronoField.YEAR), 2008); 361 assertEquals(TEST_2008.getLong(ChronoField.YEAR_OF_ERA), 2008); 362 assertEquals(TEST_2008.getLong(ChronoField.ERA), 1); 363 } 364 365 //----------------------------------------------------------------------- 366 // query(TemporalQuery) 367 //----------------------------------------------------------------------- 368 @DataProvider(name="query") 369 Object[][] data_query() { 370 return new Object[][] { 371 {TEST_2008, TemporalQuery.chronology(), IsoChronology.INSTANCE}, 372 {TEST_2008, TemporalQuery.zoneId(), null}, 373 {TEST_2008, TemporalQuery.precision(), ChronoUnit.YEARS}, 374 {TEST_2008, TemporalQuery.zone(), null}, 375 {TEST_2008, TemporalQuery.offset(), null}, 376 {TEST_2008, TemporalQuery.localDate(), null}, 377 {TEST_2008, TemporalQuery.localTime(), null}, 378 }; 379 } 380 381 @Test(dataProvider="query") 382 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 383 assertEquals(temporal.query(query), expected); 384 } 385 386 @Test(dataProvider="query") 387 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 388 assertEquals(query.queryFrom(temporal), expected); 389 } 390 391 @Test(expectedExceptions=NullPointerException.class) 392 public void test_query_null() { 393 TEST_2008.query(null); 394 } 395 396 //----------------------------------------------------------------------- 397 // isLeap() 398 //----------------------------------------------------------------------- 399 @Test 400 public void test_isLeap() { 401 assertEquals(Year.of(1999).isLeap(), false); 402 assertEquals(Year.of(2000).isLeap(), true); 403 assertEquals(Year.of(2001).isLeap(), false); 404 405 assertEquals(Year.of(2007).isLeap(), false); 406 assertEquals(Year.of(2008).isLeap(), true); 407 assertEquals(Year.of(2009).isLeap(), false); 408 assertEquals(Year.of(2010).isLeap(), false); 409 assertEquals(Year.of(2011).isLeap(), false); 410 assertEquals(Year.of(2012).isLeap(), true); 411 412 assertEquals(Year.of(2095).isLeap(), false); 413 assertEquals(Year.of(2096).isLeap(), true); 414 assertEquals(Year.of(2097).isLeap(), false); 415 assertEquals(Year.of(2098).isLeap(), false); 416 assertEquals(Year.of(2099).isLeap(), false); 417 assertEquals(Year.of(2100).isLeap(), false); 418 assertEquals(Year.of(2101).isLeap(), false); 419 assertEquals(Year.of(2102).isLeap(), false); 420 assertEquals(Year.of(2103).isLeap(), false); 421 assertEquals(Year.of(2104).isLeap(), true); 422 assertEquals(Year.of(2105).isLeap(), false); 423 424 assertEquals(Year.of(-500).isLeap(), false); 425 assertEquals(Year.of(-400).isLeap(), true); 426 assertEquals(Year.of(-300).isLeap(), false); 427 assertEquals(Year.of(-200).isLeap(), false); 428 assertEquals(Year.of(-100).isLeap(), false); 429 assertEquals(Year.of(0).isLeap(), true); 430 assertEquals(Year.of(100).isLeap(), false); 431 assertEquals(Year.of(200).isLeap(), false); 432 assertEquals(Year.of(300).isLeap(), false); 433 assertEquals(Year.of(400).isLeap(), true); 434 assertEquals(Year.of(500).isLeap(), false); 435 } 436 437 //----------------------------------------------------------------------- 438 // plus(Period) 439 //----------------------------------------------------------------------- 440 @DataProvider(name="plusValid") 441 Object[][] data_plusValid() { 442 return new Object[][] { 443 {2012, Period.ofYears(0), 2012}, 444 {2012, Period.ofYears(1), 2013}, 445 {2012, Period.ofYears(2), 2014}, 446 {2012, Period.ofYears(-2), 2010}, 447 }; 448 } 449 450 @Test(dataProvider="plusValid") 451 public void test_plusValid(int year, TemporalAmount amount, int expected) { 452 assertEquals(Year.of(year).plus(amount), Year.of(expected)); 453 } 454 455 @DataProvider(name="plusInvalidUnit") 456 Object[][] data_plusInvalidUnit() { 457 return new Object[][] { 458 {Period.of(0, 1, 0)}, 459 {Period.of(0, 0, 1)}, 460 {Period.of(0, 1, 1)}, 461 {Period.of(1, 1, 1)}, 462 {Duration.ofDays(1)}, 463 {Duration.ofHours(1)}, 464 {Duration.ofMinutes(1)}, 465 {Duration.ofSeconds(1)}, 466 }; 467 } 468 469 @Test(dataProvider="plusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class) 470 public void test_plusInvalidUnit(TemporalAmount amount) { 471 TEST_2008.plus(amount); 472 } 473 474 @Test(expectedExceptions=NullPointerException.class) 475 public void test_plus_null() { 476 TEST_2008.plus(null); 477 } 478 479 //----------------------------------------------------------------------- 480 // plusYears() 481 //----------------------------------------------------------------------- 482 @Test 483 public void test_plusYears() { 484 assertEquals(Year.of(2007).plusYears(-1), Year.of(2006)); 485 assertEquals(Year.of(2007).plusYears(0), Year.of(2007)); 486 assertEquals(Year.of(2007).plusYears(1), Year.of(2008)); 487 assertEquals(Year.of(2007).plusYears(2), Year.of(2009)); 488 489 assertEquals(Year.of(Year.MAX_VALUE - 1).plusYears(1), Year.of(Year.MAX_VALUE)); 490 assertEquals(Year.of(Year.MAX_VALUE).plusYears(0), Year.of(Year.MAX_VALUE)); 491 492 assertEquals(Year.of(Year.MIN_VALUE + 1).plusYears(-1), Year.of(Year.MIN_VALUE)); 493 assertEquals(Year.of(Year.MIN_VALUE).plusYears(0), Year.of(Year.MIN_VALUE)); 494 } 495 496 @Test 497 public void test_plusYear_zero_equals() { 498 Year base = Year.of(2007); 499 assertEquals(base.plusYears(0), base); 500 } 501 502 @Test 503 public void test_plusYears_big() { 504 long years = 20L + Year.MAX_VALUE; 505 assertEquals(Year.of(-40).plusYears(years), Year.of((int) (-40L + years))); 506 } 507 508 @Test(expectedExceptions=DateTimeException.class) 509 public void test_plusYears_max() { 510 Year.of(Year.MAX_VALUE).plusYears(1); 511 } 512 513 @Test(expectedExceptions=DateTimeException.class) 514 public void test_plusYears_maxLots() { 515 Year.of(Year.MAX_VALUE).plusYears(1000); 516 } 517 518 @Test(expectedExceptions=DateTimeException.class) 519 public void test_plusYears_min() { 520 Year.of(Year.MIN_VALUE).plusYears(-1); 521 } 522 523 @Test(expectedExceptions=DateTimeException.class) 524 public void test_plusYears_minLots() { 525 Year.of(Year.MIN_VALUE).plusYears(-1000); 526 } 527 528 //----------------------------------------------------------------------- 529 // minus(Period) 530 //----------------------------------------------------------------------- 531 @DataProvider(name="minusValid") 532 Object[][] data_minusValid() { 533 return new Object[][] { 534 {2012, Period.ofYears(0), 2012}, 535 {2012, Period.ofYears(1), 2011}, 536 {2012, Period.ofYears(2), 2010}, 537 {2012, Period.ofYears(-2), 2014}, 538 }; 539 } 540 541 @Test(dataProvider="minusValid") 542 public void test_minusValid(int year, TemporalAmount amount, int expected) { 543 assertEquals(Year.of(year).minus(amount), Year.of(expected)); 544 } 545 546 @DataProvider(name="minusInvalidUnit") 547 Object[][] data_minusInvalidUnit() { 548 return new Object[][] { 549 {Period.of(0, 1, 0)}, 550 {Period.of(0, 0, 1)}, 551 {Period.of(0, 1, 1)}, 552 {Period.of(1, 1, 1)}, 553 {Duration.ofDays(1)}, 554 {Duration.ofHours(1)}, 555 {Duration.ofMinutes(1)}, 556 {Duration.ofSeconds(1)}, 557 }; 558 } 559 560 @Test(dataProvider="minusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class) 561 public void test_minusInvalidUnit(TemporalAmount amount) { 562 TEST_2008.minus(amount); 563 } 564 565 @Test(expectedExceptions=NullPointerException.class) 566 public void test_minus_null() { 567 TEST_2008.minus(null); 568 } 569 570 //----------------------------------------------------------------------- 571 // minusYears() 572 //----------------------------------------------------------------------- 573 @Test 574 public void test_minusYears() { 575 assertEquals(Year.of(2007).minusYears(-1), Year.of(2008)); 576 assertEquals(Year.of(2007).minusYears(0), Year.of(2007)); 577 assertEquals(Year.of(2007).minusYears(1), Year.of(2006)); 578 assertEquals(Year.of(2007).minusYears(2), Year.of(2005)); 579 580 assertEquals(Year.of(Year.MAX_VALUE - 1).minusYears(-1), Year.of(Year.MAX_VALUE)); 581 assertEquals(Year.of(Year.MAX_VALUE).minusYears(0), Year.of(Year.MAX_VALUE)); 582 583 assertEquals(Year.of(Year.MIN_VALUE + 1).minusYears(1), Year.of(Year.MIN_VALUE)); 584 assertEquals(Year.of(Year.MIN_VALUE).minusYears(0), Year.of(Year.MIN_VALUE)); 585 } 586 587 @Test 588 public void test_minusYear_zero_equals() { 589 Year base = Year.of(2007); 590 assertEquals(base.minusYears(0), base); 591 } 592 593 @Test 594 public void test_minusYears_big() { 595 long years = 20L + Year.MAX_VALUE; 596 assertEquals(Year.of(40).minusYears(years), Year.of((int) (40L - years))); 597 } 598 599 @Test(expectedExceptions=DateTimeException.class) 600 public void test_minusYears_max() { 601 Year.of(Year.MAX_VALUE).minusYears(-1); 602 } 603 604 @Test(expectedExceptions=DateTimeException.class) 605 public void test_minusYears_maxLots() { 606 Year.of(Year.MAX_VALUE).minusYears(-1000); 607 } 608 609 @Test(expectedExceptions=DateTimeException.class) 610 public void test_minusYears_min() { 611 Year.of(Year.MIN_VALUE).minusYears(1); 612 } 613 614 @Test(expectedExceptions=DateTimeException.class) 615 public void test_minusYears_minLots() { 616 Year.of(Year.MIN_VALUE).minusYears(1000); 617 } 618 619 //----------------------------------------------------------------------- 620 // adjustInto() 621 //----------------------------------------------------------------------- 622 @Test 623 public void test_adjustDate() { 624 LocalDate base = LocalDate.of(2007, 2, 12); 625 for (int i = -4; i <= 2104; i++) { 626 Temporal result = Year.of(i).adjustInto(base); 627 assertEquals(result, LocalDate.of(i, 2, 12)); 628 } 629 } 630 631 @Test 632 public void test_adjustDate_resolve() { 633 Year test = Year.of(2011); 634 assertEquals(test.adjustInto(LocalDate.of(2012, 2, 29)), LocalDate.of(2011, 2, 28)); 635 } 636 637 @Test(expectedExceptions=NullPointerException.class) 638 public void test_adjustDate_nullLocalDate() { 639 Year test = Year.of(1); 640 test.adjustInto((LocalDate) null); 641 } 642 643 //----------------------------------------------------------------------- 644 // length() 645 //----------------------------------------------------------------------- 646 @Test 647 public void test_length() { 648 assertEquals(Year.of(1999).length(), 365); 649 assertEquals(Year.of(2000).length(), 366); 650 assertEquals(Year.of(2001).length(), 365); 651 652 assertEquals(Year.of(2007).length(), 365); 653 assertEquals(Year.of(2008).length(), 366); 654 assertEquals(Year.of(2009).length(), 365); 655 assertEquals(Year.of(2010).length(), 365); 656 assertEquals(Year.of(2011).length(), 365); 657 assertEquals(Year.of(2012).length(), 366); 658 659 assertEquals(Year.of(2095).length(), 365); 660 assertEquals(Year.of(2096).length(), 366); 661 assertEquals(Year.of(2097).length(), 365); 662 assertEquals(Year.of(2098).length(), 365); 663 assertEquals(Year.of(2099).length(), 365); 664 assertEquals(Year.of(2100).length(), 365); 665 assertEquals(Year.of(2101).length(), 365); 666 assertEquals(Year.of(2102).length(), 365); 667 assertEquals(Year.of(2103).length(), 365); 668 assertEquals(Year.of(2104).length(), 366); 669 assertEquals(Year.of(2105).length(), 365); 670 671 assertEquals(Year.of(-500).length(), 365); 672 assertEquals(Year.of(-400).length(), 366); 673 assertEquals(Year.of(-300).length(), 365); 674 assertEquals(Year.of(-200).length(), 365); 675 assertEquals(Year.of(-100).length(), 365); 676 assertEquals(Year.of(0).length(), 366); 677 assertEquals(Year.of(100).length(), 365); 678 assertEquals(Year.of(200).length(), 365); 679 assertEquals(Year.of(300).length(), 365); 680 assertEquals(Year.of(400).length(), 366); 681 assertEquals(Year.of(500).length(), 365); 682 } 683 684 //----------------------------------------------------------------------- 685 // isValidMonthDay(MonthDay) 686 //----------------------------------------------------------------------- 687 @DataProvider(name="isValidMonthDay") 688 Object[][] data_isValidMonthDay() { 689 return new Object[][] { 690 {Year.of(2007), MonthDay.of(6, 30), true}, 691 {Year.of(2008), MonthDay.of(2, 28), true}, 692 {Year.of(2008), MonthDay.of(2, 29), true}, 693 {Year.of(2009), MonthDay.of(2, 28), true}, 694 {Year.of(2009), MonthDay.of(2, 29), false}, 695 {Year.of(2009), null, false}, 696 }; 697 } 698 699 @Test(dataProvider="isValidMonthDay") 700 public void test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected) { 701 assertEquals(year.isValidMonthDay(monthDay), expected); 702 } 703 704 //----------------------------------------------------------------------- 705 // periodUntil(Temporal, TemporalUnit) 706 //----------------------------------------------------------------------- 707 @DataProvider(name="periodUntilUnit") 708 Object[][] data_periodUntilUnit() { 709 return new Object[][] { 710 {Year.of(2000), Year.of(-1), YEARS, -2001}, 711 {Year.of(2000), Year.of(0), YEARS, -2000}, 712 {Year.of(2000), Year.of(1), YEARS, -1999}, 713 {Year.of(2000), Year.of(1998), YEARS, -2}, 714 {Year.of(2000), Year.of(1999), YEARS, -1}, 715 {Year.of(2000), Year.of(2000), YEARS, 0}, 716 {Year.of(2000), Year.of(2001), YEARS, 1}, 717 {Year.of(2000), Year.of(2002), YEARS, 2}, 718 {Year.of(2000), Year.of(2246), YEARS, 246}, 719 720 {Year.of(2000), Year.of(-1), DECADES, -200}, 721 {Year.of(2000), Year.of(0), DECADES, -200}, 722 {Year.of(2000), Year.of(1), DECADES, -199}, 723 {Year.of(2000), Year.of(1989), DECADES, -1}, 724 {Year.of(2000), Year.of(1990), DECADES, -1}, 725 {Year.of(2000), Year.of(1991), DECADES, 0}, 726 {Year.of(2000), Year.of(2000), DECADES, 0}, 727 {Year.of(2000), Year.of(2009), DECADES, 0}, 728 {Year.of(2000), Year.of(2010), DECADES, 1}, 729 {Year.of(2000), Year.of(2011), DECADES, 1}, 730 731 {Year.of(2000), Year.of(-1), CENTURIES, -20}, 732 {Year.of(2000), Year.of(0), CENTURIES, -20}, 733 {Year.of(2000), Year.of(1), CENTURIES, -19}, 734 {Year.of(2000), Year.of(1899), CENTURIES, -1}, 735 {Year.of(2000), Year.of(1900), CENTURIES, -1}, 736 {Year.of(2000), Year.of(1901), CENTURIES, 0}, 737 {Year.of(2000), Year.of(2000), CENTURIES, 0}, 738 {Year.of(2000), Year.of(2099), CENTURIES, 0}, 739 {Year.of(2000), Year.of(2100), CENTURIES, 1}, 740 {Year.of(2000), Year.of(2101), CENTURIES, 1}, 741 742 {Year.of(2000), Year.of(-1), MILLENNIA, -2}, 743 {Year.of(2000), Year.of(0), MILLENNIA, -2}, 744 {Year.of(2000), Year.of(1), MILLENNIA, -1}, 745 {Year.of(2000), Year.of(999), MILLENNIA, -1}, 746 {Year.of(2000), Year.of(1000), MILLENNIA, -1}, 747 {Year.of(2000), Year.of(1001), MILLENNIA, 0}, 748 {Year.of(2000), Year.of(2000), MILLENNIA, 0}, 749 {Year.of(2000), Year.of(2999), MILLENNIA, 0}, 750 {Year.of(2000), Year.of(3000), MILLENNIA, 1}, 751 {Year.of(2000), Year.of(3001), MILLENNIA, 1}, 752 }; 753 } 754 755 @Test(dataProvider="periodUntilUnit") 756 public void test_periodUntil_TemporalUnit(Year year1, Year year2, TemporalUnit unit, long expected) { 757 long amount = year1.periodUntil(year2, unit); 758 assertEquals(amount, expected); 759 } 760 761 @Test(dataProvider="periodUntilUnit") 762 public void test_periodUntil_TemporalUnit_negated(Year year1, Year year2, TemporalUnit unit, long expected) { 763 long amount = year2.periodUntil(year1, unit); 764 assertEquals(amount, -expected); 765 } 766 767 @Test(expectedExceptions = UnsupportedTemporalTypeException.class) 768 public void test_periodUntil_TemporalUnit_unsupportedUnit() { 769 TEST_2008.periodUntil(TEST_2008, MONTHS); 770 } 771 772 @Test(expectedExceptions = NullPointerException.class) 773 public void test_periodUntil_TemporalUnit_nullEnd() { 774 TEST_2008.periodUntil(null, DAYS); 775 } 776 777 @Test(expectedExceptions = NullPointerException.class) 778 public void test_periodUntil_TemporalUnit_nullUnit() { 779 TEST_2008.periodUntil(TEST_2008, null); 780 } 781 782 //----------------------------------------------------------------------- 783 // format(DateTimeFormatter) 784 //----------------------------------------------------------------------- 785 @Test 786 public void test_format_formatter() { 787 DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); 788 String t = Year.of(2010).format(f); 789 assertEquals(t, "2010"); 790 } 791 792 @Test(expectedExceptions=NullPointerException.class) 793 public void test_format_formatter_null() { 794 Year.of(2010).format(null); 795 } 796 797 //----------------------------------------------------------------------- 798 // atMonth(Month) 799 //----------------------------------------------------------------------- 800 @Test 801 public void test_atMonth() { 802 Year test = Year.of(2008); 803 assertEquals(test.atMonth(Month.JUNE), YearMonth.of(2008, 6)); 804 } 805 806 @Test(expectedExceptions=NullPointerException.class) 807 public void test_atMonth_nullMonth() { 808 Year test = Year.of(2008); 809 test.atMonth((Month) null); 810 } 811 812 //----------------------------------------------------------------------- 813 // atMonth(int) 814 //----------------------------------------------------------------------- 815 @Test 816 public void test_atMonth_int() { 817 Year test = Year.of(2008); 818 assertEquals(test.atMonth(6), YearMonth.of(2008, 6)); 819 } 820 821 @Test(expectedExceptions=DateTimeException.class) 822 public void test_atMonth_int_invalidMonth() { 823 Year test = Year.of(2008); 824 test.atMonth(13); 825 } 826 827 //----------------------------------------------------------------------- 828 // atMonthDay(MonthDay) 829 //----------------------------------------------------------------------- 830 @DataProvider(name="atMonthDay") 831 Object[][] data_atMonthDay() { 832 return new Object[][] { 833 {Year.of(2008), MonthDay.of(6, 30), LocalDate.of(2008, 6, 30)}, 834 {Year.of(2008), MonthDay.of(2, 29), LocalDate.of(2008, 2, 29)}, 835 {Year.of(2009), MonthDay.of(2, 29), LocalDate.of(2009, 2, 28)}, 836 }; 837 } 838 839 @Test(dataProvider="atMonthDay") 840 public void test_atMonthDay(Year year, MonthDay monthDay, LocalDate expected) { 841 assertEquals(year.atMonthDay(monthDay), expected); 842 } 843 844 @Test(expectedExceptions=NullPointerException.class) 845 public void test_atMonthDay_nullMonthDay() { 846 Year test = Year.of(2008); 847 test.atMonthDay((MonthDay) null); 848 } 849 850 //----------------------------------------------------------------------- 851 // atDay(int) 852 //----------------------------------------------------------------------- 853 @Test 854 public void test_atDay_notLeapYear() { 855 Year test = Year.of(2007); 856 LocalDate expected = LocalDate.of(2007, 1, 1); 857 for (int i = 1; i <= 365; i++) { 858 assertEquals(test.atDay(i), expected); 859 expected = expected.plusDays(1); 860 } 861 } 862 863 @Test(expectedExceptions=DateTimeException.class) 864 public void test_atDay_notLeapYear_day366() { 865 Year test = Year.of(2007); 866 test.atDay(366); 867 } 868 869 @Test 870 public void test_atDay_leapYear() { 871 Year test = Year.of(2008); 872 LocalDate expected = LocalDate.of(2008, 1, 1); 873 for (int i = 1; i <= 366; i++) { 874 assertEquals(test.atDay(i), expected); 875 expected = expected.plusDays(1); 876 } 877 } 878 879 @Test(expectedExceptions=DateTimeException.class) 880 public void test_atDay_day0() { 881 Year test = Year.of(2007); 882 test.atDay(0); 883 } 884 885 @Test(expectedExceptions=DateTimeException.class) 886 public void test_atDay_day367() { 887 Year test = Year.of(2007); 888 test.atDay(367); 889 } 890 891 //----------------------------------------------------------------------- 892 // compareTo() 893 //----------------------------------------------------------------------- 894 @Test 895 public void test_compareTo() { 896 for (int i = -4; i <= 2104; i++) { 897 Year a = Year.of(i); 898 for (int j = -4; j <= 2104; j++) { 899 Year b = Year.of(j); 900 if (i < j) { 901 assertEquals(a.compareTo(b) < 0, true); 902 assertEquals(b.compareTo(a) > 0, true); 903 assertEquals(a.isAfter(b), false); 904 assertEquals(a.isBefore(b), true); 905 assertEquals(b.isAfter(a), true); 906 assertEquals(b.isBefore(a), false); 907 } else if (i > j) { 908 assertEquals(a.compareTo(b) > 0, true); 909 assertEquals(b.compareTo(a) < 0, true); 910 assertEquals(a.isAfter(b), true); 911 assertEquals(a.isBefore(b), false); 912 assertEquals(b.isAfter(a), false); 913 assertEquals(b.isBefore(a), true); 914 } else { 915 assertEquals(a.compareTo(b), 0); 916 assertEquals(b.compareTo(a), 0); 917 assertEquals(a.isAfter(b), false); 918 assertEquals(a.isBefore(b), false); 919 assertEquals(b.isAfter(a), false); 920 assertEquals(b.isBefore(a), false); 921 } 922 } 923 } 924 } 925 926 @Test(expectedExceptions=NullPointerException.class) 927 public void test_compareTo_nullYear() { 928 Year doy = null; 929 Year test = Year.of(1); 930 test.compareTo(doy); 931 } 932 933 //----------------------------------------------------------------------- 934 // equals() / hashCode() 935 //----------------------------------------------------------------------- 936 @Test 937 public void test_equals() { 938 for (int i = -4; i <= 2104; i++) { 939 Year a = Year.of(i); 940 for (int j = -4; j <= 2104; j++) { 941 Year b = Year.of(j); 942 assertEquals(a.equals(b), i == j); 943 assertEquals(a.hashCode() == b.hashCode(), i == j); 944 } 945 } 946 } 947 948 @Test 949 public void test_equals_same() { 950 Year test = Year.of(2011); 951 assertEquals(test.equals(test), true); 952 } 953 954 @Test 955 public void test_equals_nullYear() { 956 Year doy = null; 957 Year test = Year.of(1); 958 assertEquals(test.equals(doy), false); 959 } 960 961 @Test 962 public void test_equals_incorrectType() { 963 Year test = Year.of(1); 964 assertEquals(test.equals("Incorrect type"), false); 965 } 966 967 //----------------------------------------------------------------------- 968 // toString() 969 //----------------------------------------------------------------------- 970 @Test 971 public void test_toString() { 972 for (int i = -4; i <= 2104; i++) { 973 Year a = Year.of(i); 974 assertEquals(a.toString(), "" + i); 975 } 976 } 977 978 }