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