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