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.EPOCH_MONTH; 63 import static java.time.temporal.ChronoField.ERA; 64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 65 import static java.time.temporal.ChronoField.YEAR; 66 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 67 import static org.testng.Assert.assertEquals; 68 import static org.testng.Assert.assertTrue; 69 import static org.testng.Assert.fail; 70 71 import java.io.ByteArrayOutputStream; 72 import java.io.DataOutputStream; 73 import java.io.IOException; 74 import java.util.ArrayList; 75 import java.util.Arrays; 76 import java.util.HashSet; 77 import java.util.List; 78 import java.util.Set; 79 80 import java.time.Clock; 81 import java.time.DateTimeException; 82 import java.time.Instant; 83 import java.time.LocalDate; 84 import java.time.LocalDateTime; 85 import java.time.LocalTime; 86 import java.time.Month; 87 import java.time.ZoneId; 88 import java.time.ZoneOffset; 89 import java.time.format.DateTimeFormatter; 90 import java.time.format.DateTimeFormatters; 91 import java.time.format.DateTimeParseException; 92 import java.time.temporal.ChronoField; 93 import java.time.temporal.JulianFields; 94 import java.time.temporal.TemporalAccessor; 95 import java.time.temporal.TemporalField; 96 import java.time.temporal.Year; 97 import java.time.temporal.YearMonth; 98 99 import org.testng.annotations.BeforeMethod; 100 import org.testng.annotations.DataProvider; 101 import org.testng.annotations.Test; 102 import tck.java.time.AbstractDateTimeTest; 103 104 /** 105 * Test YearMonth. 106 */ 107 @Test 108 public class TCKYearMonth extends AbstractDateTimeTest { 109 110 private YearMonth TEST_2008_06; 111 112 @BeforeMethod(groups={"tck", "implementation"}) 113 public void setUp() { 114 TEST_2008_06 = YearMonth.of(2008, 6); 115 } 116 117 //----------------------------------------------------------------------- 118 @Override 119 protected List<TemporalAccessor> samples() { 120 TemporalAccessor[] array = {TEST_2008_06, }; 121 return Arrays.asList(array); 122 } 123 124 @Override 125 protected List<TemporalField> validFields() { 126 TemporalField[] array = { 127 MONTH_OF_YEAR, 128 EPOCH_MONTH, 129 YEAR_OF_ERA, 130 YEAR, 131 ERA, 132 }; 133 return Arrays.asList(array); 134 } 135 136 @Override 137 protected List<TemporalField> invalidFields() { 138 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 139 list.removeAll(validFields()); 140 list.add(JulianFields.JULIAN_DAY); 141 list.add(JulianFields.MODIFIED_JULIAN_DAY); 142 list.add(JulianFields.RATA_DIE); 143 return list; 144 } 145 146 //----------------------------------------------------------------------- 147 @Test 148 public void test_serialization() throws IOException, ClassNotFoundException { 149 assertSerializable(TEST_2008_06); 150 } 151 152 @Test 153 public void test_serialization_format() throws Exception { 154 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 155 try (DataOutputStream dos = new DataOutputStream(baos) ) { 156 dos.writeByte(5); 157 dos.writeInt(2012); 158 dos.writeByte(9); 159 } 160 byte[] bytes = baos.toByteArray(); 161 assertSerializedBySer(YearMonth.of(2012, 9), bytes); 162 } 163 164 //----------------------------------------------------------------------- 165 void check(YearMonth test, int y, int m) { 166 assertEquals(test.getYear(), y); 167 assertEquals(test.getMonth().getValue(), m); 168 } 169 170 //----------------------------------------------------------------------- 171 // now() 172 //----------------------------------------------------------------------- 173 @Test(groups={"tck"}) 174 public void now() { 175 YearMonth expected = YearMonth.now(Clock.systemDefaultZone()); 176 YearMonth test = YearMonth.now(); 177 for (int i = 0; i < 100; i++) { 178 if (expected.equals(test)) { 179 return; 180 } 181 expected = YearMonth.now(Clock.systemDefaultZone()); 182 test = YearMonth.now(); 183 } 184 assertEquals(test, expected); 185 } 186 187 //----------------------------------------------------------------------- 188 // now(ZoneId) 189 //----------------------------------------------------------------------- 190 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 191 public void now_ZoneId_nullZoneId() { 192 YearMonth.now((ZoneId) null); 193 } 194 195 @Test(groups={"tck"}) 196 public void now_ZoneId() { 197 ZoneId zone = ZoneId.of("UTC+01:02:03"); 198 YearMonth expected = YearMonth.now(Clock.system(zone)); 199 YearMonth test = YearMonth.now(zone); 200 for (int i = 0; i < 100; i++) { 201 if (expected.equals(test)) { 202 return; 203 } 204 expected = YearMonth.now(Clock.system(zone)); 205 test = YearMonth.now(zone); 206 } 207 assertEquals(test, expected); 208 } 209 210 //----------------------------------------------------------------------- 211 // now(Clock) 212 //----------------------------------------------------------------------- 213 @Test(groups={"tck"}) 214 public void now_Clock() { 215 Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC); 216 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 217 YearMonth test = YearMonth.now(clock); 218 assertEquals(test.getYear(), 2010); 219 assertEquals(test.getMonth(), Month.DECEMBER); 220 } 221 222 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 223 public void now_Clock_nullClock() { 224 YearMonth.now((Clock) null); 225 } 226 227 //----------------------------------------------------------------------- 228 @Test(groups={"tck"}) 229 public void factory_intsMonth() { 230 YearMonth test = YearMonth.of(2008, Month.FEBRUARY); 231 check(test, 2008, 2); 232 } 233 234 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 235 public void test_factory_intsMonth_yearTooLow() { 236 YearMonth.of(Year.MIN_VALUE - 1, Month.JANUARY); 237 } 238 239 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 240 public void test_factory_intsMonth_dayTooHigh() { 241 YearMonth.of(Year.MAX_VALUE + 1, Month.JANUARY); 242 } 243 244 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 245 public void factory_intsMonth_nullMonth() { 246 YearMonth.of(2008, null); 247 } 248 249 //----------------------------------------------------------------------- 250 @Test(groups={"tck"}) 251 public void factory_ints() { 252 YearMonth test = YearMonth.of(2008, 2); 253 check(test, 2008, 2); 254 } 255 256 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 257 public void test_factory_ints_yearTooLow() { 258 YearMonth.of(Year.MIN_VALUE - 1, 2); 259 } 260 261 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 262 public void test_factory_ints_dayTooHigh() { 263 YearMonth.of(Year.MAX_VALUE + 1, 2); 264 } 265 266 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 267 public void test_factory_ints_monthTooLow() { 268 YearMonth.of(2008, 0); 269 } 270 271 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 272 public void test_factory_ints_monthTooHigh() { 273 YearMonth.of(2008, 13); 274 } 275 276 //----------------------------------------------------------------------- 277 @Test(groups={"tck"}) 278 public void test_factory_CalendricalObject() { 279 assertEquals(YearMonth.from(LocalDate.of(2007, 7, 15)), YearMonth.of(2007, 7)); 280 } 281 282 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 283 public void test_factory_CalendricalObject_invalid_noDerive() { 284 YearMonth.from(LocalTime.of(12, 30)); 285 } 286 287 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 288 public void test_factory_CalendricalObject_null() { 289 YearMonth.from((TemporalAccessor) null); 290 } 291 292 //----------------------------------------------------------------------- 293 // parse() 294 //----------------------------------------------------------------------- 295 @DataProvider(name="goodParseData") 296 Object[][] provider_goodParseData() { 297 return new Object[][] { 298 {"0000-01", YearMonth.of(0, 1)}, 299 {"0000-12", YearMonth.of(0, 12)}, 300 {"9999-12", YearMonth.of(9999, 12)}, 301 {"2000-01", YearMonth.of(2000, 1)}, 302 {"2000-02", YearMonth.of(2000, 2)}, 303 {"2000-03", YearMonth.of(2000, 3)}, 304 {"2000-04", YearMonth.of(2000, 4)}, 305 {"2000-05", YearMonth.of(2000, 5)}, 306 {"2000-06", YearMonth.of(2000, 6)}, 307 {"2000-07", YearMonth.of(2000, 7)}, 308 {"2000-08", YearMonth.of(2000, 8)}, 309 {"2000-09", YearMonth.of(2000, 9)}, 310 {"2000-10", YearMonth.of(2000, 10)}, 311 {"2000-11", YearMonth.of(2000, 11)}, 312 {"2000-12", YearMonth.of(2000, 12)}, 313 314 {"+12345678-03", YearMonth.of(12345678, 3)}, 315 {"+123456-03", YearMonth.of(123456, 3)}, 316 {"0000-03", YearMonth.of(0, 3)}, 317 {"-1234-03", YearMonth.of(-1234, 3)}, 318 {"-12345678-03", YearMonth.of(-12345678, 3)}, 319 320 {"+" + Year.MAX_VALUE + "-03", YearMonth.of(Year.MAX_VALUE, 3)}, 321 {Year.MIN_VALUE + "-03", YearMonth.of(Year.MIN_VALUE, 3)}, 322 }; 323 } 324 325 @Test(dataProvider="goodParseData", groups={"tck"}) 326 public void factory_parse_success(String text, YearMonth expected) { 327 YearMonth yearMonth = YearMonth.parse(text); 328 assertEquals(yearMonth, expected); 329 } 330 331 //----------------------------------------------------------------------- 332 @DataProvider(name="badParseData") 333 Object[][] provider_badParseData() { 334 return new Object[][] { 335 {"", 0}, 336 {"-00", 1}, 337 {"--01-0", 1}, 338 {"A01-3", 0}, 339 {"200-01", 0}, 340 {"2009/12", 4}, 341 342 {"-0000-10", 0}, 343 {"-12345678901-10", 11}, 344 {"+1-10", 1}, 345 {"+12-10", 1}, 346 {"+123-10", 1}, 347 {"+1234-10", 0}, 348 {"12345-10", 0}, 349 {"+12345678901-10", 11}, 350 }; 351 } 352 353 @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class, groups={"tck"}) 354 public void factory_parse_fail(String text, int pos) { 355 try { 356 YearMonth.parse(text); 357 fail(String.format("Parse should have failed for %s at position %d", text, pos)); 358 } catch (DateTimeParseException ex) { 359 assertEquals(ex.getParsedString(), text); 360 assertEquals(ex.getErrorIndex(), pos); 361 throw ex; 362 } 363 } 364 365 //----------------------------------------------------------------------- 366 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"}) 367 public void factory_parse_illegalValue_Month() { 368 YearMonth.parse("2008-13"); 369 } 370 371 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 372 public void factory_parse_nullText() { 373 YearMonth.parse(null); 374 } 375 376 //----------------------------------------------------------------------- 377 // parse(DateTimeFormatter) 378 //----------------------------------------------------------------------- 379 @Test(groups={"tck"}) 380 public void factory_parse_formatter() { 381 DateTimeFormatter f = DateTimeFormatters.pattern("y M"); 382 YearMonth test = YearMonth.parse("2010 12", f); 383 assertEquals(test, YearMonth.of(2010, 12)); 384 } 385 386 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 387 public void factory_parse_formatter_nullText() { 388 DateTimeFormatter f = DateTimeFormatters.pattern("y M"); 389 YearMonth.parse((String) null, f); 390 } 391 392 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 393 public void factory_parse_formatter_nullFormatter() { 394 YearMonth.parse("ANY", null); 395 } 396 397 //----------------------------------------------------------------------- 398 // get(TemporalField) 399 //----------------------------------------------------------------------- 400 @Test 401 public void test_get_TemporalField() { 402 assertEquals(TEST_2008_06.get(ChronoField.YEAR), 2008); 403 assertEquals(TEST_2008_06.get(ChronoField.MONTH_OF_YEAR), 6); 404 assertEquals(TEST_2008_06.get(ChronoField.YEAR_OF_ERA), 2008); 405 assertEquals(TEST_2008_06.get(ChronoField.ERA), 1); 406 } 407 408 @Test 409 public void test_getLong_TemporalField() { 410 assertEquals(TEST_2008_06.getLong(ChronoField.YEAR), 2008); 411 assertEquals(TEST_2008_06.getLong(ChronoField.MONTH_OF_YEAR), 6); 412 assertEquals(TEST_2008_06.getLong(ChronoField.YEAR_OF_ERA), 2008); 413 assertEquals(TEST_2008_06.getLong(ChronoField.ERA), 1); 414 assertEquals(TEST_2008_06.getLong(ChronoField.EPOCH_MONTH), (2008 - 1970) * 12 + 6 - 1); 415 } 416 417 //----------------------------------------------------------------------- 418 // get*() 419 //----------------------------------------------------------------------- 420 @DataProvider(name="sampleDates") 421 Object[][] provider_sampleDates() { 422 return new Object[][] { 423 {2008, 1}, 424 {2008, 2}, 425 {-1, 3}, 426 {0, 12}, 427 }; 428 } 429 430 //----------------------------------------------------------------------- 431 // with(Year) 432 //----------------------------------------------------------------------- 433 @Test(groups={"tck"}) 434 public void test_with_Year() { 435 YearMonth test = YearMonth.of(2008, 6); 436 assertEquals(test.with(Year.of(2000)), YearMonth.of(2000, 6)); 437 } 438 439 @Test(groups={"tck"}) 440 public void test_with_Year_noChange_equal() { 441 YearMonth test = YearMonth.of(2008, 6); 442 assertEquals(test.with(Year.of(2008)), test); 443 } 444 445 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 446 public void test_with_Year_null() { 447 YearMonth test = YearMonth.of(2008, 6); 448 test.with((Year) null); 449 } 450 451 //----------------------------------------------------------------------- 452 // with(Month) 453 //----------------------------------------------------------------------- 454 @Test(groups={"tck"}) 455 public void test_with_Month() { 456 YearMonth test = YearMonth.of(2008, 6); 457 assertEquals(test.with(Month.JANUARY), YearMonth.of(2008, 1)); 458 } 459 460 @Test(groups={"tck"}) 461 public void test_with_Month_noChange_equal() { 462 YearMonth test = YearMonth.of(2008, 6); 463 assertEquals(test.with(Month.JUNE), test); 464 } 465 466 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 467 public void test_with_Month_null() { 468 YearMonth test = YearMonth.of(2008, 6); 469 test.with((Month) null); 470 } 471 472 //----------------------------------------------------------------------- 473 // withYear() 474 //----------------------------------------------------------------------- 475 @Test(groups={"tck"}) 476 public void test_withYear() { 477 YearMonth test = YearMonth.of(2008, 6); 478 assertEquals(test.withYear(1999), YearMonth.of(1999, 6)); 479 } 480 481 @Test(groups={"tck"}) 482 public void test_withYear_int_noChange_equal() { 483 YearMonth test = YearMonth.of(2008, 6); 484 assertEquals(test.withYear(2008), test); 485 } 486 487 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 488 public void test_withYear_tooLow() { 489 YearMonth test = YearMonth.of(2008, 6); 490 test.withYear(Year.MIN_VALUE - 1); 491 } 492 493 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 494 public void test_withYear_tooHigh() { 495 YearMonth test = YearMonth.of(2008, 6); 496 test.withYear(Year.MAX_VALUE + 1); 497 } 498 499 //----------------------------------------------------------------------- 500 // withMonth() 501 //----------------------------------------------------------------------- 502 @Test(groups={"tck"}) 503 public void test_withMonth() { 504 YearMonth test = YearMonth.of(2008, 6); 505 assertEquals(test.withMonth(1), YearMonth.of(2008, 1)); 506 } 507 508 @Test(groups={"tck"}) 509 public void test_withMonth_int_noChange_equal() { 510 YearMonth test = YearMonth.of(2008, 6); 511 assertEquals(test.withMonth(6), test); 512 } 513 514 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 515 public void test_withMonth_tooLow() { 516 YearMonth test = YearMonth.of(2008, 6); 517 test.withMonth(0); 518 } 519 520 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 521 public void test_withMonth_tooHigh() { 522 YearMonth test = YearMonth.of(2008, 6); 523 test.withMonth(13); 524 } 525 526 //----------------------------------------------------------------------- 527 // plusYears() 528 //----------------------------------------------------------------------- 529 @Test(groups={"tck"}) 530 public void test_plusYears_long() { 531 YearMonth test = YearMonth.of(2008, 6); 532 assertEquals(test.plusYears(1), YearMonth.of(2009, 6)); 533 } 534 535 @Test(groups={"tck"}) 536 public void test_plusYears_long_noChange_equal() { 537 YearMonth test = YearMonth.of(2008, 6); 538 assertEquals(test.plusYears(0), test); 539 } 540 541 @Test(groups={"tck"}) 542 public void test_plusYears_long_negative() { 543 YearMonth test = YearMonth.of(2008, 6); 544 assertEquals(test.plusYears(-1), YearMonth.of(2007, 6)); 545 } 546 547 @Test(groups={"tck"}) 548 public void test_plusYears_long_big() { 549 YearMonth test = YearMonth.of(-40, 6); 550 assertEquals(test.plusYears(20L + Year.MAX_VALUE), YearMonth.of((int) (-40L + 20L + Year.MAX_VALUE), 6)); 551 } 552 553 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 554 public void test_plusYears_long_invalidTooLarge() { 555 YearMonth test = YearMonth.of(Year.MAX_VALUE, 6); 556 test.plusYears(1); 557 } 558 559 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 560 public void test_plusYears_long_invalidTooLargeMaxAddMax() { 561 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 562 test.plusYears(Long.MAX_VALUE); 563 } 564 565 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 566 public void test_plusYears_long_invalidTooLargeMaxAddMin() { 567 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 568 test.plusYears(Long.MIN_VALUE); 569 } 570 571 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 572 public void test_plusYears_long_invalidTooSmall() { 573 YearMonth test = YearMonth.of(Year.MIN_VALUE, 6); 574 test.plusYears(-1); 575 } 576 577 //----------------------------------------------------------------------- 578 // plusMonths() 579 //----------------------------------------------------------------------- 580 @Test(groups={"tck"}) 581 public void test_plusMonths_long() { 582 YearMonth test = YearMonth.of(2008, 6); 583 assertEquals(test.plusMonths(1), YearMonth.of(2008, 7)); 584 } 585 586 @Test(groups={"tck"}) 587 public void test_plusMonths_long_noChange_equal() { 588 YearMonth test = YearMonth.of(2008, 6); 589 assertEquals(test.plusMonths(0), test); 590 } 591 592 @Test(groups={"tck"}) 593 public void test_plusMonths_long_overYears() { 594 YearMonth test = YearMonth.of(2008, 6); 595 assertEquals(test.plusMonths(7), YearMonth.of(2009, 1)); 596 } 597 598 @Test(groups={"tck"}) 599 public void test_plusMonths_long_negative() { 600 YearMonth test = YearMonth.of(2008, 6); 601 assertEquals(test.plusMonths(-1), YearMonth.of(2008, 5)); 602 } 603 604 @Test(groups={"tck"}) 605 public void test_plusMonths_long_negativeOverYear() { 606 YearMonth test = YearMonth.of(2008, 6); 607 assertEquals(test.plusMonths(-6), YearMonth.of(2007, 12)); 608 } 609 610 @Test(groups={"tck"}) 611 public void test_plusMonths_long_big() { 612 YearMonth test = YearMonth.of(-40, 6); 613 long months = 20L + Integer.MAX_VALUE; 614 assertEquals(test.plusMonths(months), YearMonth.of((int) (-40L + months / 12), 6 + (int) (months % 12))); 615 } 616 617 @Test(expectedExceptions={DateTimeException.class}, groups={"tck"}) 618 public void test_plusMonths_long_invalidTooLarge() { 619 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 620 test.plusMonths(1); 621 } 622 623 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 624 public void test_plusMonths_long_invalidTooLargeMaxAddMax() { 625 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 626 test.plusMonths(Long.MAX_VALUE); 627 } 628 629 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 630 public void test_plusMonths_long_invalidTooLargeMaxAddMin() { 631 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 632 test.plusMonths(Long.MIN_VALUE); 633 } 634 635 @Test(expectedExceptions={DateTimeException.class}, groups={"tck"}) 636 public void test_plusMonths_long_invalidTooSmall() { 637 YearMonth test = YearMonth.of(Year.MIN_VALUE, 1); 638 test.plusMonths(-1); 639 } 640 641 //----------------------------------------------------------------------- 642 // minusYears() 643 //----------------------------------------------------------------------- 644 @Test(groups={"tck"}) 645 public void test_minusYears_long() { 646 YearMonth test = YearMonth.of(2008, 6); 647 assertEquals(test.minusYears(1), YearMonth.of(2007, 6)); 648 } 649 650 @Test(groups={"tck"}) 651 public void test_minusYears_long_noChange_equal() { 652 YearMonth test = YearMonth.of(2008, 6); 653 assertEquals(test.minusYears(0), test); 654 } 655 656 @Test(groups={"tck"}) 657 public void test_minusYears_long_negative() { 658 YearMonth test = YearMonth.of(2008, 6); 659 assertEquals(test.minusYears(-1), YearMonth.of(2009, 6)); 660 } 661 662 @Test(groups={"tck"}) 663 public void test_minusYears_long_big() { 664 YearMonth test = YearMonth.of(40, 6); 665 assertEquals(test.minusYears(20L + Year.MAX_VALUE), YearMonth.of((int) (40L - 20L - Year.MAX_VALUE), 6)); 666 } 667 668 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 669 public void test_minusYears_long_invalidTooLarge() { 670 YearMonth test = YearMonth.of(Year.MAX_VALUE, 6); 671 test.minusYears(-1); 672 } 673 674 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 675 public void test_minusYears_long_invalidTooLargeMaxSubtractMax() { 676 YearMonth test = YearMonth.of(Year.MIN_VALUE, 12); 677 test.minusYears(Long.MAX_VALUE); 678 } 679 680 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 681 public void test_minusYears_long_invalidTooLargeMaxSubtractMin() { 682 YearMonth test = YearMonth.of(Year.MIN_VALUE, 12); 683 test.minusYears(Long.MIN_VALUE); 684 } 685 686 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 687 public void test_minusYears_long_invalidTooSmall() { 688 YearMonth test = YearMonth.of(Year.MIN_VALUE, 6); 689 test.minusYears(1); 690 } 691 692 //----------------------------------------------------------------------- 693 // minusMonths() 694 //----------------------------------------------------------------------- 695 @Test(groups={"tck"}) 696 public void test_minusMonths_long() { 697 YearMonth test = YearMonth.of(2008, 6); 698 assertEquals(test.minusMonths(1), YearMonth.of(2008, 5)); 699 } 700 701 @Test(groups={"tck"}) 702 public void test_minusMonths_long_noChange_equal() { 703 YearMonth test = YearMonth.of(2008, 6); 704 assertEquals(test.minusMonths(0), test); 705 } 706 707 @Test(groups={"tck"}) 708 public void test_minusMonths_long_overYears() { 709 YearMonth test = YearMonth.of(2008, 6); 710 assertEquals(test.minusMonths(6), YearMonth.of(2007, 12)); 711 } 712 713 @Test(groups={"tck"}) 714 public void test_minusMonths_long_negative() { 715 YearMonth test = YearMonth.of(2008, 6); 716 assertEquals(test.minusMonths(-1), YearMonth.of(2008, 7)); 717 } 718 719 @Test(groups={"tck"}) 720 public void test_minusMonths_long_negativeOverYear() { 721 YearMonth test = YearMonth.of(2008, 6); 722 assertEquals(test.minusMonths(-7), YearMonth.of(2009, 1)); 723 } 724 725 @Test(groups={"tck"}) 726 public void test_minusMonths_long_big() { 727 YearMonth test = YearMonth.of(40, 6); 728 long months = 20L + Integer.MAX_VALUE; 729 assertEquals(test.minusMonths(months), YearMonth.of((int) (40L - months / 12), 6 - (int) (months % 12))); 730 } 731 732 @Test(expectedExceptions={DateTimeException.class}, groups={"tck"}) 733 public void test_minusMonths_long_invalidTooLarge() { 734 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 735 test.minusMonths(-1); 736 } 737 738 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 739 public void test_minusMonths_long_invalidTooLargeMaxSubtractMax() { 740 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 741 test.minusMonths(Long.MAX_VALUE); 742 } 743 744 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 745 public void test_minusMonths_long_invalidTooLargeMaxSubtractMin() { 746 YearMonth test = YearMonth.of(Year.MAX_VALUE, 12); 747 test.minusMonths(Long.MIN_VALUE); 748 } 749 750 @Test(expectedExceptions={DateTimeException.class}, groups={"tck"}) 751 public void test_minusMonths_long_invalidTooSmall() { 752 YearMonth test = YearMonth.of(Year.MIN_VALUE, 1); 753 test.minusMonths(1); 754 } 755 756 //----------------------------------------------------------------------- 757 // adjustInto() 758 //----------------------------------------------------------------------- 759 @Test(groups={"tck"}) 760 public void test_adjustDate() { 761 YearMonth test = YearMonth.of(2008, 6); 762 LocalDate date = LocalDate.of(2007, 1, 1); 763 assertEquals(test.adjustInto(date), LocalDate.of(2008, 6, 1)); 764 } 765 766 @Test(groups={"tck"}) 767 public void test_adjustDate_preserveDoM() { 768 YearMonth test = YearMonth.of(2011, 3); 769 LocalDate date = LocalDate.of(2008, 2, 29); 770 assertEquals(test.adjustInto(date), LocalDate.of(2011, 3, 29)); 771 } 772 773 @Test(groups={"tck"}) 774 public void test_adjustDate_resolve() { 775 YearMonth test = YearMonth.of(2007, 2); 776 LocalDate date = LocalDate.of(2008, 3, 31); 777 assertEquals(test.adjustInto(date), LocalDate.of(2007, 2, 28)); 778 } 779 780 @Test(groups={"tck"}) 781 public void test_adjustDate_equal() { 782 YearMonth test = YearMonth.of(2008, 6); 783 LocalDate date = LocalDate.of(2008, 6, 30); 784 assertEquals(test.adjustInto(date), date); 785 } 786 787 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 788 public void test_adjustDate_null() { 789 TEST_2008_06.adjustInto((LocalDate) null); 790 } 791 792 //----------------------------------------------------------------------- 793 // isLeapYear() 794 //----------------------------------------------------------------------- 795 @Test(groups={"tck"}) 796 public void test_isLeapYear() { 797 assertEquals(YearMonth.of(2007, 6).isLeapYear(), false); 798 assertEquals(YearMonth.of(2008, 6).isLeapYear(), true); 799 } 800 801 //----------------------------------------------------------------------- 802 // lengthOfMonth() 803 //----------------------------------------------------------------------- 804 @Test(groups={"tck"}) 805 public void test_lengthOfMonth_june() { 806 YearMonth test = YearMonth.of(2007, 6); 807 assertEquals(test.lengthOfMonth(), 30); 808 } 809 810 @Test(groups={"tck"}) 811 public void test_lengthOfMonth_febNonLeap() { 812 YearMonth test = YearMonth.of(2007, 2); 813 assertEquals(test.lengthOfMonth(), 28); 814 } 815 816 @Test(groups={"tck"}) 817 public void test_lengthOfMonth_febLeap() { 818 YearMonth test = YearMonth.of(2008, 2); 819 assertEquals(test.lengthOfMonth(), 29); 820 } 821 822 //----------------------------------------------------------------------- 823 // lengthOfYear() 824 //----------------------------------------------------------------------- 825 @Test(groups={"tck"}) 826 public void test_lengthOfYear() { 827 assertEquals(YearMonth.of(2007, 6).lengthOfYear(), 365); 828 assertEquals(YearMonth.of(2008, 6).lengthOfYear(), 366); 829 } 830 831 //----------------------------------------------------------------------- 832 // isValidDay(int) 833 //----------------------------------------------------------------------- 834 @Test(groups={"tck"}) 835 public void test_isValidDay_int_june() { 836 YearMonth test = YearMonth.of(2007, 6); 837 assertEquals(test.isValidDay(1), true); 838 assertEquals(test.isValidDay(30), true); 839 840 assertEquals(test.isValidDay(-1), false); 841 assertEquals(test.isValidDay(0), false); 842 assertEquals(test.isValidDay(31), false); 843 assertEquals(test.isValidDay(32), false); 844 } 845 846 @Test(groups={"tck"}) 847 public void test_isValidDay_int_febNonLeap() { 848 YearMonth test = YearMonth.of(2007, 2); 849 assertEquals(test.isValidDay(1), true); 850 assertEquals(test.isValidDay(28), true); 851 852 assertEquals(test.isValidDay(-1), false); 853 assertEquals(test.isValidDay(0), false); 854 assertEquals(test.isValidDay(29), false); 855 assertEquals(test.isValidDay(32), false); 856 } 857 858 @Test(groups={"tck"}) 859 public void test_isValidDay_int_febLeap() { 860 YearMonth test = YearMonth.of(2008, 2); 861 assertEquals(test.isValidDay(1), true); 862 assertEquals(test.isValidDay(29), true); 863 864 assertEquals(test.isValidDay(-1), false); 865 assertEquals(test.isValidDay(0), false); 866 assertEquals(test.isValidDay(30), false); 867 assertEquals(test.isValidDay(32), false); 868 } 869 870 //----------------------------------------------------------------------- 871 // atDay(int) 872 //----------------------------------------------------------------------- 873 @Test(groups={"tck"}) 874 public void test_atDay_int() { 875 YearMonth test = YearMonth.of(2008, 6); 876 assertEquals(test.atDay(30), LocalDate.of(2008, 6, 30)); 877 } 878 879 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 880 public void test_atDay_int_invalidDay() { 881 YearMonth test = YearMonth.of(2008, 6); 882 test.atDay(31); 883 } 884 885 //----------------------------------------------------------------------- 886 // compareTo() 887 //----------------------------------------------------------------------- 888 @Test(groups={"tck"}) 889 public void test_comparisons() { 890 doTest_comparisons_YearMonth( 891 YearMonth.of(-1, 1), 892 YearMonth.of(0, 1), 893 YearMonth.of(0, 12), 894 YearMonth.of(1, 1), 895 YearMonth.of(1, 2), 896 YearMonth.of(1, 12), 897 YearMonth.of(2008, 1), 898 YearMonth.of(2008, 6), 899 YearMonth.of(2008, 12) 900 ); 901 } 902 903 void doTest_comparisons_YearMonth(YearMonth... localDates) { 904 for (int i = 0; i < localDates.length; i++) { 905 YearMonth a = localDates[i]; 906 for (int j = 0; j < localDates.length; j++) { 907 YearMonth b = localDates[j]; 908 if (i < j) { 909 assertTrue(a.compareTo(b) < 0, a + " <=> " + b); 910 assertEquals(a.isBefore(b), true, a + " <=> " + b); 911 assertEquals(a.isAfter(b), false, a + " <=> " + b); 912 assertEquals(a.equals(b), false, a + " <=> " + b); 913 } else if (i > j) { 914 assertTrue(a.compareTo(b) > 0, a + " <=> " + b); 915 assertEquals(a.isBefore(b), false, a + " <=> " + b); 916 assertEquals(a.isAfter(b), true, a + " <=> " + b); 917 assertEquals(a.equals(b), false, a + " <=> " + b); 918 } else { 919 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 920 assertEquals(a.isBefore(b), false, a + " <=> " + b); 921 assertEquals(a.isAfter(b), false, a + " <=> " + b); 922 assertEquals(a.equals(b), true, a + " <=> " + b); 923 } 924 } 925 } 926 } 927 928 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 929 public void test_compareTo_ObjectNull() { 930 TEST_2008_06.compareTo(null); 931 } 932 933 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 934 public void test_isBefore_ObjectNull() { 935 TEST_2008_06.isBefore(null); 936 } 937 938 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 939 public void test_isAfter_ObjectNull() { 940 TEST_2008_06.isAfter(null); 941 } 942 943 //----------------------------------------------------------------------- 944 // equals() 945 //----------------------------------------------------------------------- 946 @Test(groups={"tck"}) 947 public void test_equals() { 948 YearMonth a = YearMonth.of(2008, 6); 949 YearMonth b = YearMonth.of(2008, 6); 950 YearMonth c = YearMonth.of(2007, 6); 951 YearMonth d = YearMonth.of(2008, 5); 952 953 assertEquals(a.equals(a), true); 954 assertEquals(a.equals(b), true); 955 assertEquals(a.equals(c), false); 956 assertEquals(a.equals(d), false); 957 958 assertEquals(b.equals(a), true); 959 assertEquals(b.equals(b), true); 960 assertEquals(b.equals(c), false); 961 assertEquals(b.equals(d), false); 962 963 assertEquals(c.equals(a), false); 964 assertEquals(c.equals(b), false); 965 assertEquals(c.equals(c), true); 966 assertEquals(c.equals(d), false); 967 968 assertEquals(d.equals(a), false); 969 assertEquals(d.equals(b), false); 970 assertEquals(d.equals(c), false); 971 assertEquals(d.equals(d), true); 972 } 973 974 @Test(groups={"tck"}) 975 public void test_equals_itself_true() { 976 assertEquals(TEST_2008_06.equals(TEST_2008_06), true); 977 } 978 979 @Test(groups={"tck"}) 980 public void test_equals_string_false() { 981 assertEquals(TEST_2008_06.equals("2007-07-15"), false); 982 } 983 984 @Test(groups={"tck"}) 985 public void test_equals_null_false() { 986 assertEquals(TEST_2008_06.equals(null), false); 987 } 988 989 //----------------------------------------------------------------------- 990 // hashCode() 991 //----------------------------------------------------------------------- 992 @Test(dataProvider="sampleDates", groups={"tck"}) 993 public void test_hashCode(int y, int m) { 994 YearMonth a = YearMonth.of(y, m); 995 assertEquals(a.hashCode(), a.hashCode()); 996 YearMonth b = YearMonth.of(y, m); 997 assertEquals(a.hashCode(), b.hashCode()); 998 } 999 1000 @Test(groups={"tck"}) 1001 public void test_hashCode_unique() { 1002 Set<Integer> uniques = new HashSet<Integer>(201 * 12); 1003 for (int i = 1900; i <= 2100; i++) { 1004 for (int j = 1; j <= 12; j++) { 1005 assertTrue(uniques.add(YearMonth.of(i, j).hashCode())); 1006 } 1007 } 1008 } 1009 1010 //----------------------------------------------------------------------- 1011 // toString() 1012 //----------------------------------------------------------------------- 1013 @DataProvider(name="sampleToString") 1014 Object[][] provider_sampleToString() { 1015 return new Object[][] { 1016 {2008, 1, "2008-01"}, 1017 {2008, 12, "2008-12"}, 1018 {7, 5, "0007-05"}, 1019 {0, 5, "0000-05"}, 1020 {-1, 1, "-0001-01"}, 1021 }; 1022 } 1023 1024 @Test(dataProvider="sampleToString", groups={"tck"}) 1025 public void test_toString(int y, int m, String expected) { 1026 YearMonth test = YearMonth.of(y, m); 1027 String str = test.toString(); 1028 assertEquals(str, expected); 1029 } 1030 1031 //----------------------------------------------------------------------- 1032 // toString(DateTimeFormatter) 1033 //----------------------------------------------------------------------- 1034 @Test(groups={"tck"}) 1035 public void test_toString_formatter() { 1036 DateTimeFormatter f = DateTimeFormatters.pattern("y M"); 1037 String t = YearMonth.of(2010, 12).toString(f); 1038 assertEquals(t, "2010 12"); 1039 } 1040 1041 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1042 public void test_toString_formatter_null() { 1043 YearMonth.of(2010, 12).toString(null); 1044 } 1045 1046 }