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