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