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.temporal; 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 org.testng.Assert.assertEquals; 66 import static org.testng.Assert.fail; 67 68 import java.io.ByteArrayOutputStream; 69 import java.io.DataOutputStream; 70 import java.io.IOException; 71 import java.util.ArrayList; 72 import java.util.Arrays; 73 import java.util.List; 74 75 import java.time.Clock; 76 import java.time.DateTimeException; 77 import java.time.Instant; 78 import java.time.LocalDate; 79 import java.time.LocalTime; 80 import java.time.Month; 81 import java.time.ZoneId; 82 import java.time.ZoneOffset; 83 import java.time.format.DateTimeFormatter; 84 import java.time.format.DateTimeFormatters; 85 import java.time.format.DateTimeParseException; 86 import java.time.temporal.ChronoField; 87 import java.time.temporal.JulianFields; 88 import java.time.temporal.MonthDay; 89 import java.time.temporal.OffsetDateTime; 90 import java.time.temporal.Temporal; 91 import java.time.temporal.TemporalAccessor; 92 import java.time.temporal.TemporalField; 93 import java.time.temporal.Year; 94 import java.time.temporal.YearMonth; 95 96 import org.testng.annotations.BeforeMethod; 97 import org.testng.annotations.DataProvider; 98 import org.testng.annotations.Test; 99 import tck.java.time.AbstractDateTimeTest; 100 101 /** 102 * Test Year. 103 */ 104 @Test 105 public class TCKYear extends AbstractDateTimeTest { 106 107 private static final Year TEST_2008 = Year.of(2008); 108 109 @BeforeMethod 110 public void setUp() { 111 } 112 113 //----------------------------------------------------------------------- 114 @Override 115 protected List<TemporalAccessor> samples() { 116 TemporalAccessor[] array = {TEST_2008, }; 117 return Arrays.asList(array); 118 } 119 120 @Override 121 protected List<TemporalField> validFields() { 122 TemporalField[] array = { 123 YEAR_OF_ERA, 124 YEAR, 125 ERA, 126 }; 127 return Arrays.asList(array); 128 } 129 130 @Override 131 protected List<TemporalField> invalidFields() { 132 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 133 list.removeAll(validFields()); 134 list.add(JulianFields.JULIAN_DAY); 135 list.add(JulianFields.MODIFIED_JULIAN_DAY); 136 list.add(JulianFields.RATA_DIE); 137 return list; 138 } 139 140 //----------------------------------------------------------------------- 141 @Test 142 public void test_serialization() throws Exception { 143 assertSerializable(Year.of(2)); 144 assertSerializable(Year.of(0)); 145 assertSerializable(Year.of(-2)); 146 } 147 148 @Test 149 public void test_serialization_format() throws Exception { 150 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 151 try (DataOutputStream dos = new DataOutputStream(baos) ) { 152 dos.writeByte(4); 153 dos.writeInt(2012); 154 } 155 byte[] bytes = baos.toByteArray(); 156 assertSerializedBySer(Year.of(2012), bytes); 157 } 158 159 //----------------------------------------------------------------------- 160 // now() 161 //----------------------------------------------------------------------- 162 @Test(groups={"tck"}) 163 public void now() { 164 Year expected = Year.now(Clock.systemDefaultZone()); 165 Year test = Year.now(); 166 for (int i = 0; i < 100; i++) { 167 if (expected.equals(test)) { 168 return; 169 } 170 expected = Year.now(Clock.systemDefaultZone()); 171 test = Year.now(); 172 } 173 assertEquals(test, expected); 174 } 175 176 //----------------------------------------------------------------------- 177 // now(ZoneId) 178 //----------------------------------------------------------------------- 179 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 180 public void now_ZoneId_nullZoneId() { 181 Year.now((ZoneId) null); 182 } 183 184 @Test(groups={"tck"}) 185 public void now_ZoneId() { 186 ZoneId zone = ZoneId.of("UTC+01:02:03"); 187 Year expected = Year.now(Clock.system(zone)); 188 Year test = Year.now(zone); 189 for (int i = 0; i < 100; i++) { 190 if (expected.equals(test)) { 191 return; 192 } 193 expected = Year.now(Clock.system(zone)); 194 test = Year.now(zone); 195 } 196 assertEquals(test, expected); 197 } 198 199 //----------------------------------------------------------------------- 200 // now(Clock) 201 //----------------------------------------------------------------------- 202 @Test(groups={"tck"}) 203 public void now_Clock() { 204 Instant instant = OffsetDateTime.of(LocalDate.of(2010, 12, 31), LocalTime.of(0, 0), ZoneOffset.UTC).toInstant(); 205 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 206 Year test = Year.now(clock); 207 assertEquals(test.getValue(), 2010); 208 } 209 210 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 211 public void now_Clock_nullClock() { 212 Year.now((Clock) null); 213 } 214 215 //----------------------------------------------------------------------- 216 @Test(groups={"tck"}) 217 public void test_factory_int_singleton() { 218 for (int i = -4; i <= 2104; i++) { 219 Year test = Year.of(i); 220 assertEquals(test.getValue(), i); 221 assertEquals(Year.of(i), test); 222 } 223 } 224 225 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 226 public void test_factory_int_tooLow() { 227 Year.of(Year.MIN_VALUE - 1); 228 } 229 230 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 231 public void test_factory_int_tooHigh() { 232 Year.of(Year.MAX_VALUE + 1); 233 } 234 235 //----------------------------------------------------------------------- 236 @Test(groups={"tck"}) 237 public void test_factory_CalendricalObject() { 238 assertEquals(Year.from(LocalDate.of(2007, 7, 15)), Year.of(2007)); 239 } 240 241 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 242 public void test_factory_CalendricalObject_invalid_noDerive() { 243 Year.from(LocalTime.of(12, 30)); 244 } 245 246 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 247 public void test_factory_CalendricalObject_null() { 248 Year.from((TemporalAccessor) null); 249 } 250 251 //----------------------------------------------------------------------- 252 // parse() 253 //----------------------------------------------------------------------- 254 @DataProvider(name="goodParseData") 255 Object[][] provider_goodParseData() { 256 return new Object[][] { 257 {"0000", Year.of(0)}, 258 {"9999", Year.of(9999)}, 259 {"2000", Year.of(2000)}, 260 261 {"+12345678", Year.of(12345678)}, 262 {"+123456", Year.of(123456)}, 263 {"-1234", Year.of(-1234)}, 264 {"-12345678", Year.of(-12345678)}, 265 266 {"+" + Year.MAX_VALUE, Year.of(Year.MAX_VALUE)}, 267 {"" + Year.MIN_VALUE, Year.of(Year.MIN_VALUE)}, 268 }; 269 } 270 271 @Test(dataProvider="goodParseData", groups={"tck"}) 272 public void factory_parse_success(String text, Year expected) { 273 Year year = Year.parse(text); 274 assertEquals(year, expected); 275 } 276 277 @DataProvider(name="badParseData") 278 Object[][] provider_badParseData() { 279 return new Object[][] { 280 {"", 0}, 281 {"-00", 1}, 282 {"--01-0", 1}, 283 {"A01", 0}, 284 {"200", 0}, 285 {"2009/12", 4}, 286 287 {"-0000-10", 0}, 288 {"-12345678901-10", 11}, 289 {"+1-10", 1}, 290 {"+12-10", 1}, 291 {"+123-10", 1}, 292 {"+1234-10", 0}, 293 {"12345-10", 0}, 294 {"+12345678901-10", 11}, 295 }; 296 } 297 298 @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class, groups={"tck"}) 299 public void factory_parse_fail(String text, int pos) { 300 try { 301 Year.parse(text); 302 fail(String.format("Parse should have failed for %s at position %d", text, pos)); 303 } catch (DateTimeParseException ex) { 304 assertEquals(ex.getParsedString(), text); 305 assertEquals(ex.getErrorIndex(), pos); 306 throw ex; 307 } 308 } 309 310 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 311 public void factory_parse_nullText() { 312 Year.parse(null); 313 } 314 315 //----------------------------------------------------------------------- 316 // parse(DateTimeFormatter) 317 //----------------------------------------------------------------------- 318 @Test(groups={"tck"}) 319 public void factory_parse_formatter() { 320 DateTimeFormatter f = DateTimeFormatters.pattern("y"); 321 Year test = Year.parse("2010", f); 322 assertEquals(test, Year.of(2010)); 323 } 324 325 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 326 public void factory_parse_formatter_nullText() { 327 DateTimeFormatter f = DateTimeFormatters.pattern("y"); 328 Year.parse((String) null, f); 329 } 330 331 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 332 public void factory_parse_formatter_nullFormatter() { 333 Year.parse("ANY", null); 334 } 335 336 //----------------------------------------------------------------------- 337 // get(TemporalField) 338 //----------------------------------------------------------------------- 339 @Test 340 public void test_get_TemporalField() { 341 assertEquals(TEST_2008.get(ChronoField.YEAR), 2008); 342 assertEquals(TEST_2008.get(ChronoField.YEAR_OF_ERA), 2008); 343 assertEquals(TEST_2008.get(ChronoField.ERA), 1); 344 } 345 346 @Test 347 public void test_getLong_TemporalField() { 348 assertEquals(TEST_2008.getLong(ChronoField.YEAR), 2008); 349 assertEquals(TEST_2008.getLong(ChronoField.YEAR_OF_ERA), 2008); 350 assertEquals(TEST_2008.getLong(ChronoField.ERA), 1); 351 } 352 353 //----------------------------------------------------------------------- 354 // isLeap() 355 //----------------------------------------------------------------------- 356 @Test(groups={"tck"}) 357 public void test_isLeap() { 358 assertEquals(Year.of(1999).isLeap(), false); 359 assertEquals(Year.of(2000).isLeap(), true); 360 assertEquals(Year.of(2001).isLeap(), false); 361 362 assertEquals(Year.of(2007).isLeap(), false); 363 assertEquals(Year.of(2008).isLeap(), true); 364 assertEquals(Year.of(2009).isLeap(), false); 365 assertEquals(Year.of(2010).isLeap(), false); 366 assertEquals(Year.of(2011).isLeap(), false); 367 assertEquals(Year.of(2012).isLeap(), true); 368 369 assertEquals(Year.of(2095).isLeap(), false); 370 assertEquals(Year.of(2096).isLeap(), true); 371 assertEquals(Year.of(2097).isLeap(), false); 372 assertEquals(Year.of(2098).isLeap(), false); 373 assertEquals(Year.of(2099).isLeap(), false); 374 assertEquals(Year.of(2100).isLeap(), false); 375 assertEquals(Year.of(2101).isLeap(), false); 376 assertEquals(Year.of(2102).isLeap(), false); 377 assertEquals(Year.of(2103).isLeap(), false); 378 assertEquals(Year.of(2104).isLeap(), true); 379 assertEquals(Year.of(2105).isLeap(), false); 380 381 assertEquals(Year.of(-500).isLeap(), false); 382 assertEquals(Year.of(-400).isLeap(), true); 383 assertEquals(Year.of(-300).isLeap(), false); 384 assertEquals(Year.of(-200).isLeap(), false); 385 assertEquals(Year.of(-100).isLeap(), false); 386 assertEquals(Year.of(0).isLeap(), true); 387 assertEquals(Year.of(100).isLeap(), false); 388 assertEquals(Year.of(200).isLeap(), false); 389 assertEquals(Year.of(300).isLeap(), false); 390 assertEquals(Year.of(400).isLeap(), true); 391 assertEquals(Year.of(500).isLeap(), false); 392 } 393 394 //----------------------------------------------------------------------- 395 // plusYears() 396 //----------------------------------------------------------------------- 397 @Test(groups={"tck"}) 398 public void test_plusYears() { 399 assertEquals(Year.of(2007).plusYears(-1), Year.of(2006)); 400 assertEquals(Year.of(2007).plusYears(0), Year.of(2007)); 401 assertEquals(Year.of(2007).plusYears(1), Year.of(2008)); 402 assertEquals(Year.of(2007).plusYears(2), Year.of(2009)); 403 404 assertEquals(Year.of(Year.MAX_VALUE - 1).plusYears(1), Year.of(Year.MAX_VALUE)); 405 assertEquals(Year.of(Year.MAX_VALUE).plusYears(0), Year.of(Year.MAX_VALUE)); 406 407 assertEquals(Year.of(Year.MIN_VALUE + 1).plusYears(-1), Year.of(Year.MIN_VALUE)); 408 assertEquals(Year.of(Year.MIN_VALUE).plusYears(0), Year.of(Year.MIN_VALUE)); 409 } 410 411 @Test(groups={"tck"}) 412 public void test_plusYear_zero_equals() { 413 Year base = Year.of(2007); 414 assertEquals(base.plusYears(0), base); 415 } 416 417 @Test(groups={"tck"}) 418 public void test_plusYears_big() { 419 long years = 20L + Year.MAX_VALUE; 420 assertEquals(Year.of(-40).plusYears(years), Year.of((int) (-40L + years))); 421 } 422 423 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 424 public void test_plusYears_max() { 425 Year.of(Year.MAX_VALUE).plusYears(1); 426 } 427 428 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 429 public void test_plusYears_maxLots() { 430 Year.of(Year.MAX_VALUE).plusYears(1000); 431 } 432 433 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 434 public void test_plusYears_min() { 435 Year.of(Year.MIN_VALUE).plusYears(-1); 436 } 437 438 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 439 public void test_plusYears_minLots() { 440 Year.of(Year.MIN_VALUE).plusYears(-1000); 441 } 442 443 //----------------------------------------------------------------------- 444 // minusYears() 445 //----------------------------------------------------------------------- 446 @Test(groups={"tck"}) 447 public void test_minusYears() { 448 assertEquals(Year.of(2007).minusYears(-1), Year.of(2008)); 449 assertEquals(Year.of(2007).minusYears(0), Year.of(2007)); 450 assertEquals(Year.of(2007).minusYears(1), Year.of(2006)); 451 assertEquals(Year.of(2007).minusYears(2), Year.of(2005)); 452 453 assertEquals(Year.of(Year.MAX_VALUE - 1).minusYears(-1), Year.of(Year.MAX_VALUE)); 454 assertEquals(Year.of(Year.MAX_VALUE).minusYears(0), Year.of(Year.MAX_VALUE)); 455 456 assertEquals(Year.of(Year.MIN_VALUE + 1).minusYears(1), Year.of(Year.MIN_VALUE)); 457 assertEquals(Year.of(Year.MIN_VALUE).minusYears(0), Year.of(Year.MIN_VALUE)); 458 } 459 460 @Test(groups={"tck"}) 461 public void test_minusYear_zero_equals() { 462 Year base = Year.of(2007); 463 assertEquals(base.minusYears(0), base); 464 } 465 466 @Test(groups={"tck"}) 467 public void test_minusYears_big() { 468 long years = 20L + Year.MAX_VALUE; 469 assertEquals(Year.of(40).minusYears(years), Year.of((int) (40L - years))); 470 } 471 472 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 473 public void test_minusYears_max() { 474 Year.of(Year.MAX_VALUE).minusYears(-1); 475 } 476 477 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 478 public void test_minusYears_maxLots() { 479 Year.of(Year.MAX_VALUE).minusYears(-1000); 480 } 481 482 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 483 public void test_minusYears_min() { 484 Year.of(Year.MIN_VALUE).minusYears(1); 485 } 486 487 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 488 public void test_minusYears_minLots() { 489 Year.of(Year.MIN_VALUE).minusYears(1000); 490 } 491 492 //----------------------------------------------------------------------- 493 // adjustInto() 494 //----------------------------------------------------------------------- 495 @Test(groups={"tck"}) 496 public void test_adjustDate() { 497 LocalDate base = LocalDate.of(2007, 2, 12); 498 for (int i = -4; i <= 2104; i++) { 499 Temporal result = Year.of(i).adjustInto(base); 500 assertEquals(result, LocalDate.of(i, 2, 12)); 501 } 502 } 503 504 @Test(groups={"tck"}) 505 public void test_adjustDate_resolve() { 506 Year test = Year.of(2011); 507 assertEquals(test.adjustInto(LocalDate.of(2012, 2, 29)), LocalDate.of(2011, 2, 28)); 508 } 509 510 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 511 public void test_adjustDate_nullLocalDate() { 512 Year test = Year.of(1); 513 test.adjustInto((LocalDate) null); 514 } 515 516 //----------------------------------------------------------------------- 517 // length() 518 //----------------------------------------------------------------------- 519 @Test(groups={"tck"}) 520 public void test_length() { 521 assertEquals(Year.of(1999).length(), 365); 522 assertEquals(Year.of(2000).length(), 366); 523 assertEquals(Year.of(2001).length(), 365); 524 525 assertEquals(Year.of(2007).length(), 365); 526 assertEquals(Year.of(2008).length(), 366); 527 assertEquals(Year.of(2009).length(), 365); 528 assertEquals(Year.of(2010).length(), 365); 529 assertEquals(Year.of(2011).length(), 365); 530 assertEquals(Year.of(2012).length(), 366); 531 532 assertEquals(Year.of(2095).length(), 365); 533 assertEquals(Year.of(2096).length(), 366); 534 assertEquals(Year.of(2097).length(), 365); 535 assertEquals(Year.of(2098).length(), 365); 536 assertEquals(Year.of(2099).length(), 365); 537 assertEquals(Year.of(2100).length(), 365); 538 assertEquals(Year.of(2101).length(), 365); 539 assertEquals(Year.of(2102).length(), 365); 540 assertEquals(Year.of(2103).length(), 365); 541 assertEquals(Year.of(2104).length(), 366); 542 assertEquals(Year.of(2105).length(), 365); 543 544 assertEquals(Year.of(-500).length(), 365); 545 assertEquals(Year.of(-400).length(), 366); 546 assertEquals(Year.of(-300).length(), 365); 547 assertEquals(Year.of(-200).length(), 365); 548 assertEquals(Year.of(-100).length(), 365); 549 assertEquals(Year.of(0).length(), 366); 550 assertEquals(Year.of(100).length(), 365); 551 assertEquals(Year.of(200).length(), 365); 552 assertEquals(Year.of(300).length(), 365); 553 assertEquals(Year.of(400).length(), 366); 554 assertEquals(Year.of(500).length(), 365); 555 } 556 557 //----------------------------------------------------------------------- 558 // isValidMonthDay(Month) 559 //----------------------------------------------------------------------- 560 @Test(groups={"tck"}) 561 public void test_isValidMonthDay_june() { 562 Year test = Year.of(2007); 563 MonthDay monthDay = MonthDay.of(6, 30); 564 assertEquals(test.isValidMonthDay(monthDay), true); 565 } 566 567 @Test(groups={"tck"}) 568 public void test_isValidMonthDay_febNonLeap() { 569 Year test = Year.of(2007); 570 MonthDay monthDay = MonthDay.of(2, 29); 571 assertEquals(test.isValidMonthDay(monthDay), false); 572 } 573 574 @Test(groups={"tck"}) 575 public void test_isValidMonthDay_febLeap() { 576 Year test = Year.of(2008); 577 MonthDay monthDay = MonthDay.of(2, 29); 578 assertEquals(test.isValidMonthDay(monthDay), true); 579 } 580 581 @Test(groups={"tck"}) 582 public void test_isValidMonthDay_null() { 583 Year test = Year.of(2008); 584 assertEquals(test.isValidMonthDay(null), false); 585 } 586 587 //----------------------------------------------------------------------- 588 // atMonth(Month) 589 //----------------------------------------------------------------------- 590 @Test(groups={"tck"}) 591 public void test_atMonth() { 592 Year test = Year.of(2008); 593 assertEquals(test.atMonth(Month.JUNE), YearMonth.of(2008, 6)); 594 } 595 596 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 597 public void test_atMonth_nullMonth() { 598 Year test = Year.of(2008); 599 test.atMonth((Month) null); 600 } 601 602 //----------------------------------------------------------------------- 603 // atMonth(int) 604 //----------------------------------------------------------------------- 605 @Test(groups={"tck"}) 606 public void test_atMonth_int() { 607 Year test = Year.of(2008); 608 assertEquals(test.atMonth(6), YearMonth.of(2008, 6)); 609 } 610 611 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 612 public void test_atMonth_int_invalidMonth() { 613 Year test = Year.of(2008); 614 test.atMonth(13); 615 } 616 617 //----------------------------------------------------------------------- 618 // atMonthDay(Month) 619 //----------------------------------------------------------------------- 620 @Test(groups={"tck"}) 621 public void test_atMonthDay() { 622 Year test = Year.of(2008); 623 assertEquals(test.atMonthDay(MonthDay.of(6, 30)), LocalDate.of(2008, 6, 30)); 624 } 625 626 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 627 public void test_atMonthDay_nullMonthDay() { 628 Year test = Year.of(2008); 629 test.atMonthDay((MonthDay) null); 630 } 631 632 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 633 public void test_atMonthDay_invalidMonthDay() { 634 Year test = Year.of(2008); 635 test.atMonthDay(MonthDay.of(6, 31)); 636 } 637 638 //----------------------------------------------------------------------- 639 // atDay(int) 640 //----------------------------------------------------------------------- 641 @Test(groups={"tck"}) 642 public void test_atDay_notLeapYear() { 643 Year test = Year.of(2007); 644 LocalDate expected = LocalDate.of(2007, 1, 1); 645 for (int i = 1; i <= 365; i++) { 646 assertEquals(test.atDay(i), expected); 647 expected = expected.plusDays(1); 648 } 649 } 650 651 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 652 public void test_atDay_notLeapYear_day366() { 653 Year test = Year.of(2007); 654 test.atDay(366); 655 } 656 657 @Test(groups={"tck"}) 658 public void test_atDay_leapYear() { 659 Year test = Year.of(2008); 660 LocalDate expected = LocalDate.of(2008, 1, 1); 661 for (int i = 1; i <= 366; i++) { 662 assertEquals(test.atDay(i), expected); 663 expected = expected.plusDays(1); 664 } 665 } 666 667 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 668 public void test_atDay_day0() { 669 Year test = Year.of(2007); 670 test.atDay(0); 671 } 672 673 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 674 public void test_atDay_day367() { 675 Year test = Year.of(2007); 676 test.atDay(367); 677 } 678 679 //----------------------------------------------------------------------- 680 // compareTo() 681 //----------------------------------------------------------------------- 682 @Test(groups={"tck"}) 683 public void test_compareTo() { 684 for (int i = -4; i <= 2104; i++) { 685 Year a = Year.of(i); 686 for (int j = -4; j <= 2104; j++) { 687 Year b = Year.of(j); 688 if (i < j) { 689 assertEquals(a.compareTo(b) < 0, true); 690 assertEquals(b.compareTo(a) > 0, true); 691 assertEquals(a.isAfter(b), false); 692 assertEquals(a.isBefore(b), true); 693 assertEquals(b.isAfter(a), true); 694 assertEquals(b.isBefore(a), false); 695 } else if (i > j) { 696 assertEquals(a.compareTo(b) > 0, true); 697 assertEquals(b.compareTo(a) < 0, true); 698 assertEquals(a.isAfter(b), true); 699 assertEquals(a.isBefore(b), false); 700 assertEquals(b.isAfter(a), false); 701 assertEquals(b.isBefore(a), true); 702 } else { 703 assertEquals(a.compareTo(b), 0); 704 assertEquals(b.compareTo(a), 0); 705 assertEquals(a.isAfter(b), false); 706 assertEquals(a.isBefore(b), false); 707 assertEquals(b.isAfter(a), false); 708 assertEquals(b.isBefore(a), false); 709 } 710 } 711 } 712 } 713 714 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 715 public void test_compareTo_nullYear() { 716 Year doy = null; 717 Year test = Year.of(1); 718 test.compareTo(doy); 719 } 720 721 //----------------------------------------------------------------------- 722 // equals() / hashCode() 723 //----------------------------------------------------------------------- 724 @Test(groups={"tck"}) 725 public void test_equals() { 726 for (int i = -4; i <= 2104; i++) { 727 Year a = Year.of(i); 728 for (int j = -4; j <= 2104; j++) { 729 Year b = Year.of(j); 730 assertEquals(a.equals(b), i == j); 731 assertEquals(a.hashCode() == b.hashCode(), i == j); 732 } 733 } 734 } 735 736 @Test(groups={"tck"}) 737 public void test_equals_same() { 738 Year test = Year.of(2011); 739 assertEquals(test.equals(test), true); 740 } 741 742 @Test(groups={"tck"}) 743 public void test_equals_nullYear() { 744 Year doy = null; 745 Year test = Year.of(1); 746 assertEquals(test.equals(doy), false); 747 } 748 749 @Test(groups={"tck"}) 750 public void test_equals_incorrectType() { 751 Year test = Year.of(1); 752 assertEquals(test.equals("Incorrect type"), false); 753 } 754 755 //----------------------------------------------------------------------- 756 // toString() 757 //----------------------------------------------------------------------- 758 @Test(groups={"tck"}) 759 public void test_toString() { 760 for (int i = -4; i <= 2104; i++) { 761 Year a = Year.of(i); 762 assertEquals(a.toString(), "" + i); 763 } 764 } 765 766 //----------------------------------------------------------------------- 767 // toString(DateTimeFormatter) 768 //----------------------------------------------------------------------- 769 @Test(groups={"tck"}) 770 public void test_toString_formatter() { 771 DateTimeFormatter f = DateTimeFormatters.pattern("y"); 772 String t = Year.of(2010).toString(f); 773 assertEquals(t, "2010"); 774 } 775 776 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 777 public void test_toString_formatter_null() { 778 Year.of(2010).toString(null); 779 } 780 781 }