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; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertFalse; 36 import static org.testng.Assert.assertNotNull; 37 import static org.testng.Assert.assertSame; 38 import static org.testng.Assert.assertTrue; 39 import static org.threeten.bp.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 40 import static org.threeten.bp.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 41 import static org.threeten.bp.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 42 import static org.threeten.bp.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 43 import static org.threeten.bp.temporal.ChronoField.DAY_OF_MONTH; 44 import static org.threeten.bp.temporal.ChronoField.DAY_OF_WEEK; 45 import static org.threeten.bp.temporal.ChronoField.DAY_OF_YEAR; 46 import static org.threeten.bp.temporal.ChronoField.EPOCH_DAY; 47 import static org.threeten.bp.temporal.ChronoField.ERA; 48 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR; 49 import static org.threeten.bp.temporal.ChronoField.PROLEPTIC_MONTH; 50 import static org.threeten.bp.temporal.ChronoField.YEAR; 51 import static org.threeten.bp.temporal.ChronoField.YEAR_OF_ERA; 52 import static org.threeten.bp.temporal.ChronoUnit.CENTURIES; 53 import static org.threeten.bp.temporal.ChronoUnit.DAYS; 54 import static org.threeten.bp.temporal.ChronoUnit.DECADES; 55 import static org.threeten.bp.temporal.ChronoUnit.MILLENNIA; 56 import static org.threeten.bp.temporal.ChronoUnit.MONTHS; 57 import static org.threeten.bp.temporal.ChronoUnit.WEEKS; 58 import static org.threeten.bp.temporal.ChronoUnit.YEARS; 59 60 import java.io.ByteArrayInputStream; 61 import java.io.ByteArrayOutputStream; 62 import java.io.IOException; 63 import java.io.ObjectInputStream; 64 import java.io.ObjectOutputStream; 65 import java.lang.reflect.Field; 66 import java.lang.reflect.Modifier; 67 import java.util.ArrayList; 68 import java.util.Arrays; 69 import java.util.List; 70 71 import org.testng.annotations.BeforeMethod; 72 import org.testng.annotations.DataProvider; 73 import org.testng.annotations.Test; 74 import org.threeten.bp.chrono.IsoChronology; 75 import org.threeten.bp.format.DateTimeFormatter; 76 import org.threeten.bp.format.DateTimeParseException; 77 import org.threeten.bp.temporal.ChronoField; 78 import org.threeten.bp.temporal.ChronoUnit; 79 import org.threeten.bp.temporal.JulianFields; 80 import org.threeten.bp.temporal.MockFieldNoValue; 81 import org.threeten.bp.temporal.Temporal; 82 import org.threeten.bp.temporal.TemporalAccessor; 83 import org.threeten.bp.temporal.TemporalAdjuster; 84 import org.threeten.bp.temporal.TemporalField; 85 import org.threeten.bp.temporal.TemporalQueries; 86 import org.threeten.bp.temporal.TemporalUnit; 87 88 /** 89 * Test LocalDate. 90 */ 91 @Test 92 public class TestLocalDate extends AbstractDateTimeTest { 93 94 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1); 95 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 96 private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza"); 97 98 private LocalDate TEST_2007_07_15; 99 private long MAX_VALID_EPOCHDAYS; 100 private long MIN_VALID_EPOCHDAYS; 101 private LocalDate MAX_DATE; 102 private LocalDate MIN_DATE; 103 private Instant MAX_INSTANT; 104 private Instant MIN_INSTANT; 105 106 @BeforeMethod setUp()107 public void setUp() { 108 TEST_2007_07_15 = LocalDate.of(2007, 7, 15); 109 110 LocalDate max = LocalDate.MAX; 111 LocalDate min = LocalDate.MIN; 112 MAX_VALID_EPOCHDAYS = max.toEpochDay(); 113 MIN_VALID_EPOCHDAYS = min.toEpochDay(); 114 MAX_DATE = max; 115 MIN_DATE = min; 116 MAX_INSTANT = max.atStartOfDay(ZoneOffset.UTC).toInstant(); 117 MIN_INSTANT = min.atStartOfDay(ZoneOffset.UTC).toInstant(); 118 } 119 120 //----------------------------------------------------------------------- 121 @Override samples()122 protected List<TemporalAccessor> samples() { 123 TemporalAccessor[] array = {TEST_2007_07_15, LocalDate.MAX, LocalDate.MIN, }; 124 return Arrays.asList(array); 125 } 126 127 @Override validFields()128 protected List<TemporalField> validFields() { 129 TemporalField[] array = { 130 DAY_OF_WEEK, 131 ALIGNED_DAY_OF_WEEK_IN_MONTH, 132 ALIGNED_DAY_OF_WEEK_IN_YEAR, 133 DAY_OF_MONTH, 134 DAY_OF_YEAR, 135 EPOCH_DAY, 136 ALIGNED_WEEK_OF_MONTH, 137 ALIGNED_WEEK_OF_YEAR, 138 MONTH_OF_YEAR, 139 PROLEPTIC_MONTH, 140 YEAR_OF_ERA, 141 YEAR, 142 ERA, 143 JulianFields.JULIAN_DAY, 144 JulianFields.MODIFIED_JULIAN_DAY, 145 JulianFields.RATA_DIE, 146 }; 147 return Arrays.asList(array); 148 } 149 150 @Override invalidFields()151 protected List<TemporalField> invalidFields() { 152 List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values())); 153 list.removeAll(validFields()); 154 return list; 155 } 156 157 @Test test_serialization()158 public void test_serialization() throws IOException, ClassNotFoundException { 159 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 160 ObjectOutputStream oos = new ObjectOutputStream(baos); 161 oos.writeObject(TEST_2007_07_15); 162 oos.close(); 163 164 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( 165 baos.toByteArray())); 166 assertEquals(ois.readObject(), TEST_2007_07_15); 167 } 168 169 @Test test_immutable()170 public void test_immutable() { 171 Class<LocalDate> cls = LocalDate.class; 172 assertTrue(Modifier.isPublic(cls.getModifiers())); 173 assertTrue(Modifier.isFinal(cls.getModifiers())); 174 Field[] fields = cls.getDeclaredFields(); 175 for (Field field : fields) { 176 if (field.getName().contains("$") == false) { 177 if (Modifier.isStatic(field.getModifiers())) { 178 assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName()); 179 } else { 180 assertTrue(Modifier.isPrivate(field.getModifiers()), "Field:" + field.getName()); 181 assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName()); 182 } 183 } 184 } 185 } 186 187 //----------------------------------------------------------------------- check(LocalDate test_2008_02_29, int y, int m, int d)188 private void check(LocalDate test_2008_02_29, int y, int m, int d) { 189 assertEquals(test_2008_02_29.getYear(), y); 190 assertEquals(test_2008_02_29.getMonth().getValue(), m); 191 assertEquals(test_2008_02_29.getDayOfMonth(), d); 192 } 193 194 //----------------------------------------------------------------------- 195 // now() 196 //----------------------------------------------------------------------- 197 @Test now()198 public void now() { 199 LocalDate expected = LocalDate.now(Clock.systemDefaultZone()); 200 LocalDate test = LocalDate.now(); 201 for (int i = 0; i < 100; i++) { 202 if (expected.equals(test)) { 203 return; 204 } 205 expected = LocalDate.now(Clock.systemDefaultZone()); 206 test = LocalDate.now(); 207 } 208 assertEquals(test, expected); 209 } 210 211 //----------------------------------------------------------------------- 212 // now(ZoneId) 213 //----------------------------------------------------------------------- 214 @Test(expectedExceptions=NullPointerException.class) now_ZoneId_nullZoneId()215 public void now_ZoneId_nullZoneId() { 216 LocalDate.now((ZoneId) null); 217 } 218 219 @Test now_ZoneId()220 public void now_ZoneId() { 221 ZoneId zone = ZoneId.of("UTC+01:02:03"); 222 LocalDate expected = LocalDate.now(Clock.system(zone)); 223 LocalDate test = LocalDate.now(zone); 224 for (int i = 0; i < 100; i++) { 225 if (expected.equals(test)) { 226 return; 227 } 228 expected = LocalDate.now(Clock.system(zone)); 229 test = LocalDate.now(zone); 230 } 231 assertEquals(test, expected); 232 } 233 234 //----------------------------------------------------------------------- 235 // now(Clock) 236 //----------------------------------------------------------------------- 237 @Test(expectedExceptions=NullPointerException.class) now_Clock_nullClock()238 public void now_Clock_nullClock() { 239 LocalDate.now((Clock) null); 240 } 241 242 @Test now_Clock_allSecsInDay_utc()243 public void now_Clock_allSecsInDay_utc() { 244 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 245 Instant instant = Instant.ofEpochSecond(i); 246 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 247 LocalDate test = LocalDate.now(clock); 248 assertEquals(test.getYear(), 1970); 249 assertEquals(test.getMonth(), Month.JANUARY); 250 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2)); 251 } 252 } 253 254 @Test 255 public void now_Clock_allSecsInDay_offset() { 256 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 257 Instant instant = Instant.ofEpochSecond(i); 258 Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE); 259 LocalDate test = LocalDate.now(clock); 260 assertEquals(test.getYear(), 1970); 261 assertEquals(test.getMonth(), Month.JANUARY); 262 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2); 263 } 264 } 265 266 @Test 267 public void now_Clock_allSecsInDay_beforeEpoch() { 268 for (int i =-1; i >= -(2 * 24 * 60 * 60); i--) { 269 Instant instant = Instant.ofEpochSecond(i); 270 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 271 LocalDate test = LocalDate.now(clock); 272 assertEquals(test.getYear(), 1969); 273 assertEquals(test.getMonth(), Month.DECEMBER); 274 assertEquals(test.getDayOfMonth(), (i >= -24 * 60 * 60 ? 31 : 30)); 275 } 276 } 277 278 //----------------------------------------------------------------------- 279 @Test 280 public void now_Clock_maxYear() { 281 Clock clock = Clock.fixed(MAX_INSTANT, ZoneOffset.UTC); 282 LocalDate test = LocalDate.now(clock); 283 assertEquals(test, MAX_DATE); 284 } 285 286 @Test(expectedExceptions=DateTimeException.class) 287 public void now_Clock_tooBig() { 288 Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC); 289 LocalDate.now(clock); 290 } 291 292 @Test 293 public void now_Clock_minYear() { 294 Clock clock = Clock.fixed(MIN_INSTANT, ZoneOffset.UTC); 295 LocalDate test = LocalDate.now(clock); 296 assertEquals(test, MIN_DATE); 297 } 298 299 @Test(expectedExceptions=DateTimeException.class) 300 public void now_Clock_tooLow() { 301 Clock clock = Clock.fixed(MIN_INSTANT.minusNanos(1), ZoneOffset.UTC); 302 LocalDate.now(clock); 303 } 304 305 //----------------------------------------------------------------------- 306 // of() factories 307 //----------------------------------------------------------------------- 308 @Test 309 public void factory_of_intsMonth() { 310 assertEquals(TEST_2007_07_15, LocalDate.of(2007, Month.JULY, 15)); 311 } 312 313 @Test(expectedExceptions=DateTimeException.class) 314 public void factory_of_intsMonth_29febNonLeap() { 315 LocalDate.of(2007, Month.FEBRUARY, 29); 316 } 317 318 @Test(expectedExceptions=DateTimeException.class) 319 public void factory_of_intsMonth_31apr() { 320 LocalDate.of(2007, Month.APRIL, 31); 321 } 322 323 @Test(expectedExceptions=DateTimeException.class) 324 public void factory_of_intsMonth_dayTooLow() { 325 LocalDate.of(2007, Month.JANUARY, 0); 326 } 327 328 @Test(expectedExceptions=DateTimeException.class) 329 public void factory_of_intsMonth_dayTooHigh() { 330 LocalDate.of(2007, Month.JANUARY, 32); 331 } 332 333 @Test(expectedExceptions=NullPointerException.class) 334 public void factory_of_intsMonth_nullMonth() { 335 LocalDate.of(2007, null, 30); 336 } 337 338 @Test(expectedExceptions=DateTimeException.class) 339 public void factory_of_intsMonth_yearTooLow() { 340 LocalDate.of(Integer.MIN_VALUE, Month.JANUARY, 1); 341 } 342 343 //----------------------------------------------------------------------- 344 @Test 345 public void factory_of_ints() { 346 check(TEST_2007_07_15, 2007, 7, 15); 347 } 348 349 @Test(expectedExceptions=DateTimeException.class) 350 public void factory_of_ints_29febNonLeap() { 351 LocalDate.of(2007, 2, 29); 352 } 353 354 @Test(expectedExceptions=DateTimeException.class) 355 public void factory_of_ints_31apr() { 356 LocalDate.of(2007, 4, 31); 357 } 358 359 @Test(expectedExceptions=DateTimeException.class) 360 public void factory_of_ints_dayTooLow() { 361 LocalDate.of(2007, 1, 0); 362 } 363 364 @Test(expectedExceptions=DateTimeException.class) 365 public void factory_of_ints_dayTooHigh() { 366 LocalDate.of(2007, 1, 32); 367 } 368 369 @Test(expectedExceptions=DateTimeException.class) 370 public void factory_of_ints_monthTooLow() { 371 LocalDate.of(2007, 0, 1); 372 } 373 374 @Test(expectedExceptions=DateTimeException.class) 375 public void factory_of_ints_monthTooHigh() { 376 LocalDate.of(2007, 13, 1); 377 } 378 379 @Test(expectedExceptions=DateTimeException.class) 380 public void factory_of_ints_yearTooLow() { 381 LocalDate.of(Integer.MIN_VALUE, 1, 1); 382 } 383 384 //----------------------------------------------------------------------- 385 @Test 386 public void factory_ofYearDay_ints_nonLeap() { 387 LocalDate date = LocalDate.of(2007, 1, 1); 388 for (int i = 1; i <= 365; i++) { 389 assertEquals(LocalDate.ofYearDay(2007, i), date); 390 date = next(date); 391 } 392 } 393 394 @Test 395 public void factory_ofYearDay_ints_leap() { 396 LocalDate date = LocalDate.of(2008, 1, 1); 397 for (int i = 1; i <= 366; i++) { 398 assertEquals(LocalDate.ofYearDay(2008, i), date); 399 date = next(date); 400 } 401 } 402 403 @Test(expectedExceptions=DateTimeException.class) 404 public void factory_ofYearDay_ints_366nonLeap() { 405 LocalDate.ofYearDay(2007, 366); 406 } 407 408 @Test(expectedExceptions=DateTimeException.class) 409 public void factory_ofYearDay_ints_dayTooLow() { 410 LocalDate.ofYearDay(2007, 0); 411 } 412 413 @Test(expectedExceptions=DateTimeException.class) 414 public void factory_ofYearDay_ints_dayTooHigh() { 415 LocalDate.ofYearDay(2007, 367); 416 } 417 418 @Test(expectedExceptions=DateTimeException.class) 419 public void factory_ofYearDay_ints_yearTooLow() { 420 LocalDate.ofYearDay(Integer.MIN_VALUE, 1); 421 } 422 423 //----------------------------------------------------------------------- 424 // Since plusDays/minusDays actually depends on MJDays, it cannot be used for testing 425 private LocalDate next(LocalDate date) { 426 int newDayOfMonth = date.getDayOfMonth() + 1; 427 if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) { 428 return date.withDayOfMonth(newDayOfMonth); 429 } 430 date = date.withDayOfMonth(1); 431 if (date.getMonth() == Month.DECEMBER) { 432 date = date.withYear(date.getYear() + 1); 433 } 434 return date.with(date.getMonth().plus(1)); 435 } 436 437 private LocalDate previous(LocalDate date) { 438 int newDayOfMonth = date.getDayOfMonth() - 1; 439 if (newDayOfMonth > 0) { 440 return date.withDayOfMonth(newDayOfMonth); 441 } 442 date = date.with(date.getMonth().minus(1)); 443 if (date.getMonth() == Month.DECEMBER) { 444 date = date.withYear(date.getYear() - 1); 445 } 446 return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear()))); 447 } 448 449 //----------------------------------------------------------------------- 450 // ofEpochDay() 451 //----------------------------------------------------------------------- 452 @Test 453 public void factory_ofEpochDay() { 454 long date_0000_01_01 = -678941 - 40587; 455 assertEquals(LocalDate.ofEpochDay(0), LocalDate.of(1970, 1, 1)); 456 assertEquals(LocalDate.ofEpochDay(date_0000_01_01), LocalDate.of(0, 1, 1)); 457 assertEquals(LocalDate.ofEpochDay(date_0000_01_01 - 1), LocalDate.of(-1, 12, 31)); 458 assertEquals(LocalDate.ofEpochDay(MAX_VALID_EPOCHDAYS), LocalDate.of(Year.MAX_VALUE, 12, 31)); 459 assertEquals(LocalDate.ofEpochDay(MIN_VALID_EPOCHDAYS), LocalDate.of(Year.MIN_VALUE, 1, 1)); 460 461 LocalDate test = LocalDate.of(0, 1, 1); 462 for (long i = date_0000_01_01; i < 700000; i++) { 463 assertEquals(LocalDate.ofEpochDay(i), test); 464 test = next(test); 465 } 466 test = LocalDate.of(0, 1, 1); 467 for (long i = date_0000_01_01; i > -2000000; i--) { 468 assertEquals(LocalDate.ofEpochDay(i), test); 469 test = previous(test); 470 } 471 } 472 473 @Test(expectedExceptions=DateTimeException.class) 474 public void factory_ofEpochDay_aboveMax() { 475 LocalDate.ofEpochDay(MAX_VALID_EPOCHDAYS + 1); 476 } 477 478 @Test(expectedExceptions=DateTimeException.class) 479 public void factory_ofEpochDay_belowMin() { 480 LocalDate.ofEpochDay(MIN_VALID_EPOCHDAYS - 1); 481 } 482 483 //----------------------------------------------------------------------- 484 // from() 485 //----------------------------------------------------------------------- 486 @Test 487 public void test_factory_CalendricalObject() { 488 assertEquals(LocalDate.from(LocalDate.of(2007, 7, 15)), LocalDate.of(2007, 7, 15)); 489 assertEquals(LocalDate.from(LocalDateTime.of(2007, 7, 15, 12, 30)), LocalDate.of(2007, 7, 15)); 490 } 491 492 @Test(expectedExceptions=DateTimeException.class) 493 public void test_factory_CalendricalObject_invalid_noDerive() { 494 LocalDate.from(LocalTime.of(12, 30)); 495 } 496 497 @Test(expectedExceptions=NullPointerException.class) 498 public void test_factory_CalendricalObject_null() { 499 LocalDate.from((TemporalAccessor) null); 500 } 501 502 //----------------------------------------------------------------------- 503 // parse() 504 //----------------------------------------------------------------------- 505 @Test(dataProvider="sampleToString") 506 public void factory_parse_validText(int y, int m, int d, String parsable) { 507 LocalDate t = LocalDate.parse(parsable); 508 assertNotNull(t, parsable); 509 assertEquals(t.getYear(), y, parsable); 510 assertEquals(t.getMonth().getValue(), m, parsable); 511 assertEquals(t.getDayOfMonth(), d, parsable); 512 } 513 514 @DataProvider(name="sampleBadParse") 515 Object[][] provider_sampleBadParse() { 516 return new Object[][]{ 517 {"2008/07/05"}, 518 {"10000-01-01"}, 519 {"2008-1-1"}, 520 {"2008--01"}, 521 {"ABCD-02-01"}, 522 {"2008-AB-01"}, 523 {"2008-02-AB"}, 524 {"-0000-02-01"}, 525 {"2008-02-01Z"}, 526 {"2008-02-01+01:00"}, 527 {"2008-02-01+01:00[Europe/Paris]"}, 528 }; 529 } 530 531 @Test(dataProvider="sampleBadParse", expectedExceptions={DateTimeParseException.class}) 532 public void factory_parse_invalidText(String unparsable) { 533 LocalDate.parse(unparsable); 534 } 535 536 @Test(expectedExceptions=DateTimeParseException.class) 537 public void factory_parse_illegalValue() { 538 LocalDate.parse("2008-06-32"); 539 } 540 541 @Test(expectedExceptions=DateTimeParseException.class) 542 public void factory_parse_invalidValue() { 543 LocalDate.parse("2008-06-31"); 544 } 545 546 @Test(expectedExceptions=NullPointerException.class) 547 public void factory_parse_nullText() { 548 LocalDate.parse((String) null); 549 } 550 551 //----------------------------------------------------------------------- 552 // parse(DateTimeFormatter) 553 //----------------------------------------------------------------------- 554 @Test 555 public void factory_parse_formatter() { 556 DateTimeFormatter f = DateTimeFormatter.ofPattern("u M d"); 557 LocalDate test = LocalDate.parse("2010 12 3", f); 558 assertEquals(test, LocalDate.of(2010, 12, 3)); 559 } 560 561 @Test(expectedExceptions=NullPointerException.class) 562 public void factory_parse_formatter_nullText() { 563 DateTimeFormatter f = DateTimeFormatter.ofPattern("u M d"); 564 LocalDate.parse((String) null, f); 565 } 566 567 @Test(expectedExceptions=NullPointerException.class) 568 public void factory_parse_formatter_nullFormatter() { 569 LocalDate.parse("ANY", null); 570 } 571 572 //----------------------------------------------------------------------- 573 // get(TemporalField) 574 //----------------------------------------------------------------------- 575 @Test 576 public void test_get_TemporalField() { 577 LocalDate test = LocalDate.of(2008, 6, 30); 578 assertEquals(test.get(YEAR), 2008); 579 assertEquals(test.get(MONTH_OF_YEAR), 6); 580 assertEquals(test.get(DAY_OF_MONTH), 30); 581 assertEquals(test.get(DAY_OF_WEEK), 1); 582 assertEquals(test.get(DAY_OF_YEAR), 182); 583 assertEquals(test.get(YEAR_OF_ERA), 2008); 584 assertEquals(test.get(ERA), 1); 585 } 586 587 @Test(expectedExceptions=DateTimeException.class) 588 public void test_get_TemporalField_tooBig() { 589 TEST_2007_07_15.get(EPOCH_DAY); 590 } 591 592 @Test(expectedExceptions=NullPointerException.class) 593 public void test_get_TemporalField_null() { 594 TEST_2007_07_15.get((TemporalField) null); 595 } 596 597 @Test(expectedExceptions=DateTimeException.class) 598 public void test_get_TemporalField_invalidField() { 599 TEST_2007_07_15.get(MockFieldNoValue.INSTANCE); 600 } 601 602 @Test(expectedExceptions=DateTimeException.class) 603 public void test_get_TemporalField_timeField() { 604 TEST_2007_07_15.get(ChronoField.AMPM_OF_DAY); 605 } 606 607 //----------------------------------------------------------------------- 608 // getLong(TemporalField) 609 //----------------------------------------------------------------------- 610 @Test 611 public void test_getLong_TemporalField() { 612 LocalDate test = LocalDate.of(2008, 6, 30); 613 assertEquals(test.getLong(YEAR), 2008); 614 assertEquals(test.getLong(MONTH_OF_YEAR), 6); 615 assertEquals(test.getLong(DAY_OF_MONTH), 30); 616 assertEquals(test.getLong(DAY_OF_WEEK), 1); 617 assertEquals(test.getLong(DAY_OF_YEAR), 182); 618 assertEquals(test.getLong(YEAR_OF_ERA), 2008); 619 assertEquals(test.getLong(ERA), 1); 620 assertEquals(test.getLong(PROLEPTIC_MONTH), 2008 * 12 + 6 - 1); 621 } 622 623 @Test(expectedExceptions=NullPointerException.class) 624 public void test_getLong_TemporalField_null() { 625 TEST_2007_07_15.getLong((TemporalField) null); 626 } 627 628 @Test(expectedExceptions=DateTimeException.class) 629 public void test_getLong_TemporalField_invalidField() { 630 TEST_2007_07_15.getLong(MockFieldNoValue.INSTANCE); 631 } 632 633 @Test(expectedExceptions=DateTimeException.class) 634 public void test_getLong_TemporalField_timeField() { 635 TEST_2007_07_15.getLong(ChronoField.AMPM_OF_DAY); 636 } 637 638 //----------------------------------------------------------------------- 639 // query(TemporalQuery) 640 //----------------------------------------------------------------------- 641 @Test 642 public void test_query() { 643 assertEquals(TEST_2007_07_15.query(TemporalQueries.chronology()), IsoChronology.INSTANCE); 644 assertEquals(TEST_2007_07_15.query(TemporalQueries.localDate()), TEST_2007_07_15); 645 assertEquals(TEST_2007_07_15.query(TemporalQueries.localTime()), null); 646 assertEquals(TEST_2007_07_15.query(TemporalQueries.offset()), null); 647 assertEquals(TEST_2007_07_15.query(TemporalQueries.precision()), ChronoUnit.DAYS); 648 assertEquals(TEST_2007_07_15.query(TemporalQueries.zone()), null); 649 assertEquals(TEST_2007_07_15.query(TemporalQueries.zoneId()), null); 650 } 651 652 @Test(expectedExceptions=NullPointerException.class) 653 public void test_query_null() { 654 TEST_2007_07_15.query(null); 655 } 656 657 //----------------------------------------------------------------------- 658 // get*() 659 //----------------------------------------------------------------------- 660 @DataProvider(name="sampleDates") 661 Object[][] provider_sampleDates() { 662 return new Object[][] { 663 {2008, 7, 5}, 664 {2007, 7, 5}, 665 {2006, 7, 5}, 666 {2005, 7, 5}, 667 {2004, 1, 1}, 668 {-1, 1, 2}, 669 }; 670 } 671 672 //----------------------------------------------------------------------- 673 @Test(dataProvider="sampleDates") 674 public void test_get(int y, int m, int d) { 675 LocalDate a = LocalDate.of(y, m, d); 676 assertEquals(a.getYear(), y); 677 assertEquals(a.getMonth(), Month.of(m)); 678 assertEquals(a.getDayOfMonth(), d); 679 } 680 681 @Test(dataProvider="sampleDates") 682 public void test_getDOY(int y, int m, int d) { 683 LocalDate a = LocalDate.of(y, m, d); 684 int total = 0; 685 for (int i = 1; i < m; i++) { 686 total += Month.of(i).length(isIsoLeap(y)); 687 } 688 int doy = total + d; 689 assertEquals(a.getDayOfYear(), doy); 690 } 691 692 @Test 693 public void test_getDayOfWeek() { 694 DayOfWeek dow = DayOfWeek.MONDAY; 695 for (Month month : Month.values()) { 696 int length = month.length(false); 697 for (int i = 1; i <= length; i++) { 698 LocalDate d = LocalDate.of(2007, month, i); 699 assertSame(d.getDayOfWeek(), dow); 700 dow = dow.plus(1); 701 } 702 } 703 } 704 705 //----------------------------------------------------------------------- 706 // isLeapYear() 707 //----------------------------------------------------------------------- 708 @Test 709 public void test_isLeapYear() { 710 assertEquals(LocalDate.of(1999, 1, 1).isLeapYear(), false); 711 assertEquals(LocalDate.of(2000, 1, 1).isLeapYear(), true); 712 assertEquals(LocalDate.of(2001, 1, 1).isLeapYear(), false); 713 assertEquals(LocalDate.of(2002, 1, 1).isLeapYear(), false); 714 assertEquals(LocalDate.of(2003, 1, 1).isLeapYear(), false); 715 assertEquals(LocalDate.of(2004, 1, 1).isLeapYear(), true); 716 assertEquals(LocalDate.of(2005, 1, 1).isLeapYear(), false); 717 718 assertEquals(LocalDate.of(1500, 1, 1).isLeapYear(), false); 719 assertEquals(LocalDate.of(1600, 1, 1).isLeapYear(), true); 720 assertEquals(LocalDate.of(1700, 1, 1).isLeapYear(), false); 721 assertEquals(LocalDate.of(1800, 1, 1).isLeapYear(), false); 722 assertEquals(LocalDate.of(1900, 1, 1).isLeapYear(), false); 723 } 724 725 //----------------------------------------------------------------------- 726 // lengthOfMonth() 727 //----------------------------------------------------------------------- 728 @Test 729 public void test_lengthOfMonth_notLeapYear() { 730 assertEquals(LocalDate.of(2007, 1, 1).lengthOfMonth(), 31); 731 assertEquals(LocalDate.of(2007, 2, 1).lengthOfMonth(), 28); 732 assertEquals(LocalDate.of(2007, 3, 1).lengthOfMonth(), 31); 733 assertEquals(LocalDate.of(2007, 4, 1).lengthOfMonth(), 30); 734 assertEquals(LocalDate.of(2007, 5, 1).lengthOfMonth(), 31); 735 assertEquals(LocalDate.of(2007, 6, 1).lengthOfMonth(), 30); 736 assertEquals(LocalDate.of(2007, 7, 1).lengthOfMonth(), 31); 737 assertEquals(LocalDate.of(2007, 8, 1).lengthOfMonth(), 31); 738 assertEquals(LocalDate.of(2007, 9, 1).lengthOfMonth(), 30); 739 assertEquals(LocalDate.of(2007, 10, 1).lengthOfMonth(), 31); 740 assertEquals(LocalDate.of(2007, 11, 1).lengthOfMonth(), 30); 741 assertEquals(LocalDate.of(2007, 12, 1).lengthOfMonth(), 31); 742 } 743 744 @Test 745 public void test_lengthOfMonth_leapYear() { 746 assertEquals(LocalDate.of(2008, 1, 1).lengthOfMonth(), 31); 747 assertEquals(LocalDate.of(2008, 2, 1).lengthOfMonth(), 29); 748 assertEquals(LocalDate.of(2008, 3, 1).lengthOfMonth(), 31); 749 assertEquals(LocalDate.of(2008, 4, 1).lengthOfMonth(), 30); 750 assertEquals(LocalDate.of(2008, 5, 1).lengthOfMonth(), 31); 751 assertEquals(LocalDate.of(2008, 6, 1).lengthOfMonth(), 30); 752 assertEquals(LocalDate.of(2008, 7, 1).lengthOfMonth(), 31); 753 assertEquals(LocalDate.of(2008, 8, 1).lengthOfMonth(), 31); 754 assertEquals(LocalDate.of(2008, 9, 1).lengthOfMonth(), 30); 755 assertEquals(LocalDate.of(2008, 10, 1).lengthOfMonth(), 31); 756 assertEquals(LocalDate.of(2008, 11, 1).lengthOfMonth(), 30); 757 assertEquals(LocalDate.of(2008, 12, 1).lengthOfMonth(), 31); 758 } 759 760 //----------------------------------------------------------------------- 761 // lengthOfYear() 762 //----------------------------------------------------------------------- 763 @Test 764 public void test_lengthOfYear() { 765 assertEquals(LocalDate.of(2007, 1, 1).lengthOfYear(), 365); 766 assertEquals(LocalDate.of(2008, 1, 1).lengthOfYear(), 366); 767 } 768 769 //----------------------------------------------------------------------- 770 // with() 771 //----------------------------------------------------------------------- 772 @Test 773 public void test_with_adjustment() { 774 final LocalDate sample = LocalDate.of(2012, 3, 4); 775 TemporalAdjuster adjuster = new TemporalAdjuster() { 776 @Override 777 public Temporal adjustInto(Temporal dateTime) { 778 return sample; 779 } 780 }; 781 assertEquals(TEST_2007_07_15.with(adjuster), sample); 782 } 783 784 @Test(expectedExceptions=NullPointerException.class) 785 public void test_with_adjustment_null() { 786 TEST_2007_07_15.with((TemporalAdjuster) null); 787 } 788 789 //----------------------------------------------------------------------- 790 // with(DateTimeField,long) 791 //----------------------------------------------------------------------- 792 @Test 793 public void test_with_DateTimeField_long_normal() { 794 LocalDate t = TEST_2007_07_15.with(YEAR, 2008); 795 assertEquals(t, LocalDate.of(2008, 7, 15)); 796 } 797 798 @Test(expectedExceptions=NullPointerException.class) 799 public void test_with_DateTimeField_long_null() { 800 TEST_2007_07_15.with((TemporalField) null, 1); 801 } 802 803 @Test(expectedExceptions=DateTimeException.class) 804 public void test_with_DateTimeField_long_invalidField() { 805 TEST_2007_07_15.with(MockFieldNoValue.INSTANCE, 1); 806 } 807 808 @Test(expectedExceptions=DateTimeException.class) 809 public void test_with_DateTimeField_long_timeField() { 810 TEST_2007_07_15.with(ChronoField.AMPM_OF_DAY, 1); 811 } 812 813 @Test(expectedExceptions=DateTimeException.class) 814 public void test_with_DateTimeField_long_invalidValue() { 815 TEST_2007_07_15.with(ChronoField.DAY_OF_WEEK, -1); 816 } 817 818 //----------------------------------------------------------------------- 819 // withYear() 820 //----------------------------------------------------------------------- 821 @Test 822 public void test_withYear_int_normal() { 823 LocalDate t = TEST_2007_07_15.withYear(2008); 824 assertEquals(t, LocalDate.of(2008, 7, 15)); 825 } 826 827 @Test(expectedExceptions=DateTimeException.class) 828 public void test_withYear_int_invalid() { 829 TEST_2007_07_15.withYear(Year.MIN_VALUE - 1); 830 } 831 832 @Test 833 public void test_withYear_int_adjustDay() { 834 LocalDate t = LocalDate.of(2008, 2, 29).withYear(2007); 835 LocalDate expected = LocalDate.of(2007, 2, 28); 836 assertEquals(t, expected); 837 } 838 839 //----------------------------------------------------------------------- 840 // withMonth() 841 //----------------------------------------------------------------------- 842 @Test 843 public void test_withMonth_int_normal() { 844 LocalDate t = TEST_2007_07_15.withMonth(1); 845 assertEquals(t, LocalDate.of(2007, 1, 15)); 846 } 847 848 @Test(expectedExceptions=DateTimeException.class) 849 public void test_withMonth_int_invalid() { 850 TEST_2007_07_15.withMonth(13); 851 } 852 853 @Test 854 public void test_withMonth_int_adjustDay() { 855 LocalDate t = LocalDate.of(2007, 12, 31).withMonth(11); 856 LocalDate expected = LocalDate.of(2007, 11, 30); 857 assertEquals(t, expected); 858 } 859 860 //----------------------------------------------------------------------- 861 // withDayOfMonth() 862 //----------------------------------------------------------------------- 863 @Test 864 public void test_withDayOfMonth_normal() { 865 LocalDate t = TEST_2007_07_15.withDayOfMonth(1); 866 assertEquals(t, LocalDate.of(2007, 7, 1)); 867 } 868 869 @Test(expectedExceptions=DateTimeException.class) 870 public void test_withDayOfMonth_illegal() { 871 TEST_2007_07_15.withDayOfMonth(32); 872 } 873 874 @Test(expectedExceptions=DateTimeException.class) 875 public void test_withDayOfMonth_invalid() { 876 LocalDate.of(2007, 11, 30).withDayOfMonth(31); 877 } 878 879 //----------------------------------------------------------------------- 880 // withDayOfYear(int) 881 //----------------------------------------------------------------------- 882 @Test 883 public void test_withDayOfYear_normal() { 884 LocalDate t = TEST_2007_07_15.withDayOfYear(33); 885 assertEquals(t, LocalDate.of(2007, 2, 2)); 886 } 887 888 @Test(expectedExceptions=DateTimeException.class) 889 public void test_withDayOfYear_illegal() { 890 TEST_2007_07_15.withDayOfYear(367); 891 } 892 893 @Test(expectedExceptions=DateTimeException.class) 894 public void test_withDayOfYear_invalid() { 895 TEST_2007_07_15.withDayOfYear(366); 896 } 897 898 //----------------------------------------------------------------------- 899 // plus(Period) 900 //----------------------------------------------------------------------- 901 @Test 902 public void test_plus_Period_positiveMonths() { 903 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 904 LocalDate t = TEST_2007_07_15.plus(period); 905 assertEquals(t, LocalDate.of(2008, 2, 15)); 906 } 907 908 @Test 909 public void test_plus_Period_negativeDays() { 910 MockSimplePeriod period = MockSimplePeriod.of(-25, ChronoUnit.DAYS); 911 LocalDate t = TEST_2007_07_15.plus(period); 912 assertEquals(t, LocalDate.of(2007, 6, 20)); 913 } 914 915 @Test(expectedExceptions=DateTimeException.class) 916 public void test_plus_Period_timeNotAllowed() { 917 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.HOURS); 918 TEST_2007_07_15.plus(period); 919 } 920 921 @Test(expectedExceptions=NullPointerException.class) 922 public void test_plus_Period_null() { 923 TEST_2007_07_15.plus((MockSimplePeriod) null); 924 } 925 926 @Test(expectedExceptions=DateTimeException.class) 927 public void test_plus_Period_invalidTooLarge() { 928 MockSimplePeriod period = MockSimplePeriod.of(1, ChronoUnit.YEARS); 929 LocalDate.of(Year.MAX_VALUE, 1, 1).plus(period); 930 } 931 932 @Test(expectedExceptions=DateTimeException.class) 933 public void test_plus_Period_invalidTooSmall() { 934 MockSimplePeriod period = MockSimplePeriod.of(-1, ChronoUnit.YEARS); 935 LocalDate.of(Year.MIN_VALUE, 1, 1).plus(period); 936 } 937 938 //----------------------------------------------------------------------- 939 // plus(long,PeriodUnit) 940 //----------------------------------------------------------------------- 941 @Test 942 public void test_plus_longPeriodUnit_positiveMonths() { 943 LocalDate t = TEST_2007_07_15.plus(7, ChronoUnit.MONTHS); 944 assertEquals(t, LocalDate.of(2008, 2, 15)); 945 } 946 947 @Test 948 public void test_plus_longPeriodUnit_negativeDays() { 949 LocalDate t = TEST_2007_07_15.plus(-25, ChronoUnit.DAYS); 950 assertEquals(t, LocalDate.of(2007, 6, 20)); 951 } 952 953 @Test(expectedExceptions=DateTimeException.class) 954 public void test_plus_longPeriodUnit_timeNotAllowed() { 955 TEST_2007_07_15.plus(7, ChronoUnit.HOURS); 956 } 957 958 @Test(expectedExceptions=NullPointerException.class) 959 public void test_plus_longPeriodUnit_null() { 960 TEST_2007_07_15.plus(1, (TemporalUnit) null); 961 } 962 963 @Test(expectedExceptions=DateTimeException.class) 964 public void test_plus_longPeriodUnit_invalidTooLarge() { 965 LocalDate.of(Year.MAX_VALUE, 1, 1).plus(1, ChronoUnit.YEARS); 966 } 967 968 @Test(expectedExceptions=DateTimeException.class) 969 public void test_plus_longPeriodUnit_invalidTooSmall() { 970 LocalDate.of(Year.MIN_VALUE, 1, 1).plus(-1, ChronoUnit.YEARS); 971 } 972 973 //----------------------------------------------------------------------- 974 // plusYears() 975 //----------------------------------------------------------------------- 976 @Test 977 public void test_plusYears_long_normal() { 978 LocalDate t = TEST_2007_07_15.plusYears(1); 979 assertEquals(t, LocalDate.of(2008, 7, 15)); 980 } 981 982 @Test 983 public void test_plusYears_long_negative() { 984 LocalDate t = TEST_2007_07_15.plusYears(-1); 985 assertEquals(t, LocalDate.of(2006, 7, 15)); 986 } 987 988 @Test 989 public void test_plusYears_long_adjustDay() { 990 LocalDate t = LocalDate.of(2008, 2, 29).plusYears(1); 991 LocalDate expected = LocalDate.of(2009, 2, 28); 992 assertEquals(t, expected); 993 } 994 995 @Test 996 public void test_plusYears_long_big() { 997 long years = 20L + Year.MAX_VALUE; 998 LocalDate test = LocalDate.of(-40, 6, 1).plusYears(years); 999 assertEquals(test, LocalDate.of((int) (-40L + years), 6, 1)); 1000 } 1001 1002 @Test(expectedExceptions=DateTimeException.class) 1003 public void test_plusYears_long_invalidTooLarge() { 1004 LocalDate test = LocalDate.of(Year.MAX_VALUE, 6, 1); 1005 test.plusYears(1); 1006 } 1007 1008 @Test(expectedExceptions=DateTimeException.class) 1009 public void test_plusYears_long_invalidTooLargeMaxAddMax() { 1010 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1011 test.plusYears(Long.MAX_VALUE); 1012 } 1013 1014 @Test(expectedExceptions=DateTimeException.class) 1015 public void test_plusYears_long_invalidTooLargeMaxAddMin() { 1016 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1017 test.plusYears(Long.MIN_VALUE); 1018 } 1019 1020 @Test(expectedExceptions=DateTimeException.class) 1021 public void test_plusYears_long_invalidTooSmall_validInt() { 1022 LocalDate.of(Year.MIN_VALUE, 1, 1).plusYears(-1); 1023 } 1024 1025 @Test(expectedExceptions=DateTimeException.class) 1026 public void test_plusYears_long_invalidTooSmall_invalidInt() { 1027 LocalDate.of(Year.MIN_VALUE, 1, 1).plusYears(-10); 1028 } 1029 1030 //----------------------------------------------------------------------- 1031 // plusMonths() 1032 //----------------------------------------------------------------------- 1033 @Test 1034 public void test_plusMonths_long_normal() { 1035 LocalDate t = TEST_2007_07_15.plusMonths(1); 1036 assertEquals(t, LocalDate.of(2007, 8, 15)); 1037 } 1038 1039 @Test 1040 public void test_plusMonths_long_overYears() { 1041 LocalDate t = TEST_2007_07_15.plusMonths(25); 1042 assertEquals(t, LocalDate.of(2009, 8, 15)); 1043 } 1044 1045 @Test 1046 public void test_plusMonths_long_negative() { 1047 LocalDate t = TEST_2007_07_15.plusMonths(-1); 1048 assertEquals(t, LocalDate.of(2007, 6, 15)); 1049 } 1050 1051 @Test 1052 public void test_plusMonths_long_negativeAcrossYear() { 1053 LocalDate t = TEST_2007_07_15.plusMonths(-7); 1054 assertEquals(t, LocalDate.of(2006, 12, 15)); 1055 } 1056 1057 @Test 1058 public void test_plusMonths_long_negativeOverYears() { 1059 LocalDate t = TEST_2007_07_15.plusMonths(-31); 1060 assertEquals(t, LocalDate.of(2004, 12, 15)); 1061 } 1062 1063 @Test 1064 public void test_plusMonths_long_adjustDayFromLeapYear() { 1065 LocalDate t = LocalDate.of(2008, 2, 29).plusMonths(12); 1066 LocalDate expected = LocalDate.of(2009, 2, 28); 1067 assertEquals(t, expected); 1068 } 1069 1070 @Test 1071 public void test_plusMonths_long_adjustDayFromMonthLength() { 1072 LocalDate t = LocalDate.of(2007, 3, 31).plusMonths(1); 1073 LocalDate expected = LocalDate.of(2007, 4, 30); 1074 assertEquals(t, expected); 1075 } 1076 1077 @Test 1078 public void test_plusMonths_long_big() { 1079 long months = 20L + Integer.MAX_VALUE; 1080 LocalDate test = LocalDate.of(-40, 6, 1).plusMonths(months); 1081 assertEquals(test, LocalDate.of((int) (-40L + months / 12), 6 + (int) (months % 12), 1)); 1082 } 1083 1084 @Test(expectedExceptions={DateTimeException.class}) 1085 public void test_plusMonths_long_invalidTooLarge() { 1086 LocalDate.of(Year.MAX_VALUE, 12, 1).plusMonths(1); 1087 } 1088 1089 @Test(expectedExceptions=DateTimeException.class) 1090 public void test_plusMonths_long_invalidTooLargeMaxAddMax() { 1091 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1092 test.plusMonths(Long.MAX_VALUE); 1093 } 1094 1095 @Test(expectedExceptions=DateTimeException.class) 1096 public void test_plusMonths_long_invalidTooLargeMaxAddMin() { 1097 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1098 test.plusMonths(Long.MIN_VALUE); 1099 } 1100 1101 @Test(expectedExceptions={DateTimeException.class}) 1102 public void test_plusMonths_long_invalidTooSmall() { 1103 LocalDate.of(Year.MIN_VALUE, 1, 1).plusMonths(-1); 1104 } 1105 1106 @Test 1107 public void test_plusWeeks_normal() { 1108 LocalDate t = TEST_2007_07_15.plusWeeks(1); 1109 assertEquals(t, LocalDate.of(2007, 7, 22)); 1110 } 1111 1112 @Test 1113 public void test_plusWeeks_overMonths() { 1114 LocalDate t = TEST_2007_07_15.plusWeeks(9); 1115 assertEquals(t, LocalDate.of(2007, 9, 16)); 1116 } 1117 1118 @Test 1119 public void test_plusWeeks_overYears() { 1120 LocalDate t = LocalDate.of(2006, 7, 16).plusWeeks(52); 1121 assertEquals(t, TEST_2007_07_15); 1122 } 1123 1124 @Test 1125 public void test_plusWeeks_overLeapYears() { 1126 LocalDate t = TEST_2007_07_15.plusYears(-1).plusWeeks(104); 1127 assertEquals(t, LocalDate.of(2008, 7, 12)); 1128 } 1129 1130 @Test 1131 public void test_plusWeeks_negative() { 1132 LocalDate t = TEST_2007_07_15.plusWeeks(-1); 1133 assertEquals(t, LocalDate.of(2007, 7, 8)); 1134 } 1135 1136 @Test 1137 public void test_plusWeeks_negativeAcrossYear() { 1138 LocalDate t = TEST_2007_07_15.plusWeeks(-28); 1139 assertEquals(t, LocalDate.of(2006, 12, 31)); 1140 } 1141 1142 @Test 1143 public void test_plusWeeks_negativeOverYears() { 1144 LocalDate t = TEST_2007_07_15.plusWeeks(-104); 1145 assertEquals(t, LocalDate.of(2005, 7, 17)); 1146 } 1147 1148 @Test 1149 public void test_plusWeeks_maximum() { 1150 LocalDate t = LocalDate.of(Year.MAX_VALUE, 12, 24).plusWeeks(1); 1151 LocalDate expected = LocalDate.of(Year.MAX_VALUE, 12, 31); 1152 assertEquals(t, expected); 1153 } 1154 1155 @Test 1156 public void test_plusWeeks_minimum() { 1157 LocalDate t = LocalDate.of(Year.MIN_VALUE, 1, 8).plusWeeks(-1); 1158 LocalDate expected = LocalDate.of(Year.MIN_VALUE, 1, 1); 1159 assertEquals(t, expected); 1160 } 1161 1162 @Test(expectedExceptions={DateTimeException.class}) 1163 public void test_plusWeeks_invalidTooLarge() { 1164 LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(1); 1165 } 1166 1167 @Test(expectedExceptions={DateTimeException.class}) 1168 public void test_plusWeeks_invalidTooSmall() { 1169 LocalDate.of(Year.MIN_VALUE, 1, 7).plusWeeks(-1); 1170 } 1171 1172 @Test(expectedExceptions={ArithmeticException.class}) 1173 public void test_plusWeeks_invalidMaxMinusMax() { 1174 LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(Long.MAX_VALUE); 1175 } 1176 1177 @Test(expectedExceptions={ArithmeticException.class}) 1178 public void test_plusWeeks_invalidMaxMinusMin() { 1179 LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(Long.MIN_VALUE); 1180 } 1181 1182 @Test 1183 public void test_plusDays_normal() { 1184 LocalDate t = TEST_2007_07_15.plusDays(1); 1185 assertEquals(t, LocalDate.of(2007, 7, 16)); 1186 } 1187 1188 @Test 1189 public void test_plusDays_overMonths() { 1190 LocalDate t = TEST_2007_07_15.plusDays(62); 1191 assertEquals(t, LocalDate.of(2007, 9, 15)); 1192 } 1193 1194 @Test 1195 public void test_plusDays_overYears() { 1196 LocalDate t = LocalDate.of(2006, 7, 14).plusDays(366); 1197 assertEquals(t, TEST_2007_07_15); 1198 } 1199 1200 @Test 1201 public void test_plusDays_overLeapYears() { 1202 LocalDate t = TEST_2007_07_15.plusYears(-1).plusDays(365 + 366); 1203 assertEquals(t, LocalDate.of(2008, 7, 15)); 1204 } 1205 1206 @Test 1207 public void test_plusDays_negative() { 1208 LocalDate t = TEST_2007_07_15.plusDays(-1); 1209 assertEquals(t, LocalDate.of(2007, 7, 14)); 1210 } 1211 1212 @Test 1213 public void test_plusDays_negativeAcrossYear() { 1214 LocalDate t = TEST_2007_07_15.plusDays(-196); 1215 assertEquals(t, LocalDate.of(2006, 12, 31)); 1216 } 1217 1218 @Test 1219 public void test_plusDays_negativeOverYears() { 1220 LocalDate t = TEST_2007_07_15.plusDays(-730); 1221 assertEquals(t, LocalDate.of(2005, 7, 15)); 1222 } 1223 1224 @Test 1225 public void test_plusDays_maximum() { 1226 LocalDate t = LocalDate.of(Year.MAX_VALUE, 12, 30).plusDays(1); 1227 LocalDate expected = LocalDate.of(Year.MAX_VALUE, 12, 31); 1228 assertEquals(t, expected); 1229 } 1230 1231 @Test 1232 public void test_plusDays_minimum() { 1233 LocalDate t = LocalDate.of(Year.MIN_VALUE, 1, 2).plusDays(-1); 1234 LocalDate expected = LocalDate.of(Year.MIN_VALUE, 1, 1); 1235 assertEquals(t, expected); 1236 } 1237 1238 @Test(expectedExceptions={DateTimeException.class}) 1239 public void test_plusDays_invalidTooLarge() { 1240 LocalDate.of(Year.MAX_VALUE, 12, 31).plusDays(1); 1241 } 1242 1243 @Test(expectedExceptions={DateTimeException.class}) 1244 public void test_plusDays_invalidTooSmall() { 1245 LocalDate.of(Year.MIN_VALUE, 1, 1).plusDays(-1); 1246 } 1247 1248 @Test(expectedExceptions=ArithmeticException.class) 1249 public void test_plusDays_overflowTooLarge() { 1250 LocalDate.of(Year.MAX_VALUE, 12, 31).plusDays(Long.MAX_VALUE); 1251 } 1252 1253 @Test(expectedExceptions=ArithmeticException.class) 1254 public void test_plusDays_overflowTooSmall() { 1255 LocalDate.of(Year.MIN_VALUE, 1, 1).plusDays(Long.MIN_VALUE); 1256 } 1257 1258 //----------------------------------------------------------------------- 1259 // minus(Period) 1260 //----------------------------------------------------------------------- 1261 @Test 1262 public void test_minus_Period_positiveMonths() { 1263 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1264 LocalDate t = TEST_2007_07_15.minus(period); 1265 assertEquals(t, LocalDate.of(2006, 12, 15)); 1266 } 1267 1268 @Test 1269 public void test_minus_Period_negativeDays() { 1270 MockSimplePeriod period = MockSimplePeriod.of(-25, ChronoUnit.DAYS); 1271 LocalDate t = TEST_2007_07_15.minus(period); 1272 assertEquals(t, LocalDate.of(2007, 8, 9)); 1273 } 1274 1275 @Test(expectedExceptions=DateTimeException.class) 1276 public void test_minus_Period_timeNotAllowed() { 1277 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.HOURS); 1278 TEST_2007_07_15.minus(period); 1279 } 1280 1281 @Test(expectedExceptions=NullPointerException.class) 1282 public void test_minus_Period_null() { 1283 TEST_2007_07_15.minus((MockSimplePeriod) null); 1284 } 1285 1286 @Test(expectedExceptions=DateTimeException.class) 1287 public void test_minus_Period_invalidTooLarge() { 1288 MockSimplePeriod period = MockSimplePeriod.of(-1, ChronoUnit.YEARS); 1289 LocalDate.of(Year.MAX_VALUE, 1, 1).minus(period); 1290 } 1291 1292 @Test(expectedExceptions=DateTimeException.class) 1293 public void test_minus_Period_invalidTooSmall() { 1294 MockSimplePeriod period = MockSimplePeriod.of(1, ChronoUnit.YEARS); 1295 LocalDate.of(Year.MIN_VALUE, 1, 1).minus(period); 1296 } 1297 1298 //----------------------------------------------------------------------- 1299 // minus(long,PeriodUnit) 1300 //----------------------------------------------------------------------- 1301 @Test 1302 public void test_minus_longPeriodUnit_positiveMonths() { 1303 LocalDate t = TEST_2007_07_15.minus(7, ChronoUnit.MONTHS); 1304 assertEquals(t, LocalDate.of(2006, 12, 15)); 1305 } 1306 1307 @Test 1308 public void test_minus_longPeriodUnit_negativeDays() { 1309 LocalDate t = TEST_2007_07_15.minus(-25, ChronoUnit.DAYS); 1310 assertEquals(t, LocalDate.of(2007, 8, 9)); 1311 } 1312 1313 @Test(expectedExceptions=DateTimeException.class) 1314 public void test_minus_longPeriodUnit_timeNotAllowed() { 1315 TEST_2007_07_15.minus(7, ChronoUnit.HOURS); 1316 } 1317 1318 @Test(expectedExceptions=NullPointerException.class) 1319 public void test_minus_longPeriodUnit_null() { 1320 TEST_2007_07_15.minus(1, (TemporalUnit) null); 1321 } 1322 1323 @Test(expectedExceptions=DateTimeException.class) 1324 public void test_minus_longPeriodUnit_invalidTooLarge() { 1325 LocalDate.of(Year.MAX_VALUE, 1, 1).minus(-1, ChronoUnit.YEARS); 1326 } 1327 1328 @Test(expectedExceptions=DateTimeException.class) 1329 public void test_minus_longPeriodUnit_invalidTooSmall() { 1330 LocalDate.of(Year.MIN_VALUE, 1, 1).minus(1, ChronoUnit.YEARS); 1331 } 1332 1333 //----------------------------------------------------------------------- 1334 // minusYears() 1335 //----------------------------------------------------------------------- 1336 @Test 1337 public void test_minusYears_long_normal() { 1338 LocalDate t = TEST_2007_07_15.minusYears(1); 1339 assertEquals(t, LocalDate.of(2006, 7, 15)); 1340 } 1341 1342 @Test 1343 public void test_minusYears_long_negative() { 1344 LocalDate t = TEST_2007_07_15.minusYears(-1); 1345 assertEquals(t, LocalDate.of(2008, 7, 15)); 1346 } 1347 1348 @Test 1349 public void test_minusYears_long_adjustDay() { 1350 LocalDate t = LocalDate.of(2008, 2, 29).minusYears(1); 1351 LocalDate expected = LocalDate.of(2007, 2, 28); 1352 assertEquals(t, expected); 1353 } 1354 1355 @Test 1356 public void test_minusYears_long_big() { 1357 long years = 20L + Year.MAX_VALUE; 1358 LocalDate test = LocalDate.of(40, 6, 1).minusYears(years); 1359 assertEquals(test, LocalDate.of((int) (40L - years), 6, 1)); 1360 } 1361 1362 @Test(expectedExceptions=DateTimeException.class) 1363 public void test_minusYears_long_invalidTooLarge() { 1364 LocalDate test = LocalDate.of(Year.MAX_VALUE, 6, 1); 1365 test.minusYears(-1); 1366 } 1367 1368 @Test(expectedExceptions=DateTimeException.class) 1369 public void test_minusYears_long_invalidTooLargeMaxAddMax() { 1370 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1371 test.minusYears(Long.MAX_VALUE); 1372 } 1373 1374 @Test(expectedExceptions=DateTimeException.class) 1375 public void test_minusYears_long_invalidTooLargeMaxAddMin() { 1376 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1377 test.minusYears(Long.MIN_VALUE); 1378 } 1379 1380 @Test(expectedExceptions=DateTimeException.class) 1381 public void test_minusYears_long_invalidTooSmall() { 1382 LocalDate.of(Year.MIN_VALUE, 1, 1).minusYears(1); 1383 } 1384 1385 //----------------------------------------------------------------------- 1386 // minusMonths() 1387 //----------------------------------------------------------------------- 1388 @Test 1389 public void test_minusMonths_long_normal() { 1390 LocalDate t = TEST_2007_07_15.minusMonths(1); 1391 assertEquals(t, LocalDate.of(2007, 6, 15)); 1392 } 1393 1394 @Test 1395 public void test_minusMonths_long_overYears() { 1396 LocalDate t = TEST_2007_07_15.minusMonths(25); 1397 assertEquals(t, LocalDate.of(2005, 6, 15)); 1398 } 1399 1400 @Test 1401 public void test_minusMonths_long_negative() { 1402 LocalDate t = TEST_2007_07_15.minusMonths(-1); 1403 assertEquals(t, LocalDate.of(2007, 8, 15)); 1404 } 1405 1406 @Test 1407 public void test_minusMonths_long_negativeAcrossYear() { 1408 LocalDate t = TEST_2007_07_15.minusMonths(-7); 1409 assertEquals(t, LocalDate.of(2008, 2, 15)); 1410 } 1411 1412 @Test 1413 public void test_minusMonths_long_negativeOverYears() { 1414 LocalDate t = TEST_2007_07_15.minusMonths(-31); 1415 assertEquals(t, LocalDate.of(2010, 2, 15)); 1416 } 1417 1418 @Test 1419 public void test_minusMonths_long_adjustDayFromLeapYear() { 1420 LocalDate t = LocalDate.of(2008, 2, 29).minusMonths(12); 1421 LocalDate expected = LocalDate.of(2007, 2, 28); 1422 assertEquals(t, expected); 1423 } 1424 1425 @Test 1426 public void test_minusMonths_long_adjustDayFromMonthLength() { 1427 LocalDate t = LocalDate.of(2007, 3, 31).minusMonths(1); 1428 LocalDate expected = LocalDate.of(2007, 2, 28); 1429 assertEquals(t, expected); 1430 } 1431 1432 @Test 1433 public void test_minusMonths_long_big() { 1434 long months = 20L + Integer.MAX_VALUE; 1435 LocalDate test = LocalDate.of(40, 6, 1).minusMonths(months); 1436 assertEquals(test, LocalDate.of((int) (40L - months / 12), 6 - (int) (months % 12), 1)); 1437 } 1438 1439 @Test(expectedExceptions={DateTimeException.class}) 1440 public void test_minusMonths_long_invalidTooLarge() { 1441 LocalDate.of(Year.MAX_VALUE, 12, 1).minusMonths(-1); 1442 } 1443 1444 @Test(expectedExceptions=DateTimeException.class) 1445 public void test_minusMonths_long_invalidTooLargeMaxAddMax() { 1446 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1447 test.minusMonths(Long.MAX_VALUE); 1448 } 1449 1450 @Test(expectedExceptions=DateTimeException.class) 1451 public void test_minusMonths_long_invalidTooLargeMaxAddMin() { 1452 LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1); 1453 test.minusMonths(Long.MIN_VALUE); 1454 } 1455 1456 @Test(expectedExceptions={DateTimeException.class}) 1457 public void test_minusMonths_long_invalidTooSmall() { 1458 LocalDate.of(Year.MIN_VALUE, 1, 1).minusMonths(1); 1459 } 1460 1461 @Test 1462 public void test_minusWeeks_normal() { 1463 LocalDate t = TEST_2007_07_15.minusWeeks(1); 1464 assertEquals(t, LocalDate.of(2007, 7, 8)); 1465 } 1466 1467 @Test 1468 public void test_minusWeeks_overMonths() { 1469 LocalDate t = TEST_2007_07_15.minusWeeks(9); 1470 assertEquals(t, LocalDate.of(2007, 5, 13)); 1471 } 1472 1473 @Test 1474 public void test_minusWeeks_overYears() { 1475 LocalDate t = LocalDate.of(2008, 7, 13).minusWeeks(52); 1476 assertEquals(t, TEST_2007_07_15); 1477 } 1478 1479 @Test 1480 public void test_minusWeeks_overLeapYears() { 1481 LocalDate t = TEST_2007_07_15.minusYears(-1).minusWeeks(104); 1482 assertEquals(t, LocalDate.of(2006, 7, 18)); 1483 } 1484 1485 @Test 1486 public void test_minusWeeks_negative() { 1487 LocalDate t = TEST_2007_07_15.minusWeeks(-1); 1488 assertEquals(t, LocalDate.of(2007, 7, 22)); 1489 } 1490 1491 @Test 1492 public void test_minusWeeks_negativeAcrossYear() { 1493 LocalDate t = TEST_2007_07_15.minusWeeks(-28); 1494 assertEquals(t, LocalDate.of(2008, 1, 27)); 1495 } 1496 1497 @Test 1498 public void test_minusWeeks_negativeOverYears() { 1499 LocalDate t = TEST_2007_07_15.minusWeeks(-104); 1500 assertEquals(t, LocalDate.of(2009, 7, 12)); 1501 } 1502 1503 @Test 1504 public void test_minusWeeks_maximum() { 1505 LocalDate t = LocalDate.of(Year.MAX_VALUE, 12, 24).minusWeeks(-1); 1506 LocalDate expected = LocalDate.of(Year.MAX_VALUE, 12, 31); 1507 assertEquals(t, expected); 1508 } 1509 1510 @Test 1511 public void test_minusWeeks_minimum() { 1512 LocalDate t = LocalDate.of(Year.MIN_VALUE, 1, 8).minusWeeks(1); 1513 LocalDate expected = LocalDate.of(Year.MIN_VALUE, 1, 1); 1514 assertEquals(t, expected); 1515 } 1516 1517 @Test(expectedExceptions={DateTimeException.class}) 1518 public void test_minusWeeks_invalidTooLarge() { 1519 LocalDate.of(Year.MAX_VALUE, 12, 25).minusWeeks(-1); 1520 } 1521 1522 @Test(expectedExceptions={DateTimeException.class}) 1523 public void test_minusWeeks_invalidTooSmall() { 1524 LocalDate.of(Year.MIN_VALUE, 1, 7).minusWeeks(1); 1525 } 1526 1527 @Test(expectedExceptions={ArithmeticException.class}) 1528 public void test_minusWeeks_invalidMaxMinusMax() { 1529 LocalDate.of(Year.MAX_VALUE, 12, 25).minusWeeks(Long.MAX_VALUE); 1530 } 1531 1532 @Test(expectedExceptions={ArithmeticException.class}) 1533 public void test_minusWeeks_invalidMaxMinusMin() { 1534 LocalDate.of(Year.MAX_VALUE, 12, 25).minusWeeks(Long.MIN_VALUE); 1535 } 1536 1537 @Test 1538 public void test_minusDays_normal() { 1539 LocalDate t = TEST_2007_07_15.minusDays(1); 1540 assertEquals(t, LocalDate.of(2007, 7, 14)); 1541 } 1542 1543 @Test 1544 public void test_minusDays_overMonths() { 1545 LocalDate t = TEST_2007_07_15.minusDays(62); 1546 assertEquals(t, LocalDate.of(2007, 5, 14)); 1547 } 1548 1549 @Test 1550 public void test_minusDays_overYears() { 1551 LocalDate t = LocalDate.of(2008, 7, 16).minusDays(367); 1552 assertEquals(t, TEST_2007_07_15); 1553 } 1554 1555 @Test 1556 public void test_minusDays_overLeapYears() { 1557 LocalDate t = TEST_2007_07_15.plusYears(2).minusDays(365 + 366); 1558 assertEquals(t, TEST_2007_07_15); 1559 } 1560 1561 @Test 1562 public void test_minusDays_negative() { 1563 LocalDate t = TEST_2007_07_15.minusDays(-1); 1564 assertEquals(t, LocalDate.of(2007, 7, 16)); 1565 } 1566 1567 @Test 1568 public void test_minusDays_negativeAcrossYear() { 1569 LocalDate t = TEST_2007_07_15.minusDays(-169); 1570 assertEquals(t, LocalDate.of(2007, 12, 31)); 1571 } 1572 1573 @Test 1574 public void test_minusDays_negativeOverYears() { 1575 LocalDate t = TEST_2007_07_15.minusDays(-731); 1576 assertEquals(t, LocalDate.of(2009, 7, 15)); 1577 } 1578 1579 @Test 1580 public void test_minusDays_maximum() { 1581 LocalDate t = LocalDate.of(Year.MAX_VALUE, 12, 30).minusDays(-1); 1582 LocalDate expected = LocalDate.of(Year.MAX_VALUE, 12, 31); 1583 assertEquals(t, expected); 1584 } 1585 1586 @Test 1587 public void test_minusDays_minimum() { 1588 LocalDate t = LocalDate.of(Year.MIN_VALUE, 1, 2).minusDays(1); 1589 LocalDate expected = LocalDate.of(Year.MIN_VALUE, 1, 1); 1590 assertEquals(t, expected); 1591 } 1592 1593 @Test(expectedExceptions={DateTimeException.class}) 1594 public void test_minusDays_invalidTooLarge() { 1595 LocalDate.of(Year.MAX_VALUE, 12, 31).minusDays(-1); 1596 } 1597 1598 @Test(expectedExceptions={DateTimeException.class}) 1599 public void test_minusDays_invalidTooSmall() { 1600 LocalDate.of(Year.MIN_VALUE, 1, 1).minusDays(1); 1601 } 1602 1603 @Test(expectedExceptions=ArithmeticException.class) 1604 public void test_minusDays_overflowTooLarge() { 1605 LocalDate.of(Year.MAX_VALUE, 12, 31).minusDays(Long.MIN_VALUE); 1606 } 1607 1608 @Test(expectedExceptions=ArithmeticException.class) 1609 public void test_minusDays_overflowTooSmall() { 1610 LocalDate.of(Year.MIN_VALUE, 1, 1).minusDays(Long.MAX_VALUE); 1611 } 1612 1613 //----------------------------------------------------------------------- 1614 // until() 1615 //----------------------------------------------------------------------- 1616 @DataProvider(name="until") 1617 Object[][] provider_until() { 1618 return new Object[][]{ 1619 {"2012-06-30", "2012-06-30", DAYS, 0}, 1620 {"2012-06-30", "2012-06-30", WEEKS, 0}, 1621 {"2012-06-30", "2012-06-30", MONTHS, 0}, 1622 {"2012-06-30", "2012-06-30", YEARS, 0}, 1623 {"2012-06-30", "2012-06-30", DECADES, 0}, 1624 {"2012-06-30", "2012-06-30", CENTURIES, 0}, 1625 {"2012-06-30", "2012-06-30", MILLENNIA, 0}, 1626 1627 {"2012-06-30", "2012-07-01", DAYS, 1}, 1628 {"2012-06-30", "2012-07-01", WEEKS, 0}, 1629 {"2012-06-30", "2012-07-01", MONTHS, 0}, 1630 {"2012-06-30", "2012-07-01", YEARS, 0}, 1631 {"2012-06-30", "2012-07-01", DECADES, 0}, 1632 {"2012-06-30", "2012-07-01", CENTURIES, 0}, 1633 {"2012-06-30", "2012-07-01", MILLENNIA, 0}, 1634 1635 {"2012-06-30", "2012-07-07", DAYS, 7}, 1636 {"2012-06-30", "2012-07-07", WEEKS, 1}, 1637 {"2012-06-30", "2012-07-07", MONTHS, 0}, 1638 {"2012-06-30", "2012-07-07", YEARS, 0}, 1639 {"2012-06-30", "2012-07-07", DECADES, 0}, 1640 {"2012-06-30", "2012-07-07", CENTURIES, 0}, 1641 {"2012-06-30", "2012-07-07", MILLENNIA, 0}, 1642 1643 {"2012-06-30", "2012-07-29", MONTHS, 0}, 1644 {"2012-06-30", "2012-07-30", MONTHS, 1}, 1645 {"2012-06-30", "2012-07-31", MONTHS, 1}, 1646 }; 1647 } 1648 1649 @Test(dataProvider = "until") 1650 public void test_until(String startStr, String endStr, TemporalUnit unit, long expected) { 1651 LocalDate start = LocalDate.parse(startStr); 1652 LocalDate end = LocalDate.parse(endStr); 1653 assertEquals(start.until(end, unit), expected); 1654 assertEquals(end.until(start, unit), -expected); 1655 } 1656 1657 //----------------------------------------------------------------------- 1658 // atTime() 1659 //----------------------------------------------------------------------- 1660 @Test 1661 public void test_atTime_LocalTime() { 1662 LocalDate t = LocalDate.of(2008, 6, 30); 1663 assertEquals(t.atTime(LocalTime.of(11, 30)), LocalDateTime.of(2008, 6, 30, 11, 30)); 1664 } 1665 1666 @Test(expectedExceptions=NullPointerException.class) 1667 public void test_atTime_LocalTime_null() { 1668 LocalDate t = LocalDate.of(2008, 6, 30); 1669 t.atTime((LocalTime) null); 1670 } 1671 1672 //------------------------------------------------------------------------- 1673 @Test 1674 public void test_atTime_int_int() { 1675 LocalDate t = LocalDate.of(2008, 6, 30); 1676 assertEquals(t.atTime(11, 30), LocalDateTime.of(2008, 6, 30, 11, 30)); 1677 } 1678 1679 @Test(expectedExceptions=DateTimeException.class) 1680 public void test_atTime_int_int_hourTooSmall() { 1681 LocalDate t = LocalDate.of(2008, 6, 30); 1682 t.atTime(-1, 30); 1683 } 1684 1685 @Test(expectedExceptions=DateTimeException.class) 1686 public void test_atTime_int_int_hourTooBig() { 1687 LocalDate t = LocalDate.of(2008, 6, 30); 1688 t.atTime(24, 30); 1689 } 1690 1691 @Test(expectedExceptions=DateTimeException.class) 1692 public void test_atTime_int_int_minuteTooSmall() { 1693 LocalDate t = LocalDate.of(2008, 6, 30); 1694 t.atTime(11, -1); 1695 } 1696 1697 @Test(expectedExceptions=DateTimeException.class) 1698 public void test_atTime_int_int_minuteTooBig() { 1699 LocalDate t = LocalDate.of(2008, 6, 30); 1700 t.atTime(11, 60); 1701 } 1702 1703 @Test 1704 public void test_atTime_int_int_int() { 1705 LocalDate t = LocalDate.of(2008, 6, 30); 1706 assertEquals(t.atTime(11, 30, 40), LocalDateTime.of(2008, 6, 30, 11, 30, 40)); 1707 } 1708 1709 @Test(expectedExceptions=DateTimeException.class) 1710 public void test_atTime_int_int_int_hourTooSmall() { 1711 LocalDate t = LocalDate.of(2008, 6, 30); 1712 t.atTime(-1, 30, 40); 1713 } 1714 1715 @Test(expectedExceptions=DateTimeException.class) 1716 public void test_atTime_int_int_int_hourTooBig() { 1717 LocalDate t = LocalDate.of(2008, 6, 30); 1718 t.atTime(24, 30, 40); 1719 } 1720 1721 @Test(expectedExceptions=DateTimeException.class) 1722 public void test_atTime_int_int_int_minuteTooSmall() { 1723 LocalDate t = LocalDate.of(2008, 6, 30); 1724 t.atTime(11, -1, 40); 1725 } 1726 1727 @Test(expectedExceptions=DateTimeException.class) 1728 public void test_atTime_int_int_int_minuteTooBig() { 1729 LocalDate t = LocalDate.of(2008, 6, 30); 1730 t.atTime(11, 60, 40); 1731 } 1732 1733 @Test(expectedExceptions=DateTimeException.class) 1734 public void test_atTime_int_int_int_secondTooSmall() { 1735 LocalDate t = LocalDate.of(2008, 6, 30); 1736 t.atTime(11, 30, -1); 1737 } 1738 1739 @Test(expectedExceptions=DateTimeException.class) 1740 public void test_atTime_int_int_int_secondTooBig() { 1741 LocalDate t = LocalDate.of(2008, 6, 30); 1742 t.atTime(11, 30, 60); 1743 } 1744 1745 @Test 1746 public void test_atTime_int_int_int_int() { 1747 LocalDate t = LocalDate.of(2008, 6, 30); 1748 assertEquals(t.atTime(11, 30, 40, 50), LocalDateTime.of(2008, 6, 30, 11, 30, 40, 50)); 1749 } 1750 1751 @Test(expectedExceptions=DateTimeException.class) 1752 public void test_atTime_int_int_int_int_hourTooSmall() { 1753 LocalDate t = LocalDate.of(2008, 6, 30); 1754 t.atTime(-1, 30, 40, 50); 1755 } 1756 1757 @Test(expectedExceptions=DateTimeException.class) 1758 public void test_atTime_int_int_int_int_hourTooBig() { 1759 LocalDate t = LocalDate.of(2008, 6, 30); 1760 t.atTime(24, 30, 40, 50); 1761 } 1762 1763 @Test(expectedExceptions=DateTimeException.class) 1764 public void test_atTime_int_int_int_int_minuteTooSmall() { 1765 LocalDate t = LocalDate.of(2008, 6, 30); 1766 t.atTime(11, -1, 40, 50); 1767 } 1768 1769 @Test(expectedExceptions=DateTimeException.class) 1770 public void test_atTime_int_int_int_int_minuteTooBig() { 1771 LocalDate t = LocalDate.of(2008, 6, 30); 1772 t.atTime(11, 60, 40, 50); 1773 } 1774 1775 @Test(expectedExceptions=DateTimeException.class) 1776 public void test_atTime_int_int_int_int_secondTooSmall() { 1777 LocalDate t = LocalDate.of(2008, 6, 30); 1778 t.atTime(11, 30, -1, 50); 1779 } 1780 1781 @Test(expectedExceptions=DateTimeException.class) 1782 public void test_atTime_int_int_int_int_secondTooBig() { 1783 LocalDate t = LocalDate.of(2008, 6, 30); 1784 t.atTime(11, 30, 60, 50); 1785 } 1786 1787 @Test(expectedExceptions=DateTimeException.class) 1788 public void test_atTime_int_int_int_int_nanoTooSmall() { 1789 LocalDate t = LocalDate.of(2008, 6, 30); 1790 t.atTime(11, 30, 40, -1); 1791 } 1792 1793 @Test(expectedExceptions=DateTimeException.class) 1794 public void test_atTime_int_int_int_int_nanoTooBig() { 1795 LocalDate t = LocalDate.of(2008, 6, 30); 1796 t.atTime(11, 30, 40, 1000000000); 1797 } 1798 1799 //----------------------------------------------------------------------- 1800 // atStartOfDay() 1801 //----------------------------------------------------------------------- 1802 @Test 1803 public void test_atStartOfDay() { 1804 LocalDate t = LocalDate.of(2008, 6, 30); 1805 assertEquals(t.atStartOfDay(ZONE_PARIS), 1806 ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 0, 0), ZONE_PARIS)); 1807 } 1808 1809 @Test 1810 public void test_atStartOfDay_dstGap() { 1811 LocalDate t = LocalDate.of(2007, 4, 1); 1812 assertEquals(t.atStartOfDay(ZONE_GAZA), 1813 ZonedDateTime.of(LocalDateTime.of(2007, 4, 1, 1, 0), ZONE_GAZA)); 1814 } 1815 1816 @Test(expectedExceptions=NullPointerException.class) 1817 public void test_atStartOfDay_nullTimeZone() { 1818 LocalDate t = LocalDate.of(2008, 6, 30); 1819 t.atStartOfDay((ZoneId) null); 1820 } 1821 1822 //----------------------------------------------------------------------- 1823 // toEpochDay() 1824 //----------------------------------------------------------------------- 1825 @Test 1826 public void test_toEpochDay() { 1827 long date_0000_01_01 = -678941 - 40587; 1828 1829 LocalDate test = LocalDate.of(0, 1, 1); 1830 for (long i = date_0000_01_01; i < 700000; i++) { 1831 assertEquals(test.toEpochDay(), i); 1832 test = next(test); 1833 } 1834 test = LocalDate.of(0, 1, 1); 1835 for (long i = date_0000_01_01; i > -2000000; i--) { 1836 assertEquals(test.toEpochDay(), i); 1837 test = previous(test); 1838 } 1839 1840 assertEquals(LocalDate.of(1858, 11, 17).toEpochDay(), -40587); 1841 assertEquals(LocalDate.of(1, 1, 1).toEpochDay(), -678575 - 40587); 1842 assertEquals(LocalDate.of(1995, 9, 27).toEpochDay(), 49987 - 40587); 1843 assertEquals(LocalDate.of(1970, 1, 1).toEpochDay(), 0); 1844 assertEquals(LocalDate.of(-1, 12, 31).toEpochDay(), -678942 - 40587); 1845 } 1846 1847 //----------------------------------------------------------------------- 1848 // compareTo() 1849 //----------------------------------------------------------------------- 1850 @Test 1851 public void test_comparisons() { 1852 doTest_comparisons_LocalDate( 1853 LocalDate.of(Year.MIN_VALUE, 1, 1), 1854 LocalDate.of(Year.MIN_VALUE, 12, 31), 1855 LocalDate.of(-1, 1, 1), 1856 LocalDate.of(-1, 12, 31), 1857 LocalDate.of(0, 1, 1), 1858 LocalDate.of(0, 12, 31), 1859 LocalDate.of(1, 1, 1), 1860 LocalDate.of(1, 12, 31), 1861 LocalDate.of(2006, 1, 1), 1862 LocalDate.of(2006, 12, 31), 1863 LocalDate.of(2007, 1, 1), 1864 LocalDate.of(2007, 12, 31), 1865 LocalDate.of(2008, 1, 1), 1866 LocalDate.of(2008, 2, 29), 1867 LocalDate.of(2008, 12, 31), 1868 LocalDate.of(Year.MAX_VALUE, 1, 1), 1869 LocalDate.of(Year.MAX_VALUE, 12, 31) 1870 ); 1871 } 1872 1873 void doTest_comparisons_LocalDate(LocalDate... localDates) { 1874 for (int i = 0; i < localDates.length; i++) { 1875 LocalDate a = localDates[i]; 1876 for (int j = 0; j < localDates.length; j++) { 1877 LocalDate b = localDates[j]; 1878 if (i < j) { 1879 assertTrue(a.compareTo(b) < 0, a + " <=> " + b); 1880 assertEquals(a.isBefore(b), true, a + " <=> " + b); 1881 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1882 assertEquals(a.equals(b), false, a + " <=> " + b); 1883 } else if (i > j) { 1884 assertTrue(a.compareTo(b) > 0, a + " <=> " + b); 1885 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1886 assertEquals(a.isAfter(b), true, a + " <=> " + b); 1887 assertEquals(a.equals(b), false, a + " <=> " + b); 1888 } else { 1889 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 1890 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1891 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1892 assertEquals(a.equals(b), true, a + " <=> " + b); 1893 } 1894 } 1895 } 1896 } 1897 1898 @Test(expectedExceptions=NullPointerException.class) 1899 public void test_compareTo_ObjectNull() { 1900 TEST_2007_07_15.compareTo(null); 1901 } 1902 1903 @Test 1904 public void test_isBefore() { 1905 assertTrue(TEST_2007_07_15.isBefore(LocalDate.of(2007, 07, 16))); 1906 assertFalse(TEST_2007_07_15.isBefore(LocalDate.of(2007, 07, 14))); 1907 assertFalse(TEST_2007_07_15.isBefore(TEST_2007_07_15)); 1908 } 1909 1910 @Test(expectedExceptions=NullPointerException.class) 1911 public void test_isBefore_ObjectNull() { 1912 TEST_2007_07_15.isBefore(null); 1913 } 1914 1915 @Test(expectedExceptions=NullPointerException.class) 1916 public void test_isAfter_ObjectNull() { 1917 TEST_2007_07_15.isAfter(null); 1918 } 1919 1920 @Test 1921 public void test_isAfter() { 1922 assertTrue(TEST_2007_07_15.isAfter(LocalDate.of(2007, 07, 14))); 1923 assertFalse(TEST_2007_07_15.isAfter(LocalDate.of(2007, 07, 16))); 1924 assertFalse(TEST_2007_07_15.isAfter(TEST_2007_07_15)); 1925 } 1926 1927 @Test(expectedExceptions=ClassCastException.class) 1928 @SuppressWarnings({"unchecked", "rawtypes"}) 1929 public void compareToNonLocalDate() { 1930 Comparable c = TEST_2007_07_15; 1931 c.compareTo(new Object()); 1932 } 1933 1934 //----------------------------------------------------------------------- 1935 // equals() 1936 //----------------------------------------------------------------------- 1937 @Test(dataProvider="sampleDates" ) 1938 public void test_equals_true(int y, int m, int d) { 1939 LocalDate a = LocalDate.of(y, m, d); 1940 LocalDate b = LocalDate.of(y, m, d); 1941 assertEquals(a.equals(b), true); 1942 } 1943 @Test(dataProvider="sampleDates") 1944 public void test_equals_false_year_differs(int y, int m, int d) { 1945 LocalDate a = LocalDate.of(y, m, d); 1946 LocalDate b = LocalDate.of(y + 1, m, d); 1947 assertEquals(a.equals(b), false); 1948 } 1949 @Test(dataProvider="sampleDates") 1950 public void test_equals_false_month_differs(int y, int m, int d) { 1951 LocalDate a = LocalDate.of(y, m, d); 1952 LocalDate b = LocalDate.of(y, m + 1, d); 1953 assertEquals(a.equals(b), false); 1954 } 1955 @Test(dataProvider="sampleDates") 1956 public void test_equals_false_day_differs(int y, int m, int d) { 1957 LocalDate a = LocalDate.of(y, m, d); 1958 LocalDate b = LocalDate.of(y, m, d + 1); 1959 assertEquals(a.equals(b), false); 1960 } 1961 1962 @Test 1963 public void test_equals_itself_true() { 1964 assertEquals(TEST_2007_07_15.equals(TEST_2007_07_15), true); 1965 } 1966 1967 @Test 1968 public void test_equals_string_false() { 1969 assertEquals(TEST_2007_07_15.equals("2007-07-15"), false); 1970 } 1971 1972 @Test 1973 public void test_equals_null_false() { 1974 assertEquals(TEST_2007_07_15.equals(null), false); 1975 } 1976 1977 //----------------------------------------------------------------------- 1978 // hashCode() 1979 //----------------------------------------------------------------------- 1980 @Test(dataProvider="sampleDates") 1981 public void test_hashCode(int y, int m, int d) { 1982 LocalDate a = LocalDate.of(y, m, d); 1983 assertEquals(a.hashCode(), a.hashCode()); 1984 LocalDate b = LocalDate.of(y, m, d); 1985 assertEquals(a.hashCode(), b.hashCode()); 1986 } 1987 1988 //----------------------------------------------------------------------- 1989 // toString() 1990 //----------------------------------------------------------------------- 1991 @DataProvider(name="sampleToString") 1992 Object[][] provider_sampleToString() { 1993 return new Object[][] { 1994 {2008, 7, 5, "2008-07-05"}, 1995 {2007, 12, 31, "2007-12-31"}, 1996 {999, 12, 31, "0999-12-31"}, 1997 {-1, 1, 2, "-0001-01-02"}, 1998 {9999, 12, 31, "9999-12-31"}, 1999 {-9999, 12, 31, "-9999-12-31"}, 2000 {10000, 1, 1, "+10000-01-01"}, 2001 {-10000, 1, 1, "-10000-01-01"}, 2002 {12345678, 1, 1, "+12345678-01-01"}, 2003 {-12345678, 1, 1, "-12345678-01-01"}, 2004 }; 2005 } 2006 2007 @Test(dataProvider="sampleToString") 2008 public void test_toString(int y, int m, int d, String expected) { 2009 LocalDate t = LocalDate.of(y, m, d); 2010 String str = t.toString(); 2011 assertEquals(str, expected); 2012 } 2013 2014 //----------------------------------------------------------------------- 2015 // format(DateTimeFormatter) 2016 //----------------------------------------------------------------------- 2017 @Test 2018 public void test_format_formatter() { 2019 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d"); 2020 String t = LocalDate.of(2010, 12, 3).format(f); 2021 assertEquals(t, "2010 12 3"); 2022 } 2023 2024 @Test(expectedExceptions=NullPointerException.class) 2025 public void test_format_formatter_null() { 2026 LocalDate.of(2010, 12, 3).format(null); 2027 } 2028 2029 } 2030