1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * This file is available under and governed by the GNU General Public 26 * License version 2 only, as published by the Free Software Foundation. 27 * However, the following notice accompanied the original version of this 28 * file: 29 * 30 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos 31 * 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions are met: 36 * 37 * * Redistributions of source code must retain the above copyright notice, 38 * this list of conditions and the following disclaimer. 39 * 40 * * Redistributions in binary form must reproduce the above copyright notice, 41 * this list of conditions and the following disclaimer in the documentation 42 * and/or other materials provided with the distribution. 43 * 44 * * Neither the name of JSR-310 nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package tck.java.time; 61 62 import static java.time.temporal.ChronoField.AMPM_OF_DAY; 63 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM; 64 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY; 65 import static java.time.temporal.ChronoField.HOUR_OF_AMPM; 66 import static java.time.temporal.ChronoField.HOUR_OF_DAY; 67 import static java.time.temporal.ChronoField.MICRO_OF_DAY; 68 import static java.time.temporal.ChronoField.MICRO_OF_SECOND; 69 import static java.time.temporal.ChronoField.MILLI_OF_DAY; 70 import static java.time.temporal.ChronoField.MILLI_OF_SECOND; 71 import static java.time.temporal.ChronoField.MINUTE_OF_DAY; 72 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; 73 import static java.time.temporal.ChronoField.NANO_OF_DAY; 74 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 75 import static java.time.temporal.ChronoField.SECOND_OF_DAY; 76 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; 77 import static java.time.temporal.ChronoUnit.DAYS; 78 import static java.time.temporal.ChronoUnit.FOREVER; 79 import static java.time.temporal.ChronoUnit.HALF_DAYS; 80 import static java.time.temporal.ChronoUnit.HOURS; 81 import static java.time.temporal.ChronoUnit.MICROS; 82 import static java.time.temporal.ChronoUnit.MILLIS; 83 import static java.time.temporal.ChronoUnit.MINUTES; 84 import static java.time.temporal.ChronoUnit.MONTHS; 85 import static java.time.temporal.ChronoUnit.NANOS; 86 import static java.time.temporal.ChronoUnit.SECONDS; 87 import static java.time.temporal.ChronoUnit.WEEKS; 88 import static java.time.temporal.ChronoUnit.YEARS; 89 import static org.testng.Assert.assertEquals; 90 import static org.testng.Assert.assertNotNull; 91 import static org.testng.Assert.assertSame; 92 import static org.testng.Assert.assertTrue; 93 import static org.testng.Assert.fail; 94 95 import java.time.Clock; 96 import java.time.DateTimeException; 97 import java.time.Duration; 98 import java.time.Instant; 99 import java.time.LocalDate; 100 import java.time.LocalDateTime; 101 import java.time.LocalTime; 102 import java.time.OffsetDateTime; 103 import java.time.OffsetTime; 104 import java.time.Period; 105 import java.time.ZoneId; 106 import java.time.ZoneOffset; 107 import java.time.ZonedDateTime; 108 import java.time.format.DateTimeFormatter; 109 import java.time.format.DateTimeParseException; 110 import java.time.temporal.ChronoField; 111 import java.time.temporal.ChronoUnit; 112 import java.time.temporal.JulianFields; 113 import java.time.temporal.Temporal; 114 import java.time.temporal.TemporalAccessor; 115 import java.time.temporal.TemporalAdjuster; 116 import java.time.temporal.TemporalAmount; 117 import java.time.temporal.TemporalField; 118 import java.time.temporal.TemporalQueries; 119 import java.time.temporal.TemporalQuery; 120 import java.time.temporal.TemporalUnit; 121 import java.time.temporal.UnsupportedTemporalTypeException; 122 import java.time.temporal.ValueRange; 123 import java.util.ArrayList; 124 import java.util.Arrays; 125 import java.util.EnumSet; 126 import java.util.Iterator; 127 import java.util.List; 128 129 import org.testng.annotations.BeforeMethod; 130 import org.testng.annotations.DataProvider; 131 import org.testng.annotations.Test; 132 133 /** 134 * Test LocalTime. 135 */ 136 @Test 137 public class TCKLocalTime extends AbstractDateTimeTest { 138 139 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2); 140 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 141 142 private LocalTime TEST_12_30_40_987654321; 143 144 private static final TemporalUnit[] INVALID_UNITS; 145 static { 146 EnumSet<ChronoUnit> set = EnumSet.range(DAYS, FOREVER); 147 INVALID_UNITS = set.toArray(new TemporalUnit[set.size()]); 148 } 149 150 @BeforeMethod 151 public void setUp() { 152 TEST_12_30_40_987654321 = LocalTime.of(12, 30, 40, 987654321); 153 } 154 155 //----------------------------------------------------------------------- 156 @Override 157 protected List<TemporalAccessor> samples() { 158 TemporalAccessor[] array = {TEST_12_30_40_987654321, LocalTime.MIN, LocalTime.MAX, LocalTime.MIDNIGHT, LocalTime.NOON}; 159 return Arrays.asList(array); 160 } 161 162 @Override 163 protected List<TemporalField> validFields() { 164 TemporalField[] array = { 165 NANO_OF_SECOND, 166 NANO_OF_DAY, 167 MICRO_OF_SECOND, 168 MICRO_OF_DAY, 169 MILLI_OF_SECOND, 170 MILLI_OF_DAY, 171 SECOND_OF_MINUTE, 172 SECOND_OF_DAY, 173 MINUTE_OF_HOUR, 174 MINUTE_OF_DAY, 175 CLOCK_HOUR_OF_AMPM, 176 HOUR_OF_AMPM, 177 CLOCK_HOUR_OF_DAY, 178 HOUR_OF_DAY, 179 AMPM_OF_DAY, 180 }; 181 return Arrays.asList(array); 182 } 183 184 @Override 185 protected List<TemporalField> invalidFields() { 186 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 187 list.removeAll(validFields()); 188 list.add(JulianFields.JULIAN_DAY); 189 list.add(JulianFields.MODIFIED_JULIAN_DAY); 190 list.add(JulianFields.RATA_DIE); 191 return list; 192 } 193 194 //----------------------------------------------------------------------- 195 196 private void check(LocalTime test, int h, int m, int s, int n) { 197 assertEquals(test.getHour(), h); 198 assertEquals(test.getMinute(), m); 199 assertEquals(test.getSecond(), s); 200 assertEquals(test.getNano(), n); 201 assertEquals(test, test); 202 assertEquals(test.hashCode(), test.hashCode()); 203 assertEquals(LocalTime.of(h, m, s, n), test); 204 } 205 206 //----------------------------------------------------------------------- 207 // constants 208 //----------------------------------------------------------------------- 209 @Test 210 public void constant_MIDNIGHT() { 211 check(LocalTime.MIDNIGHT, 0, 0, 0, 0); 212 } 213 214 @Test 215 public void constant_MIDDAY() { 216 check(LocalTime.NOON, 12, 0, 0, 0); 217 } 218 219 @Test 220 public void constant_MIN() { 221 check(LocalTime.MIN, 0, 0, 0, 0); 222 } 223 224 @Test 225 public void constant_MAX() { 226 check(LocalTime.MAX, 23, 59, 59, 999999999); 227 } 228 229 //----------------------------------------------------------------------- 230 // now(ZoneId) 231 //----------------------------------------------------------------------- 232 @Test(expectedExceptions=NullPointerException.class) 233 public void now_ZoneId_nullZoneId() { 234 LocalTime.now((ZoneId) null); 235 } 236 237 @Test 238 public void now_ZoneId() { 239 ZoneId zone = ZoneId.of("UTC+01:02:03"); 240 LocalTime expected = LocalTime.now(Clock.system(zone)); 241 LocalTime test = LocalTime.now(zone); 242 for (int i = 0; i < 100; i++) { 243 if (expected.equals(test)) { 244 return; 245 } 246 expected = LocalTime.now(Clock.system(zone)); 247 test = LocalTime.now(zone); 248 } 249 assertEquals(test, expected); 250 } 251 252 //----------------------------------------------------------------------- 253 // now(Clock) 254 //----------------------------------------------------------------------- 255 @Test(expectedExceptions=NullPointerException.class) 256 public void now_Clock_nullClock() { 257 LocalTime.now((Clock) null); 258 } 259 260 @Test 261 public void now_Clock_allSecsInDay() { 262 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 263 Instant instant = Instant.ofEpochSecond(i, 8); 264 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 265 LocalTime test = LocalTime.now(clock); 266 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 267 assertEquals(test.getMinute(), (i / 60) % 60); 268 assertEquals(test.getSecond(), i % 60); 269 assertEquals(test.getNano(), 8); 270 } 271 } 272 273 @Test 274 public void now_Clock_beforeEpoch() { 275 for (int i =-1; i >= -(24 * 60 * 60); i--) { 276 Instant instant = Instant.ofEpochSecond(i, 8); 277 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 278 LocalTime test = LocalTime.now(clock); 279 assertEquals(test.getHour(), ((i + 24 * 60 * 60) / (60 * 60)) % 24); 280 assertEquals(test.getMinute(), ((i + 24 * 60 * 60) / 60) % 60); 281 assertEquals(test.getSecond(), (i + 24 * 60 * 60) % 60); 282 assertEquals(test.getNano(), 8); 283 } 284 } 285 286 //----------------------------------------------------------------------- 287 @Test 288 public void now_Clock_max() { 289 Clock clock = Clock.fixed(Instant.MAX, ZoneOffset.UTC); 290 LocalTime test = LocalTime.now(clock); 291 assertEquals(test.getHour(), 23); 292 assertEquals(test.getMinute(), 59); 293 assertEquals(test.getSecond(), 59); 294 assertEquals(test.getNano(), 999_999_999); 295 } 296 297 @Test 298 public void now_Clock_min() { 299 Clock clock = Clock.fixed(Instant.MIN, ZoneOffset.UTC); 300 LocalTime test = LocalTime.now(clock); 301 assertEquals(test.getHour(), 0); 302 assertEquals(test.getMinute(), 0); 303 assertEquals(test.getSecond(), 0); 304 assertEquals(test.getNano(), 0); 305 } 306 307 //----------------------------------------------------------------------- 308 // of() factories 309 //----------------------------------------------------------------------- 310 @Test 311 public void factory_time_2ints() { 312 LocalTime test = LocalTime.of(12, 30); 313 check(test, 12, 30, 0, 0); 314 } 315 316 @Test(expectedExceptions=DateTimeException.class) 317 public void factory_time_2ints_hourTooLow() { 318 LocalTime.of(-1, 0); 319 } 320 321 @Test(expectedExceptions=DateTimeException.class) 322 public void factory_time_2ints_hourTooHigh() { 323 LocalTime.of(24, 0); 324 } 325 326 @Test(expectedExceptions=DateTimeException.class) 327 public void factory_time_2ints_minuteTooLow() { 328 LocalTime.of(0, -1); 329 } 330 331 @Test(expectedExceptions=DateTimeException.class) 332 public void factory_time_2ints_minuteTooHigh() { 333 LocalTime.of(0, 60); 334 } 335 336 //----------------------------------------------------------------------- 337 @Test 338 public void factory_time_3ints() { 339 LocalTime test = LocalTime.of(12, 30, 40); 340 check(test, 12, 30, 40, 0); 341 } 342 343 @Test(expectedExceptions=DateTimeException.class) 344 public void factory_time_3ints_hourTooLow() { 345 LocalTime.of(-1, 0, 0); 346 } 347 348 @Test(expectedExceptions=DateTimeException.class) 349 public void factory_time_3ints_hourTooHigh() { 350 LocalTime.of(24, 0, 0); 351 } 352 353 @Test(expectedExceptions=DateTimeException.class) 354 public void factory_time_3ints_minuteTooLow() { 355 LocalTime.of(0, -1, 0); 356 } 357 358 @Test(expectedExceptions=DateTimeException.class) 359 public void factory_time_3ints_minuteTooHigh() { 360 LocalTime.of(0, 60, 0); 361 } 362 363 @Test(expectedExceptions=DateTimeException.class) 364 public void factory_time_3ints_secondTooLow() { 365 LocalTime.of(0, 0, -1); 366 } 367 368 @Test(expectedExceptions=DateTimeException.class) 369 public void factory_time_3ints_secondTooHigh() { 370 LocalTime.of(0, 0, 60); 371 } 372 373 //----------------------------------------------------------------------- 374 @Test 375 public void factory_time_4ints() { 376 LocalTime test = LocalTime.of(12, 30, 40, 987654321); 377 check(test, 12, 30, 40, 987654321); 378 test = LocalTime.of(12, 0, 40, 987654321); 379 check(test, 12, 0, 40, 987654321); 380 } 381 382 @Test(expectedExceptions=DateTimeException.class) 383 public void factory_time_4ints_hourTooLow() { 384 LocalTime.of(-1, 0, 0, 0); 385 } 386 387 @Test(expectedExceptions=DateTimeException.class) 388 public void factory_time_4ints_hourTooHigh() { 389 LocalTime.of(24, 0, 0, 0); 390 } 391 392 @Test(expectedExceptions=DateTimeException.class) 393 public void factory_time_4ints_minuteTooLow() { 394 LocalTime.of(0, -1, 0, 0); 395 } 396 397 @Test(expectedExceptions=DateTimeException.class) 398 public void factory_time_4ints_minuteTooHigh() { 399 LocalTime.of(0, 60, 0, 0); 400 } 401 402 @Test(expectedExceptions=DateTimeException.class) 403 public void factory_time_4ints_secondTooLow() { 404 LocalTime.of(0, 0, -1, 0); 405 } 406 407 @Test(expectedExceptions=DateTimeException.class) 408 public void factory_time_4ints_secondTooHigh() { 409 LocalTime.of(0, 0, 60, 0); 410 } 411 412 @Test(expectedExceptions=DateTimeException.class) 413 public void factory_time_4ints_nanoTooLow() { 414 LocalTime.of(0, 0, 0, -1); 415 } 416 417 @Test(expectedExceptions=DateTimeException.class) 418 public void factory_time_4ints_nanoTooHigh() { 419 LocalTime.of(0, 0, 0, 1000000000); 420 } 421 422 //----------------------------------------------------------------------- 423 // ofSecondOfDay(long) 424 //----------------------------------------------------------------------- 425 @Test 426 public void factory_ofSecondOfDay() { 427 LocalTime localTime = LocalTime.ofSecondOfDay(2 * 60 * 60 + 17 * 60 + 23); 428 check(localTime, 2, 17, 23, 0); 429 } 430 431 @Test(expectedExceptions=DateTimeException.class) 432 public void factory_ofSecondOfDay_tooLow() { 433 LocalTime.ofSecondOfDay(-1); 434 } 435 436 @Test(expectedExceptions=DateTimeException.class) 437 public void factory_ofSecondOfDay_tooHigh() { 438 LocalTime.ofSecondOfDay(24 * 60 * 60); 439 } 440 441 //----------------------------------------------------------------------- 442 // ofNanoOfDay(long) 443 //----------------------------------------------------------------------- 444 @Test 445 public void factory_ofNanoOfDay() { 446 LocalTime localTime = LocalTime.ofNanoOfDay(60 * 60 * 1000000000L + 17); 447 check(localTime, 1, 0, 0, 17); 448 } 449 450 @Test(expectedExceptions=DateTimeException.class) 451 public void factory_ofNanoOfDay_tooLow() { 452 LocalTime.ofNanoOfDay(-1); 453 } 454 455 @Test(expectedExceptions=DateTimeException.class) 456 public void factory_ofNanoOfDay_tooHigh() { 457 LocalTime.ofNanoOfDay(24 * 60 * 60 * 1000000000L); 458 } 459 460 //----------------------------------------------------------------------- 461 // from() 462 //----------------------------------------------------------------------- 463 @Test 464 public void factory_from_TemporalAccessor() { 465 assertEquals(LocalTime.from(LocalTime.of(17, 30)), LocalTime.of(17, 30)); 466 assertEquals(LocalTime.from(LocalDateTime.of(2012, 5, 1, 17, 30)), LocalTime.of(17, 30)); 467 } 468 469 @Test(expectedExceptions=DateTimeException.class) 470 public void factory_from_TemporalAccessor_invalid_noDerive() { 471 LocalTime.from(LocalDate.of(2007, 7, 15)); 472 } 473 474 @Test(expectedExceptions=NullPointerException.class) 475 public void factory_from_TemporalAccessor_null() { 476 LocalTime.from((TemporalAccessor) null); 477 } 478 479 //----------------------------------------------------------------------- 480 // parse() 481 //----------------------------------------------------------------------- 482 @Test(dataProvider = "sampleToString") 483 public void factory_parse_validText(int h, int m, int s, int n, String parsable) { 484 LocalTime t = LocalTime.parse(parsable); 485 assertNotNull(t, parsable); 486 assertEquals(t.getHour(), h); 487 assertEquals(t.getMinute(), m); 488 assertEquals(t.getSecond(), s); 489 assertEquals(t.getNano(), n); 490 } 491 492 @DataProvider(name="sampleBadParse") 493 Object[][] provider_sampleBadParse() { 494 return new Object[][]{ 495 {"00;00"}, 496 {"12-00"}, 497 {"-01:00"}, 498 {"00:00:00-09"}, 499 {"00:00:00,09"}, 500 {"00:00:abs"}, 501 {"11"}, 502 {"11:30+01:00"}, 503 {"11:30+01:00[Europe/Paris]"}, 504 }; 505 } 506 507 @Test(dataProvider = "sampleBadParse", expectedExceptions={DateTimeParseException.class}) 508 public void factory_parse_invalidText(String unparsable) { 509 LocalTime.parse(unparsable); 510 } 511 512 //-----------------------------------------------------------------------s 513 @Test(expectedExceptions=DateTimeParseException.class) 514 public void factory_parse_illegalHour() { 515 LocalTime.parse("25:00"); 516 } 517 518 @Test(expectedExceptions=DateTimeParseException.class) 519 public void factory_parse_illegalMinute() { 520 LocalTime.parse("12:60"); 521 } 522 523 @Test(expectedExceptions=DateTimeParseException.class) 524 public void factory_parse_illegalSecond() { 525 LocalTime.parse("12:12:60"); 526 } 527 528 //-----------------------------------------------------------------------s 529 @Test(expectedExceptions = {NullPointerException.class}) 530 public void factory_parse_nullTest() { 531 LocalTime.parse((String) null); 532 } 533 534 //----------------------------------------------------------------------- 535 // parse(DateTimeFormatter) 536 //----------------------------------------------------------------------- 537 @Test 538 public void factory_parse_formatter() { 539 DateTimeFormatter f = DateTimeFormatter.ofPattern("H m s"); 540 LocalTime test = LocalTime.parse("14 30 40", f); 541 assertEquals(test, LocalTime.of(14, 30, 40)); 542 } 543 544 @Test(expectedExceptions=NullPointerException.class) 545 public void factory_parse_formatter_nullText() { 546 DateTimeFormatter f = DateTimeFormatter.ofPattern("H m s"); 547 LocalTime.parse((String) null, f); 548 } 549 550 @Test(expectedExceptions=NullPointerException.class) 551 public void factory_parse_formatter_nullFormatter() { 552 LocalTime.parse("ANY", null); 553 } 554 555 //----------------------------------------------------------------------- 556 // isSupported(TemporalField) 557 //----------------------------------------------------------------------- 558 @Test 559 public void test_isSupported_TemporalField() { 560 assertEquals(TEST_12_30_40_987654321.isSupported((TemporalField) null), false); 561 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.NANO_OF_SECOND), true); 562 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.NANO_OF_DAY), true); 563 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.MICRO_OF_SECOND), true); 564 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.MICRO_OF_DAY), true); 565 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.MILLI_OF_SECOND), true); 566 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.MILLI_OF_DAY), true); 567 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.SECOND_OF_MINUTE), true); 568 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.SECOND_OF_DAY), true); 569 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.MINUTE_OF_HOUR), true); 570 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.MINUTE_OF_DAY), true); 571 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.HOUR_OF_AMPM), true); 572 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), true); 573 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.HOUR_OF_DAY), true); 574 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), true); 575 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.AMPM_OF_DAY), true); 576 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.DAY_OF_WEEK), false); 577 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), false); 578 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), false); 579 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.DAY_OF_MONTH), false); 580 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.DAY_OF_YEAR), false); 581 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.EPOCH_DAY), false); 582 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), false); 583 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), false); 584 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.MONTH_OF_YEAR), false); 585 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.PROLEPTIC_MONTH), false); 586 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.YEAR), false); 587 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.YEAR_OF_ERA), false); 588 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.ERA), false); 589 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.INSTANT_SECONDS), false); 590 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoField.OFFSET_SECONDS), false); 591 } 592 593 //----------------------------------------------------------------------- 594 // isSupported(TemporalUnit) 595 //----------------------------------------------------------------------- 596 @Test 597 public void test_isSupported_TemporalUnit() { 598 assertEquals(TEST_12_30_40_987654321.isSupported((TemporalUnit) null), false); 599 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.NANOS), true); 600 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.MICROS), true); 601 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.MILLIS), true); 602 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.SECONDS), true); 603 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.MINUTES), true); 604 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.HOURS), true); 605 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.HALF_DAYS), true); 606 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.DAYS), false); 607 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.WEEKS), false); 608 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.MONTHS), false); 609 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.YEARS), false); 610 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.DECADES), false); 611 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.CENTURIES), false); 612 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.MILLENNIA), false); 613 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.ERAS), false); 614 assertEquals(TEST_12_30_40_987654321.isSupported(ChronoUnit.FOREVER), false); 615 } 616 617 //----------------------------------------------------------------------- 618 // get(TemporalField) 619 //----------------------------------------------------------------------- 620 @Test 621 public void test_get_TemporalField() { 622 LocalTime test = TEST_12_30_40_987654321; 623 assertEquals(test.get(ChronoField.HOUR_OF_DAY), 12); 624 assertEquals(test.get(ChronoField.MINUTE_OF_HOUR), 30); 625 assertEquals(test.get(ChronoField.SECOND_OF_MINUTE), 40); 626 assertEquals(test.get(ChronoField.NANO_OF_SECOND), 987654321); 627 628 assertEquals(test.get(ChronoField.SECOND_OF_DAY), 12 * 3600 + 30 * 60 + 40); 629 assertEquals(test.get(ChronoField.MINUTE_OF_DAY), 12 * 60 + 30); 630 assertEquals(test.get(ChronoField.HOUR_OF_AMPM), 0); 631 assertEquals(test.get(ChronoField.CLOCK_HOUR_OF_AMPM), 12); 632 assertEquals(test.get(ChronoField.CLOCK_HOUR_OF_DAY), 12); 633 assertEquals(test.get(ChronoField.AMPM_OF_DAY), 1); 634 } 635 636 @Test 637 public void test_getLong_TemporalField() { 638 LocalTime test = TEST_12_30_40_987654321; 639 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); 640 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); 641 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); 642 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); 643 644 assertEquals(test.getLong(ChronoField.SECOND_OF_DAY), 12 * 3600 + 30 * 60 + 40); 645 assertEquals(test.getLong(ChronoField.MINUTE_OF_DAY), 12 * 60 + 30); 646 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); 647 assertEquals(test.getLong(ChronoField.CLOCK_HOUR_OF_AMPM), 12); 648 assertEquals(test.getLong(ChronoField.CLOCK_HOUR_OF_DAY), 12); 649 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); 650 } 651 652 //----------------------------------------------------------------------- 653 // query(TemporalQuery) 654 //----------------------------------------------------------------------- 655 @DataProvider(name="query") 656 Object[][] data_query() { 657 return new Object[][] { 658 {TEST_12_30_40_987654321, TemporalQueries.chronology(), null}, 659 {TEST_12_30_40_987654321, TemporalQueries.zoneId(), null}, 660 {TEST_12_30_40_987654321, TemporalQueries.precision(), ChronoUnit.NANOS}, 661 {TEST_12_30_40_987654321, TemporalQueries.zone(), null}, 662 {TEST_12_30_40_987654321, TemporalQueries.offset(), null}, 663 {TEST_12_30_40_987654321, TemporalQueries.localDate(), null}, 664 {TEST_12_30_40_987654321, TemporalQueries.localTime(), TEST_12_30_40_987654321}, 665 }; 666 } 667 668 @Test(dataProvider="query") 669 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 670 assertEquals(temporal.query(query), expected); 671 } 672 673 @Test(dataProvider="query") 674 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 675 assertEquals(query.queryFrom(temporal), expected); 676 } 677 678 @Test(expectedExceptions=NullPointerException.class) 679 public void test_query_null() { 680 TEST_12_30_40_987654321.query(null); 681 } 682 683 //----------------------------------------------------------------------- 684 // get*() 685 //----------------------------------------------------------------------- 686 @DataProvider(name="sampleTimes") 687 Object[][] provider_sampleTimes() { 688 return new Object[][] { 689 {0, 0, 0, 0}, 690 {0, 0, 0, 1}, 691 {0, 0, 1, 0}, 692 {0, 0, 1, 1}, 693 {0, 1, 0, 0}, 694 {0, 1, 0, 1}, 695 {0, 1, 1, 0}, 696 {0, 1, 1, 1}, 697 {1, 0, 0, 0}, 698 {1, 0, 0, 1}, 699 {1, 0, 1, 0}, 700 {1, 0, 1, 1}, 701 {1, 1, 0, 0}, 702 {1, 1, 0, 1}, 703 {1, 1, 1, 0}, 704 {1, 1, 1, 1}, 705 }; 706 } 707 708 //----------------------------------------------------------------------- 709 @Test(dataProvider="sampleTimes") 710 public void test_get(int h, int m, int s, int ns) { 711 LocalTime a = LocalTime.of(h, m, s, ns); 712 assertEquals(a.getHour(), h); 713 assertEquals(a.getMinute(), m); 714 assertEquals(a.getSecond(), s); 715 assertEquals(a.getNano(), ns); 716 } 717 718 //----------------------------------------------------------------------- 719 // adjustInto(Temporal) 720 //----------------------------------------------------------------------- 721 @DataProvider(name="adjustInto") 722 Object[][] data_adjustInto() { 723 return new Object[][]{ 724 {LocalTime.of(23, 5), LocalTime.of(4, 1, 1, 100), LocalTime.of(23, 5, 0, 0), null}, 725 {LocalTime.of(23, 5, 20), LocalTime.of(4, 1, 1, 100), LocalTime.of(23, 5, 20, 0), null}, 726 {LocalTime.of(23, 5, 20, 1000), LocalTime.of(4, 1, 1, 100), LocalTime.of(23, 5, 20, 1000), null}, 727 {LocalTime.of(23, 5, 20, 1000), LocalTime.MAX, LocalTime.of(23, 5, 20, 1000), null}, 728 {LocalTime.of(23, 5, 20, 1000), LocalTime.MIN, LocalTime.of(23, 5, 20, 1000), null}, 729 {LocalTime.of(23, 5, 20, 1000), LocalTime.NOON, LocalTime.of(23, 5, 20, 1000), null}, 730 {LocalTime.of(23, 5, 20, 1000), LocalTime.MIDNIGHT, LocalTime.of(23, 5, 20, 1000), null}, 731 {LocalTime.MAX, LocalTime.of(23, 5, 20, 1000), LocalTime.of(23, 59, 59, 999999999), null}, 732 {LocalTime.MIN, LocalTime.of(23, 5, 20, 1000), LocalTime.of(0, 0, 0), null}, 733 {LocalTime.NOON, LocalTime.of(23, 5, 20, 1000), LocalTime.of(12, 0, 0), null}, 734 {LocalTime.MIDNIGHT, LocalTime.of(23, 5, 20, 1000), LocalTime.of(0, 0, 0), null}, 735 736 {LocalTime.of(23, 5), LocalDateTime.of(2210, 2, 2, 1, 1), LocalDateTime.of(2210, 2, 2, 23, 5), null}, 737 {LocalTime.of(23, 5), OffsetTime.of(1, 1, 0, 0, OFFSET_PTWO), OffsetTime.of(23, 5, 0, 0, OFFSET_PTWO), null}, 738 {LocalTime.of(23, 5), OffsetDateTime.of(2210, 2, 2, 1, 1, 0, 0, OFFSET_PTWO), OffsetDateTime.of(2210, 2, 2, 23, 5, 0, 0, OFFSET_PTWO), null}, 739 {LocalTime.of(23, 5), ZonedDateTime.of(2210, 2, 2, 1, 1, 0, 0, ZONE_PARIS), ZonedDateTime.of(2210, 2, 2, 23, 5, 0, 0, ZONE_PARIS), null}, 740 741 {LocalTime.of(23, 5), LocalDate.of(2210, 2, 2), null, DateTimeException.class}, 742 {LocalTime.of(23, 5), null, null, NullPointerException.class}, 743 744 }; 745 } 746 747 @Test(dataProvider="adjustInto") 748 public void test_adjustInto(LocalTime test, Temporal temporal, Temporal expected, Class<?> expectedEx) { 749 if (expectedEx == null) { 750 Temporal result = test.adjustInto(temporal); 751 assertEquals(result, expected); 752 } else { 753 try { 754 Temporal result = test.adjustInto(temporal); 755 fail(); 756 } catch (Exception ex) { 757 assertTrue(expectedEx.isInstance(ex)); 758 } 759 } 760 } 761 762 //----------------------------------------------------------------------- 763 // with(TemporalAdjuster) 764 //----------------------------------------------------------------------- 765 @Test 766 public void test_with_adjustment() { 767 final LocalTime sample = LocalTime.of(23, 5); 768 TemporalAdjuster adjuster = new TemporalAdjuster() { 769 @Override 770 public Temporal adjustInto(Temporal dateTime) { 771 return sample; 772 } 773 }; 774 assertEquals(TEST_12_30_40_987654321.with(adjuster), sample); 775 } 776 777 @Test(expectedExceptions=NullPointerException.class) 778 public void test_with_adjustment_null() { 779 TEST_12_30_40_987654321.with((TemporalAdjuster) null); 780 } 781 782 //----------------------------------------------------------------------- 783 // with(TemporalField, long) 784 //----------------------------------------------------------------------- 785 private long[] testPoints(long max) { 786 long[] points = new long[9]; 787 points[0] = 0; 788 points[1] = 1; 789 points[2] = 2; 790 points[3] = max / 7; 791 points[4] = (max / 7) * 2; 792 points[5] = (max / 2); 793 points[6] = (max / 7) * 6;; 794 points[7] = max - 2; 795 points[8] = max - 1; 796 return points; 797 } 798 799 // Returns a {@code LocalTime} with the specified nano-of-second. 800 // The hour, minute and second will be unchanged. 801 @Test 802 public void test_with_longTemporalField_nanoOfSecond() { 803 for (long i : testPoints(1_000_000_000L)) { 804 LocalTime test = TEST_12_30_40_987654321.with(NANO_OF_SECOND, i); 805 assertEquals(test.get(NANO_OF_SECOND), i); 806 assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY)); 807 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 808 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 809 } 810 } 811 812 // Returns a {@code LocalTime} with the specified nano-of-day. 813 // This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}. 814 @Test 815 public void test_with_longTemporalField_nanoOfDay() { 816 for (long i : testPoints(86_400_000_000_000L)) { 817 LocalTime test = TEST_12_30_40_987654321.with(NANO_OF_DAY, i); 818 assertEquals(test, LocalTime.ofNanoOfDay(i)); 819 } 820 } 821 822 // Returns a {@code LocalTime} with the nano-of-second replaced by the specified 823 // micro-of-second multiplied by 1,000. 824 // The hour, minute and second will be unchanged. 825 @Test 826 public void test_with_longTemporalField_microOfSecond() { 827 for (long i : testPoints(1_000_000L)) { 828 LocalTime test = TEST_12_30_40_987654321.with(MICRO_OF_SECOND, i); 829 assertEquals(test.get(NANO_OF_SECOND), i * 1_000); 830 assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY)); 831 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 832 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 833 } 834 } 835 836 // Returns a {@code LocalTime} with the specified micro-of-day. 837 // This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)} 838 // with the micro-of-day multiplied by 1,000. 839 @Test 840 public void test_with_longTemporalField_microOfDay() { 841 for (long i : testPoints(86_400_000_000L)) { 842 LocalTime test = TEST_12_30_40_987654321.with(MICRO_OF_DAY, i); 843 assertEquals(test, LocalTime.ofNanoOfDay(i * 1000)); 844 } 845 } 846 847 // Returns a {@code LocalTime} with the nano-of-second replaced by the specified 848 // milli-of-second multiplied by 1,000,000. 849 // The hour, minute and second will be unchanged. 850 @Test 851 public void test_with_longTemporalField_milliOfSecond() { 852 for (long i : testPoints(1_000L)) { 853 LocalTime test = TEST_12_30_40_987654321.with(MILLI_OF_SECOND, i); 854 assertEquals(test.get(NANO_OF_SECOND), i * 1_000_000); 855 assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY)); 856 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 857 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 858 } 859 } 860 861 // Returns a {@code LocalTime} with the specified milli-of-day. 862 // This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)} 863 // with the milli-of-day multiplied by 1,000,000. 864 @Test 865 public void test_with_longTemporalField_milliOfDay() { 866 for (long i : testPoints(86_400_000L)) { 867 LocalTime test = TEST_12_30_40_987654321.with(MILLI_OF_DAY, i); 868 assertEquals(test, LocalTime.ofNanoOfDay(i * 1_000_000)); 869 } 870 } 871 872 // Returns a {@code LocalTime} with the specified second-of-minute. 873 // The hour, minute and nano-of-second will be unchanged. 874 @Test 875 public void test_with_longTemporalField_secondOfMinute() { 876 for (long i : testPoints(60L)) { 877 LocalTime test = TEST_12_30_40_987654321.with(SECOND_OF_MINUTE, i); 878 assertEquals(test.get(SECOND_OF_MINUTE), i); 879 assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY)); 880 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 881 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 882 } 883 } 884 885 // Returns a {@code LocalTime} with the specified second-of-day. 886 // The nano-of-second will be unchanged. 887 @Test 888 public void test_with_longTemporalField_secondOfDay() { 889 for (long i : testPoints(24 * 60 * 60)) { 890 LocalTime test = TEST_12_30_40_987654321.with(SECOND_OF_DAY, i); 891 assertEquals(test.get(SECOND_OF_DAY), i); 892 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 893 } 894 } 895 896 // Returns a {@code LocalTime} with the specified minute-of-hour. 897 // The hour, second-of-minute and nano-of-second will be unchanged. 898 @Test 899 public void test_with_longTemporalField_minuteOfHour() { 900 for (long i : testPoints(60)) { 901 LocalTime test = TEST_12_30_40_987654321.with(MINUTE_OF_HOUR, i); 902 assertEquals(test.get(MINUTE_OF_HOUR), i); 903 assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY)); 904 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 905 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 906 } 907 } 908 909 // Returns a {@code LocalTime} with the specified minute-of-day. 910 // The second-of-minute and nano-of-second will be unchanged. 911 @Test 912 public void test_with_longTemporalField_minuteOfDay() { 913 for (long i : testPoints(24 * 60)) { 914 LocalTime test = TEST_12_30_40_987654321.with(MINUTE_OF_DAY, i); 915 assertEquals(test.get(MINUTE_OF_DAY), i); 916 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 917 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 918 } 919 } 920 921 // Returns a {@code LocalTime} with the specified hour-of-am-pm. 922 // The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged. 923 @Test 924 public void test_with_longTemporalField_hourOfAmPm() { 925 for (int i = 0; i < 12; i++) { 926 LocalTime test = TEST_12_30_40_987654321.with(HOUR_OF_AMPM, i); 927 assertEquals(test.get(HOUR_OF_AMPM), i); 928 assertEquals(test.get(AMPM_OF_DAY), TEST_12_30_40_987654321.get(AMPM_OF_DAY)); 929 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 930 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 931 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 932 } 933 } 934 935 // Returns a {@code LocalTime} with the specified clock-hour-of-am-pm. 936 // The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged. 937 @Test 938 public void test_with_longTemporalField_clockHourOfAmPm() { 939 for (int i = 1; i <= 12; i++) { 940 LocalTime test = TEST_12_30_40_987654321.with(CLOCK_HOUR_OF_AMPM, i); 941 assertEquals(test.get(CLOCK_HOUR_OF_AMPM), i); 942 assertEquals(test.get(AMPM_OF_DAY), TEST_12_30_40_987654321.get(AMPM_OF_DAY)); 943 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 944 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 945 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 946 } 947 } 948 949 // Returns a {@code LocalTime} with the specified hour-of-day. 950 // The minute-of-hour, second-of-minute and nano-of-second will be unchanged. 951 @Test 952 public void test_with_longTemporalField_hourOfDay() { 953 for (int i = 0; i < 24; i++) { 954 LocalTime test = TEST_12_30_40_987654321.with(HOUR_OF_DAY, i); 955 assertEquals(test.get(HOUR_OF_DAY), i); 956 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 957 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 958 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 959 } 960 } 961 962 // Returns a {@code LocalTime} with the specified clock-hour-of-day. 963 // The minute-of-hour, second-of-minute and nano-of-second will be unchanged. 964 @Test 965 public void test_with_longTemporalField_clockHourOfDay() { 966 for (int i = 1; i <= 24; i++) { 967 LocalTime test = TEST_12_30_40_987654321.with(CLOCK_HOUR_OF_DAY, i); 968 assertEquals(test.get(CLOCK_HOUR_OF_DAY), i); 969 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 970 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 971 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 972 } 973 } 974 975 // Returns a {@code LocalTime} with the specified AM/PM. 976 // The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second will be unchanged. 977 @Test 978 public void test_with_longTemporalField_amPmOfDay() { 979 for (int i = 0; i <= 1; i++) { 980 LocalTime test = TEST_12_30_40_987654321.with(AMPM_OF_DAY, i); 981 assertEquals(test.get(AMPM_OF_DAY), i); 982 assertEquals(test.get(HOUR_OF_AMPM), TEST_12_30_40_987654321.get(HOUR_OF_AMPM)); 983 assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR)); 984 assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE)); 985 assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND)); 986 } 987 } 988 989 // The supported fields behave as follows... 990 // In all cases, if the new value is outside the valid range of values for the field 991 // then a {@code DateTimeException} will be thrown. 992 @DataProvider(name = "withTemporalField_outOfRange") 993 Object[][] data_withTemporalField_outOfRange() { 994 return new Object[][] { 995 {NANO_OF_SECOND, time(0, 0, 0, 0), NANO_OF_SECOND.range().getMinimum() - 1}, 996 {NANO_OF_SECOND, time(0, 0, 0, 0), NANO_OF_SECOND.range().getMaximum() + 1}, 997 998 {NANO_OF_DAY, time(0, 0, 0, 0), NANO_OF_DAY.range().getMinimum() - 1}, 999 {NANO_OF_DAY, time(0, 0, 0, 0), NANO_OF_DAY.range().getMaximum() + 1}, 1000 1001 {MICRO_OF_SECOND, time(0, 0, 0, 0), MICRO_OF_SECOND.range().getMinimum() - 1}, 1002 {MICRO_OF_SECOND, time(0, 0, 0, 0), MICRO_OF_SECOND.range().getMaximum() + 1}, 1003 1004 {MICRO_OF_DAY, time(0, 0, 0, 0), MICRO_OF_DAY.range().getMinimum() - 1}, 1005 {MICRO_OF_DAY, time(0, 0, 0, 0), MICRO_OF_DAY.range().getMaximum() + 1}, 1006 1007 {MILLI_OF_SECOND, time(0, 0, 0, 0), MILLI_OF_SECOND.range().getMinimum() - 1}, 1008 {MILLI_OF_SECOND, time(0, 0, 0, 0), MILLI_OF_SECOND.range().getMaximum() + 1}, 1009 1010 {MILLI_OF_DAY, time(0, 0, 0, 0), MILLI_OF_DAY.range().getMinimum() - 1}, 1011 {MILLI_OF_DAY, time(0, 0, 0, 0), MILLI_OF_DAY.range().getMaximum() + 1}, 1012 1013 {SECOND_OF_MINUTE, time(0, 0, 0, 0), SECOND_OF_MINUTE.range().getMinimum() - 1}, 1014 {SECOND_OF_MINUTE, time(0, 0, 0, 0), SECOND_OF_MINUTE.range().getMaximum() + 1}, 1015 1016 {SECOND_OF_DAY, time(0, 0, 0, 0), SECOND_OF_DAY.range().getMinimum() - 1}, 1017 {SECOND_OF_DAY, time(0, 0, 0, 0), SECOND_OF_DAY.range().getMaximum() + 1}, 1018 1019 {MINUTE_OF_HOUR, time(0, 0, 0, 0), MINUTE_OF_HOUR.range().getMinimum() - 1}, 1020 {MINUTE_OF_HOUR, time(0, 0, 0, 0), MINUTE_OF_HOUR.range().getMaximum() + 1}, 1021 1022 {MINUTE_OF_DAY, time(0, 0, 0, 0), MINUTE_OF_DAY.range().getMinimum() - 1}, 1023 {MINUTE_OF_DAY, time(0, 0, 0, 0), MINUTE_OF_DAY.range().getMaximum() + 1}, 1024 1025 {HOUR_OF_AMPM, time(0, 0, 0, 0), HOUR_OF_AMPM.range().getMinimum() - 1}, 1026 {HOUR_OF_AMPM, time(0, 0, 0, 0), HOUR_OF_AMPM.range().getMaximum() + 1}, 1027 1028 {CLOCK_HOUR_OF_AMPM, time(0, 0, 0, 0), CLOCK_HOUR_OF_AMPM.range().getMinimum() - 1}, 1029 {CLOCK_HOUR_OF_AMPM, time(0, 0, 0, 0), CLOCK_HOUR_OF_AMPM.range().getMaximum() + 1}, 1030 1031 {HOUR_OF_DAY, time(0, 0, 0, 0), HOUR_OF_DAY.range().getMinimum() - 1}, 1032 {HOUR_OF_DAY, time(0, 0, 0, 0), HOUR_OF_DAY.range().getMaximum() + 1}, 1033 1034 {CLOCK_HOUR_OF_DAY, time(0, 0, 0, 0), CLOCK_HOUR_OF_DAY.range().getMinimum() - 1}, 1035 {CLOCK_HOUR_OF_DAY, time(0, 0, 0, 0), CLOCK_HOUR_OF_DAY.range().getMaximum() + 1}, 1036 1037 {AMPM_OF_DAY, time(0, 0, 0, 0), AMPM_OF_DAY.range().getMinimum() - 1}, 1038 {AMPM_OF_DAY, time(0, 0, 0, 0), AMPM_OF_DAY.range().getMaximum() + 1}, 1039 }; 1040 } 1041 1042 @Test(dataProvider = "withTemporalField_outOfRange") 1043 public void test_with_longTemporalField_invalid(TemporalField field, LocalTime base, long newValue) { 1044 try { 1045 base.with(field, newValue); 1046 fail("Field should not be allowed " + field); 1047 } catch (DateTimeException ex) { 1048 // expected 1049 } 1050 } 1051 1052 // All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. 1053 @Test(expectedExceptions=UnsupportedTemporalTypeException.class) 1054 public void test_with_longTemporalField_otherChronoField() { 1055 TEST_12_30_40_987654321.with(ChronoField.DAY_OF_MONTH, 1); 1056 } 1057 1058 // If the field is not a {@code ChronoField}, then the result of this method 1059 // is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} 1060 // passing {@code this} as the argument. 1061 @Test 1062 public void test_with_longTemporalField_notChronoField() { 1063 final LocalTime result = LocalTime.of(12, 30); 1064 final LocalTime base = LocalTime.of(15, 45); 1065 TemporalField field = new TemporalField() { 1066 public ValueRange rangeRefinedBy(TemporalAccessor temporal) { 1067 throw new UnsupportedOperationException(); 1068 } 1069 public ValueRange range() { 1070 return null; 1071 } 1072 public boolean isTimeBased() { 1073 throw new UnsupportedOperationException(); 1074 } 1075 public boolean isSupportedBy(TemporalAccessor temporal) { 1076 throw new UnsupportedOperationException(); 1077 } 1078 public boolean isDateBased() { 1079 throw new UnsupportedOperationException(); 1080 } 1081 public TemporalUnit getRangeUnit() { 1082 throw new UnsupportedOperationException(); 1083 } 1084 public long getFrom(TemporalAccessor temporal) { 1085 throw new UnsupportedOperationException(); 1086 } 1087 public TemporalUnit getBaseUnit() { 1088 throw new UnsupportedOperationException(); 1089 } 1090 public <R extends Temporal> R adjustInto(R temporal, long newValue) { 1091 assertEquals(temporal, base); 1092 assertEquals(newValue, 12L); 1093 @SuppressWarnings("unchecked") 1094 R r = (R) result; 1095 return r; 1096 } 1097 }; 1098 LocalTime test = base.with(field, 12L); 1099 assertSame(test, result); 1100 } 1101 1102 @Test(expectedExceptions=NullPointerException.class) 1103 public void test_with_longTemporalField_null() { 1104 TEST_12_30_40_987654321.with((TemporalField) null, 1); 1105 } 1106 1107 //----------------------------------------------------------------------- 1108 // withHour() 1109 //----------------------------------------------------------------------- 1110 @Test 1111 public void test_withHour_normal() { 1112 LocalTime t = TEST_12_30_40_987654321; 1113 for (int i = 0; i < 24; i++) { 1114 t = t.withHour(i); 1115 assertEquals(t.getHour(), i); 1116 } 1117 } 1118 1119 @Test 1120 public void test_withHour_noChange_equal() { 1121 LocalTime t = TEST_12_30_40_987654321.withHour(12); 1122 assertEquals(t, TEST_12_30_40_987654321); 1123 } 1124 1125 @Test 1126 public void test_withHour_toMidnight_equal() { 1127 LocalTime t = LocalTime.of(1, 0).withHour(0); 1128 assertEquals(t, LocalTime.MIDNIGHT); 1129 } 1130 1131 @Test 1132 public void test_withHour_toMidday_equal() { 1133 LocalTime t = LocalTime.of(1, 0).withHour(12); 1134 assertEquals(t, LocalTime.NOON); 1135 } 1136 1137 @Test(expectedExceptions=DateTimeException.class) 1138 public void test_withHour_hourTooLow() { 1139 TEST_12_30_40_987654321.withHour(-1); 1140 } 1141 1142 @Test(expectedExceptions=DateTimeException.class) 1143 public void test_withHour_hourTooHigh() { 1144 TEST_12_30_40_987654321.withHour(24); 1145 } 1146 1147 //----------------------------------------------------------------------- 1148 // withMinute() 1149 //----------------------------------------------------------------------- 1150 @Test 1151 public void test_withMinute_normal() { 1152 LocalTime t = TEST_12_30_40_987654321; 1153 for (int i = 0; i < 60; i++) { 1154 t = t.withMinute(i); 1155 assertEquals(t.getMinute(), i); 1156 } 1157 } 1158 1159 @Test 1160 public void test_withMinute_noChange_equal() { 1161 LocalTime t = TEST_12_30_40_987654321.withMinute(30); 1162 assertEquals(t, TEST_12_30_40_987654321); 1163 } 1164 1165 @Test 1166 public void test_withMinute_toMidnight_equal() { 1167 LocalTime t = LocalTime.of(0, 1).withMinute(0); 1168 assertEquals(t, LocalTime.MIDNIGHT); 1169 } 1170 1171 @Test 1172 public void test_withMinute_toMidday_equals() { 1173 LocalTime t = LocalTime.of(12, 1).withMinute(0); 1174 assertEquals(t, LocalTime.NOON); 1175 } 1176 1177 @Test(expectedExceptions=DateTimeException.class) 1178 public void test_withMinute_minuteTooLow() { 1179 TEST_12_30_40_987654321.withMinute(-1); 1180 } 1181 1182 @Test(expectedExceptions=DateTimeException.class) 1183 public void test_withMinute_minuteTooHigh() { 1184 TEST_12_30_40_987654321.withMinute(60); 1185 } 1186 1187 //----------------------------------------------------------------------- 1188 // withSecond() 1189 //----------------------------------------------------------------------- 1190 @Test 1191 public void test_withSecond_normal() { 1192 LocalTime t = TEST_12_30_40_987654321; 1193 for (int i = 0; i < 60; i++) { 1194 t = t.withSecond(i); 1195 assertEquals(t.getSecond(), i); 1196 } 1197 } 1198 1199 @Test 1200 public void test_withSecond_noChange_equal() { 1201 LocalTime t = TEST_12_30_40_987654321.withSecond(40); 1202 assertEquals(t, TEST_12_30_40_987654321); 1203 } 1204 1205 @Test 1206 public void test_withSecond_toMidnight_equal() { 1207 LocalTime t = LocalTime.of(0, 0, 1).withSecond(0); 1208 assertEquals(t, LocalTime.MIDNIGHT); 1209 } 1210 1211 @Test 1212 public void test_withSecond_toMidday_equal() { 1213 LocalTime t = LocalTime.of(12, 0, 1).withSecond(0); 1214 assertEquals(t, LocalTime.NOON); 1215 } 1216 1217 @Test(expectedExceptions=DateTimeException.class) 1218 public void test_withSecond_secondTooLow() { 1219 TEST_12_30_40_987654321.withSecond(-1); 1220 } 1221 1222 @Test(expectedExceptions=DateTimeException.class) 1223 public void test_withSecond_secondTooHigh() { 1224 TEST_12_30_40_987654321.withSecond(60); 1225 } 1226 1227 //----------------------------------------------------------------------- 1228 // withNano() 1229 //----------------------------------------------------------------------- 1230 @Test 1231 public void test_withNanoOfSecond_normal() { 1232 LocalTime t = TEST_12_30_40_987654321; 1233 t = t.withNano(1); 1234 assertEquals(t.getNano(), 1); 1235 t = t.withNano(10); 1236 assertEquals(t.getNano(), 10); 1237 t = t.withNano(100); 1238 assertEquals(t.getNano(), 100); 1239 t = t.withNano(999999999); 1240 assertEquals(t.getNano(), 999999999); 1241 } 1242 1243 @Test 1244 public void test_withNanoOfSecond_noChange_equal() { 1245 LocalTime t = TEST_12_30_40_987654321.withNano(987654321); 1246 assertEquals(t, TEST_12_30_40_987654321); 1247 } 1248 1249 @Test 1250 public void test_withNanoOfSecond_toMidnight_equal() { 1251 LocalTime t = LocalTime.of(0, 0, 0, 1).withNano(0); 1252 assertEquals(t, LocalTime.MIDNIGHT); 1253 } 1254 1255 @Test 1256 public void test_withNanoOfSecond_toMidday_equal() { 1257 LocalTime t = LocalTime.of(12, 0, 0, 1).withNano(0); 1258 assertEquals(t, LocalTime.NOON); 1259 } 1260 1261 @Test(expectedExceptions=DateTimeException.class) 1262 public void test_withNanoOfSecond_nanoTooLow() { 1263 TEST_12_30_40_987654321.withNano(-1); 1264 } 1265 1266 @Test(expectedExceptions=DateTimeException.class) 1267 public void test_withNanoOfSecond_nanoTooHigh() { 1268 TEST_12_30_40_987654321.withNano(1000000000); 1269 } 1270 1271 //----------------------------------------------------------------------- 1272 // truncated(TemporalUnit) 1273 //----------------------------------------------------------------------- 1274 TemporalUnit NINETY_MINS = new TemporalUnit() { 1275 @Override 1276 public Duration getDuration() { 1277 return Duration.ofMinutes(90); 1278 } 1279 @Override 1280 public boolean isDurationEstimated() { 1281 return false; 1282 } 1283 @Override 1284 public boolean isDateBased() { 1285 return false; 1286 } 1287 @Override 1288 public boolean isTimeBased() { 1289 return true; 1290 } 1291 @Override 1292 public boolean isSupportedBy(Temporal temporal) { 1293 return false; 1294 } 1295 @Override 1296 public <R extends Temporal> R addTo(R temporal, long amount) { 1297 throw new UnsupportedOperationException(); 1298 } 1299 @Override 1300 public long between(Temporal temporal1, Temporal temporal2) { 1301 throw new UnsupportedOperationException(); 1302 } 1303 @Override 1304 public String toString() { 1305 return "NinetyMins"; 1306 } 1307 }; 1308 1309 TemporalUnit NINETY_FIVE_MINS = new TemporalUnit() { 1310 @Override 1311 public Duration getDuration() { 1312 return Duration.ofMinutes(95); 1313 } 1314 @Override 1315 public boolean isDurationEstimated() { 1316 return false; 1317 } 1318 @Override 1319 public boolean isDateBased() { 1320 return false; 1321 } 1322 @Override 1323 public boolean isTimeBased() { 1324 return false; 1325 } 1326 @Override 1327 public boolean isSupportedBy(Temporal temporal) { 1328 return false; 1329 } 1330 @Override 1331 public <R extends Temporal> R addTo(R temporal, long amount) { 1332 throw new UnsupportedOperationException(); 1333 } 1334 @Override 1335 public long between(Temporal temporal1, Temporal temporal2) { 1336 throw new UnsupportedOperationException(); 1337 } 1338 @Override 1339 public String toString() { 1340 return "NinetyFiveMins"; 1341 } 1342 }; 1343 1344 @DataProvider(name="truncatedToValid") 1345 Object[][] data_truncatedToValid() { 1346 return new Object[][] { 1347 {LocalTime.of(1, 2, 3, 123_456_789), NANOS, LocalTime.of(1, 2, 3, 123_456_789)}, 1348 {LocalTime.of(1, 2, 3, 123_456_789), MICROS, LocalTime.of(1, 2, 3, 123_456_000)}, 1349 {LocalTime.of(1, 2, 3, 123_456_789), MILLIS, LocalTime.of(1, 2, 3, 1230_00_000)}, 1350 {LocalTime.of(1, 2, 3, 123_456_789), SECONDS, LocalTime.of(1, 2, 3)}, 1351 {LocalTime.of(1, 2, 3, 123_456_789), MINUTES, LocalTime.of(1, 2)}, 1352 {LocalTime.of(1, 2, 3, 123_456_789), HOURS, LocalTime.of(1, 0)}, 1353 {LocalTime.of(1, 2, 3, 123_456_789), DAYS, LocalTime.MIDNIGHT}, 1354 1355 {LocalTime.of(1, 1, 1, 123_456_789), NINETY_MINS, LocalTime.of(0, 0)}, 1356 {LocalTime.of(2, 1, 1, 123_456_789), NINETY_MINS, LocalTime.of(1, 30)}, 1357 {LocalTime.of(3, 1, 1, 123_456_789), NINETY_MINS, LocalTime.of(3, 0)}, 1358 }; 1359 } 1360 1361 @Test(dataProvider="truncatedToValid") 1362 public void test_truncatedTo_valid(LocalTime input, TemporalUnit unit, LocalTime expected) { 1363 assertEquals(input.truncatedTo(unit), expected); 1364 } 1365 1366 @DataProvider(name="truncatedToInvalid") 1367 Object[][] data_truncatedToInvalid() { 1368 return new Object[][] { 1369 {LocalTime.of(1, 2, 3, 123_456_789), NINETY_FIVE_MINS}, 1370 {LocalTime.of(1, 2, 3, 123_456_789), WEEKS}, 1371 {LocalTime.of(1, 2, 3, 123_456_789), MONTHS}, 1372 {LocalTime.of(1, 2, 3, 123_456_789), YEARS}, 1373 }; 1374 } 1375 1376 @Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class) 1377 public void test_truncatedTo_invalid(LocalTime input, TemporalUnit unit) { 1378 input.truncatedTo(unit); 1379 } 1380 1381 @Test(expectedExceptions=NullPointerException.class) 1382 public void test_truncatedTo_null() { 1383 TEST_12_30_40_987654321.truncatedTo(null); 1384 } 1385 1386 //----------------------------------------------------------------------- 1387 // plus(TemporalAmount) 1388 //----------------------------------------------------------------------- 1389 @Test 1390 public void test_plus_TemporalAmount_positiveHours() { 1391 TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.HOURS); 1392 LocalTime t = TEST_12_30_40_987654321.plus(period); 1393 assertEquals(t, LocalTime.of(19, 30, 40, 987654321)); 1394 } 1395 1396 @Test 1397 public void test_plus_TemporalAmount_negativeMinutes() { 1398 TemporalAmount period = MockSimplePeriod.of(-25, ChronoUnit.MINUTES); 1399 LocalTime t = TEST_12_30_40_987654321.plus(period); 1400 assertEquals(t, LocalTime.of(12, 5, 40, 987654321)); 1401 } 1402 1403 @Test 1404 public void test_plus_TemporalAmount_zero() { 1405 TemporalAmount period = Period.ZERO; 1406 LocalTime t = TEST_12_30_40_987654321.plus(period); 1407 assertEquals(t, TEST_12_30_40_987654321); 1408 } 1409 1410 @Test 1411 public void test_plus_TemporalAmount_wrap() { 1412 TemporalAmount p = MockSimplePeriod.of(1, HOURS); 1413 LocalTime t = LocalTime.of(23, 30).plus(p); 1414 assertEquals(t, LocalTime.of(0, 30)); 1415 } 1416 1417 @Test(expectedExceptions=DateTimeException.class) 1418 public void test_plus_TemporalAmount_dateNotAllowed() { 1419 TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1420 TEST_12_30_40_987654321.plus(period); 1421 } 1422 1423 @Test(expectedExceptions=NullPointerException.class) 1424 public void test_plus_TemporalAmount_null() { 1425 TEST_12_30_40_987654321.plus((TemporalAmount) null); 1426 } 1427 1428 //----------------------------------------------------------------------- 1429 // plus(long,TemporalUnit) 1430 //----------------------------------------------------------------------- 1431 @Test 1432 public void test_plus_longTemporalUnit_positiveHours() { 1433 LocalTime t = TEST_12_30_40_987654321.plus(7, ChronoUnit.HOURS); 1434 assertEquals(t, LocalTime.of(19, 30, 40, 987654321)); 1435 } 1436 1437 @Test 1438 public void test_plus_longTemporalUnit_negativeMinutes() { 1439 LocalTime t = TEST_12_30_40_987654321.plus(-25, ChronoUnit.MINUTES); 1440 assertEquals(t, LocalTime.of(12, 5, 40, 987654321)); 1441 } 1442 1443 @Test 1444 public void test_plus_longTemporalUnit_zero() { 1445 LocalTime t = TEST_12_30_40_987654321.plus(0, ChronoUnit.MINUTES); 1446 assertEquals(t, TEST_12_30_40_987654321); 1447 } 1448 1449 @Test 1450 public void test_plus_longTemporalUnit_invalidUnit() { 1451 for (TemporalUnit unit : INVALID_UNITS) { 1452 try { 1453 TEST_12_30_40_987654321.plus(1, unit); 1454 fail("Unit should not be allowed " + unit); 1455 } catch (DateTimeException ex) { 1456 // expected 1457 } 1458 } 1459 } 1460 1461 @Test(expectedExceptions=NullPointerException.class) 1462 public void test_plus_longTemporalUnit_null() { 1463 TEST_12_30_40_987654321.plus(1, (TemporalUnit) null); 1464 } 1465 1466 //----------------------------------------------------------------------- 1467 // plusHours() 1468 //----------------------------------------------------------------------- 1469 @Test 1470 public void test_plusHours_one() { 1471 LocalTime t = LocalTime.MIDNIGHT; 1472 for (int i = 0; i < 50; i++) { 1473 t = t.plusHours(1); 1474 assertEquals(t.getHour(), (i + 1) % 24); 1475 } 1476 } 1477 1478 @Test 1479 public void test_plusHours_fromZero() { 1480 LocalTime base = LocalTime.MIDNIGHT; 1481 for (int i = -50; i < 50; i++) { 1482 LocalTime t = base.plusHours(i); 1483 assertEquals(t.getHour(), (i + 72) % 24); 1484 } 1485 } 1486 1487 @Test 1488 public void test_plusHours_fromOne() { 1489 LocalTime base = LocalTime.of(1, 0); 1490 for (int i = -50; i < 50; i++) { 1491 LocalTime t = base.plusHours(i); 1492 assertEquals(t.getHour(), (1 + i + 72) % 24); 1493 } 1494 } 1495 1496 @Test 1497 public void test_plusHours_noChange_equal() { 1498 LocalTime t = TEST_12_30_40_987654321.plusHours(0); 1499 assertEquals(t, TEST_12_30_40_987654321); 1500 } 1501 1502 @Test 1503 public void test_plusHours_toMidnight_equal() { 1504 LocalTime t = LocalTime.of(23, 0).plusHours(1); 1505 assertEquals(t, LocalTime.MIDNIGHT); 1506 } 1507 1508 @Test 1509 public void test_plusHours_toMidday_equal() { 1510 LocalTime t = LocalTime.of(11, 0).plusHours(1); 1511 assertEquals(t, LocalTime.NOON); 1512 } 1513 1514 @Test 1515 public void test_plusHours_big() { 1516 LocalTime t = LocalTime.of(2, 30).plusHours(Long.MAX_VALUE); 1517 int hours = (int) (Long.MAX_VALUE % 24L); 1518 assertEquals(t, LocalTime.of(2, 30).plusHours(hours)); 1519 } 1520 1521 //----------------------------------------------------------------------- 1522 // plusMinutes() 1523 //----------------------------------------------------------------------- 1524 @Test 1525 public void test_plusMinutes_one() { 1526 LocalTime t = LocalTime.MIDNIGHT; 1527 int hour = 0; 1528 int min = 0; 1529 for (int i = 0; i < 70; i++) { 1530 t = t.plusMinutes(1); 1531 min++; 1532 if (min == 60) { 1533 hour++; 1534 min = 0; 1535 } 1536 assertEquals(t.getHour(), hour); 1537 assertEquals(t.getMinute(), min); 1538 } 1539 } 1540 1541 @Test 1542 public void test_plusMinutes_fromZero() { 1543 LocalTime base = LocalTime.MIDNIGHT; 1544 int hour; 1545 int min; 1546 for (int i = -70; i < 70; i++) { 1547 LocalTime t = base.plusMinutes(i); 1548 if (i < -60) { 1549 hour = 22; 1550 min = i + 120; 1551 } else if (i < 0) { 1552 hour = 23; 1553 min = i + 60; 1554 } else if (i >= 60) { 1555 hour = 1; 1556 min = i - 60; 1557 } else { 1558 hour = 0; 1559 min = i; 1560 } 1561 assertEquals(t.getHour(), hour); 1562 assertEquals(t.getMinute(), min); 1563 } 1564 } 1565 1566 @Test 1567 public void test_plusMinutes_noChange_equal() { 1568 LocalTime t = TEST_12_30_40_987654321.plusMinutes(0); 1569 assertEquals(t, TEST_12_30_40_987654321); 1570 } 1571 1572 @Test 1573 public void test_plusMinutes_noChange_oneDay_equal() { 1574 LocalTime t = TEST_12_30_40_987654321.plusMinutes(24 * 60); 1575 assertEquals(t, TEST_12_30_40_987654321); 1576 } 1577 1578 @Test 1579 public void test_plusMinutes_toMidnight_equal() { 1580 LocalTime t = LocalTime.of(23, 59).plusMinutes(1); 1581 assertEquals(t, LocalTime.MIDNIGHT); 1582 } 1583 1584 @Test 1585 public void test_plusMinutes_toMidday_equal() { 1586 LocalTime t = LocalTime.of(11, 59).plusMinutes(1); 1587 assertEquals(t, LocalTime.NOON); 1588 } 1589 1590 @Test 1591 public void test_plusMinutes_big() { 1592 LocalTime t = LocalTime.of(2, 30).plusMinutes(Long.MAX_VALUE); 1593 int mins = (int) (Long.MAX_VALUE % (24L * 60L)); 1594 assertEquals(t, LocalTime.of(2, 30).plusMinutes(mins)); 1595 } 1596 1597 //----------------------------------------------------------------------- 1598 // plusSeconds() 1599 //----------------------------------------------------------------------- 1600 @Test 1601 public void test_plusSeconds_one() { 1602 LocalTime t = LocalTime.MIDNIGHT; 1603 int hour = 0; 1604 int min = 0; 1605 int sec = 0; 1606 for (int i = 0; i < 3700; i++) { 1607 t = t.plusSeconds(1); 1608 sec++; 1609 if (sec == 60) { 1610 min++; 1611 sec = 0; 1612 } 1613 if (min == 60) { 1614 hour++; 1615 min = 0; 1616 } 1617 assertEquals(t.getHour(), hour); 1618 assertEquals(t.getMinute(), min); 1619 assertEquals(t.getSecond(), sec); 1620 } 1621 } 1622 1623 @DataProvider(name="plusSeconds_fromZero") 1624 Iterator<Object[]> plusSeconds_fromZero() { 1625 return new Iterator<Object[]>() { 1626 int delta = 30; 1627 int i = -3660; 1628 int hour = 22; 1629 int min = 59; 1630 int sec = 0; 1631 1632 public boolean hasNext() { 1633 return i <= 3660; 1634 } 1635 1636 public Object[] next() { 1637 final Object[] ret = new Object[] {i, hour, min, sec}; 1638 i += delta; 1639 sec += delta; 1640 1641 if (sec >= 60) { 1642 min++; 1643 sec -= 60; 1644 1645 if (min == 60) { 1646 hour++; 1647 min = 0; 1648 1649 if (hour == 24) { 1650 hour = 0; 1651 } 1652 } 1653 } 1654 1655 return ret; 1656 } 1657 1658 public void remove() { 1659 throw new UnsupportedOperationException(); 1660 } 1661 }; 1662 } 1663 1664 @Test(dataProvider="plusSeconds_fromZero") 1665 public void test_plusSeconds_fromZero(int seconds, int hour, int min, int sec) { 1666 LocalTime base = LocalTime.MIDNIGHT; 1667 LocalTime t = base.plusSeconds(seconds); 1668 1669 assertEquals(hour, t.getHour()); 1670 assertEquals(min, t.getMinute()); 1671 assertEquals(sec, t.getSecond()); 1672 } 1673 1674 @Test 1675 public void test_plusSeconds_noChange_equal() { 1676 LocalTime t = TEST_12_30_40_987654321.plusSeconds(0); 1677 assertEquals(t, TEST_12_30_40_987654321); 1678 } 1679 1680 @Test 1681 public void test_plusSeconds_noChange_oneDay_equal() { 1682 LocalTime t = TEST_12_30_40_987654321.plusSeconds(24 * 60 * 60); 1683 assertEquals(t, TEST_12_30_40_987654321); 1684 } 1685 1686 @Test 1687 public void test_plusSeconds_toMidnight_equal() { 1688 LocalTime t = LocalTime.of(23, 59, 59).plusSeconds(1); 1689 assertEquals(t, LocalTime.MIDNIGHT); 1690 } 1691 1692 @Test 1693 public void test_plusSeconds_toMidday_equal() { 1694 LocalTime t = LocalTime.of(11, 59, 59).plusSeconds(1); 1695 assertEquals(t, LocalTime.NOON); 1696 } 1697 1698 //----------------------------------------------------------------------- 1699 // plusNanos() 1700 //----------------------------------------------------------------------- 1701 @Test 1702 public void test_plusNanos_halfABillion() { 1703 LocalTime t = LocalTime.MIDNIGHT; 1704 int hour = 0; 1705 int min = 0; 1706 int sec = 0; 1707 int nanos = 0; 1708 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) { 1709 t = t.plusNanos(500000000); 1710 nanos += 500000000; 1711 if (nanos == 1000000000) { 1712 sec++; 1713 nanos = 0; 1714 } 1715 if (sec == 60) { 1716 min++; 1717 sec = 0; 1718 } 1719 if (min == 60) { 1720 hour++; 1721 min = 0; 1722 } 1723 assertEquals(t.getHour(), hour); 1724 assertEquals(t.getMinute(), min); 1725 assertEquals(t.getSecond(), sec); 1726 assertEquals(t.getNano(), nanos); 1727 } 1728 } 1729 1730 @DataProvider(name="plusNanos_fromZero") 1731 Iterator<Object[]> plusNanos_fromZero() { 1732 return new Iterator<Object[]>() { 1733 long delta = 7500000000L; 1734 long i = -3660 * 1000000000L; 1735 int hour = 22; 1736 int min = 59; 1737 int sec = 0; 1738 long nanos = 0; 1739 1740 public boolean hasNext() { 1741 return i <= 3660 * 1000000000L; 1742 } 1743 1744 public Object[] next() { 1745 final Object[] ret = new Object[] {i, hour, min, sec, (int)nanos}; 1746 i += delta; 1747 nanos += delta; 1748 1749 if (nanos >= 1000000000L) { 1750 sec += nanos / 1000000000L; 1751 nanos %= 1000000000L; 1752 1753 if (sec >= 60) { 1754 min++; 1755 sec %= 60; 1756 1757 if (min == 60) { 1758 hour++; 1759 min = 0; 1760 1761 if (hour == 24) { 1762 hour = 0; 1763 } 1764 } 1765 } 1766 } 1767 1768 return ret; 1769 } 1770 1771 public void remove() { 1772 throw new UnsupportedOperationException(); 1773 } 1774 }; 1775 } 1776 1777 @Test(dataProvider="plusNanos_fromZero") 1778 public void test_plusNanos_fromZero(long nanoseconds, int hour, int min, int sec, int nanos) { 1779 LocalTime base = LocalTime.MIDNIGHT; 1780 LocalTime t = base.plusNanos(nanoseconds); 1781 1782 assertEquals(hour, t.getHour()); 1783 assertEquals(min, t.getMinute()); 1784 assertEquals(sec, t.getSecond()); 1785 assertEquals(nanos, t.getNano()); 1786 } 1787 1788 @Test 1789 public void test_plusNanos_noChange_equal() { 1790 LocalTime t = TEST_12_30_40_987654321.plusNanos(0); 1791 assertEquals(t, TEST_12_30_40_987654321); 1792 } 1793 1794 @Test 1795 public void test_plusNanos_noChange_oneDay_equal() { 1796 LocalTime t = TEST_12_30_40_987654321.plusNanos(24 * 60 * 60 * 1000000000L); 1797 assertEquals(t, TEST_12_30_40_987654321); 1798 } 1799 1800 @Test 1801 public void test_plusNanos_toMidnight_equal() { 1802 LocalTime t = LocalTime.of(23, 59, 59, 999999999).plusNanos(1); 1803 assertEquals(t, LocalTime.MIDNIGHT); 1804 } 1805 1806 @Test 1807 public void test_plusNanos_toMidday_equal() { 1808 LocalTime t = LocalTime.of(11, 59, 59, 999999999).plusNanos(1); 1809 assertEquals(t, LocalTime.NOON); 1810 } 1811 1812 //----------------------------------------------------------------------- 1813 // minus(TemporalAmount) 1814 //----------------------------------------------------------------------- 1815 @Test 1816 public void test_minus_TemporalAmount_positiveHours() { 1817 TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.HOURS); 1818 LocalTime t = TEST_12_30_40_987654321.minus(period); 1819 assertEquals(t, LocalTime.of(5, 30, 40, 987654321)); 1820 } 1821 1822 @Test 1823 public void test_minus_TemporalAmount_negativeMinutes() { 1824 TemporalAmount period = MockSimplePeriod.of(-25, ChronoUnit.MINUTES); 1825 LocalTime t = TEST_12_30_40_987654321.minus(period); 1826 assertEquals(t, LocalTime.of(12, 55, 40, 987654321)); 1827 } 1828 1829 @Test 1830 public void test_minus_TemporalAmount_zero() { 1831 TemporalAmount period = Period.ZERO; 1832 LocalTime t = TEST_12_30_40_987654321.minus(period); 1833 assertEquals(t, TEST_12_30_40_987654321); 1834 } 1835 1836 @Test 1837 public void test_minus_TemporalAmount_wrap() { 1838 TemporalAmount p = MockSimplePeriod.of(1, HOURS); 1839 LocalTime t = LocalTime.of(0, 30).minus(p); 1840 assertEquals(t, LocalTime.of(23, 30)); 1841 } 1842 1843 @Test(expectedExceptions=DateTimeException.class) 1844 public void test_minus_TemporalAmount_dateNotAllowed() { 1845 TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1846 TEST_12_30_40_987654321.minus(period); 1847 } 1848 1849 @Test(expectedExceptions=NullPointerException.class) 1850 public void test_minus_TemporalAmount_null() { 1851 TEST_12_30_40_987654321.minus((TemporalAmount) null); 1852 } 1853 1854 //----------------------------------------------------------------------- 1855 // minus(long,TemporalUnit) 1856 //----------------------------------------------------------------------- 1857 @Test 1858 public void test_minus_longTemporalUnit_positiveHours() { 1859 LocalTime t = TEST_12_30_40_987654321.minus(7, ChronoUnit.HOURS); 1860 assertEquals(t, LocalTime.of(5, 30, 40, 987654321)); 1861 } 1862 1863 @Test 1864 public void test_minus_longTemporalUnit_negativeMinutes() { 1865 LocalTime t = TEST_12_30_40_987654321.minus(-25, ChronoUnit.MINUTES); 1866 assertEquals(t, LocalTime.of(12, 55, 40, 987654321)); 1867 } 1868 1869 @Test 1870 public void test_minus_longTemporalUnit_zero() { 1871 LocalTime t = TEST_12_30_40_987654321.minus(0, ChronoUnit.MINUTES); 1872 assertEquals(t, TEST_12_30_40_987654321); 1873 } 1874 1875 @Test 1876 public void test_minus_longTemporalUnit_invalidUnit() { 1877 for (TemporalUnit unit : INVALID_UNITS) { 1878 try { 1879 TEST_12_30_40_987654321.minus(1, unit); 1880 fail("Unit should not be allowed " + unit); 1881 } catch (DateTimeException ex) { 1882 // expected 1883 } 1884 } 1885 } 1886 1887 @Test(expectedExceptions=NullPointerException.class) 1888 public void test_minus_longTemporalUnit_null() { 1889 TEST_12_30_40_987654321.minus(1, (TemporalUnit) null); 1890 } 1891 1892 //----------------------------------------------------------------------- 1893 // minusHours() 1894 //----------------------------------------------------------------------- 1895 @Test 1896 public void test_minusHours_one() { 1897 LocalTime t = LocalTime.MIDNIGHT; 1898 for (int i = 0; i < 50; i++) { 1899 t = t.minusHours(1); 1900 assertEquals(t.getHour(), (((-i + 23) % 24) + 24) % 24, String.valueOf(i)); 1901 } 1902 } 1903 1904 @Test 1905 public void test_minusHours_fromZero() { 1906 LocalTime base = LocalTime.MIDNIGHT; 1907 for (int i = -50; i < 50; i++) { 1908 LocalTime t = base.minusHours(i); 1909 assertEquals(t.getHour(), ((-i % 24) + 24) % 24); 1910 } 1911 } 1912 1913 @Test 1914 public void test_minusHours_fromOne() { 1915 LocalTime base = LocalTime.of(1, 0); 1916 for (int i = -50; i < 50; i++) { 1917 LocalTime t = base.minusHours(i); 1918 assertEquals(t.getHour(), (1 + (-i % 24) + 24) % 24); 1919 } 1920 } 1921 1922 @Test 1923 public void test_minusHours_noChange_equal() { 1924 LocalTime t = TEST_12_30_40_987654321.minusHours(0); 1925 assertEquals(t, TEST_12_30_40_987654321); 1926 } 1927 1928 @Test 1929 public void test_minusHours_toMidnight_equal() { 1930 LocalTime t = LocalTime.of(1, 0).minusHours(1); 1931 assertEquals(t, LocalTime.MIDNIGHT); 1932 } 1933 1934 @Test 1935 public void test_minusHours_toMidday_equal() { 1936 LocalTime t = LocalTime.of(13, 0).minusHours(1); 1937 assertEquals(t, LocalTime.NOON); 1938 } 1939 1940 @Test 1941 public void test_minusHours_big() { 1942 LocalTime t = LocalTime.of(2, 30).minusHours(Long.MAX_VALUE); 1943 int hours = (int) (Long.MAX_VALUE % 24L); 1944 assertEquals(t, LocalTime.of(2, 30).minusHours(hours)); 1945 } 1946 1947 //----------------------------------------------------------------------- 1948 // minusMinutes() 1949 //----------------------------------------------------------------------- 1950 @Test 1951 public void test_minusMinutes_one() { 1952 LocalTime t = LocalTime.MIDNIGHT; 1953 int hour = 0; 1954 int min = 0; 1955 for (int i = 0; i < 70; i++) { 1956 t = t.minusMinutes(1); 1957 min--; 1958 if (min == -1) { 1959 hour--; 1960 min = 59; 1961 1962 if (hour == -1) { 1963 hour = 23; 1964 } 1965 } 1966 assertEquals(t.getHour(), hour); 1967 assertEquals(t.getMinute(), min); 1968 } 1969 } 1970 1971 @Test 1972 public void test_minusMinutes_fromZero() { 1973 LocalTime base = LocalTime.MIDNIGHT; 1974 int hour = 22; 1975 int min = 49; 1976 for (int i = 70; i > -70; i--) { 1977 LocalTime t = base.minusMinutes(i); 1978 min++; 1979 1980 if (min == 60) { 1981 hour++; 1982 min = 0; 1983 1984 if (hour == 24) { 1985 hour = 0; 1986 } 1987 } 1988 1989 assertEquals(t.getHour(), hour); 1990 assertEquals(t.getMinute(), min); 1991 } 1992 } 1993 1994 @Test 1995 public void test_minusMinutes_noChange_equal() { 1996 LocalTime t = TEST_12_30_40_987654321.minusMinutes(0); 1997 assertEquals(t, TEST_12_30_40_987654321); 1998 } 1999 2000 @Test 2001 public void test_minusMinutes_noChange_oneDay_equal() { 2002 LocalTime t = TEST_12_30_40_987654321.minusMinutes(24 * 60); 2003 assertEquals(t, TEST_12_30_40_987654321); 2004 } 2005 2006 @Test 2007 public void test_minusMinutes_toMidnight_equal() { 2008 LocalTime t = LocalTime.of(0, 1).minusMinutes(1); 2009 assertEquals(t, LocalTime.MIDNIGHT); 2010 } 2011 2012 @Test 2013 public void test_minusMinutes_toMidday_equals() { 2014 LocalTime t = LocalTime.of(12, 1).minusMinutes(1); 2015 assertEquals(t, LocalTime.NOON); 2016 } 2017 2018 @Test 2019 public void test_minusMinutes_big() { 2020 LocalTime t = LocalTime.of(2, 30).minusMinutes(Long.MAX_VALUE); 2021 int mins = (int) (Long.MAX_VALUE % (24L * 60L)); 2022 assertEquals(t, LocalTime.of(2, 30).minusMinutes(mins)); 2023 } 2024 2025 //----------------------------------------------------------------------- 2026 // minusSeconds() 2027 //----------------------------------------------------------------------- 2028 @Test 2029 public void test_minusSeconds_one() { 2030 LocalTime t = LocalTime.MIDNIGHT; 2031 int hour = 0; 2032 int min = 0; 2033 int sec = 0; 2034 for (int i = 0; i < 3700; i++) { 2035 t = t.minusSeconds(1); 2036 sec--; 2037 if (sec == -1) { 2038 min--; 2039 sec = 59; 2040 2041 if (min == -1) { 2042 hour--; 2043 min = 59; 2044 2045 if (hour == -1) { 2046 hour = 23; 2047 } 2048 } 2049 } 2050 assertEquals(t.getHour(), hour); 2051 assertEquals(t.getMinute(), min); 2052 assertEquals(t.getSecond(), sec); 2053 } 2054 } 2055 2056 @DataProvider(name="minusSeconds_fromZero") 2057 Iterator<Object[]> minusSeconds_fromZero() { 2058 return new Iterator<Object[]>() { 2059 int delta = 30; 2060 int i = 3660; 2061 int hour = 22; 2062 int min = 59; 2063 int sec = 0; 2064 2065 public boolean hasNext() { 2066 return i >= -3660; 2067 } 2068 2069 public Object[] next() { 2070 final Object[] ret = new Object[] {i, hour, min, sec}; 2071 i -= delta; 2072 sec += delta; 2073 2074 if (sec >= 60) { 2075 min++; 2076 sec -= 60; 2077 2078 if (min == 60) { 2079 hour++; 2080 min = 0; 2081 2082 if (hour == 24) { 2083 hour = 0; 2084 } 2085 } 2086 } 2087 2088 return ret; 2089 } 2090 2091 public void remove() { 2092 throw new UnsupportedOperationException(); 2093 } 2094 }; 2095 } 2096 2097 @Test(dataProvider="minusSeconds_fromZero") 2098 public void test_minusSeconds_fromZero(int seconds, int hour, int min, int sec) { 2099 LocalTime base = LocalTime.MIDNIGHT; 2100 LocalTime t = base.minusSeconds(seconds); 2101 2102 assertEquals(t.getHour(), hour); 2103 assertEquals(t.getMinute(), min); 2104 assertEquals(t.getSecond(), sec); 2105 } 2106 2107 @Test 2108 public void test_minusSeconds_noChange_equal() { 2109 LocalTime t = TEST_12_30_40_987654321.minusSeconds(0); 2110 assertEquals(t, TEST_12_30_40_987654321); 2111 } 2112 2113 @Test 2114 public void test_minusSeconds_noChange_oneDay_equal() { 2115 LocalTime t = TEST_12_30_40_987654321.minusSeconds(24 * 60 * 60); 2116 assertEquals(t, TEST_12_30_40_987654321); 2117 } 2118 2119 @Test 2120 public void test_minusSeconds_toMidnight_equal() { 2121 LocalTime t = LocalTime.of(0, 0, 1).minusSeconds(1); 2122 assertEquals(t, LocalTime.MIDNIGHT); 2123 } 2124 2125 @Test 2126 public void test_minusSeconds_toMidday_equal() { 2127 LocalTime t = LocalTime.of(12, 0, 1).minusSeconds(1); 2128 assertEquals(t, LocalTime.NOON); 2129 } 2130 2131 @Test 2132 public void test_minusSeconds_big() { 2133 LocalTime t = LocalTime.of(2, 30).minusSeconds(Long.MAX_VALUE); 2134 int secs = (int) (Long.MAX_VALUE % (24L * 60L * 60L)); 2135 assertEquals(t, LocalTime.of(2, 30).minusSeconds(secs)); 2136 } 2137 2138 //----------------------------------------------------------------------- 2139 // minusNanos() 2140 //----------------------------------------------------------------------- 2141 @Test 2142 public void test_minusNanos_halfABillion() { 2143 LocalTime t = LocalTime.MIDNIGHT; 2144 int hour = 0; 2145 int min = 0; 2146 int sec = 0; 2147 int nanos = 0; 2148 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) { 2149 t = t.minusNanos(500000000); 2150 nanos -= 500000000; 2151 2152 if (nanos < 0) { 2153 sec--; 2154 nanos += 1000000000; 2155 2156 if (sec == -1) { 2157 min--; 2158 sec += 60; 2159 2160 if (min == -1) { 2161 hour--; 2162 min += 60; 2163 2164 if (hour == -1) { 2165 hour += 24; 2166 } 2167 } 2168 } 2169 } 2170 2171 assertEquals(t.getHour(), hour); 2172 assertEquals(t.getMinute(), min); 2173 assertEquals(t.getSecond(), sec); 2174 assertEquals(t.getNano(), nanos); 2175 } 2176 } 2177 2178 @DataProvider(name="minusNanos_fromZero") 2179 Iterator<Object[]> minusNanos_fromZero() { 2180 return new Iterator<Object[]>() { 2181 long delta = 7500000000L; 2182 long i = 3660 * 1000000000L; 2183 int hour = 22; 2184 int min = 59; 2185 int sec = 0; 2186 long nanos = 0; 2187 2188 public boolean hasNext() { 2189 return i >= -3660 * 1000000000L; 2190 } 2191 2192 public Object[] next() { 2193 final Object[] ret = new Object[] {i, hour, min, sec, (int)nanos}; 2194 i -= delta; 2195 nanos += delta; 2196 2197 if (nanos >= 1000000000L) { 2198 sec += nanos / 1000000000L; 2199 nanos %= 1000000000L; 2200 2201 if (sec >= 60) { 2202 min++; 2203 sec %= 60; 2204 2205 if (min == 60) { 2206 hour++; 2207 min = 0; 2208 2209 if (hour == 24) { 2210 hour = 0; 2211 } 2212 } 2213 } 2214 } 2215 2216 return ret; 2217 } 2218 2219 public void remove() { 2220 throw new UnsupportedOperationException(); 2221 } 2222 }; 2223 } 2224 2225 @Test(dataProvider="minusNanos_fromZero") 2226 public void test_minusNanos_fromZero(long nanoseconds, int hour, int min, int sec, int nanos) { 2227 LocalTime base = LocalTime.MIDNIGHT; 2228 LocalTime t = base.minusNanos(nanoseconds); 2229 2230 assertEquals(hour, t.getHour()); 2231 assertEquals(min, t.getMinute()); 2232 assertEquals(sec, t.getSecond()); 2233 assertEquals(nanos, t.getNano()); 2234 } 2235 2236 @Test 2237 public void test_minusNanos_noChange_equal() { 2238 LocalTime t = TEST_12_30_40_987654321.minusNanos(0); 2239 assertEquals(t, TEST_12_30_40_987654321); 2240 } 2241 2242 @Test 2243 public void test_minusNanos_noChange_oneDay_equal() { 2244 LocalTime t = TEST_12_30_40_987654321.minusNanos(24 * 60 * 60 * 1000000000L); 2245 assertEquals(t, TEST_12_30_40_987654321); 2246 } 2247 2248 @Test 2249 public void test_minusNanos_toMidnight_equal() { 2250 LocalTime t = LocalTime.of(0, 0, 0, 1).minusNanos(1); 2251 assertEquals(t, LocalTime.MIDNIGHT); 2252 } 2253 2254 @Test 2255 public void test_minusNanos_toMidday_equal() { 2256 LocalTime t = LocalTime.of(12, 0, 0, 1).minusNanos(1); 2257 assertEquals(t, LocalTime.NOON); 2258 } 2259 2260 //----------------------------------------------------------------------- 2261 // until(Temporal, TemporalUnit) 2262 //----------------------------------------------------------------------- 2263 @DataProvider(name="periodUntilUnit") 2264 Object[][] data_periodUntilUnit() { 2265 return new Object[][] { 2266 {time(0, 0, 0, 0), time(0, 0, 0, 0), NANOS, 0}, 2267 {time(0, 0, 0, 0), time(0, 0, 0, 0), MICROS, 0}, 2268 {time(0, 0, 0, 0), time(0, 0, 0, 0), MILLIS, 0}, 2269 {time(0, 0, 0, 0), time(0, 0, 0, 0), SECONDS, 0}, 2270 {time(0, 0, 0, 0), time(0, 0, 0, 0), MINUTES, 0}, 2271 {time(0, 0, 0, 0), time(0, 0, 0, 0), HOURS, 0}, 2272 {time(0, 0, 0, 0), time(0, 0, 0, 0), HALF_DAYS, 0}, 2273 2274 {time(0, 0, 0, 0), time(2, 0, 0, 0), NANOS, 2 * 3600 * 1_000_000_000L}, 2275 {time(0, 0, 0, 0), time(2, 0, 0, 0), MICROS, 2 * 3600 * 1_000_000L}, 2276 {time(0, 0, 0, 0), time(2, 0, 0, 0), MILLIS, 2 * 3600 * 1_000L}, 2277 {time(0, 0, 0, 0), time(2, 0, 0, 0), SECONDS, 2 * 3600}, 2278 {time(0, 0, 0, 0), time(2, 0, 0, 0), MINUTES, 2 * 60}, 2279 {time(0, 0, 0, 0), time(2, 0, 0, 0), HOURS, 2}, 2280 {time(0, 0, 0, 0), time(2, 0, 0, 0), HALF_DAYS, 0}, 2281 2282 {time(0, 0, 0, 0), time(14, 0, 0, 0), NANOS, 14 * 3600 * 1_000_000_000L}, 2283 {time(0, 0, 0, 0), time(14, 0, 0, 0), MICROS, 14 * 3600 * 1_000_000L}, 2284 {time(0, 0, 0, 0), time(14, 0, 0, 0), MILLIS, 14 * 3600 * 1_000L}, 2285 {time(0, 0, 0, 0), time(14, 0, 0, 0), SECONDS, 14 * 3600}, 2286 {time(0, 0, 0, 0), time(14, 0, 0, 0), MINUTES, 14 * 60}, 2287 {time(0, 0, 0, 0), time(14, 0, 0, 0), HOURS, 14}, 2288 {time(0, 0, 0, 0), time(14, 0, 0, 0), HALF_DAYS, 1}, 2289 2290 {time(0, 0, 0, 0), time(2, 30, 40, 1500), NANOS, (2 * 3600 + 30 * 60 + 40) * 1_000_000_000L + 1500}, 2291 {time(0, 0, 0, 0), time(2, 30, 40, 1500), MICROS, (2 * 3600 + 30 * 60 + 40) * 1_000_000L + 1}, 2292 {time(0, 0, 0, 0), time(2, 30, 40, 1500), MILLIS, (2 * 3600 + 30 * 60 + 40) * 1_000L}, 2293 {time(0, 0, 0, 0), time(2, 30, 40, 1500), SECONDS, 2 * 3600 + 30 * 60 + 40}, 2294 {time(0, 0, 0, 0), time(2, 30, 40, 1500), MINUTES, 2 * 60 + 30}, 2295 {time(0, 0, 0, 0), time(2, 30, 40, 1500), HOURS, 2}, 2296 }; 2297 } 2298 2299 @Test(dataProvider="periodUntilUnit") 2300 public void test_until_TemporalUnit(LocalTime time1, LocalTime time2, TemporalUnit unit, long expected) { 2301 long amount = time1.until(time2, unit); 2302 assertEquals(amount, expected); 2303 } 2304 2305 @Test(dataProvider="periodUntilUnit") 2306 public void test_until_TemporalUnit_negated(LocalTime time1, LocalTime time2, TemporalUnit unit, long expected) { 2307 long amount = time2.until(time1, unit); 2308 assertEquals(amount, -expected); 2309 } 2310 2311 @Test(dataProvider="periodUntilUnit") 2312 public void test_until_TemporalUnit_between(LocalTime time1, LocalTime time2, TemporalUnit unit, long expected) { 2313 long amount = unit.between(time1, time2); 2314 assertEquals(amount, expected); 2315 } 2316 2317 @Test 2318 public void test_until_convertedType() { 2319 LocalTime start = LocalTime.of(11, 30); 2320 LocalDateTime end = start.plusSeconds(2).atDate(LocalDate.of(2010, 6, 30)); 2321 assertEquals(start.until(end, SECONDS), 2); 2322 } 2323 2324 @Test(expectedExceptions=DateTimeException.class) 2325 public void test_until_invalidType() { 2326 LocalTime start = LocalTime.of(11, 30); 2327 start.until(LocalDate.of(2010, 6, 30), SECONDS); 2328 } 2329 2330 @Test(expectedExceptions = UnsupportedTemporalTypeException.class) 2331 public void test_until_TemporalUnit_unsupportedUnit() { 2332 TEST_12_30_40_987654321.until(TEST_12_30_40_987654321, DAYS); 2333 } 2334 2335 @Test(expectedExceptions = NullPointerException.class) 2336 public void test_until_TemporalUnit_nullEnd() { 2337 TEST_12_30_40_987654321.until(null, HOURS); 2338 } 2339 2340 @Test(expectedExceptions = NullPointerException.class) 2341 public void test_until_TemporalUnit_nullUnit() { 2342 TEST_12_30_40_987654321.until(TEST_12_30_40_987654321, null); 2343 } 2344 2345 //----------------------------------------------------------------------- 2346 // format(DateTimeFormatter) 2347 //----------------------------------------------------------------------- 2348 @Test 2349 public void test_format_formatter() { 2350 DateTimeFormatter f = DateTimeFormatter.ofPattern("H m s"); 2351 String t = LocalTime.of(11, 30, 45).format(f); 2352 assertEquals(t, "11 30 45"); 2353 } 2354 2355 @Test(expectedExceptions=NullPointerException.class) 2356 public void test_format_formatter_null() { 2357 LocalTime.of(11, 30, 45).format(null); 2358 } 2359 2360 //----------------------------------------------------------------------- 2361 // atDate() 2362 //----------------------------------------------------------------------- 2363 @Test 2364 public void test_atDate() { 2365 LocalTime t = LocalTime.of(11, 30); 2366 assertEquals(t.atDate(LocalDate.of(2012, 6, 30)), LocalDateTime.of(2012, 6, 30, 11, 30)); 2367 } 2368 2369 @Test(expectedExceptions=NullPointerException.class) 2370 public void test_atDate_nullDate() { 2371 TEST_12_30_40_987654321.atDate((LocalDate) null); 2372 } 2373 2374 //----------------------------------------------------------------------- 2375 // atOffset() 2376 //----------------------------------------------------------------------- 2377 @Test 2378 public void test_atOffset() { 2379 LocalTime t = LocalTime.of(11, 30); 2380 assertEquals(t.atOffset(OFFSET_PTWO), OffsetTime.of(LocalTime.of(11, 30), OFFSET_PTWO)); 2381 } 2382 2383 @Test(expectedExceptions=NullPointerException.class) 2384 public void test_atOffset_nullZoneOffset() { 2385 LocalTime t = LocalTime.of(11, 30); 2386 t.atOffset((ZoneOffset) null); 2387 } 2388 2389 //----------------------------------------------------------------------- 2390 // toSecondOfDay() 2391 //----------------------------------------------------------------------- 2392 @Test 2393 public void test_toSecondOfDay() { 2394 LocalTime t = LocalTime.of(0, 0); 2395 for (int i = 0; i < 24 * 60 * 60; i++) { 2396 assertEquals(t.toSecondOfDay(), i); 2397 t = t.plusSeconds(1); 2398 } 2399 } 2400 2401 @Test 2402 public void test_toSecondOfDay_fromNanoOfDay_symmetry() { 2403 LocalTime t = LocalTime.of(0, 0); 2404 for (int i = 0; i < 24 * 60 * 60; i++) { 2405 assertEquals(LocalTime.ofSecondOfDay(t.toSecondOfDay()), t); 2406 t = t.plusSeconds(1); 2407 } 2408 } 2409 2410 //----------------------------------------------------------------------- 2411 // toNanoOfDay() 2412 //----------------------------------------------------------------------- 2413 @Test 2414 public void test_toNanoOfDay() { 2415 LocalTime t = LocalTime.of(0, 0); 2416 for (int i = 0; i < 1000000; i++) { 2417 assertEquals(t.toNanoOfDay(), i); 2418 t = t.plusNanos(1); 2419 } 2420 t = LocalTime.of(0, 0); 2421 for (int i = 1; i <= 1000000; i++) { 2422 t = t.minusNanos(1); 2423 assertEquals(t.toNanoOfDay(), 24 * 60 * 60 * 1000000000L - i); 2424 } 2425 } 2426 2427 @Test 2428 public void test_toNanoOfDay_fromNanoOfDay_symmetry() { 2429 LocalTime t = LocalTime.of(0, 0); 2430 for (int i = 0; i < 1000000; i++) { 2431 assertEquals(LocalTime.ofNanoOfDay(t.toNanoOfDay()), t); 2432 t = t.plusNanos(1); 2433 } 2434 t = LocalTime.of(0, 0); 2435 for (int i = 1; i <= 1000000; i++) { 2436 t = t.minusNanos(1); 2437 assertEquals(LocalTime.ofNanoOfDay(t.toNanoOfDay()), t); 2438 } 2439 } 2440 2441 //----------------------------------------------------------------------- 2442 // compareTo() 2443 //----------------------------------------------------------------------- 2444 @Test 2445 public void test_comparisons() { 2446 doTest_comparisons_LocalTime( 2447 LocalTime.MIDNIGHT, 2448 LocalTime.of(0, 0, 0, 999999999), 2449 LocalTime.of(0, 0, 59, 0), 2450 LocalTime.of(0, 0, 59, 999999999), 2451 LocalTime.of(0, 59, 0, 0), 2452 LocalTime.of(0, 59, 0, 999999999), 2453 LocalTime.of(0, 59, 59, 0), 2454 LocalTime.of(0, 59, 59, 999999999), 2455 LocalTime.NOON, 2456 LocalTime.of(12, 0, 0, 999999999), 2457 LocalTime.of(12, 0, 59, 0), 2458 LocalTime.of(12, 0, 59, 999999999), 2459 LocalTime.of(12, 59, 0, 0), 2460 LocalTime.of(12, 59, 0, 999999999), 2461 LocalTime.of(12, 59, 59, 0), 2462 LocalTime.of(12, 59, 59, 999999999), 2463 LocalTime.of(23, 0, 0, 0), 2464 LocalTime.of(23, 0, 0, 999999999), 2465 LocalTime.of(23, 0, 59, 0), 2466 LocalTime.of(23, 0, 59, 999999999), 2467 LocalTime.of(23, 59, 0, 0), 2468 LocalTime.of(23, 59, 0, 999999999), 2469 LocalTime.of(23, 59, 59, 0), 2470 LocalTime.of(23, 59, 59, 999999999) 2471 ); 2472 } 2473 2474 void doTest_comparisons_LocalTime(LocalTime... localTimes) { 2475 for (int i = 0; i < localTimes.length; i++) { 2476 LocalTime a = localTimes[i]; 2477 for (int j = 0; j < localTimes.length; j++) { 2478 LocalTime b = localTimes[j]; 2479 if (i < j) { 2480 assertTrue(a.compareTo(b) < 0, a + " <=> " + b); 2481 assertEquals(a.isBefore(b), true, a + " <=> " + b); 2482 assertEquals(a.isAfter(b), false, a + " <=> " + b); 2483 assertEquals(a.equals(b), false, a + " <=> " + b); 2484 } else if (i > j) { 2485 assertTrue(a.compareTo(b) > 0, a + " <=> " + b); 2486 assertEquals(a.isBefore(b), false, a + " <=> " + b); 2487 assertEquals(a.isAfter(b), true, a + " <=> " + b); 2488 assertEquals(a.equals(b), false, a + " <=> " + b); 2489 } else { 2490 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 2491 assertEquals(a.isBefore(b), false, a + " <=> " + b); 2492 assertEquals(a.isAfter(b), false, a + " <=> " + b); 2493 assertEquals(a.equals(b), true, a + " <=> " + b); 2494 } 2495 } 2496 } 2497 } 2498 2499 @Test(expectedExceptions=NullPointerException.class) 2500 public void test_compareTo_ObjectNull() { 2501 TEST_12_30_40_987654321.compareTo(null); 2502 } 2503 2504 @Test(expectedExceptions=NullPointerException.class) 2505 public void test_isBefore_ObjectNull() { 2506 TEST_12_30_40_987654321.isBefore(null); 2507 } 2508 2509 @Test(expectedExceptions=NullPointerException.class) 2510 public void test_isAfter_ObjectNull() { 2511 TEST_12_30_40_987654321.isAfter(null); 2512 } 2513 2514 @Test(expectedExceptions=ClassCastException.class) 2515 @SuppressWarnings({"unchecked", "rawtypes"}) 2516 public void compareToNonLocalTime() { 2517 Comparable c = TEST_12_30_40_987654321; 2518 c.compareTo(new Object()); 2519 } 2520 2521 //----------------------------------------------------------------------- 2522 // equals() 2523 //----------------------------------------------------------------------- 2524 @Test(dataProvider="sampleTimes") 2525 public void test_equals_true(int h, int m, int s, int n) { 2526 LocalTime a = LocalTime.of(h, m, s, n); 2527 LocalTime b = LocalTime.of(h, m, s, n); 2528 assertEquals(a.equals(b), true); 2529 } 2530 @Test(dataProvider="sampleTimes") 2531 public void test_equals_false_hour_differs(int h, int m, int s, int n) { 2532 LocalTime a = LocalTime.of(h, m, s, n); 2533 LocalTime b = LocalTime.of(h + 1, m, s, n); 2534 assertEquals(a.equals(b), false); 2535 } 2536 @Test(dataProvider="sampleTimes") 2537 public void test_equals_false_minute_differs(int h, int m, int s, int n) { 2538 LocalTime a = LocalTime.of(h, m, s, n); 2539 LocalTime b = LocalTime.of(h, m + 1, s, n); 2540 assertEquals(a.equals(b), false); 2541 } 2542 @Test(dataProvider="sampleTimes") 2543 public void test_equals_false_second_differs(int h, int m, int s, int n) { 2544 LocalTime a = LocalTime.of(h, m, s, n); 2545 LocalTime b = LocalTime.of(h, m, s + 1, n); 2546 assertEquals(a.equals(b), false); 2547 } 2548 @Test(dataProvider="sampleTimes") 2549 public void test_equals_false_nano_differs(int h, int m, int s, int n) { 2550 LocalTime a = LocalTime.of(h, m, s, n); 2551 LocalTime b = LocalTime.of(h, m, s, n + 1); 2552 assertEquals(a.equals(b), false); 2553 } 2554 2555 @Test 2556 public void test_equals_itself_true() { 2557 assertEquals(TEST_12_30_40_987654321.equals(TEST_12_30_40_987654321), true); 2558 } 2559 2560 @Test 2561 public void test_equals_string_false() { 2562 assertEquals(TEST_12_30_40_987654321.equals("2007-07-15"), false); 2563 } 2564 2565 @Test 2566 public void test_equals_null_false() { 2567 assertEquals(TEST_12_30_40_987654321.equals(null), false); 2568 } 2569 2570 //----------------------------------------------------------------------- 2571 // hashCode() 2572 //----------------------------------------------------------------------- 2573 @Test(dataProvider="sampleTimes") 2574 public void test_hashCode_same(int h, int m, int s, int n) { 2575 LocalTime a = LocalTime.of(h, m, s, n); 2576 LocalTime b = LocalTime.of(h, m, s, n); 2577 assertEquals(a.hashCode(), b.hashCode()); 2578 } 2579 2580 @Test(dataProvider="sampleTimes") 2581 public void test_hashCode_hour_differs(int h, int m, int s, int n) { 2582 LocalTime a = LocalTime.of(h, m, s, n); 2583 LocalTime b = LocalTime.of(h + 1, m, s, n); 2584 assertEquals(a.hashCode() == b.hashCode(), false); 2585 } 2586 2587 @Test(dataProvider="sampleTimes") 2588 public void test_hashCode_minute_differs(int h, int m, int s, int n) { 2589 LocalTime a = LocalTime.of(h, m, s, n); 2590 LocalTime b = LocalTime.of(h, m + 1, s, n); 2591 assertEquals(a.hashCode() == b.hashCode(), false); 2592 } 2593 2594 @Test(dataProvider="sampleTimes") 2595 public void test_hashCode_second_differs(int h, int m, int s, int n) { 2596 LocalTime a = LocalTime.of(h, m, s, n); 2597 LocalTime b = LocalTime.of(h, m, s + 1, n); 2598 assertEquals(a.hashCode() == b.hashCode(), false); 2599 } 2600 2601 @Test(dataProvider="sampleTimes") 2602 public void test_hashCode_nano_differs(int h, int m, int s, int n) { 2603 LocalTime a = LocalTime.of(h, m, s, n); 2604 LocalTime b = LocalTime.of(h, m, s, n + 1); 2605 assertEquals(a.hashCode() == b.hashCode(), false); 2606 } 2607 2608 //----------------------------------------------------------------------- 2609 // toString() 2610 //----------------------------------------------------------------------- 2611 @DataProvider(name="sampleToString") 2612 Object[][] provider_sampleToString() { 2613 return new Object[][] { 2614 {0, 0, 0, 0, "00:00"}, 2615 {1, 0, 0, 0, "01:00"}, 2616 {23, 0, 0, 0, "23:00"}, 2617 {0, 1, 0, 0, "00:01"}, 2618 {12, 30, 0, 0, "12:30"}, 2619 {23, 59, 0, 0, "23:59"}, 2620 {0, 0, 1, 0, "00:00:01"}, 2621 {0, 0, 59, 0, "00:00:59"}, 2622 {0, 0, 0, 100000000, "00:00:00.100"}, 2623 {0, 0, 0, 10000000, "00:00:00.010"}, 2624 {0, 0, 0, 1000000, "00:00:00.001"}, 2625 {0, 0, 0, 100000, "00:00:00.000100"}, 2626 {0, 0, 0, 10000, "00:00:00.000010"}, 2627 {0, 0, 0, 1000, "00:00:00.000001"}, 2628 {0, 0, 0, 100, "00:00:00.000000100"}, 2629 {0, 0, 0, 10, "00:00:00.000000010"}, 2630 {0, 0, 0, 1, "00:00:00.000000001"}, 2631 {0, 0, 0, 999999999, "00:00:00.999999999"}, 2632 {0, 0, 0, 99999999, "00:00:00.099999999"}, 2633 {0, 0, 0, 9999999, "00:00:00.009999999"}, 2634 {0, 0, 0, 999999, "00:00:00.000999999"}, 2635 {0, 0, 0, 99999, "00:00:00.000099999"}, 2636 {0, 0, 0, 9999, "00:00:00.000009999"}, 2637 {0, 0, 0, 999, "00:00:00.000000999"}, 2638 {0, 0, 0, 99, "00:00:00.000000099"}, 2639 {0, 0, 0, 9, "00:00:00.000000009"}, 2640 }; 2641 } 2642 2643 @Test(dataProvider="sampleToString") 2644 public void test_toString(int h, int m, int s, int n, String expected) { 2645 LocalTime t = LocalTime.of(h, m, s, n); 2646 String str = t.toString(); 2647 assertEquals(str, expected); 2648 } 2649 2650 private LocalTime time(int hour, int min, int sec, int nano) { 2651 return LocalTime.of(hour, min, sec, nano); 2652 } 2653 }