test/java/time/tck/java/time/TCKYearMonth.java

Print this page




  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 {


 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*()


 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     }


 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 }


  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package tck.java.time;
  61 

  62 import static java.time.temporal.ChronoField.ERA;
  63 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  64 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
  65 import static java.time.temporal.ChronoField.YEAR;
  66 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
  67 import static java.time.temporal.ChronoUnit.CENTURIES;
  68 import static java.time.temporal.ChronoUnit.DAYS;
  69 import static java.time.temporal.ChronoUnit.DECADES;
  70 import static java.time.temporal.ChronoUnit.HOURS;
  71 import static java.time.temporal.ChronoUnit.MILLENNIA;
  72 import static java.time.temporal.ChronoUnit.MONTHS;
  73 import static java.time.temporal.ChronoUnit.WEEKS;
  74 import static java.time.temporal.ChronoUnit.YEARS;
  75 import static org.testng.Assert.assertEquals;
  76 import static org.testng.Assert.assertTrue;
  77 import static org.testng.Assert.fail;
  78 
  79 import java.io.ByteArrayOutputStream;
  80 import java.io.DataOutputStream;
  81 import java.io.IOException;
  82 import java.time.Clock;
  83 import java.time.DateTimeException;
  84 import java.time.Instant;
  85 import java.time.LocalDate;
  86 import java.time.LocalDateTime;
  87 import java.time.LocalTime;
  88 import java.time.Month;
  89 import java.time.Period;
  90 import java.time.Year;
  91 import java.time.YearMonth;
  92 import java.time.ZoneId;
  93 import java.time.ZoneOffset;
  94 import java.time.chrono.IsoChronology;
  95 import java.time.format.DateTimeFormatter;
  96 import java.time.format.DateTimeParseException;
  97 import java.time.temporal.ChronoField;
  98 import java.time.temporal.ChronoUnit;
  99 import java.time.temporal.JulianFields;

 100 import java.time.temporal.TemporalAccessor;
 101 import java.time.temporal.TemporalField;
 102 import java.time.temporal.TemporalQuery;
 103 import java.time.temporal.TemporalUnit;
 104 import java.time.temporal.UnsupportedTemporalTypeException;
 105 import java.util.ArrayList;
 106 import java.util.Arrays;
 107 import java.util.HashSet;
 108 import java.util.List;
 109 import java.util.Set;
 110 
 111 import org.testng.annotations.BeforeMethod;
 112 import org.testng.annotations.DataProvider;
 113 import org.testng.annotations.Test;
 114 
 115 /**
 116  * Test YearMonth.
 117  */
 118 @Test
 119 public class TCKYearMonth extends AbstractDateTimeTest {
 120 
 121     private YearMonth TEST_2008_06;
 122 
 123     @BeforeMethod
 124     public void setUp() {
 125         TEST_2008_06 = YearMonth.of(2008, 6);
 126     }
 127 
 128     //-----------------------------------------------------------------------
 129     @Override
 130     protected List<TemporalAccessor> samples() {
 131         TemporalAccessor[] array = {TEST_2008_06, };
 132         return Arrays.asList(array);
 133     }
 134 
 135     @Override
 136     protected List<TemporalField> validFields() {
 137         TemporalField[] array = {
 138             MONTH_OF_YEAR,
 139             PROLEPTIC_MONTH,
 140             YEAR_OF_ERA,
 141             YEAR,
 142             ERA,
 143         };
 144         return Arrays.asList(array);
 145     }
 146 
 147     @Override
 148     protected List<TemporalField> invalidFields() {
 149         List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
 150         list.removeAll(validFields());
 151         list.add(JulianFields.JULIAN_DAY);
 152         list.add(JulianFields.MODIFIED_JULIAN_DAY);
 153         list.add(JulianFields.RATA_DIE);
 154         return list;
 155     }
 156 
 157     //-----------------------------------------------------------------------
 158     @Test
 159     public void test_serialization() throws IOException, ClassNotFoundException {


 164     public void test_serialization_format() throws Exception {
 165         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 166         try (DataOutputStream dos = new DataOutputStream(baos) ) {
 167             dos.writeByte(12);       // java.time.temporal.Ser.YEAR_MONTH_TYPE
 168             dos.writeInt(2012);
 169             dos.writeByte(9);
 170         }
 171         byte[] bytes = baos.toByteArray();
 172         assertSerializedBySer(YearMonth.of(2012, 9), bytes);
 173     }
 174 
 175     //-----------------------------------------------------------------------
 176     void check(YearMonth test, int y, int m) {
 177         assertEquals(test.getYear(), y);
 178         assertEquals(test.getMonth().getValue(), m);
 179     }
 180 
 181     //-----------------------------------------------------------------------
 182     // now()
 183     //-----------------------------------------------------------------------
 184     @Test
 185     public void now() {
 186         YearMonth expected = YearMonth.now(Clock.systemDefaultZone());
 187         YearMonth test = YearMonth.now();
 188         for (int i = 0; i < 100; i++) {
 189             if (expected.equals(test)) {
 190                 return;
 191             }
 192             expected = YearMonth.now(Clock.systemDefaultZone());
 193             test = YearMonth.now();
 194         }
 195         assertEquals(test, expected);
 196     }
 197 
 198     //-----------------------------------------------------------------------
 199     // now(ZoneId)
 200     //-----------------------------------------------------------------------
 201     @Test(expectedExceptions=NullPointerException.class)
 202     public void now_ZoneId_nullZoneId() {
 203         YearMonth.now((ZoneId) null);
 204     }
 205 
 206     @Test
 207     public void now_ZoneId() {
 208         ZoneId zone = ZoneId.of("UTC+01:02:03");
 209         YearMonth expected = YearMonth.now(Clock.system(zone));
 210         YearMonth test = YearMonth.now(zone);
 211         for (int i = 0; i < 100; i++) {
 212             if (expected.equals(test)) {
 213                 return;
 214             }
 215             expected = YearMonth.now(Clock.system(zone));
 216             test = YearMonth.now(zone);
 217         }
 218         assertEquals(test, expected);
 219     }
 220 
 221     //-----------------------------------------------------------------------
 222     // now(Clock)
 223     //-----------------------------------------------------------------------
 224     @Test
 225     public void now_Clock() {
 226         Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC);
 227         Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
 228         YearMonth test = YearMonth.now(clock);
 229         assertEquals(test.getYear(), 2010);
 230         assertEquals(test.getMonth(), Month.DECEMBER);
 231     }
 232 
 233     @Test(expectedExceptions=NullPointerException.class)
 234     public void now_Clock_nullClock() {
 235         YearMonth.now((Clock) null);
 236     }
 237 
 238     //-----------------------------------------------------------------------
 239     @Test
 240     public void factory_intsMonth() {
 241         YearMonth test = YearMonth.of(2008, Month.FEBRUARY);
 242         check(test, 2008, 2);
 243     }
 244 
 245     @Test(expectedExceptions=DateTimeException.class)
 246     public void test_factory_intsMonth_yearTooLow() {
 247         YearMonth.of(Year.MIN_VALUE - 1, Month.JANUARY);
 248     }
 249 
 250     @Test(expectedExceptions=DateTimeException.class)
 251     public void test_factory_intsMonth_dayTooHigh() {
 252         YearMonth.of(Year.MAX_VALUE + 1, Month.JANUARY);
 253     }
 254 
 255     @Test(expectedExceptions=NullPointerException.class)
 256     public void factory_intsMonth_nullMonth() {
 257         YearMonth.of(2008, null);
 258     }
 259 
 260     //-----------------------------------------------------------------------
 261     @Test
 262     public void factory_ints() {
 263         YearMonth test = YearMonth.of(2008, 2);
 264         check(test, 2008, 2);
 265     }
 266 
 267     @Test(expectedExceptions=DateTimeException.class)
 268     public void test_factory_ints_yearTooLow() {
 269         YearMonth.of(Year.MIN_VALUE - 1, 2);
 270     }
 271 
 272     @Test(expectedExceptions=DateTimeException.class)
 273     public void test_factory_ints_dayTooHigh() {
 274         YearMonth.of(Year.MAX_VALUE + 1, 2);
 275     }
 276 
 277     @Test(expectedExceptions=DateTimeException.class)
 278     public void test_factory_ints_monthTooLow() {
 279         YearMonth.of(2008, 0);
 280     }
 281 
 282     @Test(expectedExceptions=DateTimeException.class)
 283     public void test_factory_ints_monthTooHigh() {
 284         YearMonth.of(2008, 13);
 285     }
 286 
 287     //-----------------------------------------------------------------------
 288     @Test
 289     public void test_factory_CalendricalObject() {
 290         assertEquals(YearMonth.from(LocalDate.of(2007, 7, 15)), YearMonth.of(2007, 7));
 291     }
 292 
 293     @Test(expectedExceptions=DateTimeException.class)
 294     public void test_factory_CalendricalObject_invalid_noDerive() {
 295         YearMonth.from(LocalTime.of(12, 30));
 296     }
 297 
 298     @Test(expectedExceptions=NullPointerException.class)
 299     public void test_factory_CalendricalObject_null() {
 300         YearMonth.from((TemporalAccessor) null);
 301     }
 302 
 303     //-----------------------------------------------------------------------
 304     // parse()
 305     //-----------------------------------------------------------------------
 306     @DataProvider(name="goodParseData")
 307     Object[][] provider_goodParseData() {
 308         return new Object[][] {
 309                 {"0000-01", YearMonth.of(0, 1)},
 310                 {"0000-12", YearMonth.of(0, 12)},
 311                 {"9999-12", YearMonth.of(9999, 12)},
 312                 {"2000-01", YearMonth.of(2000, 1)},
 313                 {"2000-02", YearMonth.of(2000, 2)},
 314                 {"2000-03", YearMonth.of(2000, 3)},
 315                 {"2000-04", YearMonth.of(2000, 4)},
 316                 {"2000-05", YearMonth.of(2000, 5)},
 317                 {"2000-06", YearMonth.of(2000, 6)},
 318                 {"2000-07", YearMonth.of(2000, 7)},
 319                 {"2000-08", YearMonth.of(2000, 8)},
 320                 {"2000-09", YearMonth.of(2000, 9)},
 321                 {"2000-10", YearMonth.of(2000, 10)},
 322                 {"2000-11", YearMonth.of(2000, 11)},
 323                 {"2000-12", YearMonth.of(2000, 12)},
 324 
 325                 {"+12345678-03", YearMonth.of(12345678, 3)},
 326                 {"+123456-03", YearMonth.of(123456, 3)},
 327                 {"0000-03", YearMonth.of(0, 3)},
 328                 {"-1234-03", YearMonth.of(-1234, 3)},
 329                 {"-12345678-03", YearMonth.of(-12345678, 3)},
 330 
 331                 {"+" + Year.MAX_VALUE + "-03", YearMonth.of(Year.MAX_VALUE, 3)},
 332                 {Year.MIN_VALUE + "-03", YearMonth.of(Year.MIN_VALUE, 3)},
 333         };
 334     }
 335 
 336     @Test(dataProvider="goodParseData")
 337     public void factory_parse_success(String text, YearMonth expected) {
 338         YearMonth yearMonth = YearMonth.parse(text);
 339         assertEquals(yearMonth, expected);
 340     }
 341 
 342     //-----------------------------------------------------------------------
 343     @DataProvider(name="badParseData")
 344     Object[][] provider_badParseData() {
 345         return new Object[][] {
 346                 {"", 0},
 347                 {"-00", 1},
 348                 {"--01-0", 1},
 349                 {"A01-3", 0},
 350                 {"200-01", 0},
 351                 {"2009/12", 4},
 352 
 353                 {"-0000-10", 0},
 354                 {"-12345678901-10", 11},
 355                 {"+1-10", 1},
 356                 {"+12-10", 1},
 357                 {"+123-10", 1},
 358                 {"+1234-10", 0},
 359                 {"12345-10", 0},
 360                 {"+12345678901-10", 11},
 361         };
 362     }
 363 
 364     @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class)
 365     public void factory_parse_fail(String text, int pos) {
 366         try {
 367             YearMonth.parse(text);
 368             fail(String.format("Parse should have failed for %s at position %d", text, pos));
 369         } catch (DateTimeParseException ex) {
 370             assertEquals(ex.getParsedString(), text);
 371             assertEquals(ex.getErrorIndex(), pos);
 372             throw ex;
 373         }
 374     }
 375 
 376     //-----------------------------------------------------------------------
 377     @Test(expectedExceptions=DateTimeParseException.class)
 378     public void factory_parse_illegalValue_Month() {
 379         YearMonth.parse("2008-13");
 380     }
 381 
 382     @Test(expectedExceptions=NullPointerException.class)
 383     public void factory_parse_nullText() {
 384         YearMonth.parse(null);
 385     }
 386 
 387     //-----------------------------------------------------------------------
 388     // parse(DateTimeFormatter)
 389     //-----------------------------------------------------------------------
 390     @Test
 391     public void factory_parse_formatter() {
 392         DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
 393         YearMonth test = YearMonth.parse("2010 12", f);
 394         assertEquals(test, YearMonth.of(2010, 12));
 395     }
 396 
 397     @Test(expectedExceptions=NullPointerException.class)
 398     public void factory_parse_formatter_nullText() {
 399         DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
 400         YearMonth.parse((String) null, f);
 401     }
 402 
 403     @Test(expectedExceptions=NullPointerException.class)
 404     public void factory_parse_formatter_nullFormatter() {
 405         YearMonth.parse("ANY", null);
 406     }
 407 
 408     //-----------------------------------------------------------------------
 409     // get(TemporalField)
 410     //-----------------------------------------------------------------------
 411     @Test
 412     public void test_get_TemporalField() {
 413         assertEquals(TEST_2008_06.get(YEAR), 2008);
 414         assertEquals(TEST_2008_06.get(MONTH_OF_YEAR), 6);
 415         assertEquals(TEST_2008_06.get(YEAR_OF_ERA), 2008);
 416         assertEquals(TEST_2008_06.get(ERA), 1);
 417     }
 418 
 419     @Test
 420     public void test_getLong_TemporalField() {
 421         assertEquals(TEST_2008_06.getLong(YEAR), 2008);
 422         assertEquals(TEST_2008_06.getLong(MONTH_OF_YEAR), 6);
 423         assertEquals(TEST_2008_06.getLong(YEAR_OF_ERA), 2008);
 424         assertEquals(TEST_2008_06.getLong(ERA), 1);
 425         assertEquals(TEST_2008_06.getLong(PROLEPTIC_MONTH), 2008 * 12 + 6 - 1);
 426     }
 427 
 428     //-----------------------------------------------------------------------
 429     // query(TemporalQuery)
 430     //-----------------------------------------------------------------------
 431     @DataProvider(name="query")
 432     Object[][] data_query() {
 433         return new Object[][] {
 434                 {TEST_2008_06, TemporalQuery.chronology(), IsoChronology.INSTANCE},
 435                 {TEST_2008_06, TemporalQuery.zoneId(), null},
 436                 {TEST_2008_06, TemporalQuery.precision(), ChronoUnit.MONTHS},
 437                 {TEST_2008_06, TemporalQuery.zone(), null},
 438                 {TEST_2008_06, TemporalQuery.offset(), null},
 439                 {TEST_2008_06, TemporalQuery.localDate(), null},
 440                 {TEST_2008_06, TemporalQuery.localTime(), null},
 441         };
 442     }
 443 
 444     @Test(dataProvider="query")
 445     public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 446         assertEquals(temporal.query(query), expected);
 447     }
 448 
 449     @Test(dataProvider="query")
 450     public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 451         assertEquals(query.queryFrom(temporal), expected);
 452     }
 453 
 454     @Test(expectedExceptions=NullPointerException.class)
 455     public void test_query_null() {
 456         TEST_2008_06.query(null);
 457     }
 458 
 459     //-----------------------------------------------------------------------
 460     // get*()


 463     Object[][] provider_sampleDates() {
 464         return new Object[][] {
 465             {2008, 1},
 466             {2008, 2},
 467             {-1, 3},
 468             {0, 12},
 469         };
 470     }
 471 
 472     @Test(dataProvider="sampleDates")
 473     public void test_get(int y, int m) {
 474         YearMonth a = YearMonth.of(y, m);
 475         assertEquals(a.getYear(), y);
 476         assertEquals(a.getMonth(), Month.of(m));
 477         assertEquals(a.getMonthValue(), m);
 478     }
 479 
 480     //-----------------------------------------------------------------------
 481     // with(Year)
 482     //-----------------------------------------------------------------------
 483     @Test
 484     public void test_with_Year() {
 485         YearMonth test = YearMonth.of(2008, 6);
 486         assertEquals(test.with(Year.of(2000)), YearMonth.of(2000, 6));
 487     }
 488 
 489     @Test
 490     public void test_with_Year_noChange_equal() {
 491         YearMonth test = YearMonth.of(2008, 6);
 492         assertEquals(test.with(Year.of(2008)), test);
 493     }
 494 
 495     @Test(expectedExceptions=NullPointerException.class)
 496     public void test_with_Year_null() {
 497         YearMonth test = YearMonth.of(2008, 6);
 498         test.with((Year) null);
 499     }
 500 
 501     //-----------------------------------------------------------------------
 502     // with(Month)
 503     //-----------------------------------------------------------------------
 504     @Test
 505     public void test_with_Month() {
 506         YearMonth test = YearMonth.of(2008, 6);
 507         assertEquals(test.with(Month.JANUARY), YearMonth.of(2008, 1));
 508     }
 509 
 510     @Test
 511     public void test_with_Month_noChange_equal() {
 512         YearMonth test = YearMonth.of(2008, 6);
 513         assertEquals(test.with(Month.JUNE), test);
 514     }
 515 
 516     @Test(expectedExceptions=NullPointerException.class)
 517     public void test_with_Month_null() {
 518         YearMonth test = YearMonth.of(2008, 6);
 519         test.with((Month) null);
 520     }
 521 
 522     //-----------------------------------------------------------------------
 523     // withYear()
 524     //-----------------------------------------------------------------------
 525     @Test
 526     public void test_withYear() {
 527         YearMonth test = YearMonth.of(2008, 6);
 528         assertEquals(test.withYear(1999), YearMonth.of(1999, 6));
 529     }
 530 
 531     @Test
 532     public void test_withYear_int_noChange_equal() {
 533         YearMonth test = YearMonth.of(2008, 6);
 534         assertEquals(test.withYear(2008), test);
 535     }
 536 
 537     @Test(expectedExceptions=DateTimeException.class)
 538     public void test_withYear_tooLow() {
 539         YearMonth test = YearMonth.of(2008, 6);
 540         test.withYear(Year.MIN_VALUE - 1);
 541     }
 542 
 543     @Test(expectedExceptions=DateTimeException.class)
 544     public void test_withYear_tooHigh() {
 545         YearMonth test = YearMonth.of(2008, 6);
 546         test.withYear(Year.MAX_VALUE + 1);
 547     }
 548 
 549     //-----------------------------------------------------------------------
 550     // withMonth()
 551     //-----------------------------------------------------------------------
 552     @Test
 553     public void test_withMonth() {
 554         YearMonth test = YearMonth.of(2008, 6);
 555         assertEquals(test.withMonth(1), YearMonth.of(2008, 1));
 556     }
 557 
 558     @Test
 559     public void test_withMonth_int_noChange_equal() {
 560         YearMonth test = YearMonth.of(2008, 6);
 561         assertEquals(test.withMonth(6), test);
 562     }
 563 
 564     @Test(expectedExceptions=DateTimeException.class)
 565     public void test_withMonth_tooLow() {
 566         YearMonth test = YearMonth.of(2008, 6);
 567         test.withMonth(0);
 568     }
 569 
 570     @Test(expectedExceptions=DateTimeException.class)
 571     public void test_withMonth_tooHigh() {
 572         YearMonth test = YearMonth.of(2008, 6);
 573         test.withMonth(13);
 574     }
 575 
 576     //-----------------------------------------------------------------------
 577     // plusYears()
 578     //-----------------------------------------------------------------------
 579     @Test
 580     public void test_plusYears_long() {
 581         YearMonth test = YearMonth.of(2008, 6);
 582         assertEquals(test.plusYears(1), YearMonth.of(2009, 6));
 583     }
 584 
 585     @Test
 586     public void test_plusYears_long_noChange_equal() {
 587         YearMonth test = YearMonth.of(2008, 6);
 588         assertEquals(test.plusYears(0), test);
 589     }
 590 
 591     @Test
 592     public void test_plusYears_long_negative() {
 593         YearMonth test = YearMonth.of(2008, 6);
 594         assertEquals(test.plusYears(-1), YearMonth.of(2007, 6));
 595     }
 596 
 597     @Test
 598     public void test_plusYears_long_big() {
 599         YearMonth test = YearMonth.of(-40, 6);
 600         assertEquals(test.plusYears(20L + Year.MAX_VALUE), YearMonth.of((int) (-40L + 20L + Year.MAX_VALUE), 6));
 601     }
 602 
 603     @Test(expectedExceptions=DateTimeException.class)
 604     public void test_plusYears_long_invalidTooLarge() {
 605         YearMonth test = YearMonth.of(Year.MAX_VALUE, 6);
 606         test.plusYears(1);
 607     }
 608 
 609     @Test(expectedExceptions=DateTimeException.class)
 610     public void test_plusYears_long_invalidTooLargeMaxAddMax() {
 611         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 612         test.plusYears(Long.MAX_VALUE);
 613     }
 614 
 615     @Test(expectedExceptions=DateTimeException.class)
 616     public void test_plusYears_long_invalidTooLargeMaxAddMin() {
 617         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 618         test.plusYears(Long.MIN_VALUE);
 619     }
 620 
 621     @Test(expectedExceptions=DateTimeException.class)
 622     public void test_plusYears_long_invalidTooSmall() {
 623         YearMonth test = YearMonth.of(Year.MIN_VALUE, 6);
 624         test.plusYears(-1);
 625     }
 626 
 627     //-----------------------------------------------------------------------
 628     // plusMonths()
 629     //-----------------------------------------------------------------------
 630     @Test
 631     public void test_plusMonths_long() {
 632         YearMonth test = YearMonth.of(2008, 6);
 633         assertEquals(test.plusMonths(1), YearMonth.of(2008, 7));
 634     }
 635 
 636     @Test
 637     public void test_plusMonths_long_noChange_equal() {
 638         YearMonth test = YearMonth.of(2008, 6);
 639         assertEquals(test.plusMonths(0), test);
 640     }
 641 
 642     @Test
 643     public void test_plusMonths_long_overYears() {
 644         YearMonth test = YearMonth.of(2008, 6);
 645         assertEquals(test.plusMonths(7), YearMonth.of(2009, 1));
 646     }
 647 
 648     @Test
 649     public void test_plusMonths_long_negative() {
 650         YearMonth test = YearMonth.of(2008, 6);
 651         assertEquals(test.plusMonths(-1), YearMonth.of(2008, 5));
 652     }
 653 
 654     @Test
 655     public void test_plusMonths_long_negativeOverYear() {
 656         YearMonth test = YearMonth.of(2008, 6);
 657         assertEquals(test.plusMonths(-6), YearMonth.of(2007, 12));
 658     }
 659 
 660     @Test
 661     public void test_plusMonths_long_big() {
 662         YearMonth test = YearMonth.of(-40, 6);
 663         long months = 20L + Integer.MAX_VALUE;
 664         assertEquals(test.plusMonths(months), YearMonth.of((int) (-40L + months / 12), 6 + (int) (months % 12)));
 665     }
 666 
 667     @Test(expectedExceptions={DateTimeException.class})
 668     public void test_plusMonths_long_invalidTooLarge() {
 669         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 670         test.plusMonths(1);
 671     }
 672 
 673     @Test(expectedExceptions=DateTimeException.class)
 674     public void test_plusMonths_long_invalidTooLargeMaxAddMax() {
 675         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 676         test.plusMonths(Long.MAX_VALUE);
 677     }
 678 
 679     @Test(expectedExceptions=DateTimeException.class)
 680     public void test_plusMonths_long_invalidTooLargeMaxAddMin() {
 681         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 682         test.plusMonths(Long.MIN_VALUE);
 683     }
 684 
 685     @Test(expectedExceptions={DateTimeException.class})
 686     public void test_plusMonths_long_invalidTooSmall() {
 687         YearMonth test = YearMonth.of(Year.MIN_VALUE, 1);
 688         test.plusMonths(-1);
 689     }
 690 
 691     //-----------------------------------------------------------------------
 692     // minusYears()
 693     //-----------------------------------------------------------------------
 694     @Test
 695     public void test_minusYears_long() {
 696         YearMonth test = YearMonth.of(2008, 6);
 697         assertEquals(test.minusYears(1), YearMonth.of(2007, 6));
 698     }
 699 
 700     @Test
 701     public void test_minusYears_long_noChange_equal() {
 702         YearMonth test = YearMonth.of(2008, 6);
 703         assertEquals(test.minusYears(0), test);
 704     }
 705 
 706     @Test
 707     public void test_minusYears_long_negative() {
 708         YearMonth test = YearMonth.of(2008, 6);
 709         assertEquals(test.minusYears(-1), YearMonth.of(2009, 6));
 710     }
 711 
 712     @Test
 713     public void test_minusYears_long_big() {
 714         YearMonth test = YearMonth.of(40, 6);
 715         assertEquals(test.minusYears(20L + Year.MAX_VALUE), YearMonth.of((int) (40L - 20L - Year.MAX_VALUE), 6));
 716     }
 717 
 718     @Test(expectedExceptions=DateTimeException.class)
 719     public void test_minusYears_long_invalidTooLarge() {
 720         YearMonth test = YearMonth.of(Year.MAX_VALUE, 6);
 721         test.minusYears(-1);
 722     }
 723 
 724     @Test(expectedExceptions=DateTimeException.class)
 725     public void test_minusYears_long_invalidTooLargeMaxSubtractMax() {
 726         YearMonth test = YearMonth.of(Year.MIN_VALUE, 12);
 727         test.minusYears(Long.MAX_VALUE);
 728     }
 729 
 730     @Test(expectedExceptions=DateTimeException.class)
 731     public void test_minusYears_long_invalidTooLargeMaxSubtractMin() {
 732         YearMonth test = YearMonth.of(Year.MIN_VALUE, 12);
 733         test.minusYears(Long.MIN_VALUE);
 734     }
 735 
 736     @Test(expectedExceptions=DateTimeException.class)
 737     public void test_minusYears_long_invalidTooSmall() {
 738         YearMonth test = YearMonth.of(Year.MIN_VALUE, 6);
 739         test.minusYears(1);
 740     }
 741 
 742     //-----------------------------------------------------------------------
 743     // minusMonths()
 744     //-----------------------------------------------------------------------
 745     @Test
 746     public void test_minusMonths_long() {
 747         YearMonth test = YearMonth.of(2008, 6);
 748         assertEquals(test.minusMonths(1), YearMonth.of(2008, 5));
 749     }
 750 
 751     @Test
 752     public void test_minusMonths_long_noChange_equal() {
 753         YearMonth test = YearMonth.of(2008, 6);
 754         assertEquals(test.minusMonths(0), test);
 755     }
 756 
 757     @Test
 758     public void test_minusMonths_long_overYears() {
 759         YearMonth test = YearMonth.of(2008, 6);
 760         assertEquals(test.minusMonths(6), YearMonth.of(2007, 12));
 761     }
 762 
 763     @Test
 764     public void test_minusMonths_long_negative() {
 765         YearMonth test = YearMonth.of(2008, 6);
 766         assertEquals(test.minusMonths(-1), YearMonth.of(2008, 7));
 767     }
 768 
 769     @Test
 770     public void test_minusMonths_long_negativeOverYear() {
 771         YearMonth test = YearMonth.of(2008, 6);
 772         assertEquals(test.minusMonths(-7), YearMonth.of(2009, 1));
 773     }
 774 
 775     @Test
 776     public void test_minusMonths_long_big() {
 777         YearMonth test = YearMonth.of(40, 6);
 778         long months = 20L + Integer.MAX_VALUE;
 779         assertEquals(test.minusMonths(months), YearMonth.of((int) (40L - months / 12), 6 - (int) (months % 12)));
 780     }
 781 
 782     @Test(expectedExceptions={DateTimeException.class})
 783     public void test_minusMonths_long_invalidTooLarge() {
 784         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 785         test.minusMonths(-1);
 786     }
 787 
 788     @Test(expectedExceptions=DateTimeException.class)
 789     public void test_minusMonths_long_invalidTooLargeMaxSubtractMax() {
 790         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 791         test.minusMonths(Long.MAX_VALUE);
 792     }
 793 
 794     @Test(expectedExceptions=DateTimeException.class)
 795     public void test_minusMonths_long_invalidTooLargeMaxSubtractMin() {
 796         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
 797         test.minusMonths(Long.MIN_VALUE);
 798     }
 799 
 800     @Test(expectedExceptions={DateTimeException.class})
 801     public void test_minusMonths_long_invalidTooSmall() {
 802         YearMonth test = YearMonth.of(Year.MIN_VALUE, 1);
 803         test.minusMonths(1);
 804     }
 805 
 806     //-----------------------------------------------------------------------
 807     // adjustInto()
 808     //-----------------------------------------------------------------------
 809     @Test
 810     public void test_adjustDate() {
 811         YearMonth test = YearMonth.of(2008, 6);
 812         LocalDate date = LocalDate.of(2007, 1, 1);
 813         assertEquals(test.adjustInto(date), LocalDate.of(2008, 6, 1));
 814     }
 815 
 816     @Test
 817     public void test_adjustDate_preserveDoM() {
 818         YearMonth test = YearMonth.of(2011, 3);
 819         LocalDate date = LocalDate.of(2008, 2, 29);
 820         assertEquals(test.adjustInto(date), LocalDate.of(2011, 3, 29));
 821     }
 822 
 823     @Test
 824     public void test_adjustDate_resolve() {
 825         YearMonth test = YearMonth.of(2007, 2);
 826         LocalDate date = LocalDate.of(2008, 3, 31);
 827         assertEquals(test.adjustInto(date), LocalDate.of(2007, 2, 28));
 828     }
 829 
 830     @Test
 831     public void test_adjustDate_equal() {
 832         YearMonth test = YearMonth.of(2008, 6);
 833         LocalDate date = LocalDate.of(2008, 6, 30);
 834         assertEquals(test.adjustInto(date), date);
 835     }
 836 
 837     @Test(expectedExceptions=NullPointerException.class)
 838     public void test_adjustDate_null() {
 839         TEST_2008_06.adjustInto((LocalDate) null);
 840     }
 841 
 842     //-----------------------------------------------------------------------
 843     // isLeapYear()
 844     //-----------------------------------------------------------------------
 845     @Test
 846     public void test_isLeapYear() {
 847         assertEquals(YearMonth.of(2007, 6).isLeapYear(), false);
 848         assertEquals(YearMonth.of(2008, 6).isLeapYear(), true);
 849     }
 850 
 851     //-----------------------------------------------------------------------
 852     // lengthOfMonth()
 853     //-----------------------------------------------------------------------
 854     @Test
 855     public void test_lengthOfMonth_june() {
 856         YearMonth test = YearMonth.of(2007, 6);
 857         assertEquals(test.lengthOfMonth(), 30);
 858     }
 859 
 860     @Test
 861     public void test_lengthOfMonth_febNonLeap() {
 862         YearMonth test = YearMonth.of(2007, 2);
 863         assertEquals(test.lengthOfMonth(), 28);
 864     }
 865 
 866     @Test
 867     public void test_lengthOfMonth_febLeap() {
 868         YearMonth test = YearMonth.of(2008, 2);
 869         assertEquals(test.lengthOfMonth(), 29);
 870     }
 871 
 872     //-----------------------------------------------------------------------
 873     // lengthOfYear()
 874     //-----------------------------------------------------------------------
 875     @Test
 876     public void test_lengthOfYear() {
 877         assertEquals(YearMonth.of(2007, 6).lengthOfYear(), 365);
 878         assertEquals(YearMonth.of(2008, 6).lengthOfYear(), 366);
 879     }
 880 
 881     //-----------------------------------------------------------------------
 882     // isValidDay(int)
 883     //-----------------------------------------------------------------------
 884     @Test
 885     public void test_isValidDay_int_june() {
 886         YearMonth test = YearMonth.of(2007, 6);
 887         assertEquals(test.isValidDay(1), true);
 888         assertEquals(test.isValidDay(30), true);
 889 
 890         assertEquals(test.isValidDay(-1), false);
 891         assertEquals(test.isValidDay(0), false);
 892         assertEquals(test.isValidDay(31), false);
 893         assertEquals(test.isValidDay(32), false);
 894     }
 895 
 896     @Test
 897     public void test_isValidDay_int_febNonLeap() {
 898         YearMonth test = YearMonth.of(2007, 2);
 899         assertEquals(test.isValidDay(1), true);
 900         assertEquals(test.isValidDay(28), true);
 901 
 902         assertEquals(test.isValidDay(-1), false);
 903         assertEquals(test.isValidDay(0), false);
 904         assertEquals(test.isValidDay(29), false);
 905         assertEquals(test.isValidDay(32), false);
 906     }
 907 
 908     @Test
 909     public void test_isValidDay_int_febLeap() {
 910         YearMonth test = YearMonth.of(2008, 2);
 911         assertEquals(test.isValidDay(1), true);
 912         assertEquals(test.isValidDay(29), true);
 913 
 914         assertEquals(test.isValidDay(-1), false);
 915         assertEquals(test.isValidDay(0), false);
 916         assertEquals(test.isValidDay(30), false);
 917         assertEquals(test.isValidDay(32), false);
 918     }
 919 
 920     //-----------------------------------------------------------------------
 921     // periodUntil(Temporal, TemporalUnit)
 922     //-----------------------------------------------------------------------
 923     @DataProvider(name="periodUntilUnit")
 924     Object[][] data_periodUntilUnit() {
 925         return new Object[][] {
 926                 {ym(2000, 1), ym(-1, 12), MONTHS, -2000 * 12 - 1},
 927                 {ym(2000, 1), ym(0, 1), MONTHS, -2000 * 12},
 928                 {ym(2000, 1), ym(0, 12), MONTHS, -1999 * 12 - 1},
 929                 {ym(2000, 1), ym(1, 1), MONTHS, -1999 * 12},
 930                 {ym(2000, 1), ym(1999, 12), MONTHS, -1},
 931                 {ym(2000, 1), ym(2000, 1), MONTHS, 0},
 932                 {ym(2000, 1), ym(2000, 2), MONTHS, 1},
 933                 {ym(2000, 1), ym(2000, 3), MONTHS, 2},
 934                 {ym(2000, 1), ym(2000, 12), MONTHS, 11},
 935                 {ym(2000, 1), ym(2001, 1), MONTHS, 12},
 936                 {ym(2000, 1), ym(2246, 5), MONTHS, 246 * 12 + 4},
 937 
 938                 {ym(2000, 1), ym(-1, 12), YEARS, -2000},
 939                 {ym(2000, 1), ym(0, 1), YEARS, -2000},
 940                 {ym(2000, 1), ym(0, 12), YEARS, -1999},
 941                 {ym(2000, 1), ym(1, 1), YEARS, -1999},
 942                 {ym(2000, 1), ym(1998, 12), YEARS, -1},
 943                 {ym(2000, 1), ym(1999, 1), YEARS, -1},
 944                 {ym(2000, 1), ym(1999, 2), YEARS, 0},
 945                 {ym(2000, 1), ym(1999, 12), YEARS, 0},
 946                 {ym(2000, 1), ym(2000, 1), YEARS, 0},
 947                 {ym(2000, 1), ym(2000, 2), YEARS, 0},
 948                 {ym(2000, 1), ym(2000, 12), YEARS, 0},
 949                 {ym(2000, 1), ym(2001, 1), YEARS, 1},
 950                 {ym(2000, 1), ym(2246, 5), YEARS, 246},
 951 
 952                 {ym(2000, 5), ym(-1, 5), DECADES, -200},
 953                 {ym(2000, 5), ym(0, 4), DECADES, -200},
 954                 {ym(2000, 5), ym(0, 5), DECADES, -200},
 955                 {ym(2000, 5), ym(0, 6), DECADES, -199},
 956                 {ym(2000, 5), ym(1, 5), DECADES, -199},
 957                 {ym(2000, 5), ym(1990, 4), DECADES, -1},
 958                 {ym(2000, 5), ym(1990, 5), DECADES, -1},
 959                 {ym(2000, 5), ym(1990, 6), DECADES, 0},
 960                 {ym(2000, 5), ym(2000, 4), DECADES, 0},
 961                 {ym(2000, 5), ym(2000, 5), DECADES, 0},
 962                 {ym(2000, 5), ym(2000, 6), DECADES, 0},
 963                 {ym(2000, 5), ym(2010, 4), DECADES, 0},
 964                 {ym(2000, 5), ym(2010, 5), DECADES, 1},
 965                 {ym(2000, 5), ym(2010, 6), DECADES, 1},
 966 
 967                 {ym(2000, 5), ym(-1, 5), CENTURIES, -20},
 968                 {ym(2000, 5), ym(0, 4), CENTURIES, -20},
 969                 {ym(2000, 5), ym(0, 5), CENTURIES, -20},
 970                 {ym(2000, 5), ym(0, 6), CENTURIES, -19},
 971                 {ym(2000, 5), ym(1, 5), CENTURIES, -19},
 972                 {ym(2000, 5), ym(1900, 4), CENTURIES, -1},
 973                 {ym(2000, 5), ym(1900, 5), CENTURIES, -1},
 974                 {ym(2000, 5), ym(1900, 6), CENTURIES, 0},
 975                 {ym(2000, 5), ym(2000, 4), CENTURIES, 0},
 976                 {ym(2000, 5), ym(2000, 5), CENTURIES, 0},
 977                 {ym(2000, 5), ym(2000, 6), CENTURIES, 0},
 978                 {ym(2000, 5), ym(2100, 4), CENTURIES, 0},
 979                 {ym(2000, 5), ym(2100, 5), CENTURIES, 1},
 980                 {ym(2000, 5), ym(2100, 6), CENTURIES, 1},
 981 
 982                 {ym(2000, 5), ym(-1, 5), MILLENNIA, -2},
 983                 {ym(2000, 5), ym(0, 4), MILLENNIA, -2},
 984                 {ym(2000, 5), ym(0, 5), MILLENNIA, -2},
 985                 {ym(2000, 5), ym(0, 6), MILLENNIA, -1},
 986                 {ym(2000, 5), ym(1, 5), MILLENNIA, -1},
 987                 {ym(2000, 5), ym(1000, 4), MILLENNIA, -1},
 988                 {ym(2000, 5), ym(1000, 5), MILLENNIA, -1},
 989                 {ym(2000, 5), ym(1000, 6), MILLENNIA, 0},
 990                 {ym(2000, 5), ym(2000, 4), MILLENNIA, 0},
 991                 {ym(2000, 5), ym(2000, 5), MILLENNIA, 0},
 992                 {ym(2000, 5), ym(2000, 6), MILLENNIA, 0},
 993                 {ym(2000, 5), ym(3000, 4), MILLENNIA, 0},
 994                 {ym(2000, 5), ym(3000, 5), MILLENNIA, 1},
 995                 {ym(2000, 5), ym(3000, 5), MILLENNIA, 1},
 996         };
 997     }
 998 
 999     @Test(dataProvider="periodUntilUnit")
