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