1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertNotNull; 36 import static org.testng.Assert.assertTrue; 37 import static org.threeten.bp.temporal.ChronoField.AMPM_OF_DAY; 38 import static org.threeten.bp.temporal.ChronoField.CLOCK_HOUR_OF_AMPM; 39 import static org.threeten.bp.temporal.ChronoField.CLOCK_HOUR_OF_DAY; 40 import static org.threeten.bp.temporal.ChronoField.HOUR_OF_AMPM; 41 import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY; 42 import static org.threeten.bp.temporal.ChronoField.MICRO_OF_DAY; 43 import static org.threeten.bp.temporal.ChronoField.MICRO_OF_SECOND; 44 import static org.threeten.bp.temporal.ChronoField.MILLI_OF_DAY; 45 import static org.threeten.bp.temporal.ChronoField.MILLI_OF_SECOND; 46 import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_DAY; 47 import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR; 48 import static org.threeten.bp.temporal.ChronoField.NANO_OF_DAY; 49 import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND; 50 import static org.threeten.bp.temporal.ChronoField.OFFSET_SECONDS; 51 import static org.threeten.bp.temporal.ChronoField.SECOND_OF_DAY; 52 import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE; 53 import static org.threeten.bp.temporal.ChronoUnit.DAYS; 54 import static org.threeten.bp.temporal.ChronoUnit.NANOS; 55 import static org.threeten.bp.temporal.ChronoUnit.SECONDS; 56 57 import java.lang.reflect.Constructor; 58 import java.lang.reflect.InvocationTargetException; 59 import java.util.ArrayList; 60 import java.util.Arrays; 61 import java.util.List; 62 63 import org.testng.annotations.BeforeMethod; 64 import org.testng.annotations.DataProvider; 65 import org.testng.annotations.Test; 66 import org.threeten.bp.format.DateTimeFormatter; 67 import org.threeten.bp.format.DateTimeParseException; 68 import org.threeten.bp.temporal.ChronoField; 69 import org.threeten.bp.temporal.ChronoUnit; 70 import org.threeten.bp.temporal.JulianFields; 71 import org.threeten.bp.temporal.Temporal; 72 import org.threeten.bp.temporal.TemporalAccessor; 73 import org.threeten.bp.temporal.TemporalAdjuster; 74 import org.threeten.bp.temporal.TemporalField; 75 import org.threeten.bp.temporal.TemporalQueries; 76 77 /** 78 * Test OffsetTime. 79 */ 80 @Test 81 public class TestOffsetTime extends AbstractDateTimeTest { 82 83 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1); 84 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2); 85 private static final LocalDate DATE = LocalDate.of(2008, 12, 3); 86 private OffsetTime TEST_11_30_59_500_PONE; 87 88 @BeforeMethod setUp()89 public void setUp() { 90 TEST_11_30_59_500_PONE = OffsetTime.of(LocalTime.of(11, 30, 59, 500), OFFSET_PONE); 91 } 92 93 //----------------------------------------------------------------------- 94 @Override samples()95 protected List<TemporalAccessor> samples() { 96 TemporalAccessor[] array = {TEST_11_30_59_500_PONE, OffsetTime.MIN, OffsetTime.MAX}; 97 return Arrays.asList(array); 98 } 99 100 @Override validFields()101 protected List<TemporalField> validFields() { 102 TemporalField[] array = { 103 NANO_OF_SECOND, 104 NANO_OF_DAY, 105 MICRO_OF_SECOND, 106 MICRO_OF_DAY, 107 MILLI_OF_SECOND, 108 MILLI_OF_DAY, 109 SECOND_OF_MINUTE, 110 SECOND_OF_DAY, 111 MINUTE_OF_HOUR, 112 MINUTE_OF_DAY, 113 CLOCK_HOUR_OF_AMPM, 114 HOUR_OF_AMPM, 115 CLOCK_HOUR_OF_DAY, 116 HOUR_OF_DAY, 117 AMPM_OF_DAY, 118 OFFSET_SECONDS, 119 }; 120 return Arrays.asList(array); 121 } 122 123 @Override invalidFields()124 protected List<TemporalField> invalidFields() { 125 List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values())); 126 list.removeAll(validFields()); 127 list.add(JulianFields.JULIAN_DAY); 128 list.add(JulianFields.MODIFIED_JULIAN_DAY); 129 list.add(JulianFields.RATA_DIE); 130 return list; 131 } 132 133 //----------------------------------------------------------------------- 134 @Test test_serialization()135 public void test_serialization() throws Exception { 136 assertSerializable(TEST_11_30_59_500_PONE); 137 assertSerializable(OffsetTime.MIN); 138 assertSerializable(OffsetTime.MAX); 139 } 140 141 @Test test_serialization_format()142 public void test_serialization_format() throws Exception { 143 assertEqualsSerialisedForm(OffsetTime.of(LocalTime.of(22, 17, 59, 464000000), ZoneOffset.ofHours(1))); 144 } 145 146 //----------------------------------------------------------------------- 147 // constants 148 //----------------------------------------------------------------------- 149 @Test constant_MIN()150 public void constant_MIN() { 151 check(OffsetTime.MIN, 0, 0, 0, 0, ZoneOffset.MAX); 152 } 153 154 @Test constant_MAX()155 public void constant_MAX() { 156 check(OffsetTime.MAX, 23, 59, 59, 999999999, ZoneOffset.MIN); 157 } 158 159 //----------------------------------------------------------------------- 160 // now() 161 //----------------------------------------------------------------------- 162 @Test now()163 public void now() { 164 ZonedDateTime nowDT = ZonedDateTime.now(); 165 166 OffsetTime expected = OffsetTime.now(Clock.systemDefaultZone()); 167 OffsetTime test = OffsetTime.now(); 168 long diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 169 assertTrue(diff < 100000000); // less than 0.1 secs 170 assertEquals(test.getOffset(), nowDT.getOffset()); 171 } 172 173 //----------------------------------------------------------------------- 174 // now(Clock) 175 //----------------------------------------------------------------------- 176 @Test 177 public void now_Clock_allSecsInDay() { 178 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 179 Instant instant = Instant.ofEpochSecond(i, 8); 180 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 181 OffsetTime test = OffsetTime.now(clock); 182 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 183 assertEquals(test.getMinute(), (i / 60) % 60); 184 assertEquals(test.getSecond(), i % 60); 185 assertEquals(test.getNano(), 8); 186 assertEquals(test.getOffset(), ZoneOffset.UTC); 187 } 188 } 189 190 @Test 191 public void now_Clock_beforeEpoch() { 192 for (int i =-1; i >= -(24 * 60 * 60); i--) { 193 Instant instant = Instant.ofEpochSecond(i, 8); 194 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 195 OffsetTime test = OffsetTime.now(clock); 196 assertEquals(test.getHour(), ((i + 24 * 60 * 60) / (60 * 60)) % 24); 197 assertEquals(test.getMinute(), ((i + 24 * 60 * 60) / 60) % 60); 198 assertEquals(test.getSecond(), (i + 24 * 60 * 60) % 60); 199 assertEquals(test.getNano(), 8); 200 assertEquals(test.getOffset(), ZoneOffset.UTC); 201 } 202 } 203 204 @Test 205 public void now_Clock_offsets() { 206 Instant base = LocalDateTime.of(1970, 1, 1, 12, 0).toInstant(ZoneOffset.UTC); 207 for (int i = -9; i < 15; i++) { 208 ZoneOffset offset = ZoneOffset.ofHours(i); 209 Clock clock = Clock.fixed(base, offset); 210 OffsetTime test = OffsetTime.now(clock); 211 assertEquals(test.getHour(), (12 + i) % 24); 212 assertEquals(test.getMinute(), 0); 213 assertEquals(test.getSecond(), 0); 214 assertEquals(test.getNano(), 0); 215 assertEquals(test.getOffset(), offset); 216 } 217 } 218 219 @Test(expectedExceptions=NullPointerException.class) 220 public void now_Clock_nullZoneId() { 221 OffsetTime.now((ZoneId) null); 222 } 223 224 @Test(expectedExceptions=NullPointerException.class) 225 public void now_Clock_nullClock() { 226 OffsetTime.now((Clock) null); 227 } 228 229 //----------------------------------------------------------------------- 230 // factories 231 //----------------------------------------------------------------------- 232 private void check(OffsetTime test, int h, int m, int s, int n, ZoneOffset offset) { 233 assertEquals(test.toLocalTime(), LocalTime.of(h, m, s, n)); 234 assertEquals(test.getOffset(), offset); 235 236 assertEquals(test.getHour(), h); 237 assertEquals(test.getMinute(), m); 238 assertEquals(test.getSecond(), s); 239 assertEquals(test.getNano(), n); 240 241 assertEquals(test, test); 242 assertEquals(test.hashCode(), test.hashCode()); 243 assertEquals(OffsetTime.of(LocalTime.of(h, m, s, n), offset), test); 244 } 245 246 //----------------------------------------------------------------------- 247 @Test 248 public void factory_intsHM() { 249 OffsetTime test = OffsetTime.of(LocalTime.of(11, 30), OFFSET_PONE); 250 check(test, 11, 30, 0, 0, OFFSET_PONE); 251 } 252 253 //----------------------------------------------------------------------- 254 @Test 255 public void factory_intsHMS() { 256 OffsetTime test = OffsetTime.of(LocalTime.of(11, 30, 10), OFFSET_PONE); 257 check(test, 11, 30, 10, 0, OFFSET_PONE); 258 } 259 260 //----------------------------------------------------------------------- 261 @Test 262 public void factory_intsHMSN() { 263 OffsetTime test = OffsetTime.of(LocalTime.of(11, 30, 10, 500), OFFSET_PONE); 264 check(test, 11, 30, 10, 500, OFFSET_PONE); 265 } 266 267 //----------------------------------------------------------------------- 268 @Test 269 public void factory_LocalTimeZoneOffset() { 270 LocalTime localTime = LocalTime.of(11, 30, 10, 500); 271 OffsetTime test = OffsetTime.of(localTime, OFFSET_PONE); 272 check(test, 11, 30, 10, 500, OFFSET_PONE); 273 } 274 275 @Test(expectedExceptions=NullPointerException.class) 276 public void factory_LocalTimeZoneOffset_nullTime() { 277 OffsetTime.of((LocalTime) null, OFFSET_PONE); 278 } 279 280 @Test(expectedExceptions=NullPointerException.class) 281 public void factory_LocalTimeZoneOffset_nullOffset() { 282 LocalTime localTime = LocalTime.of(11, 30, 10, 500); 283 OffsetTime.of(localTime, (ZoneOffset) null); 284 } 285 286 //----------------------------------------------------------------------- 287 // ofInstant() 288 //----------------------------------------------------------------------- 289 @Test(expectedExceptions=NullPointerException.class) 290 public void factory_ofInstant_nullInstant() { 291 OffsetTime.ofInstant((Instant) null, ZoneOffset.UTC); 292 } 293 294 @Test(expectedExceptions=NullPointerException.class) 295 public void factory_ofInstant_nullOffset() { 296 Instant instant = Instant.ofEpochSecond(0L); 297 OffsetTime.ofInstant(instant, (ZoneOffset) null); 298 } 299 300 @Test 301 public void factory_ofInstant_allSecsInDay() { 302 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 303 Instant instant = Instant.ofEpochSecond(i, 8); 304 OffsetTime test = OffsetTime.ofInstant(instant, ZoneOffset.UTC); 305 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 306 assertEquals(test.getMinute(), (i / 60) % 60); 307 assertEquals(test.getSecond(), i % 60); 308 assertEquals(test.getNano(), 8); 309 } 310 } 311 312 @Test 313 public void factory_ofInstant_beforeEpoch() { 314 for (int i =-1; i >= -(24 * 60 * 60); i--) { 315 Instant instant = Instant.ofEpochSecond(i, 8); 316 OffsetTime test = OffsetTime.ofInstant(instant, ZoneOffset.UTC); 317 assertEquals(test.getHour(), ((i + 24 * 60 * 60) / (60 * 60)) % 24); 318 assertEquals(test.getMinute(), ((i + 24 * 60 * 60) / 60) % 60); 319 assertEquals(test.getSecond(), (i + 24 * 60 * 60) % 60); 320 assertEquals(test.getNano(), 8); 321 } 322 } 323 324 //----------------------------------------------------------------------- 325 @Test 326 public void factory_ofInstant_maxYear() { 327 OffsetTime test = OffsetTime.ofInstant(Instant.MAX, ZoneOffset.UTC); 328 assertEquals(test.getHour(), 23); 329 assertEquals(test.getMinute(), 59); 330 assertEquals(test.getSecond(), 59); 331 assertEquals(test.getNano(), 999999999); 332 } 333 334 @Test 335 public void factory_ofInstant_minYear() { 336 OffsetTime test = OffsetTime.ofInstant(Instant.MIN, ZoneOffset.UTC); 337 assertEquals(test.getHour(), 0); 338 assertEquals(test.getMinute(), 0); 339 assertEquals(test.getSecond(), 0); 340 assertEquals(test.getNano(), 0); 341 } 342 343 //----------------------------------------------------------------------- 344 // from(TemporalAccessor) 345 //----------------------------------------------------------------------- 346 @Test 347 public void factory_from_TemporalAccessor_OT() { 348 assertEquals(OffsetTime.from(OffsetTime.of(LocalTime.of(17, 30), OFFSET_PONE)), OffsetTime.of(LocalTime.of(17, 30), OFFSET_PONE)); 349 } 350 351 @Test 352 public void test_from_TemporalAccessor_ZDT() { 353 ZonedDateTime base = LocalDateTime.of(2007, 7, 15, 11, 30, 59, 500).atZone(OFFSET_PONE); 354 assertEquals(OffsetTime.from(base), TEST_11_30_59_500_PONE); 355 } 356 357 @Test(expectedExceptions=DateTimeException.class) 358 public void factory_from_TemporalAccessor_invalid_noDerive() { 359 OffsetTime.from(LocalDate.of(2007, 7, 15)); 360 } 361 362 @Test(expectedExceptions=NullPointerException.class) 363 public void factory_from_TemporalAccessor_null() { 364 OffsetTime.from((TemporalAccessor) null); 365 } 366 367 //----------------------------------------------------------------------- 368 // parse() 369 //----------------------------------------------------------------------- 370 @Test(dataProvider = "sampleToString") 371 public void factory_parse_validText(int h, int m, int s, int n, String offsetId, String parsable) { 372 OffsetTime t = OffsetTime.parse(parsable); 373 assertNotNull(t, parsable); 374 check(t, h, m, s, n, ZoneOffset.of(offsetId)); 375 } 376 377 @DataProvider(name="sampleBadParse") 378 Object[][] provider_sampleBadParse() { 379 return new Object[][]{ 380 {"00;00"}, 381 {"12-00"}, 382 {"-01:00"}, 383 {"00:00:00-09"}, 384 {"00:00:00,09"}, 385 {"00:00:abs"}, 386 {"11"}, 387 {"11:30"}, 388 {"11:30+01:00[Europe/Paris]"}, 389 }; 390 } 391 392 @Test(dataProvider = "sampleBadParse", expectedExceptions={DateTimeParseException.class}) 393 public void factory_parse_invalidText(String unparsable) { 394 OffsetTime.parse(unparsable); 395 } 396 397 //-----------------------------------------------------------------------s 398 @Test(expectedExceptions={DateTimeParseException.class}) 399 public void factory_parse_illegalHour() { 400 OffsetTime.parse("25:00+01:00"); 401 } 402 403 @Test(expectedExceptions={DateTimeParseException.class}) 404 public void factory_parse_illegalMinute() { 405 OffsetTime.parse("12:60+01:00"); 406 } 407 408 @Test(expectedExceptions={DateTimeParseException.class}) 409 public void factory_parse_illegalSecond() { 410 OffsetTime.parse("12:12:60+01:00"); 411 } 412 413 //----------------------------------------------------------------------- 414 // parse(DateTimeFormatter) 415 //----------------------------------------------------------------------- 416 @Test 417 public void factory_parse_formatter() { 418 DateTimeFormatter f = DateTimeFormatter.ofPattern("H m s XXX"); 419 OffsetTime test = OffsetTime.parse("11 30 0 +01:00", f); 420 assertEquals(test, OffsetTime.of(LocalTime.of(11, 30), ZoneOffset.ofHours(1))); 421 } 422 423 @Test(expectedExceptions=NullPointerException.class) 424 public void factory_parse_formatter_nullText() { 425 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 426 OffsetTime.parse((String) null, f); 427 } 428 429 @Test(expectedExceptions=NullPointerException.class) 430 public void factory_parse_formatter_nullFormatter() { 431 OffsetTime.parse("ANY", null); 432 } 433 434 //----------------------------------------------------------------------- 435 // constructor 436 //----------------------------------------------------------------------- 437 @Test(expectedExceptions=NullPointerException.class) 438 public void constructor_nullTime() throws Throwable { 439 Constructor<OffsetTime> con = OffsetTime.class.getDeclaredConstructor(LocalTime.class, ZoneOffset.class); 440 con.setAccessible(true); 441 try { 442 con.newInstance(null, OFFSET_PONE); 443 } catch (InvocationTargetException ex) { 444 throw ex.getCause(); 445 } 446 } 447 448 @Test(expectedExceptions=NullPointerException.class) 449 public void constructor_nullOffset() throws Throwable { 450 Constructor<OffsetTime> con = OffsetTime.class.getDeclaredConstructor(LocalTime.class, ZoneOffset.class); 451 con.setAccessible(true); 452 try { 453 con.newInstance(LocalTime.of(11, 30), null); 454 } catch (InvocationTargetException ex) { 455 throw ex.getCause(); 456 } 457 } 458 459 //----------------------------------------------------------------------- 460 // basics 461 //----------------------------------------------------------------------- 462 @DataProvider(name="sampleTimes") 463 Object[][] provider_sampleTimes() { 464 return new Object[][] { 465 {11, 30, 20, 500, OFFSET_PONE}, 466 {11, 0, 0, 0, OFFSET_PONE}, 467 {23, 59, 59, 999999999, OFFSET_PONE}, 468 }; 469 } 470 471 @Test(dataProvider="sampleTimes") 472 public void test_get(int h, int m, int s, int n, ZoneOffset offset) { 473 LocalTime localTime = LocalTime.of(h, m, s, n); 474 OffsetTime a = OffsetTime.of(localTime, offset); 475 476 assertEquals(a.toLocalTime(), localTime); 477 assertEquals(a.getOffset(), offset); 478 assertEquals(a.toString(), localTime.toString() + offset.toString()); 479 assertEquals(a.getHour(), localTime.getHour()); 480 assertEquals(a.getMinute(), localTime.getMinute()); 481 assertEquals(a.getSecond(), localTime.getSecond()); 482 assertEquals(a.getNano(), localTime.getNano()); 483 } 484 485 //----------------------------------------------------------------------- 486 // get(TemporalField) 487 //----------------------------------------------------------------------- 488 @Test 489 public void test_get_TemporalField() { 490 OffsetTime test = OffsetTime.of(LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE); 491 assertEquals(test.get(ChronoField.HOUR_OF_DAY), 12); 492 assertEquals(test.get(ChronoField.MINUTE_OF_HOUR), 30); 493 assertEquals(test.get(ChronoField.SECOND_OF_MINUTE), 40); 494 assertEquals(test.get(ChronoField.NANO_OF_SECOND), 987654321); 495 assertEquals(test.get(ChronoField.HOUR_OF_AMPM), 0); 496 assertEquals(test.get(ChronoField.AMPM_OF_DAY), 1); 497 498 assertEquals(test.get(ChronoField.OFFSET_SECONDS), 3600); 499 } 500 501 @Test 502 public void test_getLong_TemporalField() { 503 OffsetTime test = OffsetTime.of(LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE); 504 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); 505 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); 506 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); 507 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); 508 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); 509 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); 510 511 assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); 512 } 513 514 //----------------------------------------------------------------------- 515 // query(TemporalQuery) 516 //----------------------------------------------------------------------- 517 @Test 518 public void test_query() { 519 assertEquals(TEST_11_30_59_500_PONE.query(TemporalQueries.chronology()), null); 520 assertEquals(TEST_11_30_59_500_PONE.query(TemporalQueries.localDate()), null); 521 assertEquals(TEST_11_30_59_500_PONE.query(TemporalQueries.localTime()), TEST_11_30_59_500_PONE.toLocalTime()); 522 assertEquals(TEST_11_30_59_500_PONE.query(TemporalQueries.offset()), TEST_11_30_59_500_PONE.getOffset()); 523 assertEquals(TEST_11_30_59_500_PONE.query(TemporalQueries.precision()), ChronoUnit.NANOS); 524 assertEquals(TEST_11_30_59_500_PONE.query(TemporalQueries.zone()), TEST_11_30_59_500_PONE.getOffset()); 525 assertEquals(TEST_11_30_59_500_PONE.query(TemporalQueries.zoneId()), null); 526 } 527 528 @Test(expectedExceptions=NullPointerException.class) 529 public void test_query_null() { 530 TEST_11_30_59_500_PONE.query(null); 531 } 532 533 //----------------------------------------------------------------------- 534 // withOffsetSameLocal() 535 //----------------------------------------------------------------------- 536 @Test 537 public void test_withOffsetSameLocal() { 538 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 539 OffsetTime test = base.withOffsetSameLocal(OFFSET_PTWO); 540 assertEquals(test.toLocalTime(), base.toLocalTime()); 541 assertEquals(test.getOffset(), OFFSET_PTWO); 542 } 543 544 @Test 545 public void test_withOffsetSameLocal_noChange() { 546 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 547 OffsetTime test = base.withOffsetSameLocal(OFFSET_PONE); 548 assertEquals(test, base); 549 } 550 551 @Test(expectedExceptions=NullPointerException.class) 552 public void test_withOffsetSameLocal_null() { 553 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 554 base.withOffsetSameLocal(null); 555 } 556 557 //----------------------------------------------------------------------- 558 // withOffsetSameInstant() 559 //----------------------------------------------------------------------- 560 @Test 561 public void test_withOffsetSameInstant() { 562 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 563 OffsetTime test = base.withOffsetSameInstant(OFFSET_PTWO); 564 OffsetTime expected = OffsetTime.of(LocalTime.of(12, 30, 59), OFFSET_PTWO); 565 assertEquals(test, expected); 566 } 567 568 @Test 569 public void test_withOffsetSameInstant_noChange() { 570 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 571 OffsetTime test = base.withOffsetSameInstant(OFFSET_PONE); 572 assertEquals(test, base); 573 } 574 575 @Test(expectedExceptions=NullPointerException.class) 576 public void test_withOffsetSameInstant_null() { 577 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 578 base.withOffsetSameInstant(null); 579 } 580 581 //----------------------------------------------------------------------- 582 // with(WithAdjuster) 583 //----------------------------------------------------------------------- 584 @Test 585 public void test_with_adjustment() { 586 final OffsetTime sample = OffsetTime.of(LocalTime.of(23, 5), OFFSET_PONE); 587 TemporalAdjuster adjuster = new TemporalAdjuster() { 588 @Override 589 public Temporal adjustInto(Temporal dateTime) { 590 return sample; 591 } 592 }; 593 assertEquals(TEST_11_30_59_500_PONE.with(adjuster), sample); 594 } 595 596 @Test 597 public void test_with_adjustment_LocalTime() { 598 OffsetTime test = TEST_11_30_59_500_PONE.with(LocalTime.of(13, 30)); 599 assertEquals(test, OffsetTime.of(LocalTime.of(13, 30), OFFSET_PONE)); 600 } 601 602 @Test 603 public void test_with_adjustment_OffsetTime() { 604 OffsetTime test = TEST_11_30_59_500_PONE.with(OffsetTime.of(LocalTime.of(13, 35), OFFSET_PTWO)); 605 assertEquals(test, OffsetTime.of(LocalTime.of(13, 35), OFFSET_PTWO)); 606 } 607 608 @Test 609 public void test_with_adjustment_ZoneOffset() { 610 OffsetTime test = TEST_11_30_59_500_PONE.with(OFFSET_PTWO); 611 assertEquals(test, OffsetTime.of(LocalTime.of(11, 30, 59, 500), OFFSET_PTWO)); 612 } 613 614 @Test 615 public void test_with_adjustment_AmPm() { 616 OffsetTime test = TEST_11_30_59_500_PONE.with(new TemporalAdjuster() { 617 @Override 618 public Temporal adjustInto(Temporal dateTime) { 619 return dateTime.with(HOUR_OF_DAY, 23); 620 } 621 }); 622 assertEquals(test, OffsetTime.of(LocalTime.of(23, 30, 59, 500), OFFSET_PONE)); 623 } 624 625 @Test(expectedExceptions=NullPointerException.class) 626 public void test_with_adjustment_null() { 627 TEST_11_30_59_500_PONE.with((TemporalAdjuster) null); 628 } 629 630 //----------------------------------------------------------------------- 631 // with(TemporalField, long) 632 //----------------------------------------------------------------------- 633 @Test 634 public void test_with_TemporalField() { 635 OffsetTime test = OffsetTime.of(LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE); 636 assertEquals(test.with(ChronoField.HOUR_OF_DAY, 15), OffsetTime.of(LocalTime.of(15, 30, 40, 987654321), OFFSET_PONE)); 637 assertEquals(test.with(ChronoField.MINUTE_OF_HOUR, 50), OffsetTime.of(LocalTime.of(12, 50, 40, 987654321), OFFSET_PONE)); 638 assertEquals(test.with(ChronoField.SECOND_OF_MINUTE, 50), OffsetTime.of(LocalTime.of(12, 30, 50, 987654321), OFFSET_PONE)); 639 assertEquals(test.with(ChronoField.NANO_OF_SECOND, 12345), OffsetTime.of(LocalTime.of(12, 30, 40, 12345), OFFSET_PONE)); 640 assertEquals(test.with(ChronoField.HOUR_OF_AMPM, 6), OffsetTime.of(LocalTime.of(18, 30, 40, 987654321), OFFSET_PONE)); 641 assertEquals(test.with(ChronoField.AMPM_OF_DAY, 0), OffsetTime.of(LocalTime.of(0, 30, 40, 987654321), OFFSET_PONE)); 642 643 assertEquals(test.with(ChronoField.OFFSET_SECONDS, 7205), OffsetTime.of(LocalTime.of(12, 30, 40, 987654321), ZoneOffset.ofHoursMinutesSeconds(2, 0, 5))); 644 } 645 646 @Test(expectedExceptions=NullPointerException.class) 647 public void test_with_TemporalField_null() { 648 TEST_11_30_59_500_PONE.with((TemporalField) null, 0); 649 } 650 651 @Test(expectedExceptions=DateTimeException.class) 652 public void test_with_TemporalField_invalidField() { 653 TEST_11_30_59_500_PONE.with(ChronoField.YEAR, 0); 654 } 655 656 //----------------------------------------------------------------------- 657 // withHour() 658 //----------------------------------------------------------------------- 659 @Test 660 public void test_withHour_normal() { 661 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 662 OffsetTime test = base.withHour(15); 663 assertEquals(test, OffsetTime.of(LocalTime.of(15, 30, 59), OFFSET_PONE)); 664 } 665 666 @Test 667 public void test_withHour_noChange() { 668 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 669 OffsetTime test = base.withHour(11); 670 assertEquals(test, base); 671 } 672 673 //----------------------------------------------------------------------- 674 // withMinute() 675 //----------------------------------------------------------------------- 676 @Test 677 public void test_withMinute_normal() { 678 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 679 OffsetTime test = base.withMinute(15); 680 assertEquals(test, OffsetTime.of(LocalTime.of(11, 15, 59), OFFSET_PONE)); 681 } 682 683 @Test 684 public void test_withMinute_noChange() { 685 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 686 OffsetTime test = base.withMinute(30); 687 assertEquals(test, base); 688 } 689 690 //----------------------------------------------------------------------- 691 // withSecond() 692 //----------------------------------------------------------------------- 693 @Test 694 public void test_withSecond_normal() { 695 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 696 OffsetTime test = base.withSecond(15); 697 assertEquals(test, OffsetTime.of(LocalTime.of(11, 30, 15), OFFSET_PONE)); 698 } 699 700 @Test 701 public void test_withSecond_noChange() { 702 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 703 OffsetTime test = base.withSecond(59); 704 assertEquals(test, base); 705 } 706 707 //----------------------------------------------------------------------- 708 // withNano() 709 //----------------------------------------------------------------------- 710 @Test 711 public void test_withNanoOfSecond_normal() { 712 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59, 1), OFFSET_PONE); 713 OffsetTime test = base.withNano(15); 714 assertEquals(test, OffsetTime.of(LocalTime.of(11, 30, 59, 15), OFFSET_PONE)); 715 } 716 717 @Test 718 public void test_withNanoOfSecond_noChange() { 719 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59, 1), OFFSET_PONE); 720 OffsetTime test = base.withNano(1); 721 assertEquals(test, base); 722 } 723 724 //----------------------------------------------------------------------- 725 // truncatedTo(TemporalUnit) 726 //----------------------------------------------------------------------- 727 @Test 728 public void test_truncatedTo_normal() { 729 assertEquals(TEST_11_30_59_500_PONE.truncatedTo(NANOS), TEST_11_30_59_500_PONE); 730 assertEquals(TEST_11_30_59_500_PONE.truncatedTo(SECONDS), TEST_11_30_59_500_PONE.withNano(0)); 731 assertEquals(TEST_11_30_59_500_PONE.truncatedTo(DAYS), TEST_11_30_59_500_PONE.with(LocalTime.MIDNIGHT)); 732 } 733 734 @Test(expectedExceptions=NullPointerException.class) 735 public void test_truncatedTo_null() { 736 TEST_11_30_59_500_PONE.truncatedTo(null); 737 } 738 739 //----------------------------------------------------------------------- 740 // plus(PlusAdjuster) 741 //----------------------------------------------------------------------- 742 @Test 743 public void test_plus_PlusAdjuster() { 744 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MINUTES); 745 OffsetTime t = TEST_11_30_59_500_PONE.plus(period); 746 assertEquals(t, OffsetTime.of(LocalTime.of(11, 37, 59, 500), OFFSET_PONE)); 747 } 748 749 @Test 750 public void test_plus_PlusAdjuster_noChange() { 751 OffsetTime t = TEST_11_30_59_500_PONE.plus(MockSimplePeriod.of(0, SECONDS)); 752 assertEquals(t, TEST_11_30_59_500_PONE); 753 } 754 755 @Test 756 public void test_plus_PlusAdjuster_zero() { 757 OffsetTime t = TEST_11_30_59_500_PONE.plus(Period.ZERO); 758 assertEquals(t, TEST_11_30_59_500_PONE); 759 } 760 761 @Test(expectedExceptions=NullPointerException.class) 762 public void test_plus_PlusAdjuster_null() { 763 TEST_11_30_59_500_PONE.plus(null); 764 } 765 766 //----------------------------------------------------------------------- 767 // plusHours() 768 //----------------------------------------------------------------------- 769 @Test 770 public void test_plusHours() { 771 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 772 OffsetTime test = base.plusHours(13); 773 assertEquals(test, OffsetTime.of(LocalTime.of(0, 30, 59), OFFSET_PONE)); 774 } 775 776 @Test 777 public void test_plusHours_zero() { 778 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 779 OffsetTime test = base.plusHours(0); 780 assertEquals(test, base); 781 } 782 783 //----------------------------------------------------------------------- 784 // plusMinutes() 785 //----------------------------------------------------------------------- 786 @Test 787 public void test_plusMinutes() { 788 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 789 OffsetTime test = base.plusMinutes(30); 790 assertEquals(test, OffsetTime.of(LocalTime.of(12, 0, 59), OFFSET_PONE)); 791 } 792 793 @Test 794 public void test_plusMinutes_zero() { 795 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 796 OffsetTime test = base.plusMinutes(0); 797 assertEquals(test, base); 798 } 799 800 //----------------------------------------------------------------------- 801 // plusSeconds() 802 //----------------------------------------------------------------------- 803 @Test 804 public void test_plusSeconds() { 805 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 806 OffsetTime test = base.plusSeconds(1); 807 assertEquals(test, OffsetTime.of(LocalTime.of(11, 31, 0), OFFSET_PONE)); 808 } 809 810 @Test 811 public void test_plusSeconds_zero() { 812 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 813 OffsetTime test = base.plusSeconds(0); 814 assertEquals(test, base); 815 } 816 817 //----------------------------------------------------------------------- 818 // plusNanos() 819 //----------------------------------------------------------------------- 820 @Test 821 public void test_plusNanos() { 822 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59, 0), OFFSET_PONE); 823 OffsetTime test = base.plusNanos(1); 824 assertEquals(test, OffsetTime.of(LocalTime.of(11, 30, 59, 1), OFFSET_PONE)); 825 } 826 827 @Test 828 public void test_plusNanos_zero() { 829 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 830 OffsetTime test = base.plusNanos(0); 831 assertEquals(test, base); 832 } 833 834 //----------------------------------------------------------------------- 835 // minus(MinusAdjuster) 836 //----------------------------------------------------------------------- 837 @Test 838 public void test_minus_MinusAdjuster() { 839 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MINUTES); 840 OffsetTime t = TEST_11_30_59_500_PONE.minus(period); 841 assertEquals(t, OffsetTime.of(LocalTime.of(11, 23, 59, 500), OFFSET_PONE)); 842 } 843 844 @Test 845 public void test_minus_MinusAdjuster_noChange() { 846 OffsetTime t = TEST_11_30_59_500_PONE.minus(MockSimplePeriod.of(0, SECONDS)); 847 assertEquals(t, TEST_11_30_59_500_PONE); 848 } 849 850 @Test 851 public void test_minus_MinusAdjuster_zero() { 852 OffsetTime t = TEST_11_30_59_500_PONE.minus(Period.ZERO); 853 assertEquals(t, TEST_11_30_59_500_PONE); 854 } 855 856 @Test(expectedExceptions=NullPointerException.class) 857 public void test_minus_MinusAdjuster_null() { 858 TEST_11_30_59_500_PONE.minus(null); 859 } 860 861 //----------------------------------------------------------------------- 862 // minusHours() 863 //----------------------------------------------------------------------- 864 @Test 865 public void test_minusHours() { 866 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 867 OffsetTime test = base.minusHours(-13); 868 assertEquals(test, OffsetTime.of(LocalTime.of(0, 30, 59), OFFSET_PONE)); 869 } 870 871 @Test 872 public void test_minusHours_zero() { 873 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 874 OffsetTime test = base.minusHours(0); 875 assertEquals(test, base); 876 } 877 878 //----------------------------------------------------------------------- 879 // minusMinutes() 880 //----------------------------------------------------------------------- 881 @Test 882 public void test_minusMinutes() { 883 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 884 OffsetTime test = base.minusMinutes(50); 885 assertEquals(test, OffsetTime.of(LocalTime.of(10, 40, 59), OFFSET_PONE)); 886 } 887 888 @Test 889 public void test_minusMinutes_zero() { 890 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 891 OffsetTime test = base.minusMinutes(0); 892 assertEquals(test, base); 893 } 894 895 //----------------------------------------------------------------------- 896 // minusSeconds() 897 //----------------------------------------------------------------------- 898 @Test 899 public void test_minusSeconds() { 900 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 901 OffsetTime test = base.minusSeconds(60); 902 assertEquals(test, OffsetTime.of(LocalTime.of(11, 29, 59), OFFSET_PONE)); 903 } 904 905 @Test 906 public void test_minusSeconds_zero() { 907 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 908 OffsetTime test = base.minusSeconds(0); 909 assertEquals(test, base); 910 } 911 912 //----------------------------------------------------------------------- 913 // minusNanos() 914 //----------------------------------------------------------------------- 915 @Test 916 public void test_minusNanos() { 917 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59, 0), OFFSET_PONE); 918 OffsetTime test = base.minusNanos(1); 919 assertEquals(test, OffsetTime.of(LocalTime.of(11, 30, 58, 999999999), OFFSET_PONE)); 920 } 921 922 @Test 923 public void test_minusNanos_zero() { 924 OffsetTime base = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 925 OffsetTime test = base.minusNanos(0); 926 assertEquals(test, base); 927 } 928 929 //----------------------------------------------------------------------- 930 // compareTo() 931 //----------------------------------------------------------------------- 932 @Test 933 public void test_compareTo_time() { 934 OffsetTime a = OffsetTime.of(LocalTime.of(11, 29), OFFSET_PONE); 935 OffsetTime b = OffsetTime.of(LocalTime.of(11, 30), OFFSET_PONE); // a is before b due to time 936 assertEquals(a.compareTo(b) < 0, true); 937 assertEquals(b.compareTo(a) > 0, true); 938 assertEquals(a.compareTo(a) == 0, true); 939 assertEquals(b.compareTo(b) == 0, true); 940 assertEquals(convertInstant(a).compareTo(convertInstant(b)) < 0, true); 941 } 942 943 @Test 944 public void test_compareTo_offset() { 945 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30), OFFSET_PTWO); 946 OffsetTime b = OffsetTime.of(LocalTime.of(11, 30), OFFSET_PONE); // a is before b due to offset 947 assertEquals(a.compareTo(b) < 0, true); 948 assertEquals(b.compareTo(a) > 0, true); 949 assertEquals(a.compareTo(a) == 0, true); 950 assertEquals(b.compareTo(b) == 0, true); 951 assertEquals(convertInstant(a).compareTo(convertInstant(b)) < 0, true); 952 } 953 954 @Test 955 public void test_compareTo_both() { 956 OffsetTime a = OffsetTime.of(LocalTime.of(11, 50), OFFSET_PTWO); 957 OffsetTime b = OffsetTime.of(LocalTime.of(11, 20), OFFSET_PONE); // a is before b on instant scale 958 assertEquals(a.compareTo(b) < 0, true); 959 assertEquals(b.compareTo(a) > 0, true); 960 assertEquals(a.compareTo(a) == 0, true); 961 assertEquals(b.compareTo(b) == 0, true); 962 assertEquals(convertInstant(a).compareTo(convertInstant(b)) < 0, true); 963 } 964 965 @Test 966 public void test_compareTo_bothNearStartOfDay() { 967 OffsetTime a = OffsetTime.of(LocalTime.of(0, 10), OFFSET_PONE); 968 OffsetTime b = OffsetTime.of(LocalTime.of(2, 30), OFFSET_PTWO); // a is before b on instant scale 969 assertEquals(a.compareTo(b) < 0, true); 970 assertEquals(b.compareTo(a) > 0, true); 971 assertEquals(a.compareTo(a) == 0, true); 972 assertEquals(b.compareTo(b) == 0, true); 973 assertEquals(convertInstant(a).compareTo(convertInstant(b)) < 0, true); 974 } 975 976 @Test 977 public void test_compareTo_hourDifference() { 978 OffsetTime a = OffsetTime.of(LocalTime.of(10, 0), OFFSET_PONE); 979 OffsetTime b = OffsetTime.of(LocalTime.of(11, 0), OFFSET_PTWO); // a is before b despite being same time-line time 980 assertEquals(a.compareTo(b) < 0, true); 981 assertEquals(b.compareTo(a) > 0, true); 982 assertEquals(a.compareTo(a) == 0, true); 983 assertEquals(b.compareTo(b) == 0, true); 984 assertEquals(convertInstant(a).compareTo(convertInstant(b)) == 0, true); 985 } 986 987 @Test(expectedExceptions=NullPointerException.class) 988 public void test_compareTo_null() { 989 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 990 a.compareTo(null); 991 } 992 993 @Test(expectedExceptions=ClassCastException.class) 994 @SuppressWarnings({"unchecked", "rawtypes"}) 995 public void compareToNonOffsetTime() { 996 Comparable c = TEST_11_30_59_500_PONE; 997 c.compareTo(new Object()); 998 } 999 1000 private Instant convertInstant(OffsetTime ot) { 1001 return DATE.atTime(ot.toLocalTime()).toInstant(ot.getOffset()); 1002 } 1003 1004 //----------------------------------------------------------------------- 1005 // isAfter() / isBefore() / isEqual() 1006 //----------------------------------------------------------------------- 1007 @Test 1008 public void test_isBeforeIsAfterIsEqual1() { 1009 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 58), OFFSET_PONE); 1010 OffsetTime b = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); // a is before b due to time 1011 assertEquals(a.isBefore(b), true); 1012 assertEquals(a.isEqual(b), false); 1013 assertEquals(a.isAfter(b), false); 1014 1015 assertEquals(b.isBefore(a), false); 1016 assertEquals(b.isEqual(a), false); 1017 assertEquals(b.isAfter(a), true); 1018 1019 assertEquals(a.isBefore(a), false); 1020 assertEquals(b.isBefore(b), false); 1021 1022 assertEquals(a.isEqual(a), true); 1023 assertEquals(b.isEqual(b), true); 1024 1025 assertEquals(a.isAfter(a), false); 1026 assertEquals(b.isAfter(b), false); 1027 } 1028 1029 @Test 1030 public void test_isBeforeIsAfterIsEqual1nanos() { 1031 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59, 3), OFFSET_PONE); 1032 OffsetTime b = OffsetTime.of(LocalTime.of(11, 30, 59, 4), OFFSET_PONE); // a is before b due to time 1033 assertEquals(a.isBefore(b), true); 1034 assertEquals(a.isEqual(b), false); 1035 assertEquals(a.isAfter(b), false); 1036 1037 assertEquals(b.isBefore(a), false); 1038 assertEquals(b.isEqual(a), false); 1039 assertEquals(b.isAfter(a), true); 1040 1041 assertEquals(a.isBefore(a), false); 1042 assertEquals(b.isBefore(b), false); 1043 1044 assertEquals(a.isEqual(a), true); 1045 assertEquals(b.isEqual(b), true); 1046 1047 assertEquals(a.isAfter(a), false); 1048 assertEquals(b.isAfter(b), false); 1049 } 1050 1051 @Test 1052 public void test_isBeforeIsAfterIsEqual2() { 1053 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PTWO); 1054 OffsetTime b = OffsetTime.of(LocalTime.of(11, 30, 58), OFFSET_PONE); // a is before b due to offset 1055 assertEquals(a.isBefore(b), true); 1056 assertEquals(a.isEqual(b), false); 1057 assertEquals(a.isAfter(b), false); 1058 1059 assertEquals(b.isBefore(a), false); 1060 assertEquals(b.isEqual(a), false); 1061 assertEquals(b.isAfter(a), true); 1062 1063 assertEquals(a.isBefore(a), false); 1064 assertEquals(b.isBefore(b), false); 1065 1066 assertEquals(a.isEqual(a), true); 1067 assertEquals(b.isEqual(b), true); 1068 1069 assertEquals(a.isAfter(a), false); 1070 assertEquals(b.isAfter(b), false); 1071 } 1072 1073 @Test 1074 public void test_isBeforeIsAfterIsEqual2nanos() { 1075 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59, 4), ZoneOffset.ofTotalSeconds(OFFSET_PONE.getTotalSeconds() + 1)); 1076 OffsetTime b = OffsetTime.of(LocalTime.of(11, 30, 59, 3), OFFSET_PONE); // a is before b due to offset 1077 assertEquals(a.isBefore(b), true); 1078 assertEquals(a.isEqual(b), false); 1079 assertEquals(a.isAfter(b), false); 1080 1081 assertEquals(b.isBefore(a), false); 1082 assertEquals(b.isEqual(a), false); 1083 assertEquals(b.isAfter(a), true); 1084 1085 assertEquals(a.isBefore(a), false); 1086 assertEquals(b.isBefore(b), false); 1087 1088 assertEquals(a.isEqual(a), true); 1089 assertEquals(b.isEqual(b), true); 1090 1091 assertEquals(a.isAfter(a), false); 1092 assertEquals(b.isAfter(b), false); 1093 } 1094 1095 @Test 1096 public void test_isBeforeIsAfterIsEqual_instantComparison() { 1097 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PTWO); 1098 OffsetTime b = OffsetTime.of(LocalTime.of(10, 30, 59), OFFSET_PONE); // a is same instant as b 1099 assertEquals(a.isBefore(b), false); 1100 assertEquals(a.isEqual(b), true); 1101 assertEquals(a.isAfter(b), false); 1102 1103 assertEquals(b.isBefore(a), false); 1104 assertEquals(b.isEqual(a), true); 1105 assertEquals(b.isAfter(a), false); 1106 1107 assertEquals(a.isBefore(a), false); 1108 assertEquals(b.isBefore(b), false); 1109 1110 assertEquals(a.isEqual(a), true); 1111 assertEquals(b.isEqual(b), true); 1112 1113 assertEquals(a.isAfter(a), false); 1114 assertEquals(b.isAfter(b), false); 1115 } 1116 1117 @Test(expectedExceptions=NullPointerException.class) 1118 public void test_isBefore_null() { 1119 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 1120 a.isBefore(null); 1121 } 1122 1123 @Test(expectedExceptions=NullPointerException.class) 1124 public void test_isAfter_null() { 1125 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 1126 a.isAfter(null); 1127 } 1128 1129 @Test(expectedExceptions=NullPointerException.class) 1130 public void test_isEqual_null() { 1131 OffsetTime a = OffsetTime.of(LocalTime.of(11, 30, 59), OFFSET_PONE); 1132 a.isEqual(null); 1133 } 1134 1135 //----------------------------------------------------------------------- 1136 // equals() / hashCode() 1137 //----------------------------------------------------------------------- 1138 @Test(dataProvider="sampleTimes") 1139 public void test_equals_true(int h, int m, int s, int n, ZoneOffset ignored) { 1140 OffsetTime a = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PONE); 1141 OffsetTime b = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PONE); 1142 assertEquals(a.equals(b), true); 1143 assertEquals(a.hashCode() == b.hashCode(), true); 1144 } 1145 @Test(dataProvider="sampleTimes") 1146 public void test_equals_false_hour_differs(int h, int m, int s, int n, ZoneOffset ignored) { 1147 h = (h == 23 ? 22 : h); 1148 OffsetTime a = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PONE); 1149 OffsetTime b = OffsetTime.of(LocalTime.of(h + 1, m, s, n), OFFSET_PONE); 1150 assertEquals(a.equals(b), false); 1151 } 1152 @Test(dataProvider="sampleTimes") 1153 public void test_equals_false_minute_differs(int h, int m, int s, int n, ZoneOffset ignored) { 1154 m = (m == 59 ? 58 : m); 1155 OffsetTime a = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PONE); 1156 OffsetTime b = OffsetTime.of(LocalTime.of(h, m + 1, s, n), OFFSET_PONE); 1157 assertEquals(a.equals(b), false); 1158 } 1159 @Test(dataProvider="sampleTimes") 1160 public void test_equals_false_second_differs(int h, int m, int s, int n, ZoneOffset ignored) { 1161 s = (s == 59 ? 58 : s); 1162 OffsetTime a = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PONE); 1163 OffsetTime b = OffsetTime.of(LocalTime.of(h, m, s + 1, n), OFFSET_PONE); 1164 assertEquals(a.equals(b), false); 1165 } 1166 @Test(dataProvider="sampleTimes") 1167 public void test_equals_false_nano_differs(int h, int m, int s, int n, ZoneOffset ignored) { 1168 n = (n == 999999999 ? 999999998 : n); 1169 OffsetTime a = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PONE); 1170 OffsetTime b = OffsetTime.of(LocalTime.of(h, m, s, n + 1), OFFSET_PONE); 1171 assertEquals(a.equals(b), false); 1172 } 1173 @Test(dataProvider="sampleTimes") 1174 public void test_equals_false_offset_differs(int h, int m, int s, int n, ZoneOffset ignored) { 1175 OffsetTime a = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PONE); 1176 OffsetTime b = OffsetTime.of(LocalTime.of(h, m, s, n), OFFSET_PTWO); 1177 assertEquals(a.equals(b), false); 1178 } 1179 1180 @Test 1181 public void test_equals_itself_true() { 1182 assertEquals(TEST_11_30_59_500_PONE.equals(TEST_11_30_59_500_PONE), true); 1183 } 1184 1185 @Test 1186 public void test_equals_string_false() { 1187 assertEquals(TEST_11_30_59_500_PONE.equals("2007-07-15"), false); 1188 } 1189 1190 @Test 1191 public void test_equals_null_false() { 1192 assertEquals(TEST_11_30_59_500_PONE.equals(null), false); 1193 } 1194 1195 //----------------------------------------------------------------------- 1196 // toString() 1197 //----------------------------------------------------------------------- 1198 @DataProvider(name="sampleToString") 1199 Object[][] provider_sampleToString() { 1200 return new Object[][] { 1201 {11, 30, 59, 0, "Z", "11:30:59Z"}, 1202 {11, 30, 59, 0, "+01:00", "11:30:59+01:00"}, 1203 {11, 30, 59, 999000000, "Z", "11:30:59.999Z"}, 1204 {11, 30, 59, 999000000, "+01:00", "11:30:59.999+01:00"}, 1205 {11, 30, 59, 999000, "Z", "11:30:59.000999Z"}, 1206 {11, 30, 59, 999000, "+01:00", "11:30:59.000999+01:00"}, 1207 {11, 30, 59, 999, "Z", "11:30:59.000000999Z"}, 1208 {11, 30, 59, 999, "+01:00", "11:30:59.000000999+01:00"}, 1209 }; 1210 } 1211 1212 @Test(dataProvider="sampleToString") 1213 public void test_toString(int h, int m, int s, int n, String offsetId, String expected) { 1214 OffsetTime t = OffsetTime.of(LocalTime.of(h, m, s, n), ZoneOffset.of(offsetId)); 1215 String str = t.toString(); 1216 assertEquals(str, expected); 1217 } 1218 1219 //----------------------------------------------------------------------- 1220 // format(DateTimeFormatter) 1221 //----------------------------------------------------------------------- 1222 @Test 1223 public void test_format_formatter() { 1224 DateTimeFormatter f = DateTimeFormatter.ofPattern("H m s"); 1225 String t = OffsetTime.of(LocalTime.of(11, 30), OFFSET_PONE).format(f); 1226 assertEquals(t, "11 30 0"); 1227 } 1228 1229 @Test(expectedExceptions=NullPointerException.class) 1230 public void test_format_formatter_null() { 1231 OffsetTime.of(LocalTime.of(11, 30), OFFSET_PONE).format(null); 1232 } 1233 1234 } 1235