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