1000     public void test_periodUntil_TemporalUnit(YearMonth ym1, YearMonth ym2, TemporalUnit unit, long expected) {
1001         long amount = ym1.periodUntil(ym2, unit);
1002         assertEquals(amount, expected);
1003     }
1004 
1005     @Test(dataProvider="periodUntilUnit")
1006     public void test_periodUntil_TemporalUnit_negated(YearMonth ym1, YearMonth ym2, TemporalUnit unit, long expected) {
1007         long amount = ym2.periodUntil(ym1, unit);
1008         assertEquals(amount, -expected);
1009     }
1010 
1011     @Test(expectedExceptions = UnsupportedTemporalTypeException.class)
1012     public void test_periodUntil_TemporalUnit_unsupportedUnit() {
1013         TEST_2008_06.periodUntil(TEST_2008_06, HOURS);
1014     }
1015 
1016     @Test(expectedExceptions = NullPointerException.class)
1017     public void test_periodUntil_TemporalUnit_nullEnd() {
1018         TEST_2008_06.periodUntil(null, DAYS);
1019     }
1020 
1021     @Test(expectedExceptions = NullPointerException.class)
1022     public void test_periodUntil_TemporalUnit_nullUnit() {
1023         TEST_2008_06.periodUntil(TEST_2008_06, null);
1024     }
1025 
1026     //-----------------------------------------------------------------------
1027     // format(DateTimeFormatter)
1028     //-----------------------------------------------------------------------
1029     @Test
1030     public void test_format_formatter() {
1031         DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
1032         String t = YearMonth.of(2010, 12).format(f);
1033         assertEquals(t, "2010 12");
1034     }
1035 
1036     @Test(expectedExceptions=NullPointerException.class)
1037     public void test_format_formatter_null() {
1038         YearMonth.of(2010, 12).format(null);
1039     }
1040 
1041     //-----------------------------------------------------------------------
1042     // atDay(int)
1043     //-----------------------------------------------------------------------
1044     @DataProvider(name="atDay")
1045     Object[][] data_atDay() {
1046         return new Object[][] {
1047                 {YearMonth.of(2008, 6), 8, LocalDate.of(2008, 6, 8)},
1048 
1049                 {YearMonth.of(2008, 1), 31, LocalDate.of(2008, 1, 31)},
1050                 {YearMonth.of(2008, 2), 29, LocalDate.of(2008, 2, 29)},
1051                 {YearMonth.of(2008, 3), 31, LocalDate.of(2008, 3, 31)},
1052                 {YearMonth.of(2008, 4), 30, LocalDate.of(2008, 4, 30)},
1053 
1054                 {YearMonth.of(2009, 1), 32, null},
1055                 {YearMonth.of(2009, 1), 0, null},
1056                 {YearMonth.of(2009, 2), 29, null},
1057                 {YearMonth.of(2009, 2), 30, null},
1058                 {YearMonth.of(2009, 2), 31, null},
1059                 {YearMonth.of(2009, 4), 31, null},
1060         };
1061     }


