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