1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertFalse; 36 import static org.testng.Assert.assertSame; 37 import static org.testng.Assert.assertTrue; 38 import static org.threeten.bp.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 39 import static org.threeten.bp.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 40 import static org.threeten.bp.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 41 import static org.threeten.bp.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 42 import static org.threeten.bp.temporal.ChronoField.AMPM_OF_DAY; 43 import static org.threeten.bp.temporal.ChronoField.CLOCK_HOUR_OF_AMPM; 44 import static org.threeten.bp.temporal.ChronoField.CLOCK_HOUR_OF_DAY; 45 import static org.threeten.bp.temporal.ChronoField.DAY_OF_MONTH; 46 import static org.threeten.bp.temporal.ChronoField.DAY_OF_WEEK; 47 import static org.threeten.bp.temporal.ChronoField.DAY_OF_YEAR; 48 import static org.threeten.bp.temporal.ChronoField.EPOCH_DAY; 49 import static org.threeten.bp.temporal.ChronoField.ERA; 50 import static org.threeten.bp.temporal.ChronoField.HOUR_OF_AMPM; 51 import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY; 52 import static org.threeten.bp.temporal.ChronoField.MICRO_OF_DAY; 53 import static org.threeten.bp.temporal.ChronoField.MICRO_OF_SECOND; 54 import static org.threeten.bp.temporal.ChronoField.MILLI_OF_DAY; 55 import static org.threeten.bp.temporal.ChronoField.MILLI_OF_SECOND; 56 import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_DAY; 57 import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR; 58 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR; 59 import static org.threeten.bp.temporal.ChronoField.NANO_OF_DAY; 60 import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND; 61 import static org.threeten.bp.temporal.ChronoField.PROLEPTIC_MONTH; 62 import static org.threeten.bp.temporal.ChronoField.SECOND_OF_DAY; 63 import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE; 64 import static org.threeten.bp.temporal.ChronoField.YEAR; 65 import static org.threeten.bp.temporal.ChronoField.YEAR_OF_ERA; 66 import static org.threeten.bp.temporal.ChronoUnit.HALF_DAYS; 67 import static org.threeten.bp.temporal.ChronoUnit.HOURS; 68 import static org.threeten.bp.temporal.ChronoUnit.MICROS; 69 import static org.threeten.bp.temporal.ChronoUnit.MILLIS; 70 import static org.threeten.bp.temporal.ChronoUnit.MINUTES; 71 import static org.threeten.bp.temporal.ChronoUnit.NANOS; 72 import static org.threeten.bp.temporal.ChronoUnit.SECONDS; 73 74 import java.io.ByteArrayInputStream; 75 import java.io.ByteArrayOutputStream; 76 import java.io.IOException; 77 import java.io.ObjectInputStream; 78 import java.io.ObjectOutputStream; 79 import java.lang.reflect.Field; 80 import java.lang.reflect.Modifier; 81 import java.util.ArrayList; 82 import java.util.Arrays; 83 import java.util.Iterator; 84 import java.util.List; 85 86 import org.testng.annotations.BeforeMethod; 87 import org.testng.annotations.DataProvider; 88 import org.testng.annotations.Test; 89 import org.threeten.bp.chrono.IsoChronology; 90 import org.threeten.bp.format.DateTimeFormatter; 91 import org.threeten.bp.format.DateTimeParseException; 92 import org.threeten.bp.temporal.ChronoField; 93 import org.threeten.bp.temporal.ChronoUnit; 94 import org.threeten.bp.temporal.JulianFields; 95 import org.threeten.bp.temporal.MockFieldNoValue; 96 import org.threeten.bp.temporal.Temporal; 97 import org.threeten.bp.temporal.TemporalAccessor; 98 import org.threeten.bp.temporal.TemporalAdjuster; 99 import org.threeten.bp.temporal.TemporalField; 100 import org.threeten.bp.temporal.TemporalQueries; 101 import org.threeten.bp.temporal.TemporalUnit; 102 103 /** 104 * Test LocalDateTime. 105 */ 106 @Test 107 public class TestLocalDateTime extends AbstractDateTimeTest { 108 109 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1); 110 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2); 111 private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2); 112 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 113 private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza"); 114 115 private LocalDateTime TEST_2007_07_15_12_30_40_987654321 = LocalDateTime.of(2007, 7, 15, 12, 30, 40, 987654321); 116 private LocalDateTime MAX_DATE_TIME; 117 private LocalDateTime MIN_DATE_TIME; 118 private Instant MAX_INSTANT; 119 private Instant MIN_INSTANT; 120 121 @BeforeMethod setUp()122 public void setUp() { 123 MAX_DATE_TIME = LocalDateTime.MAX; 124 MIN_DATE_TIME = LocalDateTime.MIN; 125 MAX_INSTANT = MAX_DATE_TIME.atZone(ZoneOffset.UTC).toInstant(); 126 MIN_INSTANT = MIN_DATE_TIME.atZone(ZoneOffset.UTC).toInstant(); 127 } 128 129 //----------------------------------------------------------------------- 130 @Override samples()131 protected List<TemporalAccessor> samples() { 132 TemporalAccessor[] array = {TEST_2007_07_15_12_30_40_987654321, LocalDateTime.MAX, LocalDateTime.MIN, }; 133 return Arrays.asList(array); 134 } 135 136 @Override validFields()137 protected List<TemporalField> validFields() { 138 TemporalField[] array = { 139 NANO_OF_SECOND, 140 NANO_OF_DAY, 141 MICRO_OF_SECOND, 142 MICRO_OF_DAY, 143 MILLI_OF_SECOND, 144 MILLI_OF_DAY, 145 SECOND_OF_MINUTE, 146 SECOND_OF_DAY, 147 MINUTE_OF_HOUR, 148 MINUTE_OF_DAY, 149 CLOCK_HOUR_OF_AMPM, 150 HOUR_OF_AMPM, 151 CLOCK_HOUR_OF_DAY, 152 HOUR_OF_DAY, 153 AMPM_OF_DAY, 154 DAY_OF_WEEK, 155 ALIGNED_DAY_OF_WEEK_IN_MONTH, 156 ALIGNED_DAY_OF_WEEK_IN_YEAR, 157 DAY_OF_MONTH, 158 DAY_OF_YEAR, 159 EPOCH_DAY, 160 ALIGNED_WEEK_OF_MONTH, 161 ALIGNED_WEEK_OF_YEAR, 162 MONTH_OF_YEAR, 163 PROLEPTIC_MONTH, 164 YEAR_OF_ERA, 165 YEAR, 166 ERA, 167 JulianFields.JULIAN_DAY, 168 JulianFields.MODIFIED_JULIAN_DAY, 169 JulianFields.RATA_DIE, 170 }; 171 return Arrays.asList(array); 172 } 173 174 @Override invalidFields()175 protected List<TemporalField> invalidFields() { 176 List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values())); 177 list.removeAll(validFields()); 178 return list; 179 } 180 181 //----------------------------------------------------------------------- check(LocalDateTime dateTime, int y, int m, int d, int h, int mi, int s, int n)182 private void check(LocalDateTime dateTime, int y, int m, int d, int h, int mi, int s, int n) { 183 assertEquals(dateTime.getYear(), y); 184 assertEquals(dateTime.getMonth().getValue(), m); 185 assertEquals(dateTime.getDayOfMonth(), d); 186 assertEquals(dateTime.getHour(), h); 187 assertEquals(dateTime.getMinute(), mi); 188 assertEquals(dateTime.getSecond(), s); 189 assertEquals(dateTime.getNano(), n); 190 } 191 createDateMidnight(int year, int month, int day)192 private LocalDateTime createDateMidnight(int year, int month, int day) { 193 return LocalDateTime.of(year, month, day, 0, 0); 194 } 195 196 @Test test_serialization()197 public void test_serialization() throws IOException, ClassNotFoundException { 198 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 199 ObjectOutputStream oos = new ObjectOutputStream(baos); 200 oos.writeObject(TEST_2007_07_15_12_30_40_987654321); 201 oos.close(); 202 203 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( 204 baos.toByteArray())); 205 assertEquals(ois.readObject(), TEST_2007_07_15_12_30_40_987654321); 206 } 207 208 @Test test_immutable()209 public void test_immutable() { 210 Class<LocalDateTime> cls = LocalDateTime.class; 211 assertTrue(Modifier.isPublic(cls.getModifiers())); 212 assertTrue(Modifier.isFinal(cls.getModifiers())); 213 Field[] fields = cls.getDeclaredFields(); 214 for (Field field : fields) { 215 if (field.getName().contains("$") == false) { 216 if (Modifier.isStatic(field.getModifiers())) { 217 assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName()); 218 } else { 219 assertTrue(Modifier.isPrivate(field.getModifiers()), "Field:" + field.getName()); 220 assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName()); 221 } 222 } 223 } 224 } 225 226 //----------------------------------------------------------------------- 227 // now() 228 //----------------------------------------------------------------------- 229 @Test(timeOut=30000) // TODO: remove when time zone loading is faster now()230 public void now() { 231 LocalDateTime expected = LocalDateTime.now(Clock.systemDefaultZone()); 232 LocalDateTime test = LocalDateTime.now(); 233 long diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 234 if (diff >= 100000000) { 235 // may be date change 236 expected = LocalDateTime.now(Clock.systemDefaultZone()); 237 test = LocalDateTime.now(); 238 diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 239 } 240 assertTrue(diff < 100000000); // less than 0.1 secs 241 } 242 243 //----------------------------------------------------------------------- 244 // now(ZoneId) 245 //----------------------------------------------------------------------- 246 @Test(expectedExceptions=NullPointerException.class) 247 public void now_ZoneId_nullZoneId() { 248 LocalDateTime.now((ZoneId) null); 249 } 250 251 @Test 252 public void now_ZoneId() { 253 ZoneId zone = ZoneId.of("UTC+01:02:03"); 254 LocalDateTime expected = LocalDateTime.now(Clock.system(zone)); 255 LocalDateTime test = LocalDateTime.now(zone); 256 for (int i = 0; i < 100; i++) { 257 if (expected.equals(test)) { 258 return; 259 } 260 expected = LocalDateTime.now(Clock.system(zone)); 261 test = LocalDateTime.now(zone); 262 } 263 assertEquals(test, expected); 264 } 265 266 //----------------------------------------------------------------------- 267 // now(Clock) 268 //----------------------------------------------------------------------- 269 @Test(expectedExceptions=NullPointerException.class) 270 public void now_Clock_nullClock() { 271 LocalDateTime.now((Clock) null); 272 } 273 274 @Test 275 public void now_Clock_allSecsInDay_utc() { 276 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 277 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 278 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 279 LocalDateTime test = LocalDateTime.now(clock); 280 assertEquals(test.getYear(), 1970); 281 assertEquals(test.getMonth(), Month.JANUARY); 282 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2)); 283 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 284 assertEquals(test.getMinute(), (i / 60) % 60); 285 assertEquals(test.getSecond(), i % 60); 286 assertEquals(test.getNano(), 123456789); 287 } 288 } 289 290 @Test 291 public void now_Clock_allSecsInDay_offset() { 292 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 293 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 294 Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE); 295 LocalDateTime test = LocalDateTime.now(clock); 296 assertEquals(test.getYear(), 1970); 297 assertEquals(test.getMonth(), Month.JANUARY); 298 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2); 299 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 300 assertEquals(test.getMinute(), (i / 60) % 60); 301 assertEquals(test.getSecond(), i % 60); 302 assertEquals(test.getNano(), 123456789); 303 } 304 } 305 306 @Test 307 public void now_Clock_allSecsInDay_beforeEpoch() { 308 LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L); 309 for (int i =-1; i >= -(24 * 60 * 60); i--) { 310 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 311 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 312 LocalDateTime test = LocalDateTime.now(clock); 313 assertEquals(test.getYear(), 1969); 314 assertEquals(test.getMonth(), Month.DECEMBER); 315 assertEquals(test.getDayOfMonth(), 31); 316 expected = expected.minusSeconds(1); 317 assertEquals(test.toLocalTime(), expected); 318 } 319 } 320 321 //----------------------------------------------------------------------- 322 @Test 323 public void now_Clock_maxYear() { 324 Clock clock = Clock.fixed(MAX_INSTANT, ZoneOffset.UTC); 325 LocalDateTime test = LocalDateTime.now(clock); 326 assertEquals(test, MAX_DATE_TIME); 327 } 328 329 @Test(expectedExceptions=DateTimeException.class) 330 public void now_Clock_tooBig() { 331 Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC); 332 LocalDateTime.now(clock); 333 } 334 335 @Test 336 public void now_Clock_minYear() { 337 Clock clock = Clock.fixed(MIN_INSTANT, ZoneOffset.UTC); 338 LocalDateTime test = LocalDateTime.now(clock); 339 assertEquals(test, MIN_DATE_TIME); 340 } 341 342 @Test(expectedExceptions=DateTimeException.class) 343 public void now_Clock_tooLow() { 344 Clock clock = Clock.fixed(MIN_INSTANT.minusNanos(1), ZoneOffset.UTC); 345 LocalDateTime.now(clock); 346 } 347 348 //----------------------------------------------------------------------- 349 // of() factories 350 //----------------------------------------------------------------------- 351 //----------------------------------------------------------------------- 352 @Test 353 public void factory_of_4intsMonth() { 354 LocalDateTime dateTime = LocalDateTime.of(2007, Month.JULY, 15, 12, 30); 355 check(dateTime, 2007, 7, 15, 12, 30, 0, 0); 356 } 357 358 @Test(expectedExceptions=DateTimeException.class) 359 public void factory_of_4intsMonth_yearTooLow() { 360 LocalDateTime.of(Integer.MIN_VALUE, Month.JULY, 15, 12, 30); 361 } 362 363 @Test(expectedExceptions=NullPointerException.class) 364 public void factory_of_4intsMonth_nullMonth() { 365 LocalDateTime.of(2007, null, 15, 12, 30); 366 } 367 368 @Test(expectedExceptions=DateTimeException.class) 369 public void factory_of_4intsMonth_dayTooLow() { 370 LocalDateTime.of(2007, Month.JULY, -1, 12, 30); 371 } 372 373 @Test(expectedExceptions=DateTimeException.class) 374 public void factory_of_4intsMonth_dayTooHigh() { 375 LocalDateTime.of(2007, Month.JULY, 32, 12, 30); 376 } 377 378 @Test(expectedExceptions=DateTimeException.class) 379 public void factory_of_4intsMonth_hourTooLow() { 380 LocalDateTime.of(2007, Month.JULY, 15, -1, 30); 381 } 382 383 @Test(expectedExceptions=DateTimeException.class) 384 public void factory_of_4intsMonth_hourTooHigh() { 385 LocalDateTime.of(2007, Month.JULY, 15, 24, 30); 386 } 387 388 @Test(expectedExceptions=DateTimeException.class) 389 public void factory_of_4intsMonth_minuteTooLow() { 390 LocalDateTime.of(2007, Month.JULY, 15, 12, -1); 391 } 392 393 @Test(expectedExceptions=DateTimeException.class) 394 public void factory_of_4intsMonth_minuteTooHigh() { 395 LocalDateTime.of(2007, Month.JULY, 15, 12, 60); 396 } 397 398 //----------------------------------------------------------------------- 399 @Test 400 public void factory_of_5intsMonth() { 401 LocalDateTime dateTime = LocalDateTime.of(2007, Month.JULY, 15, 12, 30, 40); 402 check(dateTime, 2007, 7, 15, 12, 30, 40, 0); 403 } 404 405 @Test(expectedExceptions=DateTimeException.class) 406 public void factory_of_5intsMonth_yearTooLow() { 407 LocalDateTime.of(Integer.MIN_VALUE, Month.JULY, 15, 12, 30, 40); 408 } 409 410 @Test(expectedExceptions=NullPointerException.class) 411 public void factory_of_5intsMonth_nullMonth() { 412 LocalDateTime.of(2007, null, 15, 12, 30, 40); 413 } 414 415 @Test(expectedExceptions=DateTimeException.class) 416 public void factory_of_5intsMonth_dayTooLow() { 417 LocalDateTime.of(2007, Month.JULY, -1, 12, 30, 40); 418 } 419 420 @Test(expectedExceptions=DateTimeException.class) 421 public void factory_of_5intsMonth_dayTooHigh() { 422 LocalDateTime.of(2007, Month.JULY, 32, 12, 30, 40); 423 } 424 425 @Test(expectedExceptions=DateTimeException.class) 426 public void factory_of_5intsMonth_hourTooLow() { 427 LocalDateTime.of(2007, Month.JULY, 15, -1, 30, 40); 428 } 429 430 @Test(expectedExceptions=DateTimeException.class) 431 public void factory_of_5intsMonth_hourTooHigh() { 432 LocalDateTime.of(2007, Month.JULY, 15, 24, 30, 40); 433 } 434 435 @Test(expectedExceptions=DateTimeException.class) 436 public void factory_of_5intsMonth_minuteTooLow() { 437 LocalDateTime.of(2007, Month.JULY, 15, 12, -1, 40); 438 } 439 440 @Test(expectedExceptions=DateTimeException.class) 441 public void factory_of_5intsMonth_minuteTooHigh() { 442 LocalDateTime.of(2007, Month.JULY, 15, 12, 60, 40); 443 } 444 445 @Test(expectedExceptions=DateTimeException.class) 446 public void factory_of_5intsMonth_secondTooLow() { 447 LocalDateTime.of(2007, Month.JULY, 15, 12, 30, -1); 448 } 449 450 @Test(expectedExceptions=DateTimeException.class) 451 public void factory_of_5intsMonth_secondTooHigh() { 452 LocalDateTime.of(2007, Month.JULY, 15, 12, 30, 60); 453 } 454 455 //----------------------------------------------------------------------- 456 @Test 457 public void factory_of_6intsMonth() { 458 LocalDateTime dateTime = LocalDateTime.of(2007, Month.JULY, 15, 12, 30, 40, 987654321); 459 check(dateTime, 2007, 7, 15, 12, 30, 40, 987654321); 460 } 461 462 @Test(expectedExceptions=DateTimeException.class) 463 public void factory_of_6intsMonth_yearTooLow() { 464 LocalDateTime.of(Integer.MIN_VALUE, Month.JULY, 15, 12, 30, 40, 987654321); 465 } 466 467 @Test(expectedExceptions=NullPointerException.class) 468 public void factory_of_6intsMonth_nullMonth() { 469 LocalDateTime.of(2007, null, 15, 12, 30, 40, 987654321); 470 } 471 472 @Test(expectedExceptions=DateTimeException.class) 473 public void factory_of_6intsMonth_dayTooLow() { 474 LocalDateTime.of(2007, Month.JULY, -1, 12, 30, 40, 987654321); 475 } 476 477 @Test(expectedExceptions=DateTimeException.class) 478 public void factory_of_6intsMonth_dayTooHigh() { 479 LocalDateTime.of(2007, Month.JULY, 32, 12, 30, 40, 987654321); 480 } 481 482 @Test(expectedExceptions=DateTimeException.class) 483 public void factory_of_6intsMonth_hourTooLow() { 484 LocalDateTime.of(2007, Month.JULY, 15, -1, 30, 40, 987654321); 485 } 486 487 @Test(expectedExceptions=DateTimeException.class) 488 public void factory_of_6intsMonth_hourTooHigh() { 489 LocalDateTime.of(2007, Month.JULY, 15, 24, 30, 40, 987654321); 490 } 491 492 @Test(expectedExceptions=DateTimeException.class) 493 public void factory_of_6intsMonth_minuteTooLow() { 494 LocalDateTime.of(2007, Month.JULY, 15, 12, -1, 40, 987654321); 495 } 496 497 @Test(expectedExceptions=DateTimeException.class) 498 public void factory_of_6intsMonth_minuteTooHigh() { 499 LocalDateTime.of(2007, Month.JULY, 15, 12, 60, 40, 987654321); 500 } 501 502 @Test(expectedExceptions=DateTimeException.class) 503 public void factory_of_6intsMonth_secondTooLow() { 504 LocalDateTime.of(2007, Month.JULY, 15, 12, 30, -1, 987654321); 505 } 506 507 @Test(expectedExceptions=DateTimeException.class) 508 public void factory_of_6intsMonth_secondTooHigh() { 509 LocalDateTime.of(2007, Month.JULY, 15, 12, 30, 60, 987654321); 510 } 511 512 @Test(expectedExceptions=DateTimeException.class) 513 public void factory_of_6intsMonth_nanoTooLow() { 514 LocalDateTime.of(2007, Month.JULY, 15, 12, 30, 40, -1); 515 } 516 517 @Test(expectedExceptions=DateTimeException.class) 518 public void factory_of_6intsMonth_nanoTooHigh() { 519 LocalDateTime.of(2007, Month.JULY, 15, 12, 30, 40, 1000000000); 520 } 521 522 //----------------------------------------------------------------------- 523 @Test 524 public void factory_of_5ints() { 525 LocalDateTime dateTime = LocalDateTime.of(2007, 7, 15, 12, 30); 526 check(dateTime, 2007, 7, 15, 12, 30, 0, 0); 527 } 528 529 @Test(expectedExceptions=DateTimeException.class) 530 public void factory_of_5ints_yearTooLow() { 531 LocalDateTime.of(Integer.MIN_VALUE, 7, 15, 12, 30); 532 } 533 534 @Test(expectedExceptions=DateTimeException.class) 535 public void factory_of_5ints_monthTooLow() { 536 LocalDateTime.of(2007, 0, 15, 12, 30); 537 } 538 539 @Test(expectedExceptions=DateTimeException.class) 540 public void factory_of_5ints_monthTooHigh() { 541 LocalDateTime.of(2007, 13, 15, 12, 30); 542 } 543 544 @Test(expectedExceptions=DateTimeException.class) 545 public void factory_of_5ints_dayTooLow() { 546 LocalDateTime.of(2007, 7, -1, 12, 30); 547 } 548 549 @Test(expectedExceptions=DateTimeException.class) 550 public void factory_of_5ints_dayTooHigh() { 551 LocalDateTime.of(2007, 7, 32, 12, 30); 552 } 553 554 @Test(expectedExceptions=DateTimeException.class) 555 public void factory_of_5ints_hourTooLow() { 556 LocalDateTime.of(2007, 7, 15, -1, 30); 557 } 558 559 @Test(expectedExceptions=DateTimeException.class) 560 public void factory_of_5ints_hourTooHigh() { 561 LocalDateTime.of(2007, 7, 15, 24, 30); 562 } 563 564 @Test(expectedExceptions=DateTimeException.class) 565 public void factory_of_5ints_minuteTooLow() { 566 LocalDateTime.of(2007, 7, 15, 12, -1); 567 } 568 569 @Test(expectedExceptions=DateTimeException.class) 570 public void factory_of_5ints_minuteTooHigh() { 571 LocalDateTime.of(2007, 7, 15, 12, 60); 572 } 573 574 //----------------------------------------------------------------------- 575 @Test 576 public void factory_of_6ints() { 577 LocalDateTime dateTime = LocalDateTime.of(2007, 7, 15, 12, 30, 40); 578 check(dateTime, 2007, 7, 15, 12, 30, 40, 0); 579 } 580 581 @Test(expectedExceptions=DateTimeException.class) 582 public void factory_of_6ints_yearTooLow() { 583 LocalDateTime.of(Integer.MIN_VALUE, 7, 15, 12, 30, 40); 584 } 585 586 @Test(expectedExceptions=DateTimeException.class) 587 public void factory_of_6ints_monthTooLow() { 588 LocalDateTime.of(2007, 0, 15, 12, 30, 40); 589 } 590 591 @Test(expectedExceptions=DateTimeException.class) 592 public void factory_of_6ints_monthTooHigh() { 593 LocalDateTime.of(2007, 13, 15, 12, 30, 40); 594 } 595 596 @Test(expectedExceptions=DateTimeException.class) 597 public void factory_of_6ints_dayTooLow() { 598 LocalDateTime.of(2007, 7, -1, 12, 30, 40); 599 } 600 601 @Test(expectedExceptions=DateTimeException.class) 602 public void factory_of_6ints_dayTooHigh() { 603 LocalDateTime.of(2007, 7, 32, 12, 30, 40); 604 } 605 606 @Test(expectedExceptions=DateTimeException.class) 607 public void factory_of_6ints_hourTooLow() { 608 LocalDateTime.of(2007, 7, 15, -1, 30, 40); 609 } 610 611 @Test(expectedExceptions=DateTimeException.class) 612 public void factory_of_6ints_hourTooHigh() { 613 LocalDateTime.of(2007, 7, 15, 24, 30, 40); 614 } 615 616 @Test(expectedExceptions=DateTimeException.class) 617 public void factory_of_6ints_minuteTooLow() { 618 LocalDateTime.of(2007, 7, 15, 12, -1, 40); 619 } 620 621 @Test(expectedExceptions=DateTimeException.class) 622 public void factory_of_6ints_minuteTooHigh() { 623 LocalDateTime.of(2007, 7, 15, 12, 60, 40); 624 } 625 626 @Test(expectedExceptions=DateTimeException.class) 627 public void factory_of_6ints_secondTooLow() { 628 LocalDateTime.of(2007, 7, 15, 12, 30, -1); 629 } 630 631 @Test(expectedExceptions=DateTimeException.class) 632 public void factory_of_6ints_secondTooHigh() { 633 LocalDateTime.of(2007, 7, 15, 12, 30, 60); 634 } 635 636 //----------------------------------------------------------------------- 637 @Test 638 public void factory_of_7ints() { 639 LocalDateTime dateTime = LocalDateTime.of(2007, 7, 15, 12, 30, 40, 987654321); 640 check(dateTime, 2007, 7, 15, 12, 30, 40, 987654321); 641 } 642 643 @Test(expectedExceptions=DateTimeException.class) 644 public void factory_of_7ints_yearTooLow() { 645 LocalDateTime.of(Integer.MIN_VALUE, 7, 15, 12, 30, 40, 987654321); 646 } 647 648 @Test(expectedExceptions=DateTimeException.class) 649 public void factory_of_7ints_monthTooLow() { 650 LocalDateTime.of(2007, 0, 15, 12, 30, 40, 987654321); 651 } 652 653 @Test(expectedExceptions=DateTimeException.class) 654 public void factory_of_7ints_monthTooHigh() { 655 LocalDateTime.of(2007, 13, 15, 12, 30, 40, 987654321); 656 } 657 658 @Test(expectedExceptions=DateTimeException.class) 659 public void factory_of_7ints_dayTooLow() { 660 LocalDateTime.of(2007, 7, -1, 12, 30, 40, 987654321); 661 } 662 663 @Test(expectedExceptions=DateTimeException.class) 664 public void factory_of_7ints_dayTooHigh() { 665 LocalDateTime.of(2007, 7, 32, 12, 30, 40, 987654321); 666 } 667 668 @Test(expectedExceptions=DateTimeException.class) 669 public void factory_of_7ints_hourTooLow() { 670 LocalDateTime.of(2007, 7, 15, -1, 30, 40, 987654321); 671 } 672 673 @Test(expectedExceptions=DateTimeException.class) 674 public void factory_of_7ints_hourTooHigh() { 675 LocalDateTime.of(2007, 7, 15, 24, 30, 40, 987654321); 676 } 677 678 @Test(expectedExceptions=DateTimeException.class) 679 public void factory_of_7ints_minuteTooLow() { 680 LocalDateTime.of(2007, 7, 15, 12, -1, 40, 987654321); 681 } 682 683 @Test(expectedExceptions=DateTimeException.class) 684 public void factory_of_7ints_minuteTooHigh() { 685 LocalDateTime.of(2007, 7, 15, 12, 60, 40, 987654321); 686 } 687 688 @Test(expectedExceptions=DateTimeException.class) 689 public void factory_of_7ints_secondTooLow() { 690 LocalDateTime.of(2007, 7, 15, 12, 30, -1, 987654321); 691 } 692 693 @Test(expectedExceptions=DateTimeException.class) 694 public void factory_of_7ints_secondTooHigh() { 695 LocalDateTime.of(2007, 7, 15, 12, 30, 60, 987654321); 696 } 697 698 @Test(expectedExceptions=DateTimeException.class) 699 public void factory_of_7ints_nanoTooLow() { 700 LocalDateTime.of(2007, 7, 15, 12, 30, 40, -1); 701 } 702 703 @Test(expectedExceptions=DateTimeException.class) 704 public void factory_of_7ints_nanoTooHigh() { 705 LocalDateTime.of(2007, 7, 15, 12, 30, 40, 1000000000); 706 } 707 708 //----------------------------------------------------------------------- 709 @Test 710 public void factory_of_LocalDate_LocalTime() { 711 LocalDateTime dateTime = LocalDateTime.of(LocalDate.of(2007, 7, 15), LocalTime.of(12, 30, 40, 987654321)); 712 check(dateTime, 2007, 7, 15, 12, 30, 40, 987654321); 713 } 714 715 @Test(expectedExceptions=NullPointerException.class) 716 public void factory_of_LocalDate_LocalTime_nullLocalDate() { 717 LocalDateTime.of(null, LocalTime.of(12, 30, 40, 987654321)); 718 } 719 720 @Test(expectedExceptions=NullPointerException.class) 721 public void factory_of_LocalDate_LocalTime_nullLocalTime() { 722 LocalDateTime.of(LocalDate.of(2007, 7, 15), null); 723 } 724 725 //----------------------------------------------------------------------- 726 // ofInstant() 727 //----------------------------------------------------------------------- 728 @Test 729 public void factory_ofInstant_zone() { 730 LocalDateTime test = LocalDateTime.ofInstant(Instant.ofEpochSecond(86400 + 3600 + 120 + 4, 500), ZONE_PARIS); 731 assertEquals(test, LocalDateTime.of(1970, 1, 2, 2, 2, 4, 500)); // offset +01:00 732 } 733 734 @Test 735 public void factory_ofInstant_offset() { 736 LocalDateTime test = LocalDateTime.ofInstant(Instant.ofEpochSecond(86400 + 3600 + 120 + 4, 500), OFFSET_MTWO); 737 assertEquals(test, LocalDateTime.of(1970, 1, 1, 23, 2, 4, 500)); 738 } 739 740 @Test 741 public void factory_ofInstant_offsetBeforeEpoch() { 742 LocalDateTime test = LocalDateTime.ofInstant(Instant.ofEpochSecond(-86400 + 4, 500), OFFSET_PTWO); 743 assertEquals(test, LocalDateTime.of(1969, 12, 31, 2, 0, 4, 500)); 744 } 745 746 @Test(expectedExceptions=DateTimeException.class) 747 public void factory_ofInstant_instantTooBig() { 748 LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.MAX_VALUE), OFFSET_PONE) ; 749 } 750 751 @Test(expectedExceptions=DateTimeException.class) 752 public void factory_ofInstant_instantTooSmall() { 753 LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.MIN_VALUE), OFFSET_PONE) ; 754 } 755 756 @Test(expectedExceptions=NullPointerException.class) 757 public void factory_ofInstant_nullInstant() { 758 LocalDateTime.ofInstant((Instant) null, ZONE_GAZA); 759 } 760 761 @Test(expectedExceptions=NullPointerException.class) 762 public void factory_ofInstant_nullZone() { 763 LocalDateTime.ofInstant(Instant.EPOCH, (ZoneId) null); 764 } 765 766 //----------------------------------------------------------------------- 767 // ofEpochSecond() 768 //----------------------------------------------------------------------- 769 @Test 770 public void factory_ofEpochSecond_longOffset_afterEpoch() { 771 LocalDateTime base = LocalDateTime.of(1970, 1, 1, 2, 0, 0, 500); 772 for (int i = 0; i < 100000; i++) { 773 LocalDateTime test = LocalDateTime.ofEpochSecond(i, 500, OFFSET_PTWO); 774 assertEquals(test, base.plusSeconds(i)); 775 } 776 } 777 778 @Test 779 public void factory_ofEpochSecond_longOffset_beforeEpoch() { 780 LocalDateTime base = LocalDateTime.of(1970, 1, 1, 2, 0, 0, 500); 781 for (int i = 0; i < 100000; i++) { 782 LocalDateTime test = LocalDateTime.ofEpochSecond(-i, 500, OFFSET_PTWO); 783 assertEquals(test, base.minusSeconds(i)); 784 } 785 } 786 787 @Test(expectedExceptions=DateTimeException.class) 788 public void factory_ofEpochSecond_longOffset_tooBig() { 789 LocalDateTime.ofEpochSecond(Long.MAX_VALUE, 500, OFFSET_PONE); // TODO: better test 790 } 791 792 @Test(expectedExceptions=DateTimeException.class) 793 public void factory_ofEpochSecond_longOffset_tooSmall() { 794 LocalDateTime.ofEpochSecond(Long.MIN_VALUE, 500, OFFSET_PONE); // TODO: better test 795 } 796 797 @Test(expectedExceptions=DateTimeException.class) 798 public void factory_ofEpochSecond_badNanos_toBig() { 799 LocalDateTime.ofEpochSecond(0, 1000000000, OFFSET_PONE); 800 } 801 802 @Test(expectedExceptions=DateTimeException.class) 803 public void factory_ofEpochSecond_badNanos_toSmall() { 804 LocalDateTime.ofEpochSecond(0, -1, OFFSET_PONE); 805 } 806 807 @Test(expectedExceptions=NullPointerException.class) 808 public void factory_ofEpochSecond_longOffset_nullOffset() { 809 LocalDateTime.ofEpochSecond(0L, 500, null); 810 } 811 812 //----------------------------------------------------------------------- 813 // from() 814 //----------------------------------------------------------------------- 815 @Test 816 public void test_from_Accessor() { 817 LocalDateTime base = LocalDateTime.of(2007, 7, 15, 17, 30); 818 assertEquals(LocalDateTime.from(base), base); 819 assertEquals(LocalDateTime.from(ZonedDateTime.of(base, ZoneOffset.ofHours(2))), base); 820 } 821 822 @Test(expectedExceptions=DateTimeException.class) 823 public void test_from_Accessor_invalid_noDerive() { 824 LocalDateTime.from(LocalTime.of(12, 30)); 825 } 826 827 @Test(expectedExceptions=NullPointerException.class) 828 public void test_from_Accessor_null() { 829 LocalDateTime.from((TemporalAccessor) null); 830 } 831 832 //----------------------------------------------------------------------- 833 // parse() 834 //----------------------------------------------------------------------- 835 @Test(dataProvider="sampleToString") 836 public void test_parse(int y, int month, int d, int h, int m, int s, int n, String text) { 837 LocalDateTime t = LocalDateTime.parse(text); 838 assertEquals(t.getYear(), y); 839 assertEquals(t.getMonth().getValue(), month); 840 assertEquals(t.getDayOfMonth(), d); 841 assertEquals(t.getHour(), h); 842 assertEquals(t.getMinute(), m); 843 assertEquals(t.getSecond(), s); 844 assertEquals(t.getNano(), n); 845 } 846 847 @Test(expectedExceptions=DateTimeParseException.class) 848 public void factory_parse_illegalValue() { 849 LocalDateTime.parse("2008-06-32T11:15"); 850 } 851 852 @Test(expectedExceptions=DateTimeParseException.class) 853 public void factory_parse_invalidValue() { 854 LocalDateTime.parse("2008-06-31T11:15"); 855 } 856 857 @Test(expectedExceptions=NullPointerException.class) 858 public void factory_parse_nullText() { 859 LocalDateTime.parse((String) null); 860 } 861 862 //----------------------------------------------------------------------- 863 // parse(DateTimeFormatter) 864 //----------------------------------------------------------------------- 865 @Test 866 public void factory_parse_formatter() { 867 DateTimeFormatter f = DateTimeFormatter.ofPattern("u M d H m s"); 868 LocalDateTime test = LocalDateTime.parse("2010 12 3 11 30 45", f); 869 assertEquals(test, LocalDateTime.of(2010, 12, 3, 11, 30, 45)); 870 } 871 872 @Test(expectedExceptions=NullPointerException.class) 873 public void factory_parse_formatter_nullText() { 874 DateTimeFormatter f = DateTimeFormatter.ofPattern("u M d H m s"); 875 LocalDateTime.parse((String) null, f); 876 } 877 878 @Test(expectedExceptions=NullPointerException.class) 879 public void factory_parse_formatter_nullFormatter() { 880 LocalDateTime.parse("ANY", null); 881 } 882 883 //----------------------------------------------------------------------- 884 // get(DateTimeField) 885 //----------------------------------------------------------------------- 886 @Test 887 public void test_get_DateTimeField() { 888 LocalDateTime test = LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321); 889 assertEquals(test.getLong(ChronoField.YEAR), 2008); 890 assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6); 891 assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30); 892 assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); 893 assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); 894 895 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); 896 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); 897 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); 898 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); 899 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); 900 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); 901 } 902 903 @Test(expectedExceptions=NullPointerException.class) 904 public void test_get_DateTimeField_null() { 905 LocalDateTime test = LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321); 906 test.getLong((TemporalField) null); 907 } 908 909 @Test(expectedExceptions=DateTimeException.class) 910 public void test_get_DateTimeField_invalidField() { 911 TEST_2007_07_15_12_30_40_987654321.getLong(MockFieldNoValue.INSTANCE); 912 } 913 914 //----------------------------------------------------------------------- 915 // query(TemporalQuery) 916 //----------------------------------------------------------------------- 917 @Test 918 public void test_query() { 919 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(TemporalQueries.chronology()), IsoChronology.INSTANCE); 920 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(TemporalQueries.localDate()), TEST_2007_07_15_12_30_40_987654321.toLocalDate()); 921 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(TemporalQueries.localTime()), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 922 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(TemporalQueries.offset()), null); 923 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(TemporalQueries.precision()), ChronoUnit.NANOS); 924 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(TemporalQueries.zone()), null); 925 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(TemporalQueries.zoneId()), null); 926 } 927 928 @Test(expectedExceptions=NullPointerException.class) 929 public void test_query_null() { 930 TEST_2007_07_15_12_30_40_987654321.query(null); 931 } 932 933 //----------------------------------------------------------------------- 934 @DataProvider(name="sampleDates") 935 Object[][] provider_sampleDates() { 936 return new Object[][] { 937 {2008, 7, 5}, 938 {2007, 7, 5}, 939 {2006, 7, 5}, 940 {2005, 7, 5}, 941 {2004, 1, 1}, 942 {-1, 1, 2}, 943 }; 944 } 945 946 @DataProvider(name="sampleTimes") 947 Object[][] provider_sampleTimes() { 948 return new Object[][] { 949 {0, 0, 0, 0}, 950 {0, 0, 0, 1}, 951 {0, 0, 1, 0}, 952 {0, 0, 1, 1}, 953 {0, 1, 0, 0}, 954 {0, 1, 0, 1}, 955 {0, 1, 1, 0}, 956 {0, 1, 1, 1}, 957 {1, 0, 0, 0}, 958 {1, 0, 0, 1}, 959 {1, 0, 1, 0}, 960 {1, 0, 1, 1}, 961 {1, 1, 0, 0}, 962 {1, 1, 0, 1}, 963 {1, 1, 1, 0}, 964 {1, 1, 1, 1}, 965 }; 966 } 967 968 //----------------------------------------------------------------------- 969 // get*() 970 //----------------------------------------------------------------------- 971 @Test(dataProvider="sampleDates") 972 public void test_get_dates(int y, int m, int d) { 973 LocalDateTime a = LocalDateTime.of(y, m, d, 12, 30); 974 assertEquals(a.getYear(), y); 975 assertEquals(a.getMonth(), Month.of(m)); 976 assertEquals(a.getDayOfMonth(), d); 977 } 978 979 @Test(dataProvider="sampleDates") 980 public void test_getDOY(int y, int m, int d) { 981 LocalDateTime a = LocalDateTime.of(y, m, d, 12 ,30); 982 int total = 0; 983 for (int i = 1; i < m; i++) { 984 total += Month.of(i).length(isIsoLeap(y)); 985 } 986 int doy = total + d; 987 assertEquals(a.getDayOfYear(), doy); 988 } 989 990 @Test(dataProvider="sampleTimes") 991 public void test_get_times(int h, int m, int s, int ns) { 992 LocalDateTime a = LocalDateTime.of(TEST_2007_07_15_12_30_40_987654321.toLocalDate(), LocalTime.of(h, m, s, ns)); 993 assertEquals(a.getHour(), h); 994 assertEquals(a.getMinute(), m); 995 assertEquals(a.getSecond(), s); 996 assertEquals(a.getNano(), ns); 997 } 998 999 //----------------------------------------------------------------------- 1000 // getDayOfWeek() 1001 //----------------------------------------------------------------------- 1002 @Test 1003 public void test_getDayOfWeek() { 1004 DayOfWeek dow = DayOfWeek.MONDAY; 1005 for (Month month : Month.values()) { 1006 int length = month.length(false); 1007 for (int i = 1; i <= length; i++) { 1008 LocalDateTime d = LocalDateTime.of(LocalDate.of(2007, month, i), 1009 TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 1010 assertSame(d.getDayOfWeek(), dow); 1011 dow = dow.plus(1); 1012 } 1013 } 1014 } 1015 1016 //----------------------------------------------------------------------- 1017 // with() 1018 //----------------------------------------------------------------------- 1019 @Test 1020 public void test_with_adjustment() { 1021 final LocalDateTime sample = LocalDateTime.of(2012, 3, 4, 23, 5); 1022 TemporalAdjuster adjuster = new TemporalAdjuster() { 1023 @Override 1024 public Temporal adjustInto(Temporal dateTime) { 1025 return sample; 1026 } 1027 }; 1028 assertEquals(TEST_2007_07_15_12_30_40_987654321.with(adjuster), sample); 1029 } 1030 1031 @Test(expectedExceptions=NullPointerException.class) 1032 public void test_with_adjustment_null() { 1033 TEST_2007_07_15_12_30_40_987654321.with((TemporalAdjuster) null); 1034 } 1035 1036 //----------------------------------------------------------------------- 1037 // withYear() 1038 //----------------------------------------------------------------------- 1039 @Test 1040 public void test_withYear_int_normal() { 1041 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withYear(2008); 1042 check(t, 2008, 7, 15, 12, 30, 40, 987654321); 1043 } 1044 1045 @Test(expectedExceptions=DateTimeException.class) 1046 public void test_withYear_int_invalid() { 1047 TEST_2007_07_15_12_30_40_987654321.withYear(Year.MIN_VALUE - 1); 1048 } 1049 1050 @Test 1051 public void test_withYear_int_adjustDay() { 1052 LocalDateTime t = LocalDateTime.of(2008, 2, 29, 12, 30).withYear(2007); 1053 LocalDateTime expected = LocalDateTime.of(2007, 2, 28, 12, 30); 1054 assertEquals(t, expected); 1055 } 1056 1057 //----------------------------------------------------------------------- 1058 // withMonth() 1059 //----------------------------------------------------------------------- 1060 @Test 1061 public void test_withMonth_int_normal() { 1062 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withMonth(1); 1063 check(t, 2007, 1, 15, 12, 30, 40, 987654321); 1064 } 1065 1066 @Test(expectedExceptions=DateTimeException.class) 1067 public void test_withMonth_int_invalid() { 1068 TEST_2007_07_15_12_30_40_987654321.withMonth(13); 1069 } 1070 1071 @Test 1072 public void test_withMonth_int_adjustDay() { 1073 LocalDateTime t = LocalDateTime.of(2007, 12, 31, 12, 30).withMonth(11); 1074 LocalDateTime expected = LocalDateTime.of(2007, 11, 30, 12, 30); 1075 assertEquals(t, expected); 1076 } 1077 1078 //----------------------------------------------------------------------- 1079 // withDayOfMonth() 1080 //----------------------------------------------------------------------- 1081 @Test 1082 public void test_withDayOfMonth_normal() { 1083 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withDayOfMonth(1); 1084 check(t, 2007, 7, 1, 12, 30, 40, 987654321); 1085 } 1086 1087 @Test(expectedExceptions=DateTimeException.class) 1088 public void test_withDayOfMonth_invalid() { 1089 LocalDateTime.of(2007, 11, 30, 12, 30).withDayOfMonth(32); 1090 } 1091 1092 @Test(expectedExceptions=DateTimeException.class) 1093 public void test_withDayOfMonth_invalidCombination() { 1094 LocalDateTime.of(2007, 11, 30, 12, 30).withDayOfMonth(31); 1095 } 1096 1097 //----------------------------------------------------------------------- 1098 // withDayOfYear(int) 1099 //----------------------------------------------------------------------- 1100 @Test 1101 public void test_withDayOfYear_normal() { 1102 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withDayOfYear(33); 1103 assertEquals(t, LocalDateTime.of(2007, 2, 2, 12, 30, 40, 987654321)); 1104 } 1105 1106 @Test(expectedExceptions=DateTimeException.class) 1107 public void test_withDayOfYear_illegal() { 1108 TEST_2007_07_15_12_30_40_987654321.withDayOfYear(367); 1109 } 1110 1111 @Test(expectedExceptions=DateTimeException.class) 1112 public void test_withDayOfYear_invalid() { 1113 TEST_2007_07_15_12_30_40_987654321.withDayOfYear(366); 1114 } 1115 1116 //----------------------------------------------------------------------- 1117 // withHour() 1118 //----------------------------------------------------------------------- 1119 @Test 1120 public void test_withHour_normal() { 1121 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321; 1122 for (int i = 0; i < 24; i++) { 1123 t = t.withHour(i); 1124 assertEquals(t.getHour(), i); 1125 } 1126 } 1127 1128 @Test(expectedExceptions=DateTimeException.class) 1129 public void test_withHour_hourTooLow() { 1130 TEST_2007_07_15_12_30_40_987654321.withHour(-1); 1131 } 1132 1133 @Test(expectedExceptions=DateTimeException.class) 1134 public void test_withHour_hourTooHigh() { 1135 TEST_2007_07_15_12_30_40_987654321.withHour(24); 1136 } 1137 1138 //----------------------------------------------------------------------- 1139 // withMinute() 1140 //----------------------------------------------------------------------- 1141 @Test 1142 public void test_withMinute_normal() { 1143 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321; 1144 for (int i = 0; i < 60; i++) { 1145 t = t.withMinute(i); 1146 assertEquals(t.getMinute(), i); 1147 } 1148 } 1149 1150 @Test(expectedExceptions=DateTimeException.class) 1151 public void test_withMinute_minuteTooLow() { 1152 TEST_2007_07_15_12_30_40_987654321.withMinute(-1); 1153 } 1154 1155 @Test(expectedExceptions=DateTimeException.class) 1156 public void test_withMinute_minuteTooHigh() { 1157 TEST_2007_07_15_12_30_40_987654321.withMinute(60); 1158 } 1159 1160 //----------------------------------------------------------------------- 1161 // withSecond() 1162 //----------------------------------------------------------------------- 1163 @Test 1164 public void test_withSecond_normal() { 1165 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321; 1166 for (int i = 0; i < 60; i++) { 1167 t = t.withSecond(i); 1168 assertEquals(t.getSecond(), i); 1169 } 1170 } 1171 1172 @Test(expectedExceptions=DateTimeException.class) 1173 public void test_withSecond_secondTooLow() { 1174 TEST_2007_07_15_12_30_40_987654321.withSecond(-1); 1175 } 1176 1177 @Test(expectedExceptions=DateTimeException.class) 1178 public void test_withSecond_secondTooHigh() { 1179 TEST_2007_07_15_12_30_40_987654321.withSecond(60); 1180 } 1181 1182 //----------------------------------------------------------------------- 1183 // withNano() 1184 //----------------------------------------------------------------------- 1185 @Test 1186 public void test_withNanoOfSecond_normal() { 1187 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321; 1188 t = t.withNano(1); 1189 assertEquals(t.getNano(), 1); 1190 t = t.withNano(10); 1191 assertEquals(t.getNano(), 10); 1192 t = t.withNano(100); 1193 assertEquals(t.getNano(), 100); 1194 t = t.withNano(999999999); 1195 assertEquals(t.getNano(), 999999999); 1196 } 1197 1198 @Test(expectedExceptions=DateTimeException.class) 1199 public void test_withNanoOfSecond_nanoTooLow() { 1200 TEST_2007_07_15_12_30_40_987654321.withNano(-1); 1201 } 1202 1203 @Test(expectedExceptions=DateTimeException.class) 1204 public void test_withNanoOfSecond_nanoTooHigh() { 1205 TEST_2007_07_15_12_30_40_987654321.withNano(1000000000); 1206 } 1207 1208 //----------------------------------------------------------------------- 1209 // plus(adjuster) 1210 //----------------------------------------------------------------------- 1211 @Test 1212 public void test_plus_adjuster() { 1213 Duration p = Duration.ofSeconds(62, 3); 1214 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(p); 1215 assertEquals(t, LocalDateTime.of(2007, 7, 15, 12, 31, 42, 987654324)); 1216 } 1217 1218 @Test(expectedExceptions=NullPointerException.class) 1219 public void test_plus_adjuster_null() { 1220 TEST_2007_07_15_12_30_40_987654321.plus(null); 1221 } 1222 1223 //----------------------------------------------------------------------- 1224 // plus(Period) 1225 //----------------------------------------------------------------------- 1226 @Test 1227 public void test_plus_Period_positiveMonths() { 1228 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1229 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(period); 1230 assertEquals(t, LocalDateTime.of(2008, 2, 15, 12, 30, 40, 987654321)); 1231 } 1232 1233 @Test 1234 public void test_plus_Period_negativeDays() { 1235 MockSimplePeriod period = MockSimplePeriod.of(-25, ChronoUnit.DAYS); 1236 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(period); 1237 assertEquals(t, LocalDateTime.of(2007, 6, 20, 12, 30, 40, 987654321)); 1238 } 1239 1240 @Test(expectedExceptions=NullPointerException.class) 1241 public void test_plus_Period_null() { 1242 TEST_2007_07_15_12_30_40_987654321.plus((MockSimplePeriod) null); 1243 } 1244 1245 @Test(expectedExceptions=DateTimeException.class) 1246 public void test_plus_Period_invalidTooLarge() { 1247 MockSimplePeriod period = MockSimplePeriod.of(1, ChronoUnit.YEARS); 1248 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).plus(period); 1249 } 1250 1251 @Test(expectedExceptions=DateTimeException.class) 1252 public void test_plus_Period_invalidTooSmall() { 1253 MockSimplePeriod period = MockSimplePeriod.of(-1, ChronoUnit.YEARS); 1254 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).plus(period); 1255 } 1256 1257 //----------------------------------------------------------------------- 1258 // plus(long,PeriodUnit) 1259 //----------------------------------------------------------------------- 1260 @Test 1261 public void test_plus_longPeriodUnit_positiveMonths() { 1262 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(7, ChronoUnit.MONTHS); 1263 assertEquals(t, LocalDateTime.of(2008, 2, 15, 12, 30, 40, 987654321)); 1264 } 1265 1266 @Test 1267 public void test_plus_longPeriodUnit_negativeDays() { 1268 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(-25, ChronoUnit.DAYS); 1269 assertEquals(t, LocalDateTime.of(2007, 6, 20, 12, 30, 40, 987654321)); 1270 } 1271 1272 @Test(expectedExceptions=NullPointerException.class) 1273 public void test_plus_longPeriodUnit_null() { 1274 TEST_2007_07_15_12_30_40_987654321.plus(1, (TemporalUnit) null); 1275 } 1276 1277 @Test(expectedExceptions=DateTimeException.class) 1278 public void test_plus_longPeriodUnit_invalidTooLarge() { 1279 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).plus(1, ChronoUnit.YEARS); 1280 } 1281 1282 @Test(expectedExceptions=DateTimeException.class) 1283 public void test_plus_longPeriodUnit_invalidTooSmall() { 1284 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).plus(-1, ChronoUnit.YEARS); 1285 } 1286 1287 //----------------------------------------------------------------------- 1288 // plusYears() 1289 //----------------------------------------------------------------------- 1290 @Test 1291 public void test_plusYears_int_normal() { 1292 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusYears(1); 1293 check(t, 2008, 7, 15, 12, 30, 40, 987654321); 1294 } 1295 1296 @Test 1297 public void test_plusYears_int_negative() { 1298 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusYears(-1); 1299 check(t, 2006, 7, 15, 12, 30, 40, 987654321); 1300 } 1301 1302 @Test 1303 public void test_plusYears_int_adjustDay() { 1304 LocalDateTime t = createDateMidnight(2008, 2, 29).plusYears(1); 1305 check(t, 2009, 2, 28, 0, 0, 0, 0); 1306 } 1307 1308 @Test(expectedExceptions=DateTimeException.class) 1309 public void test_plusYears_int_invalidTooLarge() { 1310 createDateMidnight(Year.MAX_VALUE, 1, 1).plusYears(1); 1311 } 1312 1313 @Test(expectedExceptions=DateTimeException.class) 1314 public void test_plusYears_int_invalidTooSmall() { 1315 LocalDate.of(Year.MIN_VALUE, 1, 1).plusYears(-1); 1316 } 1317 1318 //----------------------------------------------------------------------- 1319 // plusMonths() 1320 //----------------------------------------------------------------------- 1321 @Test 1322 public void test_plusMonths_int_normal() { 1323 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMonths(1); 1324 check(t, 2007, 8, 15, 12, 30, 40, 987654321); 1325 } 1326 1327 @Test 1328 public void test_plusMonths_int_overYears() { 1329 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMonths(25); 1330 check(t, 2009, 8, 15, 12, 30, 40, 987654321); 1331 } 1332 1333 @Test 1334 public void test_plusMonths_int_negative() { 1335 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMonths(-1); 1336 check(t, 2007, 6, 15, 12, 30, 40, 987654321); 1337 } 1338 1339 @Test 1340 public void test_plusMonths_int_negativeAcrossYear() { 1341 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMonths(-7); 1342 check(t, 2006, 12, 15, 12, 30, 40, 987654321); 1343 } 1344 1345 @Test 1346 public void test_plusMonths_int_negativeOverYears() { 1347 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMonths(-31); 1348 check(t, 2004, 12, 15, 12, 30, 40, 987654321); 1349 } 1350 1351 @Test 1352 public void test_plusMonths_int_adjustDayFromLeapYear() { 1353 LocalDateTime t = createDateMidnight(2008, 2, 29).plusMonths(12); 1354 check(t, 2009, 2, 28, 0, 0, 0, 0); 1355 } 1356 1357 @Test 1358 public void test_plusMonths_int_adjustDayFromMonthLength() { 1359 LocalDateTime t = createDateMidnight(2007, 3, 31).plusMonths(1); 1360 check(t, 2007, 4, 30, 0, 0, 0, 0); 1361 } 1362 1363 @Test(expectedExceptions=DateTimeException.class) 1364 public void test_plusMonths_int_invalidTooLarge() { 1365 createDateMidnight(Year.MAX_VALUE, 12, 1).plusMonths(1); 1366 } 1367 1368 @Test(expectedExceptions=DateTimeException.class) 1369 public void test_plusMonths_int_invalidTooSmall() { 1370 createDateMidnight(Year.MIN_VALUE, 1, 1).plusMonths(-1); 1371 } 1372 1373 //----------------------------------------------------------------------- 1374 // plusWeeks() 1375 //----------------------------------------------------------------------- 1376 @DataProvider(name="samplePlusWeeksSymmetry") 1377 Object[][] provider_samplePlusWeeksSymmetry() { 1378 return new Object[][] { 1379 {createDateMidnight(-1, 1, 1)}, 1380 {createDateMidnight(-1, 2, 28)}, 1381 {createDateMidnight(-1, 3, 1)}, 1382 {createDateMidnight(-1, 12, 31)}, 1383 {createDateMidnight(0, 1, 1)}, 1384 {createDateMidnight(0, 2, 28)}, 1385 {createDateMidnight(0, 2, 29)}, 1386 {createDateMidnight(0, 3, 1)}, 1387 {createDateMidnight(0, 12, 31)}, 1388 {createDateMidnight(2007, 1, 1)}, 1389 {createDateMidnight(2007, 2, 28)}, 1390 {createDateMidnight(2007, 3, 1)}, 1391 {createDateMidnight(2007, 12, 31)}, 1392 {createDateMidnight(2008, 1, 1)}, 1393 {createDateMidnight(2008, 2, 28)}, 1394 {createDateMidnight(2008, 2, 29)}, 1395 {createDateMidnight(2008, 3, 1)}, 1396 {createDateMidnight(2008, 12, 31)}, 1397 {createDateMidnight(2099, 1, 1)}, 1398 {createDateMidnight(2099, 2, 28)}, 1399 {createDateMidnight(2099, 3, 1)}, 1400 {createDateMidnight(2099, 12, 31)}, 1401 {createDateMidnight(2100, 1, 1)}, 1402 {createDateMidnight(2100, 2, 28)}, 1403 {createDateMidnight(2100, 3, 1)}, 1404 {createDateMidnight(2100, 12, 31)}, 1405 }; 1406 } 1407 1408 @Test(dataProvider="samplePlusWeeksSymmetry") 1409 public void test_plusWeeks_symmetry(LocalDateTime reference) { 1410 for (int weeks = 0; weeks < 365 * 8; weeks++) { 1411 LocalDateTime t = reference.plusWeeks(weeks).plusWeeks(-weeks); 1412 assertEquals(t, reference); 1413 1414 t = reference.plusWeeks(-weeks).plusWeeks(weeks); 1415 assertEquals(t, reference); 1416 } 1417 } 1418 1419 @Test 1420 public void test_plusWeeks_normal() { 1421 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusWeeks(1); 1422 check(t, 2007, 7, 22, 12, 30, 40, 987654321); 1423 } 1424 1425 @Test 1426 public void test_plusWeeks_overMonths() { 1427 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusWeeks(9); 1428 check(t, 2007, 9, 16, 12, 30, 40, 987654321); 1429 } 1430 1431 @Test 1432 public void test_plusWeeks_overYears() { 1433 LocalDateTime t = LocalDateTime.of(2006, 7, 16, 12, 30, 40, 987654321).plusWeeks(52); 1434 assertEquals(t, TEST_2007_07_15_12_30_40_987654321); 1435 } 1436 1437 @Test 1438 public void test_plusWeeks_overLeapYears() { 1439 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusYears(-1).plusWeeks(104); 1440 check(t, 2008, 7, 12, 12, 30, 40, 987654321); 1441 } 1442 1443 @Test 1444 public void test_plusWeeks_negative() { 1445 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusWeeks(-1); 1446 check(t, 2007, 7, 8, 12, 30, 40, 987654321); 1447 } 1448 1449 @Test 1450 public void test_plusWeeks_negativeAcrossYear() { 1451 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusWeeks(-28); 1452 check(t, 2006, 12, 31, 12, 30, 40, 987654321); 1453 } 1454 1455 @Test 1456 public void test_plusWeeks_negativeOverYears() { 1457 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusWeeks(-104); 1458 check(t, 2005, 7, 17, 12, 30, 40, 987654321); 1459 } 1460 1461 @Test 1462 public void test_plusWeeks_maximum() { 1463 LocalDateTime t = createDateMidnight(Year.MAX_VALUE, 12, 24).plusWeeks(1); 1464 check(t, Year.MAX_VALUE, 12, 31, 0, 0, 0, 0); 1465 } 1466 1467 @Test 1468 public void test_plusWeeks_minimum() { 1469 LocalDateTime t = createDateMidnight(Year.MIN_VALUE, 1, 8).plusWeeks(-1); 1470 check(t, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0); 1471 } 1472 1473 @Test(expectedExceptions=DateTimeException.class) 1474 public void test_plusWeeks_invalidTooLarge() { 1475 createDateMidnight(Year.MAX_VALUE, 12, 25).plusWeeks(1); 1476 } 1477 1478 @Test(expectedExceptions=DateTimeException.class) 1479 public void test_plusWeeks_invalidTooSmall() { 1480 createDateMidnight(Year.MIN_VALUE, 1, 7).plusWeeks(-1); 1481 } 1482 1483 //----------------------------------------------------------------------- 1484 // plusDays() 1485 //----------------------------------------------------------------------- 1486 @DataProvider(name="samplePlusDaysSymmetry") 1487 Object[][] provider_samplePlusDaysSymmetry() { 1488 return new Object[][] { 1489 {createDateMidnight(-1, 1, 1)}, 1490 {createDateMidnight(-1, 2, 28)}, 1491 {createDateMidnight(-1, 3, 1)}, 1492 {createDateMidnight(-1, 12, 31)}, 1493 {createDateMidnight(0, 1, 1)}, 1494 {createDateMidnight(0, 2, 28)}, 1495 {createDateMidnight(0, 2, 29)}, 1496 {createDateMidnight(0, 3, 1)}, 1497 {createDateMidnight(0, 12, 31)}, 1498 {createDateMidnight(2007, 1, 1)}, 1499 {createDateMidnight(2007, 2, 28)}, 1500 {createDateMidnight(2007, 3, 1)}, 1501 {createDateMidnight(2007, 12, 31)}, 1502 {createDateMidnight(2008, 1, 1)}, 1503 {createDateMidnight(2008, 2, 28)}, 1504 {createDateMidnight(2008, 2, 29)}, 1505 {createDateMidnight(2008, 3, 1)}, 1506 {createDateMidnight(2008, 12, 31)}, 1507 {createDateMidnight(2099, 1, 1)}, 1508 {createDateMidnight(2099, 2, 28)}, 1509 {createDateMidnight(2099, 3, 1)}, 1510 {createDateMidnight(2099, 12, 31)}, 1511 {createDateMidnight(2100, 1, 1)}, 1512 {createDateMidnight(2100, 2, 28)}, 1513 {createDateMidnight(2100, 3, 1)}, 1514 {createDateMidnight(2100, 12, 31)}, 1515 }; 1516 } 1517 1518 @Test(dataProvider="samplePlusDaysSymmetry") 1519 public void test_plusDays_symmetry(LocalDateTime reference) { 1520 for (int days = 0; days < 365 * 8; days++) { 1521 LocalDateTime t = reference.plusDays(days).plusDays(-days); 1522 assertEquals(t, reference); 1523 1524 t = reference.plusDays(-days).plusDays(days); 1525 assertEquals(t, reference); 1526 } 1527 } 1528 1529 @Test 1530 public void test_plusDays_normal() { 1531 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusDays(1); 1532 check(t, 2007, 7, 16, 12, 30, 40, 987654321); 1533 } 1534 1535 @Test 1536 public void test_plusDays_overMonths() { 1537 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusDays(62); 1538 check(t, 2007, 9, 15, 12, 30, 40, 987654321); 1539 } 1540 1541 @Test 1542 public void test_plusDays_overYears() { 1543 LocalDateTime t = LocalDateTime.of(2006, 7, 14, 12, 30, 40, 987654321).plusDays(366); 1544 assertEquals(t, TEST_2007_07_15_12_30_40_987654321); 1545 } 1546 1547 @Test 1548 public void test_plusDays_overLeapYears() { 1549 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusYears(-1).plusDays(365 + 366); 1550 check(t, 2008, 7, 15, 12, 30, 40, 987654321); 1551 } 1552 1553 @Test 1554 public void test_plusDays_negative() { 1555 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusDays(-1); 1556 check(t, 2007, 7, 14, 12, 30, 40, 987654321); 1557 } 1558 1559 @Test 1560 public void test_plusDays_negativeAcrossYear() { 1561 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusDays(-196); 1562 check(t, 2006, 12, 31, 12, 30, 40, 987654321); 1563 } 1564 1565 @Test 1566 public void test_plusDays_negativeOverYears() { 1567 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusDays(-730); 1568 check(t, 2005, 7, 15, 12, 30, 40, 987654321); 1569 } 1570 1571 @Test 1572 public void test_plusDays_maximum() { 1573 LocalDateTime t = createDateMidnight(Year.MAX_VALUE, 12, 30).plusDays(1); 1574 check(t, Year.MAX_VALUE, 12, 31, 0, 0, 0, 0); 1575 } 1576 1577 @Test 1578 public void test_plusDays_minimum() { 1579 LocalDateTime t = createDateMidnight(Year.MIN_VALUE, 1, 2).plusDays(-1); 1580 check(t, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0); 1581 } 1582 1583 @Test(expectedExceptions=DateTimeException.class) 1584 public void test_plusDays_invalidTooLarge() { 1585 createDateMidnight(Year.MAX_VALUE, 12, 31).plusDays(1); 1586 } 1587 1588 @Test(expectedExceptions=DateTimeException.class) 1589 public void test_plusDays_invalidTooSmall() { 1590 createDateMidnight(Year.MIN_VALUE, 1, 1).plusDays(-1); 1591 } 1592 1593 @Test(expectedExceptions=ArithmeticException.class) 1594 public void test_plusDays_overflowTooLarge() { 1595 createDateMidnight(Year.MAX_VALUE, 12, 31).plusDays(Long.MAX_VALUE); 1596 } 1597 1598 @Test(expectedExceptions=ArithmeticException.class) 1599 public void test_plusDays_overflowTooSmall() { 1600 createDateMidnight(Year.MIN_VALUE, 1, 1).plusDays(Long.MIN_VALUE); 1601 } 1602 1603 //----------------------------------------------------------------------- 1604 // plusHours() 1605 //----------------------------------------------------------------------- 1606 @Test 1607 public void test_plusHours_one() { 1608 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1609 LocalDate d = t.toLocalDate(); 1610 1611 for (int i = 0; i < 50; i++) { 1612 t = t.plusHours(1); 1613 1614 if ((i + 1) % 24 == 0) { 1615 d = d.plusDays(1); 1616 } 1617 1618 assertEquals(t.toLocalDate(), d); 1619 assertEquals(t.getHour(), (i + 1) % 24); 1620 } 1621 } 1622 1623 @Test 1624 public void test_plusHours_fromZero() { 1625 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1626 LocalDate d = base.toLocalDate().minusDays(3); 1627 LocalTime t = LocalTime.of(21, 0); 1628 1629 for (int i = -50; i < 50; i++) { 1630 LocalDateTime dt = base.plusHours(i); 1631 t = t.plusHours(1); 1632 1633 if (t.getHour() == 0) { 1634 d = d.plusDays(1); 1635 } 1636 1637 assertEquals(dt.toLocalDate(), d); 1638 assertEquals(dt.toLocalTime(), t); 1639 } 1640 } 1641 1642 @Test 1643 public void test_plusHours_fromOne() { 1644 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0)); 1645 LocalDate d = base.toLocalDate().minusDays(3); 1646 LocalTime t = LocalTime.of(22, 0); 1647 1648 for (int i = -50; i < 50; i++) { 1649 LocalDateTime dt = base.plusHours(i); 1650 1651 t = t.plusHours(1); 1652 1653 if (t.getHour() == 0) { 1654 d = d.plusDays(1); 1655 } 1656 1657 assertEquals(dt.toLocalDate(), d); 1658 assertEquals(dt.toLocalTime(), t); 1659 } 1660 } 1661 1662 //----------------------------------------------------------------------- 1663 // plusMinutes() 1664 //----------------------------------------------------------------------- 1665 @Test 1666 public void test_plusMinutes_one() { 1667 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1668 LocalDate d = t.toLocalDate(); 1669 1670 int hour = 0; 1671 int min = 0; 1672 1673 for (int i = 0; i < 70; i++) { 1674 t = t.plusMinutes(1); 1675 min++; 1676 if (min == 60) { 1677 hour++; 1678 min = 0; 1679 } 1680 1681 assertEquals(t.toLocalDate(), d); 1682 assertEquals(t.getHour(), hour); 1683 assertEquals(t.getMinute(), min); 1684 } 1685 } 1686 1687 @Test 1688 public void test_plusMinutes_fromZero() { 1689 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1690 LocalDate d = base.toLocalDate().minusDays(1); 1691 LocalTime t = LocalTime.of(22, 49); 1692 1693 for (int i = -70; i < 70; i++) { 1694 LocalDateTime dt = base.plusMinutes(i); 1695 t = t.plusMinutes(1); 1696 1697 if (t == LocalTime.MIDNIGHT) { 1698 d = d.plusDays(1); 1699 } 1700 1701 assertEquals(dt.toLocalDate(), d, String.valueOf(i)); 1702 assertEquals(dt.toLocalTime(), t, String.valueOf(i)); 1703 } 1704 } 1705 1706 @Test 1707 public void test_plusMinutes_noChange_oneDay() { 1708 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMinutes(24 * 60); 1709 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().plusDays(1)); 1710 } 1711 1712 //----------------------------------------------------------------------- 1713 // plusSeconds() 1714 //----------------------------------------------------------------------- 1715 @Test 1716 public void test_plusSeconds_one() { 1717 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1718 LocalDate d = t.toLocalDate(); 1719 1720 int hour = 0; 1721 int min = 0; 1722 int sec = 0; 1723 1724 for (int i = 0; i < 3700; i++) { 1725 t = t.plusSeconds(1); 1726 sec++; 1727 if (sec == 60) { 1728 min++; 1729 sec = 0; 1730 } 1731 if (min == 60) { 1732 hour++; 1733 min = 0; 1734 } 1735 1736 assertEquals(t.toLocalDate(), d); 1737 assertEquals(t.getHour(), hour); 1738 assertEquals(t.getMinute(), min); 1739 assertEquals(t.getSecond(), sec); 1740 } 1741 } 1742 1743 @DataProvider(name="plusSeconds_fromZero") 1744 Iterator<Object[]> plusSeconds_fromZero() { 1745 return new Iterator<Object[]>() { 1746 int delta = 30; 1747 1748 int i = -3660; 1749 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1); 1750 int hour = 22; 1751 int min = 59; 1752 int sec = 0; 1753 1754 public boolean hasNext() { 1755 return i <= 3660; 1756 } 1757 1758 public Object[] next() { 1759 final Object[] ret = new Object[] {i, date, hour, min, sec}; 1760 i += delta; 1761 sec += delta; 1762 1763 if (sec >= 60) { 1764 min++; 1765 sec -= 60; 1766 1767 if (min == 60) { 1768 hour++; 1769 min = 0; 1770 1771 if (hour == 24) { 1772 hour = 0; 1773 } 1774 } 1775 } 1776 1777 if (i == 0) { 1778 date = date.plusDays(1); 1779 } 1780 1781 return ret; 1782 } 1783 1784 public void remove() { 1785 throw new UnsupportedOperationException(); 1786 } 1787 }; 1788 } 1789 1790 @Test(dataProvider="plusSeconds_fromZero") 1791 public void test_plusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) { 1792 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1793 LocalDateTime t = base.plusSeconds(seconds); 1794 1795 assertEquals(date, t.toLocalDate()); 1796 assertEquals(hour, t.getHour()); 1797 assertEquals(min, t.getMinute()); 1798 assertEquals(sec, t.getSecond()); 1799 } 1800 1801 @Test 1802 public void test_plusSeconds_noChange_oneDay() { 1803 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusSeconds(24 * 60 * 60); 1804 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().plusDays(1)); 1805 } 1806 1807 //----------------------------------------------------------------------- 1808 // plusNanos() 1809 //----------------------------------------------------------------------- 1810 @Test 1811 public void test_plusNanos_halfABillion() { 1812 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1813 LocalDate d = t.toLocalDate(); 1814 1815 int hour = 0; 1816 int min = 0; 1817 int sec = 0; 1818 int nanos = 0; 1819 1820 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) { 1821 t = t.plusNanos(500000000); 1822 nanos += 500000000; 1823 if (nanos == 1000000000) { 1824 sec++; 1825 nanos = 0; 1826 } 1827 if (sec == 60) { 1828 min++; 1829 sec = 0; 1830 } 1831 if (min == 60) { 1832 hour++; 1833 min = 0; 1834 } 1835 1836 assertEquals(t.toLocalDate(), d, String.valueOf(i)); 1837 assertEquals(t.getHour(), hour); 1838 assertEquals(t.getMinute(), min); 1839 assertEquals(t.getSecond(), sec); 1840 assertEquals(t.getNano(), nanos); 1841 } 1842 } 1843 1844 @DataProvider(name="plusNanos_fromZero") 1845 Iterator<Object[]> plusNanos_fromZero() { 1846 return new Iterator<Object[]>() { 1847 long delta = 7500000000L; 1848 1849 long i = -3660 * 1000000000L; 1850 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1); 1851 int hour = 22; 1852 int min = 59; 1853 long sec = 0; 1854 long nanos = 0; 1855 1856 public boolean hasNext() { 1857 return i <= 3660 * 1000000000L; 1858 } 1859 1860 public Object[] next() { 1861 final Object[] ret = new Object[] {i, date, hour, min, sec, nanos}; 1862 i += delta; 1863 nanos += delta; 1864 1865 if (nanos >= 1000000000L) { 1866 sec += nanos / 1000000000L; 1867 nanos %= 1000000000L; 1868 1869 if (sec >= 60) { 1870 min++; 1871 sec %= 60; 1872 1873 if (min == 60) { 1874 hour++; 1875 min = 0; 1876 1877 if (hour == 24) { 1878 hour = 0; 1879 date = date.plusDays(1); 1880 } 1881 } 1882 } 1883 } 1884 1885 return ret; 1886 } 1887 1888 public void remove() { 1889 throw new UnsupportedOperationException(); 1890 } 1891 }; 1892 } 1893 1894 @Test(dataProvider="plusNanos_fromZero") 1895 public void test_plusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, long sec, long nanos) { 1896 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 1897 LocalDateTime t = base.plusNanos(nanoseconds); 1898 1899 assertEquals(date, t.toLocalDate()); 1900 assertEquals(hour, t.getHour()); 1901 assertEquals(min, t.getMinute()); 1902 assertEquals(sec, t.getSecond()); 1903 assertEquals(nanos, t.getNano()); 1904 } 1905 1906 @Test 1907 public void test_plusNanos_noChange_oneDay() { 1908 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusNanos(24 * 60 * 60 * 1000000000L); 1909 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().plusDays(1)); 1910 } 1911 1912 //----------------------------------------------------------------------- 1913 // minus(adjuster) 1914 //----------------------------------------------------------------------- 1915 @Test 1916 public void test_minus_adjuster() { 1917 Duration p = Duration.ofSeconds(62, 3); 1918 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(p); 1919 assertEquals(t, LocalDateTime.of(2007, 7, 15, 12, 29, 38, 987654318)); 1920 } 1921 1922 @Test(expectedExceptions=NullPointerException.class) 1923 public void test_minus_adjuster_null() { 1924 TEST_2007_07_15_12_30_40_987654321.minus(null); 1925 } 1926 1927 //----------------------------------------------------------------------- 1928 // minus(Period) 1929 //----------------------------------------------------------------------- 1930 @Test 1931 public void test_minus_Period_positiveMonths() { 1932 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1933 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(period); 1934 assertEquals(t, LocalDateTime.of(2006, 12, 15, 12, 30, 40, 987654321)); 1935 } 1936 1937 @Test 1938 public void test_minus_Period_negativeDays() { 1939 MockSimplePeriod period = MockSimplePeriod.of(-25, ChronoUnit.DAYS); 1940 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(period); 1941 assertEquals(t, LocalDateTime.of(2007, 8, 9, 12, 30, 40, 987654321)); 1942 } 1943 1944 @Test(expectedExceptions=NullPointerException.class) 1945 public void test_minus_Period_null() { 1946 TEST_2007_07_15_12_30_40_987654321.minus((MockSimplePeriod) null); 1947 } 1948 1949 @Test(expectedExceptions=DateTimeException.class) 1950 public void test_minus_Period_invalidTooLarge() { 1951 MockSimplePeriod period = MockSimplePeriod.of(-1, ChronoUnit.YEARS); 1952 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).minus(period); 1953 } 1954 1955 @Test(expectedExceptions=DateTimeException.class) 1956 public void test_minus_Period_invalidTooSmall() { 1957 MockSimplePeriod period = MockSimplePeriod.of(1, ChronoUnit.YEARS); 1958 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).minus(period); 1959 } 1960 1961 //----------------------------------------------------------------------- 1962 // minus(long,PeriodUnit) 1963 //----------------------------------------------------------------------- 1964 @Test 1965 public void test_minus_longPeriodUnit_positiveMonths() { 1966 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(7, ChronoUnit.MONTHS); 1967 assertEquals(t, LocalDateTime.of(2006, 12, 15, 12, 30, 40, 987654321)); 1968 } 1969 1970 @Test 1971 public void test_minus_longPeriodUnit_negativeDays() { 1972 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(-25, ChronoUnit.DAYS); 1973 assertEquals(t, LocalDateTime.of(2007, 8, 9, 12, 30, 40, 987654321)); 1974 } 1975 1976 @Test(expectedExceptions=NullPointerException.class) 1977 public void test_minus_longPeriodUnit_null() { 1978 TEST_2007_07_15_12_30_40_987654321.minus(1, (TemporalUnit) null); 1979 } 1980 1981 @Test(expectedExceptions=DateTimeException.class) 1982 public void test_minus_longPeriodUnit_invalidTooLarge() { 1983 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).minus(-1, ChronoUnit.YEARS); 1984 } 1985 1986 @Test(expectedExceptions=DateTimeException.class) 1987 public void test_minus_longPeriodUnit_invalidTooSmall() { 1988 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).minus(1, ChronoUnit.YEARS); 1989 } 1990 1991 //----------------------------------------------------------------------- 1992 // minusYears() 1993 //----------------------------------------------------------------------- 1994 @Test 1995 public void test_minusYears_int_normal() { 1996 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusYears(1); 1997 check(t, 2006, 7, 15, 12, 30, 40, 987654321); 1998 } 1999 2000 @Test 2001 public void test_minusYears_int_negative() { 2002 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusYears(-1); 2003 check(t, 2008, 7, 15, 12, 30, 40, 987654321); 2004 } 2005 2006 @Test 2007 public void test_minusYears_int_adjustDay() { 2008 LocalDateTime t = createDateMidnight(2008, 2, 29).minusYears(1); 2009 check(t, 2007, 2, 28, 0, 0, 0, 0); 2010 } 2011 2012 @Test(expectedExceptions=DateTimeException.class) 2013 public void test_minusYears_int_invalidTooLarge() { 2014 createDateMidnight(Year.MAX_VALUE, 1, 1).minusYears(-1); 2015 } 2016 2017 @Test(expectedExceptions=DateTimeException.class) 2018 public void test_minusYears_int_invalidTooSmall() { 2019 createDateMidnight(Year.MIN_VALUE, 1, 1).minusYears(1); 2020 } 2021 2022 //----------------------------------------------------------------------- 2023 // minusMonths() 2024 //----------------------------------------------------------------------- 2025 @Test 2026 public void test_minusMonths_int_normal() { 2027 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMonths(1); 2028 check(t, 2007, 6, 15, 12, 30, 40, 987654321); 2029 } 2030 2031 @Test 2032 public void test_minusMonths_int_overYears() { 2033 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMonths(25); 2034 check(t, 2005, 6, 15, 12, 30, 40, 987654321); 2035 } 2036 2037 @Test 2038 public void test_minusMonths_int_negative() { 2039 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMonths(-1); 2040 check(t, 2007, 8, 15, 12, 30, 40, 987654321); 2041 } 2042 2043 @Test 2044 public void test_minusMonths_int_negativeAcrossYear() { 2045 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMonths(-7); 2046 check(t, 2008, 2, 15, 12, 30, 40, 987654321); 2047 } 2048 2049 @Test 2050 public void test_minusMonths_int_negativeOverYears() { 2051 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMonths(-31); 2052 check(t, 2010, 2, 15, 12, 30, 40, 987654321); 2053 } 2054 2055 @Test 2056 public void test_minusMonths_int_adjustDayFromLeapYear() { 2057 LocalDateTime t = createDateMidnight(2008, 2, 29).minusMonths(12); 2058 check(t, 2007, 2, 28, 0, 0, 0, 0); 2059 } 2060 2061 @Test 2062 public void test_minusMonths_int_adjustDayFromMonthLength() { 2063 LocalDateTime t = createDateMidnight(2007, 3, 31).minusMonths(1); 2064 check(t, 2007, 2, 28, 0, 0, 0, 0); 2065 } 2066 2067 @Test(expectedExceptions=DateTimeException.class) 2068 public void test_minusMonths_int_invalidTooLarge() { 2069 createDateMidnight(Year.MAX_VALUE, 12, 1).minusMonths(-1); 2070 } 2071 2072 @Test(expectedExceptions=DateTimeException.class) 2073 public void test_minusMonths_int_invalidTooSmall() { 2074 createDateMidnight(Year.MIN_VALUE, 1, 1).minusMonths(1); 2075 } 2076 2077 //----------------------------------------------------------------------- 2078 // minusWeeks() 2079 //----------------------------------------------------------------------- 2080 @DataProvider(name="sampleMinusWeeksSymmetry") 2081 Object[][] provider_sampleMinusWeeksSymmetry() { 2082 return new Object[][] { 2083 {createDateMidnight(-1, 1, 1)}, 2084 {createDateMidnight(-1, 2, 28)}, 2085 {createDateMidnight(-1, 3, 1)}, 2086 {createDateMidnight(-1, 12, 31)}, 2087 {createDateMidnight(0, 1, 1)}, 2088 {createDateMidnight(0, 2, 28)}, 2089 {createDateMidnight(0, 2, 29)}, 2090 {createDateMidnight(0, 3, 1)}, 2091 {createDateMidnight(0, 12, 31)}, 2092 {createDateMidnight(2007, 1, 1)}, 2093 {createDateMidnight(2007, 2, 28)}, 2094 {createDateMidnight(2007, 3, 1)}, 2095 {createDateMidnight(2007, 12, 31)}, 2096 {createDateMidnight(2008, 1, 1)}, 2097 {createDateMidnight(2008, 2, 28)}, 2098 {createDateMidnight(2008, 2, 29)}, 2099 {createDateMidnight(2008, 3, 1)}, 2100 {createDateMidnight(2008, 12, 31)}, 2101 {createDateMidnight(2099, 1, 1)}, 2102 {createDateMidnight(2099, 2, 28)}, 2103 {createDateMidnight(2099, 3, 1)}, 2104 {createDateMidnight(2099, 12, 31)}, 2105 {createDateMidnight(2100, 1, 1)}, 2106 {createDateMidnight(2100, 2, 28)}, 2107 {createDateMidnight(2100, 3, 1)}, 2108 {createDateMidnight(2100, 12, 31)}, 2109 }; 2110 } 2111 2112 @Test(dataProvider="sampleMinusWeeksSymmetry") 2113 public void test_minusWeeks_symmetry(LocalDateTime reference) { 2114 for (int weeks = 0; weeks < 365 * 8; weeks++) { 2115 LocalDateTime t = reference.minusWeeks(weeks).minusWeeks(-weeks); 2116 assertEquals(t, reference); 2117 2118 t = reference.minusWeeks(-weeks).minusWeeks(weeks); 2119 assertEquals(t, reference); 2120 } 2121 } 2122 2123 @Test 2124 public void test_minusWeeks_normal() { 2125 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusWeeks(1); 2126 check(t, 2007, 7, 8, 12, 30, 40, 987654321); 2127 } 2128 2129 @Test 2130 public void test_minusWeeks_overMonths() { 2131 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusWeeks(9); 2132 check(t, 2007, 5, 13, 12, 30, 40, 987654321); 2133 } 2134 2135 @Test 2136 public void test_minusWeeks_overYears() { 2137 LocalDateTime t = LocalDateTime.of(2008, 7, 13, 12, 30, 40, 987654321).minusWeeks(52); 2138 assertEquals(t, TEST_2007_07_15_12_30_40_987654321); 2139 } 2140 2141 @Test 2142 public void test_minusWeeks_overLeapYears() { 2143 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusYears(-1).minusWeeks(104); 2144 check(t, 2006, 7, 18, 12, 30, 40, 987654321); 2145 } 2146 2147 @Test 2148 public void test_minusWeeks_negative() { 2149 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusWeeks(-1); 2150 check(t, 2007, 7, 22, 12, 30, 40, 987654321); 2151 } 2152 2153 @Test 2154 public void test_minusWeeks_negativeAcrossYear() { 2155 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusWeeks(-28); 2156 check(t, 2008, 1, 27, 12, 30, 40, 987654321); 2157 } 2158 2159 @Test 2160 public void test_minusWeeks_negativeOverYears() { 2161 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusWeeks(-104); 2162 check(t, 2009, 7, 12, 12, 30, 40, 987654321); 2163 } 2164 2165 @Test 2166 public void test_minusWeeks_maximum() { 2167 LocalDateTime t = createDateMidnight(Year.MAX_VALUE, 12, 24).minusWeeks(-1); 2168 check(t, Year.MAX_VALUE, 12, 31, 0, 0, 0, 0); 2169 } 2170 2171 @Test 2172 public void test_minusWeeks_minimum() { 2173 LocalDateTime t = createDateMidnight(Year.MIN_VALUE, 1, 8).minusWeeks(1); 2174 check(t, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0); 2175 } 2176 2177 @Test(expectedExceptions=DateTimeException.class) 2178 public void test_minusWeeks_invalidTooLarge() { 2179 createDateMidnight(Year.MAX_VALUE, 12, 25).minusWeeks(-1); 2180 } 2181 2182 @Test(expectedExceptions=DateTimeException.class) 2183 public void test_minusWeeks_invalidTooSmall() { 2184 createDateMidnight(Year.MIN_VALUE, 1, 7).minusWeeks(1); 2185 } 2186 2187 //----------------------------------------------------------------------- 2188 // minusDays() 2189 //----------------------------------------------------------------------- 2190 @DataProvider(name="sampleMinusDaysSymmetry") 2191 Object[][] provider_sampleMinusDaysSymmetry() { 2192 return new Object[][] { 2193 {createDateMidnight(-1, 1, 1)}, 2194 {createDateMidnight(-1, 2, 28)}, 2195 {createDateMidnight(-1, 3, 1)}, 2196 {createDateMidnight(-1, 12, 31)}, 2197 {createDateMidnight(0, 1, 1)}, 2198 {createDateMidnight(0, 2, 28)}, 2199 {createDateMidnight(0, 2, 29)}, 2200 {createDateMidnight(0, 3, 1)}, 2201 {createDateMidnight(0, 12, 31)}, 2202 {createDateMidnight(2007, 1, 1)}, 2203 {createDateMidnight(2007, 2, 28)}, 2204 {createDateMidnight(2007, 3, 1)}, 2205 {createDateMidnight(2007, 12, 31)}, 2206 {createDateMidnight(2008, 1, 1)}, 2207 {createDateMidnight(2008, 2, 28)}, 2208 {createDateMidnight(2008, 2, 29)}, 2209 {createDateMidnight(2008, 3, 1)}, 2210 {createDateMidnight(2008, 12, 31)}, 2211 {createDateMidnight(2099, 1, 1)}, 2212 {createDateMidnight(2099, 2, 28)}, 2213 {createDateMidnight(2099, 3, 1)}, 2214 {createDateMidnight(2099, 12, 31)}, 2215 {createDateMidnight(2100, 1, 1)}, 2216 {createDateMidnight(2100, 2, 28)}, 2217 {createDateMidnight(2100, 3, 1)}, 2218 {createDateMidnight(2100, 12, 31)}, 2219 }; 2220 } 2221 2222 @Test(dataProvider="sampleMinusDaysSymmetry") 2223 public void test_minusDays_symmetry(LocalDateTime reference) { 2224 for (int days = 0; days < 365 * 8; days++) { 2225 LocalDateTime t = reference.minusDays(days).minusDays(-days); 2226 assertEquals(t, reference); 2227 2228 t = reference.minusDays(-days).minusDays(days); 2229 assertEquals(t, reference); 2230 } 2231 } 2232 2233 @Test 2234 public void test_minusDays_normal() { 2235 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusDays(1); 2236 check(t, 2007, 7, 14, 12, 30, 40, 987654321); 2237 } 2238 2239 @Test 2240 public void test_minusDays_overMonths() { 2241 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusDays(62); 2242 check(t, 2007, 5, 14, 12, 30, 40, 987654321); 2243 } 2244 2245 @Test 2246 public void test_minusDays_overYears() { 2247 LocalDateTime t = LocalDateTime.of(2008, 7, 16, 12, 30, 40, 987654321).minusDays(367); 2248 assertEquals(t, TEST_2007_07_15_12_30_40_987654321); 2249 } 2250 2251 @Test 2252 public void test_minusDays_overLeapYears() { 2253 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusYears(2).minusDays(365 + 366); 2254 assertEquals(t, TEST_2007_07_15_12_30_40_987654321); 2255 } 2256 2257 @Test 2258 public void test_minusDays_negative() { 2259 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusDays(-1); 2260 check(t, 2007, 7, 16, 12, 30, 40, 987654321); 2261 } 2262 2263 @Test 2264 public void test_minusDays_negativeAcrossYear() { 2265 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusDays(-169); 2266 check(t, 2007, 12, 31, 12, 30, 40, 987654321); 2267 } 2268 2269 @Test 2270 public void test_minusDays_negativeOverYears() { 2271 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusDays(-731); 2272 check(t, 2009, 7, 15, 12, 30, 40, 987654321); 2273 } 2274 2275 @Test 2276 public void test_minusDays_maximum() { 2277 LocalDateTime t = createDateMidnight(Year.MAX_VALUE, 12, 30).minusDays(-1); 2278 check(t, Year.MAX_VALUE, 12, 31, 0, 0, 0, 0); 2279 } 2280 2281 @Test 2282 public void test_minusDays_minimum() { 2283 LocalDateTime t = createDateMidnight(Year.MIN_VALUE, 1, 2).minusDays(1); 2284 check(t, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0); 2285 } 2286 2287 @Test(expectedExceptions=DateTimeException.class) 2288 public void test_minusDays_invalidTooLarge() { 2289 createDateMidnight(Year.MAX_VALUE, 12, 31).minusDays(-1); 2290 } 2291 2292 @Test(expectedExceptions=DateTimeException.class) 2293 public void test_minusDays_invalidTooSmall() { 2294 createDateMidnight(Year.MIN_VALUE, 1, 1).minusDays(1); 2295 } 2296 2297 @Test(expectedExceptions=ArithmeticException.class) 2298 public void test_minusDays_overflowTooLarge() { 2299 createDateMidnight(Year.MAX_VALUE, 12, 31).minusDays(Long.MIN_VALUE); 2300 } 2301 2302 @Test(expectedExceptions=ArithmeticException.class) 2303 public void test_minusDays_overflowTooSmall() { 2304 createDateMidnight(Year.MIN_VALUE, 1, 1).minusDays(Long.MAX_VALUE); 2305 } 2306 2307 //----------------------------------------------------------------------- 2308 // minusHours() 2309 //----------------------------------------------------------------------- 2310 @Test 2311 public void test_minusHours_one() { 2312 LocalDateTime t =TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2313 LocalDate d = t.toLocalDate(); 2314 2315 for (int i = 0; i < 50; i++) { 2316 t = t.minusHours(1); 2317 2318 if (i % 24 == 0) { 2319 d = d.minusDays(1); 2320 } 2321 2322 assertEquals(t.toLocalDate(), d); 2323 assertEquals(t.getHour(), (((-i + 23) % 24) + 24) % 24); 2324 } 2325 } 2326 2327 @Test 2328 public void test_minusHours_fromZero() { 2329 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2330 LocalDate d = base.toLocalDate().plusDays(2); 2331 LocalTime t = LocalTime.of(3, 0); 2332 2333 for (int i = -50; i < 50; i++) { 2334 LocalDateTime dt = base.minusHours(i); 2335 t = t.minusHours(1); 2336 2337 if (t.getHour() == 23) { 2338 d = d.minusDays(1); 2339 } 2340 2341 assertEquals(dt.toLocalDate(), d, String.valueOf(i)); 2342 assertEquals(dt.toLocalTime(), t); 2343 } 2344 } 2345 2346 @Test 2347 public void test_minusHours_fromOne() { 2348 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0)); 2349 LocalDate d = base.toLocalDate().plusDays(2); 2350 LocalTime t = LocalTime.of(4, 0); 2351 2352 for (int i = -50; i < 50; i++) { 2353 LocalDateTime dt = base.minusHours(i); 2354 2355 t = t.minusHours(1); 2356 2357 if (t.getHour() == 23) { 2358 d = d.minusDays(1); 2359 } 2360 2361 assertEquals(dt.toLocalDate(), d, String.valueOf(i)); 2362 assertEquals(dt.toLocalTime(), t); 2363 } 2364 } 2365 2366 //----------------------------------------------------------------------- 2367 // minusMinutes() 2368 //----------------------------------------------------------------------- 2369 @Test 2370 public void test_minusMinutes_one() { 2371 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2372 LocalDate d = t.toLocalDate().minusDays(1); 2373 2374 int hour = 0; 2375 int min = 0; 2376 2377 for (int i = 0; i < 70; i++) { 2378 t = t.minusMinutes(1); 2379 min--; 2380 if (min == -1) { 2381 hour--; 2382 min = 59; 2383 2384 if (hour == -1) { 2385 hour = 23; 2386 } 2387 } 2388 assertEquals(t.toLocalDate(), d); 2389 assertEquals(t.getHour(), hour); 2390 assertEquals(t.getMinute(), min); 2391 } 2392 } 2393 2394 @Test 2395 public void test_minusMinutes_fromZero() { 2396 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2397 LocalDate d = base.toLocalDate().minusDays(1); 2398 LocalTime t = LocalTime.of(22, 49); 2399 2400 for (int i = 70; i > -70; i--) { 2401 LocalDateTime dt = base.minusMinutes(i); 2402 t = t.plusMinutes(1); 2403 2404 if (t == LocalTime.MIDNIGHT) { 2405 d = d.plusDays(1); 2406 } 2407 2408 assertEquals(dt.toLocalDate(), d); 2409 assertEquals(dt.toLocalTime(), t); 2410 } 2411 } 2412 2413 @Test 2414 public void test_minusMinutes_noChange_oneDay() { 2415 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMinutes(24 * 60); 2416 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1)); 2417 } 2418 2419 //----------------------------------------------------------------------- 2420 // minusSeconds() 2421 //----------------------------------------------------------------------- 2422 @Test 2423 public void test_minusSeconds_one() { 2424 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2425 LocalDate d = t.toLocalDate().minusDays(1); 2426 2427 int hour = 0; 2428 int min = 0; 2429 int sec = 0; 2430 2431 for (int i = 0; i < 3700; i++) { 2432 t = t.minusSeconds(1); 2433 sec--; 2434 if (sec == -1) { 2435 min--; 2436 sec = 59; 2437 2438 if (min == -1) { 2439 hour--; 2440 min = 59; 2441 2442 if (hour == -1) { 2443 hour = 23; 2444 } 2445 } 2446 } 2447 2448 assertEquals(t.toLocalDate(), d); 2449 assertEquals(t.getHour(), hour); 2450 assertEquals(t.getMinute(), min); 2451 assertEquals(t.getSecond(), sec); 2452 } 2453 } 2454 2455 @DataProvider(name="minusSeconds_fromZero") 2456 Iterator<Object[]> minusSeconds_fromZero() { 2457 return new Iterator<Object[]>() { 2458 int delta = 30; 2459 2460 int i = 3660; 2461 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1); 2462 int hour = 22; 2463 int min = 59; 2464 int sec = 0; 2465 2466 public boolean hasNext() { 2467 return i >= -3660; 2468 } 2469 2470 public Object[] next() { 2471 final Object[] ret = new Object[] {i, date, hour, min, sec}; 2472 i -= delta; 2473 sec += delta; 2474 2475 if (sec >= 60) { 2476 min++; 2477 sec -= 60; 2478 2479 if (min == 60) { 2480 hour++; 2481 min = 0; 2482 2483 if (hour == 24) { 2484 hour = 0; 2485 } 2486 } 2487 } 2488 2489 if (i == 0) { 2490 date = date.plusDays(1); 2491 } 2492 2493 return ret; 2494 } 2495 2496 public void remove() { 2497 throw new UnsupportedOperationException(); 2498 } 2499 }; 2500 } 2501 2502 @Test(dataProvider="minusSeconds_fromZero") 2503 public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) { 2504 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2505 LocalDateTime t = base.minusSeconds(seconds); 2506 2507 assertEquals(date, t.toLocalDate()); 2508 assertEquals(hour, t.getHour()); 2509 assertEquals(min, t.getMinute()); 2510 assertEquals(sec, t.getSecond()); 2511 } 2512 2513 //----------------------------------------------------------------------- 2514 // minusNanos() 2515 //----------------------------------------------------------------------- 2516 @Test 2517 public void test_minusNanos_halfABillion() { 2518 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2519 LocalDate d = t.toLocalDate().minusDays(1); 2520 2521 int hour = 0; 2522 int min = 0; 2523 int sec = 0; 2524 int nanos = 0; 2525 2526 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) { 2527 t = t.minusNanos(500000000); 2528 nanos -= 500000000; 2529 2530 if (nanos < 0) { 2531 sec--; 2532 nanos += 1000000000; 2533 2534 if (sec == -1) { 2535 min--; 2536 sec += 60; 2537 2538 if (min == -1) { 2539 hour--; 2540 min += 60; 2541 2542 if (hour == -1) { 2543 hour += 24; 2544 } 2545 } 2546 } 2547 } 2548 2549 assertEquals(t.toLocalDate(), d); 2550 assertEquals(t.getHour(), hour); 2551 assertEquals(t.getMinute(), min); 2552 assertEquals(t.getSecond(), sec); 2553 assertEquals(t.getNano(), nanos); 2554 } 2555 } 2556 2557 @DataProvider(name="minusNanos_fromZero") 2558 Iterator<Object[]> minusNanos_fromZero() { 2559 return new Iterator<Object[]>() { 2560 long delta = 7500000000L; 2561 2562 long i = 3660 * 1000000000L; 2563 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1); 2564 int hour = 22; 2565 int min = 59; 2566 long sec = 0; 2567 long nanos = 0; 2568 2569 public boolean hasNext() { 2570 return i >= -3660 * 1000000000L; 2571 } 2572 2573 public Object[] next() { 2574 final Object[] ret = new Object[] {i, date, hour, min, sec, nanos}; 2575 i -= delta; 2576 nanos += delta; 2577 2578 if (nanos >= 1000000000L) { 2579 sec += nanos / 1000000000L; 2580 nanos %= 1000000000L; 2581 2582 if (sec >= 60) { 2583 min++; 2584 sec %= 60; 2585 2586 if (min == 60) { 2587 hour++; 2588 min = 0; 2589 2590 if (hour == 24) { 2591 hour = 0; 2592 date = date.plusDays(1); 2593 } 2594 } 2595 } 2596 } 2597 2598 return ret; 2599 } 2600 2601 public void remove() { 2602 throw new UnsupportedOperationException(); 2603 } 2604 }; 2605 } 2606 2607 @Test(dataProvider="minusNanos_fromZero") 2608 public void test_minusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, long sec, long nanos) { 2609 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT); 2610 LocalDateTime t = base.minusNanos(nanoseconds); 2611 2612 assertEquals(date, t.toLocalDate()); 2613 assertEquals(hour, t.getHour()); 2614 assertEquals(min, t.getMinute()); 2615 assertEquals(sec, t.getSecond()); 2616 assertEquals(nanos, t.getNano()); 2617 } 2618 2619 //----------------------------------------------------------------------- 2620 // until() 2621 //----------------------------------------------------------------------- 2622 @DataProvider(name="until") 2623 Object[][] provider_until() { 2624 return new Object[][]{ 2625 {"2012-06-15T00:00", "2012-06-15T00:00", NANOS, 0}, 2626 {"2012-06-15T00:00", "2012-06-15T00:00", MICROS, 0}, 2627 {"2012-06-15T00:00", "2012-06-15T00:00", MILLIS, 0}, 2628 {"2012-06-15T00:00", "2012-06-15T00:00", SECONDS, 0}, 2629 {"2012-06-15T00:00", "2012-06-15T00:00", MINUTES, 0}, 2630 {"2012-06-15T00:00", "2012-06-15T00:00", HOURS, 0}, 2631 {"2012-06-15T00:00", "2012-06-15T00:00", HALF_DAYS, 0}, 2632 2633 {"2012-06-15T00:00", "2012-06-15T00:00:01", NANOS, 1000000000}, 2634 {"2012-06-15T00:00", "2012-06-15T00:00:01", MICROS, 1000000}, 2635 {"2012-06-15T00:00", "2012-06-15T00:00:01", MILLIS, 1000}, 2636 {"2012-06-15T00:00", "2012-06-15T00:00:01", SECONDS, 1}, 2637 {"2012-06-15T00:00", "2012-06-15T00:00:01", MINUTES, 0}, 2638 {"2012-06-15T00:00", "2012-06-15T00:00:01", HOURS, 0}, 2639 {"2012-06-15T00:00", "2012-06-15T00:00:01", HALF_DAYS, 0}, 2640 2641 {"2012-06-15T00:00", "2012-06-15T00:01", NANOS, 60000000000L}, 2642 {"2012-06-15T00:00", "2012-06-15T00:01", MICROS, 60000000}, 2643 {"2012-06-15T00:00", "2012-06-15T00:01", MILLIS, 60000}, 2644 {"2012-06-15T00:00", "2012-06-15T00:01", SECONDS, 60}, 2645 {"2012-06-15T00:00", "2012-06-15T00:01", MINUTES, 1}, 2646 {"2012-06-15T00:00", "2012-06-15T00:01", HOURS, 0}, 2647 {"2012-06-15T00:00", "2012-06-15T00:01", HALF_DAYS, 0}, 2648 2649 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:39.499", SECONDS, -1}, 2650 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:39.500", SECONDS, -1}, 2651 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:39.501", SECONDS, 0}, 2652 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:40.499", SECONDS, 0}, 2653 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:40.500", SECONDS, 0}, 2654 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:40.501", SECONDS, 0}, 2655 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:41.499", SECONDS, 0}, 2656 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:41.500", SECONDS, 1}, 2657 {"2012-06-15T12:30:40.500", "2012-06-15T12:30:41.501", SECONDS, 1}, 2658 2659 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:39.499", SECONDS, 86400 - 2}, 2660 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:39.500", SECONDS, 86400 - 1}, 2661 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:39.501", SECONDS, 86400 - 1}, 2662 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:40.499", SECONDS, 86400 - 1}, 2663 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:40.500", SECONDS, 86400 + 0}, 2664 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:40.501", SECONDS, 86400 + 0}, 2665 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:41.499", SECONDS, 86400 + 0}, 2666 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:41.500", SECONDS, 86400 + 1}, 2667 {"2012-06-15T12:30:40.500", "2012-06-16T12:30:41.501", SECONDS, 86400 + 1}, 2668 }; 2669 } 2670 2671 @Test(dataProvider = "until") 2672 public void test_until(String startStr, String endStr, TemporalUnit unit, long expected) { 2673 LocalDateTime start = LocalDateTime.parse(startStr); 2674 LocalDateTime end = LocalDateTime.parse(endStr); 2675 assertEquals(start.until(end, unit), expected); 2676 } 2677 2678 @Test(dataProvider = "until") 2679 public void test_until_reveresed(String startStr, String endStr, TemporalUnit unit, long expected) { 2680 LocalDateTime start = LocalDateTime.parse(startStr); 2681 LocalDateTime end = LocalDateTime.parse(endStr); 2682 assertEquals(end.until(start, unit), -expected); 2683 } 2684 2685 //----------------------------------------------------------------------- 2686 // atZone() 2687 //----------------------------------------------------------------------- 2688 @Test 2689 public void test_atZone() { 2690 LocalDateTime t = LocalDateTime.of(2008, 6, 30, 11, 30); 2691 assertEquals(t.atZone(ZONE_PARIS), 2692 ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30), ZONE_PARIS)); 2693 } 2694 2695 @Test 2696 public void test_atZone_Offset() { 2697 LocalDateTime t = LocalDateTime.of(2008, 6, 30, 11, 30); 2698 assertEquals(t.atZone(OFFSET_PTWO), ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30), OFFSET_PTWO)); 2699 } 2700 2701 @Test 2702 public void test_atZone_dstGap() { 2703 LocalDateTime t = LocalDateTime.of(2007, 4, 1, 0, 0); 2704 assertEquals(t.atZone(ZONE_GAZA), 2705 ZonedDateTime.of(LocalDateTime.of(2007, 4, 1, 1, 0), ZONE_GAZA)); 2706 } 2707 2708 @Test 2709 public void test_atZone_dstOverlap() { 2710 LocalDateTime t = LocalDateTime.of(2007, 10, 28, 2, 30); 2711 assertEquals(t.atZone(ZONE_PARIS), 2712 ZonedDateTime.ofStrict(LocalDateTime.of(2007, 10, 28, 2, 30), OFFSET_PTWO, ZONE_PARIS)); 2713 } 2714 2715 @Test(expectedExceptions=NullPointerException.class) 2716 public void test_atZone_nullTimeZone() { 2717 LocalDateTime t = LocalDateTime.of(2008, 6, 30, 11, 30); 2718 t.atZone((ZoneId) null); 2719 } 2720 2721 //----------------------------------------------------------------------- 2722 // toEpochSecond() 2723 //----------------------------------------------------------------------- 2724 @Test 2725 public void test_toEpochSecond_afterEpoch() { 2726 for (int i = -5; i < 5; i++) { 2727 ZoneOffset offset = ZoneOffset.ofHours(i); 2728 for (int j = 0; j < 100000; j++) { 2729 LocalDateTime a = LocalDateTime.of(1970, 1, 1, 0, 0).plusSeconds(j); 2730 assertEquals(a.toEpochSecond(offset), j - i * 3600); 2731 } 2732 } 2733 } 2734 2735 @Test 2736 public void test_toEpochSecond_beforeEpoch() { 2737 for (int i = 0; i < 100000; i++) { 2738 LocalDateTime a = LocalDateTime.of(1970, 1, 1, 0, 0).minusSeconds(i); 2739 assertEquals(a.toEpochSecond(ZoneOffset.UTC), -i); 2740 } 2741 } 2742 2743 //----------------------------------------------------------------------- 2744 // compareTo() 2745 //----------------------------------------------------------------------- 2746 @Test 2747 public void test_comparisons() { 2748 test_comparisons_LocalDateTime( 2749 LocalDate.of(Year.MIN_VALUE, 1, 1), 2750 LocalDate.of(Year.MIN_VALUE, 12, 31), 2751 LocalDate.of(-1, 1, 1), 2752 LocalDate.of(-1, 12, 31), 2753 LocalDate.of(0, 1, 1), 2754 LocalDate.of(0, 12, 31), 2755 LocalDate.of(1, 1, 1), 2756 LocalDate.of(1, 12, 31), 2757 LocalDate.of(2008, 1, 1), 2758 LocalDate.of(2008, 2, 29), 2759 LocalDate.of(2008, 12, 31), 2760 LocalDate.of(Year.MAX_VALUE, 1, 1), 2761 LocalDate.of(Year.MAX_VALUE, 12, 31) 2762 ); 2763 } 2764 2765 void test_comparisons_LocalDateTime(LocalDate... localDates) { 2766 test_comparisons_LocalDateTime( 2767 localDates, 2768 LocalTime.MIDNIGHT, 2769 LocalTime.of(0, 0, 0, 999999999), 2770 LocalTime.of(0, 0, 59, 0), 2771 LocalTime.of(0, 0, 59, 999999999), 2772 LocalTime.of(0, 59, 0, 0), 2773 LocalTime.of(0, 59, 59, 999999999), 2774 LocalTime.NOON, 2775 LocalTime.of(12, 0, 0, 999999999), 2776 LocalTime.of(12, 0, 59, 0), 2777 LocalTime.of(12, 0, 59, 999999999), 2778 LocalTime.of(12, 59, 0, 0), 2779 LocalTime.of(12, 59, 59, 999999999), 2780 LocalTime.of(23, 0, 0, 0), 2781 LocalTime.of(23, 0, 0, 999999999), 2782 LocalTime.of(23, 0, 59, 0), 2783 LocalTime.of(23, 0, 59, 999999999), 2784 LocalTime.of(23, 59, 0, 0), 2785 LocalTime.of(23, 59, 59, 999999999) 2786 ); 2787 } 2788 2789 void test_comparisons_LocalDateTime(LocalDate[] localDates, LocalTime... localTimes) { 2790 LocalDateTime[] localDateTimes = new LocalDateTime[localDates.length * localTimes.length]; 2791 int i = 0; 2792 2793 for (LocalDate localDate : localDates) { 2794 for (LocalTime localTime : localTimes) { 2795 localDateTimes[i++] = LocalDateTime.of(localDate, localTime); 2796 } 2797 } 2798 2799 doTest_comparisons_LocalDateTime(localDateTimes); 2800 } 2801 2802 void doTest_comparisons_LocalDateTime(LocalDateTime[] localDateTimes) { 2803 for (int i = 0; i < localDateTimes.length; i++) { 2804 LocalDateTime a = localDateTimes[i]; 2805 for (int j = 0; j < localDateTimes.length; j++) { 2806 LocalDateTime b = localDateTimes[j]; 2807 if (i < j) { 2808 assertTrue(a.compareTo(b) < 0, a + " <=> " + b); 2809 assertEquals(a.isBefore(b), true, a + " <=> " + b); 2810 assertEquals(a.isAfter(b), false, a + " <=> " + b); 2811 assertEquals(a.equals(b), false, a + " <=> " + b); 2812 } else if (i > j) { 2813 assertTrue(a.compareTo(b) > 0, a + " <=> " + b); 2814 assertEquals(a.isBefore(b), false, a + " <=> " + b); 2815 assertEquals(a.isAfter(b), true, a + " <=> " + b); 2816 assertEquals(a.equals(b), false, a + " <=> " + b); 2817 } else { 2818 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 2819 assertEquals(a.isBefore(b), false, a + " <=> " + b); 2820 assertEquals(a.isAfter(b), false, a + " <=> " + b); 2821 assertEquals(a.equals(b), true, a + " <=> " + b); 2822 } 2823 } 2824 } 2825 } 2826 2827 @Test(expectedExceptions=NullPointerException.class) 2828 public void test_compareTo_ObjectNull() { 2829 TEST_2007_07_15_12_30_40_987654321.compareTo(null); 2830 } 2831 2832 @Test(expectedExceptions=NullPointerException.class) 2833 public void test_isBefore_ObjectNull() { 2834 TEST_2007_07_15_12_30_40_987654321.isBefore(null); 2835 } 2836 2837 @Test(expectedExceptions=NullPointerException.class) 2838 public void test_isAfter_ObjectNull() { 2839 TEST_2007_07_15_12_30_40_987654321.isAfter(null); 2840 } 2841 2842 @Test(expectedExceptions=ClassCastException.class) 2843 @SuppressWarnings({"unchecked", "rawtypes"}) 2844 public void compareToNonLocalDateTime() { 2845 Comparable c = TEST_2007_07_15_12_30_40_987654321; 2846 c.compareTo(new Object()); 2847 } 2848 2849 //----------------------------------------------------------------------- 2850 // equals() 2851 //----------------------------------------------------------------------- 2852 @DataProvider(name="sampleDateTimes") 2853 Iterator<Object[]> provider_sampleDateTimes() { 2854 return new Iterator<Object[]>() { 2855 Object[][] sampleDates = provider_sampleDates(); 2856 Object[][] sampleTimes = provider_sampleTimes(); 2857 int datesIndex = 0; 2858 int timesIndex = 0; 2859 2860 public boolean hasNext() { 2861 return datesIndex < sampleDates.length; 2862 } 2863 2864 public Object[] next() { 2865 Object[] sampleDate = sampleDates[datesIndex]; 2866 Object[] sampleTime = sampleTimes[timesIndex]; 2867 2868 Object[] ret = new Object[sampleDate.length + sampleTime.length]; 2869 2870 System.arraycopy(sampleDate, 0, ret, 0, sampleDate.length); 2871 System.arraycopy(sampleTime, 0, ret, sampleDate.length, sampleTime.length); 2872 2873 if (++timesIndex == sampleTimes.length) { 2874 datesIndex++; 2875 timesIndex = 0; 2876 } 2877 2878 return ret; 2879 } 2880 2881 public void remove() { 2882 throw new UnsupportedOperationException(); 2883 } 2884 }; 2885 } 2886 2887 @Test(dataProvider="sampleDateTimes") 2888 public void test_equals_true(int y, int m, int d, int h, int mi, int s, int n) { 2889 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2890 LocalDateTime b = LocalDateTime.of(y, m, d, h, mi, s, n); 2891 assertTrue(a.equals(b)); 2892 } 2893 2894 @Test(dataProvider="sampleDateTimes") 2895 public void test_equals_false_year_differs(int y, int m, int d, int h, int mi, int s, int n) { 2896 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2897 LocalDateTime b = LocalDateTime.of(y + 1, m, d, h, mi, s, n); 2898 assertFalse(a.equals(b)); 2899 } 2900 2901 @Test(dataProvider="sampleDateTimes") 2902 public void test_equals_false_month_differs(int y, int m, int d, int h, int mi, int s, int n) { 2903 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2904 LocalDateTime b = LocalDateTime.of(y, m + 1, d, h, mi, s, n); 2905 assertFalse(a.equals(b)); 2906 } 2907 2908 @Test(dataProvider="sampleDateTimes") 2909 public void test_equals_false_day_differs(int y, int m, int d, int h, int mi, int s, int n) { 2910 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2911 LocalDateTime b = LocalDateTime.of(y, m, d + 1, h, mi, s, n); 2912 assertFalse(a.equals(b)); 2913 } 2914 2915 @Test(dataProvider="sampleDateTimes") 2916 public void test_equals_false_hour_differs(int y, int m, int d, int h, int mi, int s, int n) { 2917 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2918 LocalDateTime b = LocalDateTime.of(y, m, d, h + 1, mi, s, n); 2919 assertFalse(a.equals(b)); 2920 } 2921 2922 @Test(dataProvider="sampleDateTimes") 2923 public void test_equals_false_minute_differs(int y, int m, int d, int h, int mi, int s, int n) { 2924 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2925 LocalDateTime b = LocalDateTime.of(y, m, d, h, mi + 1, s, n); 2926 assertFalse(a.equals(b)); 2927 } 2928 2929 @Test(dataProvider="sampleDateTimes") 2930 public void test_equals_false_second_differs(int y, int m, int d, int h, int mi, int s, int n) { 2931 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2932 LocalDateTime b = LocalDateTime.of(y, m, d, h, mi, s + 1, n); 2933 assertFalse(a.equals(b)); 2934 } 2935 2936 @Test(dataProvider="sampleDateTimes") 2937 public void test_equals_false_nano_differs(int y, int m, int d, int h, int mi, int s, int n) { 2938 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2939 LocalDateTime b = LocalDateTime.of(y, m, d, h, mi, s, n + 1); 2940 assertFalse(a.equals(b)); 2941 } 2942 2943 @Test 2944 public void test_equals_itself_true() { 2945 assertEquals(TEST_2007_07_15_12_30_40_987654321.equals(TEST_2007_07_15_12_30_40_987654321), true); 2946 } 2947 2948 @Test 2949 public void test_equals_string_false() { 2950 assertEquals(TEST_2007_07_15_12_30_40_987654321.equals("2007-07-15T12:30:40.987654321"), false); 2951 } 2952 2953 @Test 2954 public void test_equals_null_false() { 2955 assertEquals(TEST_2007_07_15_12_30_40_987654321.equals(null), false); 2956 } 2957 2958 //----------------------------------------------------------------------- 2959 // hashCode() 2960 //----------------------------------------------------------------------- 2961 @Test(dataProvider="sampleDateTimes") 2962 public void test_hashCode(int y, int m, int d, int h, int mi, int s, int n) { 2963 LocalDateTime a = LocalDateTime.of(y, m, d, h, mi, s, n); 2964 assertEquals(a.hashCode(), a.hashCode()); 2965 LocalDateTime b = LocalDateTime.of(y, m, d, h, mi, s, n); 2966 assertEquals(a.hashCode(), b.hashCode()); 2967 } 2968 2969 //----------------------------------------------------------------------- 2970 // toString() 2971 //----------------------------------------------------------------------- 2972 @DataProvider(name="sampleToString") 2973 Object[][] provider_sampleToString() { 2974 return new Object[][] { 2975 {2008, 7, 5, 2, 1, 0, 0, "2008-07-05T02:01"}, 2976 {2007, 12, 31, 23, 59, 1, 0, "2007-12-31T23:59:01"}, 2977 {999, 12, 31, 23, 59, 59, 990000000, "0999-12-31T23:59:59.990"}, 2978 {-1, 1, 2, 23, 59, 59, 999990000, "-0001-01-02T23:59:59.999990"}, 2979 {-2008, 1, 2, 23, 59, 59, 999999990, "-2008-01-02T23:59:59.999999990"}, 2980 }; 2981 } 2982 2983 @Test(dataProvider="sampleToString") 2984 public void test_toString(int y, int m, int d, int h, int mi, int s, int n, String expected) { 2985 LocalDateTime t = LocalDateTime.of(y, m, d, h, mi, s, n); 2986 String str = t.toString(); 2987 assertEquals(str, expected); 2988 } 2989 2990 //----------------------------------------------------------------------- 2991 // format(DateTimeFormatter) 2992 //----------------------------------------------------------------------- 2993 @Test 2994 public void test_format_formatter() { 2995 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 2996 String t = LocalDateTime.of(2010, 12, 3, 11, 30, 45).format(f); 2997 assertEquals(t, "2010 12 3 11 30 45"); 2998 } 2999 3000 @Test(expectedExceptions=NullPointerException.class) 3001 public void test_format_formatter_null() { 3002 LocalDateTime.of(2010, 12, 3, 11, 30, 45).format(null); 3003 } 3004 3005 } 3006