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