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