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