1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp.temporal; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertTrue; 36 import static org.testng.Assert.fail; 37 import static org.threeten.bp.temporal.ChronoField.DAY_OF_MONTH; 38 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR; 39 40 import java.io.IOException; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.HashSet; 44 import java.util.List; 45 import java.util.Set; 46 47 import org.testng.annotations.BeforeMethod; 48 import org.testng.annotations.DataProvider; 49 import org.testng.annotations.Test; 50 import org.threeten.bp.AbstractDateTimeTest; 51 import org.threeten.bp.Clock; 52 import org.threeten.bp.DateTimeException; 53 import org.threeten.bp.Instant; 54 import org.threeten.bp.LocalDate; 55 import org.threeten.bp.LocalDateTime; 56 import org.threeten.bp.LocalTime; 57 import org.threeten.bp.Month; 58 import org.threeten.bp.MonthDay; 59 import org.threeten.bp.YearMonth; 60 import org.threeten.bp.ZoneId; 61 import org.threeten.bp.ZoneOffset; 62 import org.threeten.bp.chrono.IsoChronology; 63 import org.threeten.bp.format.DateTimeFormatter; 64 import org.threeten.bp.format.DateTimeParseException; 65 66 /** 67 * Test MonthDay. 68 */ 69 @Test 70 public class TestMonthDay extends AbstractDateTimeTest { 71 72 private MonthDay TEST_07_15; 73 74 @BeforeMethod setUp()75 public void setUp() { 76 TEST_07_15 = MonthDay.of(7, 15); 77 } 78 79 //----------------------------------------------------------------------- 80 @Override samples()81 protected List<TemporalAccessor> samples() { 82 TemporalAccessor[] array = {TEST_07_15, }; 83 return Arrays.asList(array); 84 } 85 86 @Override validFields()87 protected List<TemporalField> validFields() { 88 TemporalField[] array = { 89 DAY_OF_MONTH, 90 MONTH_OF_YEAR, 91 }; 92 return Arrays.asList(array); 93 } 94 95 @Override invalidFields()96 protected List<TemporalField> invalidFields() { 97 List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values())); 98 list.removeAll(validFields()); 99 list.add(JulianFields.JULIAN_DAY); 100 list.add(JulianFields.MODIFIED_JULIAN_DAY); 101 list.add(JulianFields.RATA_DIE); 102 return list; 103 } 104 105 //----------------------------------------------------------------------- 106 @Test test_immutable()107 public void test_immutable() { 108 assertImmutable(YearMonth.class); 109 } 110 111 @Test test_serialization()112 public void test_serialization() throws ClassNotFoundException, IOException { 113 assertSerializable(TEST_07_15); 114 } 115 116 @Test test_serialization_format()117 public void test_serialization_format() throws ClassNotFoundException, IOException { 118 assertEqualsSerialisedForm(MonthDay.of(9, 16)); 119 } 120 121 //----------------------------------------------------------------------- check(MonthDay test, int m, int d)122 void check(MonthDay test, int m, int d) { 123 assertEquals(test.getMonth().getValue(), m); 124 assertEquals(test.getDayOfMonth(), d); 125 } 126 127 //----------------------------------------------------------------------- 128 // now() 129 //----------------------------------------------------------------------- 130 @Test now()131 public void now() { 132 MonthDay expected = MonthDay.now(Clock.systemDefaultZone()); 133 MonthDay test = MonthDay.now(); 134 for (int i = 0; i < 100; i++) { 135 if (expected.equals(test)) { 136 return; 137 } 138 expected = MonthDay.now(Clock.systemDefaultZone()); 139 test = MonthDay.now(); 140 } 141 assertEquals(test, expected); 142 } 143 144 //----------------------------------------------------------------------- 145 // now(ZoneId) 146 //----------------------------------------------------------------------- 147 @Test(expectedExceptions=NullPointerException.class) now_ZoneId_nullZoneId()148 public void now_ZoneId_nullZoneId() { 149 MonthDay.now((ZoneId) null); 150 } 151 152 @Test now_ZoneId()153 public void now_ZoneId() { 154 ZoneId zone = ZoneId.of("UTC+01:02:03"); 155 MonthDay expected = MonthDay.now(Clock.system(zone)); 156 MonthDay test = MonthDay.now(zone); 157 for (int i = 0; i < 100; i++) { 158 if (expected.equals(test)) { 159 return; 160 } 161 expected = MonthDay.now(Clock.system(zone)); 162 test = MonthDay.now(zone); 163 } 164 assertEquals(test, expected); 165 } 166 167 //----------------------------------------------------------------------- 168 // now(Clock) 169 //----------------------------------------------------------------------- 170 @Test now_Clock()171 public void now_Clock() { 172 Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC); 173 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 174 MonthDay test = MonthDay.now(clock); 175 assertEquals(test.getMonth(), Month.DECEMBER); 176 assertEquals(test.getDayOfMonth(), 31); 177 } 178 179 @Test(expectedExceptions=NullPointerException.class) now_Clock_nullClock()180 public void now_Clock_nullClock() { 181 MonthDay.now((Clock) null); 182 } 183 184 //----------------------------------------------------------------------- 185 @Test factory_intMonth()186 public void factory_intMonth() { 187 assertEquals(TEST_07_15, MonthDay.of(Month.JULY, 15)); 188 } 189 190 @Test(expectedExceptions=DateTimeException.class) test_factory_intMonth_dayTooLow()191 public void test_factory_intMonth_dayTooLow() { 192 MonthDay.of(Month.JANUARY, 0); 193 } 194 195 @Test(expectedExceptions=DateTimeException.class) test_factory_intMonth_dayTooHigh()196 public void test_factory_intMonth_dayTooHigh() { 197 MonthDay.of(Month.JANUARY, 32); 198 } 199 200 @Test(expectedExceptions=NullPointerException.class) factory_intMonth_nullMonth()201 public void factory_intMonth_nullMonth() { 202 MonthDay.of(null, 15); 203 } 204 205 //----------------------------------------------------------------------- 206 @Test factory_ints()207 public void factory_ints() { 208 check(TEST_07_15, 7, 15); 209 } 210 211 @Test(expectedExceptions=DateTimeException.class) test_factory_ints_dayTooLow()212 public void test_factory_ints_dayTooLow() { 213 MonthDay.of(1, 0); 214 } 215 216 @Test(expectedExceptions=DateTimeException.class) test_factory_ints_dayTooHigh()217 public void test_factory_ints_dayTooHigh() { 218 MonthDay.of(1, 32); 219 } 220 221 222 @Test(expectedExceptions=DateTimeException.class) test_factory_ints_monthTooLow()223 public void test_factory_ints_monthTooLow() { 224 MonthDay.of(0, 1); 225 } 226 227 @Test(expectedExceptions=DateTimeException.class) test_factory_ints_monthTooHigh()228 public void test_factory_ints_monthTooHigh() { 229 MonthDay.of(13, 1); 230 } 231 232 //----------------------------------------------------------------------- 233 @Test test_factory_CalendricalObject()234 public void test_factory_CalendricalObject() { 235 assertEquals(MonthDay.from(LocalDate.of(2007, 7, 15)), TEST_07_15); 236 } 237 238 @Test(expectedExceptions=DateTimeException.class) test_factory_CalendricalObject_invalid_noDerive()239 public void test_factory_CalendricalObject_invalid_noDerive() { 240 MonthDay.from(LocalTime.of(12, 30)); 241 } 242 243 @Test(expectedExceptions=NullPointerException.class) test_factory_CalendricalObject_null()244 public void test_factory_CalendricalObject_null() { 245 MonthDay.from((TemporalAccessor) null); 246 } 247 248 //----------------------------------------------------------------------- 249 // parse() 250 //----------------------------------------------------------------------- 251 @DataProvider(name="goodParseData") provider_goodParseData()252 Object[][] provider_goodParseData() { 253 return new Object[][] { 254 {"--01-01", MonthDay.of(1, 1)}, 255 {"--01-31", MonthDay.of(1, 31)}, 256 {"--02-01", MonthDay.of(2, 1)}, 257 {"--02-29", MonthDay.of(2, 29)}, 258 {"--03-01", MonthDay.of(3, 1)}, 259 {"--03-31", MonthDay.of(3, 31)}, 260 {"--04-01", MonthDay.of(4, 1)}, 261 {"--04-30", MonthDay.of(4, 30)}, 262 {"--05-01", MonthDay.of(5, 1)}, 263 {"--05-31", MonthDay.of(5, 31)}, 264 {"--06-01", MonthDay.of(6, 1)}, 265 {"--06-30", MonthDay.of(6, 30)}, 266 {"--07-01", MonthDay.of(7, 1)}, 267 {"--07-31", MonthDay.of(7, 31)}, 268 {"--08-01", MonthDay.of(8, 1)}, 269 {"--08-31", MonthDay.of(8, 31)}, 270 {"--09-01", MonthDay.of(9, 1)}, 271 {"--09-30", MonthDay.of(9, 30)}, 272 {"--10-01", MonthDay.of(10, 1)}, 273 {"--10-31", MonthDay.of(10, 31)}, 274 {"--11-01", MonthDay.of(11, 1)}, 275 {"--11-30", MonthDay.of(11, 30)}, 276 {"--12-01", MonthDay.of(12, 1)}, 277 {"--12-31", MonthDay.of(12, 31)}, 278 }; 279 } 280 281 @Test(dataProvider="goodParseData") factory_parse_success(String text, MonthDay expected)282 public void factory_parse_success(String text, MonthDay expected) { 283 MonthDay monthDay = MonthDay.parse(text); 284 assertEquals(monthDay, expected); 285 } 286 287 //----------------------------------------------------------------------- 288 @DataProvider(name="badParseData") provider_badParseData()289 Object[][] provider_badParseData() { 290 return new Object[][] { 291 {"", 0}, 292 {"-00", 0}, 293 {"--FEB-23", 2}, 294 {"--01-0", 5}, 295 {"--01-3A", 5}, 296 }; 297 } 298 299 @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class) factory_parse_fail(String text, int pos)300 public void factory_parse_fail(String text, int pos) { 301 try { 302 MonthDay.parse(text); 303 fail(String.format("Parse should have failed for %s at position %d", text, pos)); 304 } 305 catch (DateTimeParseException ex) { 306 assertEquals(ex.getParsedString(), text); 307 assertEquals(ex.getErrorIndex(), pos); 308 throw ex; 309 } 310 } 311 312 //----------------------------------------------------------------------- 313 @Test(expectedExceptions=DateTimeParseException.class) factory_parse_illegalValue_Day()314 public void factory_parse_illegalValue_Day() { 315 MonthDay.parse("--06-32"); 316 } 317 318 @Test(expectedExceptions=DateTimeParseException.class) factory_parse_invalidValue_Day()319 public void factory_parse_invalidValue_Day() { 320 MonthDay.parse("--06-31"); 321 } 322 323 @Test(expectedExceptions=DateTimeParseException.class) factory_parse_illegalValue_Month()324 public void factory_parse_illegalValue_Month() { 325 MonthDay.parse("--13-25"); 326 } 327 328 @Test(expectedExceptions=NullPointerException.class) factory_parse_nullText()329 public void factory_parse_nullText() { 330 MonthDay.parse(null); 331 } 332 333 //----------------------------------------------------------------------- 334 // parse(DateTimeFormatter) 335 //----------------------------------------------------------------------- 336 @Test factory_parse_formatter()337 public void factory_parse_formatter() { 338 DateTimeFormatter f = DateTimeFormatter.ofPattern("M d"); 339 MonthDay test = MonthDay.parse("12 3", f); 340 assertEquals(test, MonthDay.of(12, 3)); 341 } 342 343 @Test(expectedExceptions=NullPointerException.class) factory_parse_formatter_nullText()344 public void factory_parse_formatter_nullText() { 345 DateTimeFormatter f = DateTimeFormatter.ofPattern("M d"); 346 MonthDay.parse((String) null, f); 347 } 348 349 @Test(expectedExceptions=NullPointerException.class) factory_parse_formatter_nullFormatter()350 public void factory_parse_formatter_nullFormatter() { 351 MonthDay.parse("ANY", null); 352 } 353 354 //----------------------------------------------------------------------- 355 // get(DateTimeField) 356 //----------------------------------------------------------------------- 357 @Test test_get_DateTimeField()358 public void test_get_DateTimeField() { 359 assertEquals(TEST_07_15.getLong(ChronoField.DAY_OF_MONTH), 15); 360 assertEquals(TEST_07_15.getLong(ChronoField.MONTH_OF_YEAR), 7); 361 } 362 363 @Test(expectedExceptions=NullPointerException.class) test_get_DateTimeField_null()364 public void test_get_DateTimeField_null() { 365 TEST_07_15.getLong((TemporalField) null); 366 } 367 368 @Test(expectedExceptions=DateTimeException.class) test_get_DateTimeField_invalidField()369 public void test_get_DateTimeField_invalidField() { 370 TEST_07_15.getLong(MockFieldNoValue.INSTANCE); 371 } 372 373 @Test(expectedExceptions=DateTimeException.class) test_get_DateTimeField_timeField()374 public void test_get_DateTimeField_timeField() { 375 TEST_07_15.getLong(ChronoField.AMPM_OF_DAY); 376 } 377 378 //----------------------------------------------------------------------- 379 // get*() 380 //----------------------------------------------------------------------- 381 @DataProvider(name="sampleDates") provider_sampleDates()382 Object[][] provider_sampleDates() { 383 return new Object[][] { 384 {1, 1}, 385 {1, 31}, 386 {2, 1}, 387 {2, 28}, 388 {2, 29}, 389 {7, 4}, 390 {7, 5}, 391 }; 392 } 393 394 @Test(dataProvider="sampleDates") test_get(int m, int d)395 public void test_get(int m, int d) { 396 MonthDay a = MonthDay.of(m, d); 397 assertEquals(a.getMonth(), Month.of(m)); 398 assertEquals(a.getDayOfMonth(), d); 399 } 400 401 //----------------------------------------------------------------------- 402 // with(Month) 403 //----------------------------------------------------------------------- 404 @Test test_with_Month()405 public void test_with_Month() { 406 assertEquals(MonthDay.of(6, 30).with(Month.JANUARY), MonthDay.of(1, 30)); 407 } 408 409 @Test test_with_Month_adjustToValid()410 public void test_with_Month_adjustToValid() { 411 assertEquals(MonthDay.of(7, 31).with(Month.JUNE), MonthDay.of(6, 30)); 412 } 413 414 @Test test_with_Month_adjustToValidFeb()415 public void test_with_Month_adjustToValidFeb() { 416 assertEquals(MonthDay.of(7, 31).with(Month.FEBRUARY), MonthDay.of(2, 29)); 417 } 418 419 @Test test_with_Month_noChangeEqual()420 public void test_with_Month_noChangeEqual() { 421 MonthDay test = MonthDay.of(6, 30); 422 assertEquals(test.with(Month.JUNE), test); 423 } 424 425 @Test(expectedExceptions=NullPointerException.class) test_with_Month_null()426 public void test_with_Month_null() { 427 MonthDay.of(6, 30).with((Month) null); 428 } 429 430 //----------------------------------------------------------------------- 431 // withMonth() 432 //----------------------------------------------------------------------- 433 @Test test_withMonth()434 public void test_withMonth() { 435 assertEquals(MonthDay.of(6, 30).withMonth(1), MonthDay.of(1, 30)); 436 } 437 438 @Test test_withMonth_adjustToValid()439 public void test_withMonth_adjustToValid() { 440 assertEquals(MonthDay.of(7, 31).withMonth(6), MonthDay.of(6, 30)); 441 } 442 443 @Test test_withMonth_adjustToValidFeb()444 public void test_withMonth_adjustToValidFeb() { 445 assertEquals(MonthDay.of(7, 31).withMonth(2), MonthDay.of(2, 29)); 446 } 447 448 @Test test_withMonth_int_noChangeEqual()449 public void test_withMonth_int_noChangeEqual() { 450 MonthDay test = MonthDay.of(6, 30); 451 assertEquals(test.withMonth(6), test); 452 } 453 454 @Test(expectedExceptions=DateTimeException.class) test_withMonth_tooLow()455 public void test_withMonth_tooLow() { 456 MonthDay.of(6, 30).withMonth(0); 457 } 458 459 @Test(expectedExceptions=DateTimeException.class) test_withMonth_tooHigh()460 public void test_withMonth_tooHigh() { 461 MonthDay.of(6, 30).withMonth(13); 462 } 463 464 //----------------------------------------------------------------------- 465 // withDayOfMonth() 466 //----------------------------------------------------------------------- 467 @Test test_withDayOfMonth()468 public void test_withDayOfMonth() { 469 assertEquals(MonthDay.of(6, 30).withDayOfMonth(1), MonthDay.of(6, 1)); 470 } 471 472 @Test(expectedExceptions=DateTimeException.class) test_withDayOfMonth_invalid()473 public void test_withDayOfMonth_invalid() { 474 MonthDay.of(6, 30).withDayOfMonth(31); 475 } 476 477 @Test test_withDayOfMonth_adjustToValidFeb()478 public void test_withDayOfMonth_adjustToValidFeb() { 479 assertEquals(MonthDay.of(2, 1).withDayOfMonth(29), MonthDay.of(2, 29)); 480 } 481 482 @Test test_withDayOfMonth_noChangeEqual()483 public void test_withDayOfMonth_noChangeEqual() { 484 MonthDay test = MonthDay.of(6, 30); 485 assertEquals(test.withDayOfMonth(30), test); 486 } 487 488 @Test(expectedExceptions=DateTimeException.class) test_withDayOfMonth_tooLow()489 public void test_withDayOfMonth_tooLow() { 490 MonthDay.of(6, 30).withDayOfMonth(0); 491 } 492 493 @Test(expectedExceptions=DateTimeException.class) test_withDayOfMonth_tooHigh()494 public void test_withDayOfMonth_tooHigh() { 495 MonthDay.of(6, 30).withDayOfMonth(32); 496 } 497 498 //----------------------------------------------------------------------- 499 // adjust() 500 //----------------------------------------------------------------------- 501 @Test test_adjustDate()502 public void test_adjustDate() { 503 MonthDay test = MonthDay.of(6, 30); 504 LocalDate date = LocalDate.of(2007, 1, 1); 505 assertEquals(test.adjustInto(date), LocalDate.of(2007, 6, 30)); 506 } 507 508 @Test test_adjustDate_resolve()509 public void test_adjustDate_resolve() { 510 MonthDay test = MonthDay.of(2, 29); 511 LocalDate date = LocalDate.of(2007, 6, 30); 512 assertEquals(test.adjustInto(date), LocalDate.of(2007, 2, 28)); 513 } 514 515 @Test test_adjustDate_equal()516 public void test_adjustDate_equal() { 517 MonthDay test = MonthDay.of(6, 30); 518 LocalDate date = LocalDate.of(2007, 6, 30); 519 assertEquals(test.adjustInto(date), date); 520 } 521 522 @Test(expectedExceptions=NullPointerException.class) test_adjustDate_null()523 public void test_adjustDate_null() { 524 TEST_07_15.adjustInto((LocalDate) null); 525 } 526 527 //----------------------------------------------------------------------- 528 // isValidYear(int) 529 //----------------------------------------------------------------------- 530 @Test test_isValidYear_june()531 public void test_isValidYear_june() { 532 MonthDay test = MonthDay.of(6, 30); 533 assertEquals(test.isValidYear(2007), true); 534 } 535 536 @Test test_isValidYear_febNonLeap()537 public void test_isValidYear_febNonLeap() { 538 MonthDay test = MonthDay.of(2, 29); 539 assertEquals(test.isValidYear(2007), false); 540 } 541 542 @Test test_isValidYear_febLeap()543 public void test_isValidYear_febLeap() { 544 MonthDay test = MonthDay.of(2, 29); 545 assertEquals(test.isValidYear(2008), true); 546 } 547 548 //----------------------------------------------------------------------- 549 // atYear(int) 550 //----------------------------------------------------------------------- 551 @Test test_atYear_int()552 public void test_atYear_int() { 553 MonthDay test = MonthDay.of(6, 30); 554 assertEquals(test.atYear(2008), LocalDate.of(2008, 6, 30)); 555 } 556 557 @Test test_atYear_int_leapYearAdjust()558 public void test_atYear_int_leapYearAdjust() { 559 MonthDay test = MonthDay.of(2, 29); 560 assertEquals(test.atYear(2005), LocalDate.of(2005, 2, 28)); 561 } 562 563 @Test(expectedExceptions=DateTimeException.class) test_atYear_int_invalidYear()564 public void test_atYear_int_invalidYear() { 565 MonthDay test = MonthDay.of(6, 30); 566 test.atYear(Integer.MIN_VALUE); 567 } 568 569 //----------------------------------------------------------------------- 570 // query(TemporalQuery) 571 //----------------------------------------------------------------------- 572 @Test test_query()573 public void test_query() { 574 assertEquals(TEST_07_15.query(TemporalQueries.chronology()), IsoChronology.INSTANCE); 575 assertEquals(TEST_07_15.query(TemporalQueries.localDate()), null); 576 assertEquals(TEST_07_15.query(TemporalQueries.localTime()), null); 577 assertEquals(TEST_07_15.query(TemporalQueries.offset()), null); 578 assertEquals(TEST_07_15.query(TemporalQueries.precision()), null); 579 assertEquals(TEST_07_15.query(TemporalQueries.zone()), null); 580 assertEquals(TEST_07_15.query(TemporalQueries.zoneId()), null); 581 } 582 583 @Test(expectedExceptions=NullPointerException.class) test_query_null()584 public void test_query_null() { 585 TEST_07_15.query(null); 586 } 587 588 //----------------------------------------------------------------------- 589 // compareTo() 590 //----------------------------------------------------------------------- 591 @Test test_comparisons()592 public void test_comparisons() { 593 doTest_comparisons_MonthDay( 594 MonthDay.of(1, 1), 595 MonthDay.of(1, 31), 596 MonthDay.of(2, 1), 597 MonthDay.of(2, 29), 598 MonthDay.of(3, 1), 599 MonthDay.of(12, 31) 600 ); 601 } 602 doTest_comparisons_MonthDay(MonthDay... localDates)603 void doTest_comparisons_MonthDay(MonthDay... localDates) { 604 for (int i = 0; i < localDates.length; i++) { 605 MonthDay a = localDates[i]; 606 for (int j = 0; j < localDates.length; j++) { 607 MonthDay b = localDates[j]; 608 if (i < j) { 609 assertTrue(a.compareTo(b) < 0, a + " <=> " + b); 610 assertEquals(a.isBefore(b), true, a + " <=> " + b); 611 assertEquals(a.isAfter(b), false, a + " <=> " + b); 612 assertEquals(a.equals(b), false, a + " <=> " + b); 613 } else if (i > j) { 614 assertTrue(a.compareTo(b) > 0, a + " <=> " + b); 615 assertEquals(a.isBefore(b), false, a + " <=> " + b); 616 assertEquals(a.isAfter(b), true, a + " <=> " + b); 617 assertEquals(a.equals(b), false, a + " <=> " + b); 618 } else { 619 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 620 assertEquals(a.isBefore(b), false, a + " <=> " + b); 621 assertEquals(a.isAfter(b), false, a + " <=> " + b); 622 assertEquals(a.equals(b), true, a + " <=> " + b); 623 } 624 } 625 } 626 } 627 628 @Test(expectedExceptions=NullPointerException.class) test_compareTo_ObjectNull()629 public void test_compareTo_ObjectNull() { 630 TEST_07_15.compareTo(null); 631 } 632 633 @Test(expectedExceptions=NullPointerException.class) test_isBefore_ObjectNull()634 public void test_isBefore_ObjectNull() { 635 TEST_07_15.isBefore(null); 636 } 637 638 @Test(expectedExceptions=NullPointerException.class) test_isAfter_ObjectNull()639 public void test_isAfter_ObjectNull() { 640 TEST_07_15.isAfter(null); 641 } 642 643 //----------------------------------------------------------------------- 644 // equals() 645 //----------------------------------------------------------------------- 646 @Test test_equals()647 public void test_equals() { 648 MonthDay a = MonthDay.of(1, 1); 649 MonthDay b = MonthDay.of(1, 1); 650 MonthDay c = MonthDay.of(2, 1); 651 MonthDay d = MonthDay.of(1, 2); 652 653 assertEquals(a.equals(a), true); 654 assertEquals(a.equals(b), true); 655 assertEquals(a.equals(c), false); 656 assertEquals(a.equals(d), false); 657 658 assertEquals(b.equals(a), true); 659 assertEquals(b.equals(b), true); 660 assertEquals(b.equals(c), false); 661 assertEquals(b.equals(d), false); 662 663 assertEquals(c.equals(a), false); 664 assertEquals(c.equals(b), false); 665 assertEquals(c.equals(c), true); 666 assertEquals(c.equals(d), false); 667 668 assertEquals(d.equals(a), false); 669 assertEquals(d.equals(b), false); 670 assertEquals(d.equals(c), false); 671 assertEquals(d.equals(d), true); 672 } 673 674 @Test test_equals_itself_true()675 public void test_equals_itself_true() { 676 assertEquals(TEST_07_15.equals(TEST_07_15), true); 677 } 678 679 @Test test_equals_string_false()680 public void test_equals_string_false() { 681 assertEquals(TEST_07_15.equals("2007-07-15"), false); 682 } 683 684 @Test test_equals_null_false()685 public void test_equals_null_false() { 686 assertEquals(TEST_07_15.equals(null), false); 687 } 688 689 //----------------------------------------------------------------------- 690 // hashCode() 691 //----------------------------------------------------------------------- 692 @Test(dataProvider="sampleDates") test_hashCode(int m, int d)693 public void test_hashCode(int m, int d) { 694 MonthDay a = MonthDay.of(m, d); 695 assertEquals(a.hashCode(), a.hashCode()); 696 MonthDay b = MonthDay.of(m, d); 697 assertEquals(a.hashCode(), b.hashCode()); 698 } 699 700 @Test test_hashCode_unique()701 public void test_hashCode_unique() { 702 int leapYear = 2008; 703 Set<Integer> uniques = new HashSet<Integer>(366); 704 for (int i = 1; i <= 12; i++) { 705 for (int j = 1; j <= 31; j++) { 706 if (YearMonth.of(leapYear, i).isValidDay(j)) { 707 assertTrue(uniques.add(MonthDay.of(i, j).hashCode())); 708 } 709 } 710 } 711 } 712 713 //----------------------------------------------------------------------- 714 // toString() 715 //----------------------------------------------------------------------- 716 @DataProvider(name="sampleToString") provider_sampleToString()717 Object[][] provider_sampleToString() { 718 return new Object[][] { 719 {7, 5, "--07-05"}, 720 {12, 31, "--12-31"}, 721 {1, 2, "--01-02"}, 722 }; 723 } 724 725 @Test(dataProvider="sampleToString") test_toString(int m, int d, String expected)726 public void test_toString(int m, int d, String expected) { 727 MonthDay test = MonthDay.of(m, d); 728 String str = test.toString(); 729 assertEquals(str, expected); 730 } 731 732 //----------------------------------------------------------------------- 733 // format(DateTimeFormatter) 734 //----------------------------------------------------------------------- 735 @Test test_format_formatter()736 public void test_format_formatter() { 737 DateTimeFormatter f = DateTimeFormatter.ofPattern("M d"); 738 String t = MonthDay.of(12, 3).format(f); 739 assertEquals(t, "12 3"); 740 } 741 742 @Test(expectedExceptions=NullPointerException.class) test_format_formatter_null()743 public void test_format_formatter_null() { 744 MonthDay.of(12, 3).format(null); 745 } 746 747 } 748