1089                 {YearMonth.of(2008, 12), LocalDate.of(2008, 12, 31)},
1090 
1091                 {YearMonth.of(2009, 1), LocalDate.of(2009, 1, 31)},
1092                 {YearMonth.of(2009, 2), LocalDate.of(2009, 2, 28)},
1093                 {YearMonth.of(2009, 3), LocalDate.of(2009, 3, 31)},
1094                 {YearMonth.of(2009, 4), LocalDate.of(2009, 4, 30)},
1095                 {YearMonth.of(2009, 5), LocalDate.of(2009, 5, 31)},
1096                 {YearMonth.of(2009, 6), LocalDate.of(2009, 6, 30)},
1097                 {YearMonth.of(2009, 12), LocalDate.of(2009, 12, 31)},
1098         };
1099     }
1100 
1101     @Test(dataProvider="atEndOfMonth")
1102     public void test_atEndOfMonth(YearMonth test, LocalDate expected) {
1103         assertEquals(test.atEndOfMonth(), expected);
1104     }
1105 
1106     //-----------------------------------------------------------------------
1107     // compareTo()
1108     //-----------------------------------------------------------------------
1109     @Test
1110     public void test_comparisons() {
1111         doTest_comparisons_YearMonth(
1112             YearMonth.of(-1, 1),
1113             YearMonth.of(0, 1),
1114             YearMonth.of(0, 12),
1115             YearMonth.of(1, 1),
1116             YearMonth.of(1, 2),
1117             YearMonth.of(1, 12),
1118             YearMonth.of(2008, 1),
1119             YearMonth.of(2008, 6),
1120             YearMonth.of(2008, 12)
1121         );
1122     }
1123 
1124     void doTest_comparisons_YearMonth(YearMonth... localDates) {
1125         for (int i = 0; i < localDates.length; i++) {
1126             YearMonth a = localDates[i];
1127             for (int j = 0; j < localDates.length; j++) {
1128                 YearMonth b = localDates[j];
1129                 if (i < j) {
1130                     assertTrue(a.compareTo(b) < 0, a + " <=> " + b);
1131                     assertEquals(a.isBefore(b), true, a + " <=> " + b);
1132                     assertEquals(a.isAfter(b), false, a + " <=> " + b);
1133                     assertEquals(a.equals(b), false, a + " <=> " + b);
1134                 } else if (i > j) {
1135                     assertTrue(a.compareTo(b) > 0, a + " <=> " + b);
1136                     assertEquals(a.isBefore(b), false, a + " <=> " + b);
1137                     assertEquals(a.isAfter(b), true, a + " <=> " + b);
1138                     assertEquals(a.equals(b), false, a + " <=> " + b);
1139                 } else {
1140                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);
1141                     assertEquals(a.isBefore(b), false, a + " <=> " + b);
1142                     assertEquals(a.isAfter(b), false, a + " <=> " + b);
1143                     assertEquals(a.equals(b), true, a + " <=> " + b);
1144                 }
1145             }
1146         }
1147     }
1148 
1149     @Test(expectedExceptions=NullPointerException.class)
1150     public void test_compareTo_ObjectNull() {
1151         TEST_2008_06.compareTo(null);
1152     }
1153 
1154     @Test(expectedExceptions=NullPointerException.class)
1155     public void test_isBefore_ObjectNull() {
1156         TEST_2008_06.isBefore(null);
1157     }
1158 
1159     @Test(expectedExceptions=NullPointerException.class)
1160     public void test_isAfter_ObjectNull() {
1161         TEST_2008_06.isAfter(null);
1162     }
1163 
1164     //-----------------------------------------------------------------------
1165     // equals()
1166     //-----------------------------------------------------------------------
1167     @Test
1168     public void test_equals() {
1169         YearMonth a = YearMonth.of(2008, 6);
1170         YearMonth b = YearMonth.of(2008, 6);
1171         YearMonth c = YearMonth.of(2007, 6);
1172         YearMonth d = YearMonth.of(2008, 5);
1173 
1174         assertEquals(a.equals(a), true);
1175         assertEquals(a.equals(b), true);
1176         assertEquals(a.equals(c), false);
1177         assertEquals(a.equals(d), false);
1178 
1179         assertEquals(b.equals(a), true);
1180         assertEquals(b.equals(b), true);
1181         assertEquals(b.equals(c), false);
1182         assertEquals(b.equals(d), false);
1183 
1184         assertEquals(c.equals(a), false);
1185         assertEquals(c.equals(b), false);
1186         assertEquals(c.equals(c), true);
1187         assertEquals(c.equals(d), false);
1188 
1189         assertEquals(d.equals(a), false);
1190         assertEquals(d.equals(b), false);
1191         assertEquals(d.equals(c), false);
1192         assertEquals(d.equals(d), true);
1193     }
1194 
1195     @Test
1196     public void test_equals_itself_true() {
1197         assertEquals(TEST_2008_06.equals(TEST_2008_06), true);
1198     }
1199 
1200     @Test
1201     public void test_equals_string_false() {
1202         assertEquals(TEST_2008_06.equals("2007-07-15"), false);
1203     }
1204 
1205     @Test
1206     public void test_equals_null_false() {
1207         assertEquals(TEST_2008_06.equals(null), false);
1208     }
1209 
1210     //-----------------------------------------------------------------------
1211     // hashCode()
1212     //-----------------------------------------------------------------------
1213     @Test(dataProvider="sampleDates")
1214     public void test_hashCode(int y, int m) {
1215         YearMonth a = YearMonth.of(y, m);
1216         assertEquals(a.hashCode(), a.hashCode());
1217         YearMonth b = YearMonth.of(y, m);
1218         assertEquals(a.hashCode(), b.hashCode());
1219     }
1220 
1221     @Test
1222     public void test_hashCode_unique() {
1223         Set<Integer> uniques = new HashSet<Integer>(201 * 12);
1224         for (int i = 1900; i <= 2100; i++) {
1225             for (int j = 1; j <= 12; j++) {
1226                 assertTrue(uniques.add(YearMonth.of(i, j).hashCode()));
1227             }
1228         }
1229     }
1230 
1231     //-----------------------------------------------------------------------
1232     // toString()
1233     //-----------------------------------------------------------------------
1234     @DataProvider(name="sampleToString")
1235     Object[][] provider_sampleToString() {
1236         return new Object[][] {
1237             {2008, 1, "2008-01"},
1238             {2008, 12, "2008-12"},
1239             {7, 5, "0007-05"},
1240             {0, 5, "0000-05"},
1241             {-1, 1, "-0001-01"},
1242         };
1243     }
1244 
1245     @Test(dataProvider="sampleToString")
1246     public void test_toString(int y, int m, String expected) {
1247         YearMonth test = YearMonth.of(y, m);
1248         String str = test.toString();
1249         assertEquals(str, expected);
1250     }
1251 
1252     private YearMonth ym(int year, int month) {
1253         return YearMonth.of(year, month);











1254     }
1255 
1256 }