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