1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * This file is available under and governed by the GNU General Public 26 * License version 2 only, as published by the Free Software Foundation. 27 * However, the following notice accompanied the original version of this 28 * file: 29 * 30 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 31 * 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions are met: 36 * 37 * * Redistributions of source code must retain the above copyright notice, 38 * this list of conditions and the following disclaimer. 39 * 40 * * Redistributions in binary form must reproduce the above copyright notice, 41 * this list of conditions and the following disclaimer in the documentation 42 * and/or other materials provided with the distribution. 43 * 44 * * Neither the name of JSR-310 nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package tck.java.time; 61 62 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 63 import static org.testng.Assert.assertEquals; 64 import static org.testng.Assert.assertSame; 65 import static org.testng.Assert.assertTrue; 66 import static org.testng.Assert.fail; 67 68 import java.io.ByteArrayOutputStream; 69 import java.io.DataOutputStream; 70 import java.time.DateTimeException; 71 import java.time.Duration; 72 import java.time.Instant; 73 import java.time.LocalDate; 74 import java.time.LocalDateTime; 75 import java.time.LocalTime; 76 import java.time.ZoneOffset; 77 import java.time.ZonedDateTime; 78 import java.time.temporal.ChronoField; 79 import java.time.temporal.JulianFields; 80 import java.time.temporal.Queries; 81 import java.time.temporal.TemporalAccessor; 82 import java.time.temporal.TemporalField; 83 import java.time.temporal.TemporalQuery; 84 import java.util.ArrayList; 85 import java.util.Arrays; 86 import java.util.List; 87 88 import org.testng.annotations.DataProvider; 89 import org.testng.annotations.Test; 90 91 /** 92 * Test ZoneOffset. 93 */ 94 @Test 95 public class TCKZoneOffset extends AbstractDateTimeTest { 96 97 //----------------------------------------------------------------------- 98 @Override 99 protected List<TemporalAccessor> samples() { 100 TemporalAccessor[] array = {ZoneOffset.ofHours(1), ZoneOffset.ofHoursMinutesSeconds(-5, -6, -30) }; 101 return Arrays.asList(array); 102 } 103 104 @Override 105 protected List<TemporalField> validFields() { 106 TemporalField[] array = { 107 OFFSET_SECONDS, 108 }; 109 return Arrays.asList(array); 110 } 111 112 @Override 113 protected List<TemporalField> invalidFields() { 114 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 115 list.removeAll(validFields()); 116 list.add(JulianFields.JULIAN_DAY); 117 list.add(JulianFields.MODIFIED_JULIAN_DAY); 118 list.add(JulianFields.RATA_DIE); 119 return list; 120 } 121 122 //----------------------------------------------------------------------- 123 @Test 124 public void test_serialization() throws Exception { 125 assertSerializable(ZoneOffset.of("+01:30")); 126 } 127 128 @Test 129 public void test_serialization_format_quarterPositive() throws Exception { 130 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 131 try (DataOutputStream dos = new DataOutputStream(baos) ) { 132 dos.writeByte(8); 133 dos.writeByte(6); // stored as quarter hours 134 } 135 byte[] bytes = baos.toByteArray(); 136 assertSerializedBySer(ZoneOffset.ofHoursMinutes(1, 30), bytes); 137 } 138 139 @Test 140 public void test_serialization_format_quarterNegative() throws Exception { 141 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 142 try (DataOutputStream dos = new DataOutputStream(baos) ) { 143 dos.writeByte(8); 144 dos.writeByte(-10); // stored as quarter hours 145 } 146 byte[] bytes = baos.toByteArray(); 147 assertSerializedBySer(ZoneOffset.ofHoursMinutes(-2, -30), bytes); 148 } 149 150 @Test 151 public void test_serialization_format_full() throws Exception { 152 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 153 try (DataOutputStream dos = new DataOutputStream(baos) ) { 154 dos.writeByte(8); 155 dos.writeByte(127); 156 dos.writeInt(53265); 157 } 158 byte[] bytes = baos.toByteArray(); 159 assertSerializedBySer(ZoneOffset.ofTotalSeconds(53265), bytes); 160 } 161 162 //----------------------------------------------------------------------- 163 // constants 164 //----------------------------------------------------------------------- 165 @Test 166 public void test_constant_UTC() { 167 ZoneOffset test = ZoneOffset.UTC; 168 doTestOffset(test, 0, 0, 0); 169 } 170 171 @Test 172 public void test_constant_MIN() { 173 ZoneOffset test = ZoneOffset.MIN; 174 doTestOffset(test, -18, 0, 0); 175 } 176 177 @Test 178 public void test_constant_MAX() { 179 ZoneOffset test = ZoneOffset.MAX; 180 doTestOffset(test, 18, 0, 0); 181 } 182 183 //----------------------------------------------------------------------- 184 // of(String) 185 //----------------------------------------------------------------------- 186 @Test(groups={"tck"}) 187 public void test_factory_string_UTC() { 188 String[] values = new String[] { 189 "Z", "+0", 190 "+00","+0000","+00:00","+000000","+00:00:00", 191 "-00","-0000","-00:00","-000000","-00:00:00", 192 }; 193 for (int i = 0; i < values.length; i++) { 194 ZoneOffset test = ZoneOffset.of(values[i]); 195 assertSame(test, ZoneOffset.UTC); 196 } 197 } 198 199 @Test(groups={"tck"}) 200 public void test_factory_string_invalid() { 201 String[] values = new String[] { 202 "","A","B","C","D","E","F","G","H","I","J","K","L","M", 203 "N","O","P","Q","R","S","T","U","V","W","X","Y","ZZ", 204 "0", "+0:00","+00:0","+0:0", 205 "+000","+00000", 206 "+0:00:00","+00:0:00","+00:00:0","+0:0:0","+0:0:00","+00:0:0","+0:00:0", 207 "1", "+01_00","+01;00","+01@00","+01:AA", 208 "+19","+19:00","+18:01","+18:00:01","+1801","+180001", 209 "-0:00","-00:0","-0:0", 210 "-000","-00000", 211 "-0:00:00","-00:0:00","-00:00:0","-0:0:0","-0:0:00","-00:0:0","-0:00:0", 212 "-19","-19:00","-18:01","-18:00:01","-1801","-180001", 213 "-01_00","-01;00","-01@00","-01:AA", 214 "@01:00", 215 }; 216 for (int i = 0; i < values.length; i++) { 217 try { 218 ZoneOffset.of(values[i]); 219 fail("Should have failed:" + values[i]); 220 } catch (DateTimeException ex) { 221 // expected 222 } 223 } 224 } 225 226 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 227 public void test_factory_string_null() { 228 ZoneOffset.of((String) null); 229 } 230 231 //----------------------------------------------------------------------- 232 @Test(groups={"tck"}) 233 public void test_factory_string_singleDigitHours() { 234 for (int i = -9; i <= 9; i++) { 235 String str = (i < 0 ? "-" : "+") + Math.abs(i); 236 ZoneOffset test = ZoneOffset.of(str); 237 doTestOffset(test, i, 0, 0); 238 } 239 } 240 241 @Test(groups={"tck"}) 242 public void test_factory_string_hours() { 243 for (int i = -18; i <= 18; i++) { 244 String str = (i < 0 ? "-" : "+") + Integer.toString(Math.abs(i) + 100).substring(1); 245 ZoneOffset test = ZoneOffset.of(str); 246 doTestOffset(test, i, 0, 0); 247 } 248 } 249 250 @Test(groups={"tck"}) 251 public void test_factory_string_hours_minutes_noColon() { 252 for (int i = -17; i <= 17; i++) { 253 for (int j = -59; j <= 59; j++) { 254 if ((i < 0 && j <= 0) || (i > 0 && j >= 0) || i == 0) { 255 String str = (i < 0 || j < 0 ? "-" : "+") + 256 Integer.toString(Math.abs(i) + 100).substring(1) + 257 Integer.toString(Math.abs(j) + 100).substring(1); 258 ZoneOffset test = ZoneOffset.of(str); 259 doTestOffset(test, i, j, 0); 260 } 261 } 262 } 263 ZoneOffset test1 = ZoneOffset.of("-1800"); 264 doTestOffset(test1, -18, 0, 0); 265 ZoneOffset test2 = ZoneOffset.of("+1800"); 266 doTestOffset(test2, 18, 0, 0); 267 } 268 269 @Test(groups={"tck"}) 270 public void test_factory_string_hours_minutes_colon() { 271 for (int i = -17; i <= 17; i++) { 272 for (int j = -59; j <= 59; j++) { 273 if ((i < 0 && j <= 0) || (i > 0 && j >= 0) || i == 0) { 274 String str = (i < 0 || j < 0 ? "-" : "+") + 275 Integer.toString(Math.abs(i) + 100).substring(1) + ":" + 276 Integer.toString(Math.abs(j) + 100).substring(1); 277 ZoneOffset test = ZoneOffset.of(str); 278 doTestOffset(test, i, j, 0); 279 } 280 } 281 } 282 ZoneOffset test1 = ZoneOffset.of("-18:00"); 283 doTestOffset(test1, -18, 0, 0); 284 ZoneOffset test2 = ZoneOffset.of("+18:00"); 285 doTestOffset(test2, 18, 0, 0); 286 } 287 288 @Test(groups={"tck"}) 289 public void test_factory_string_hours_minutes_seconds_noColon() { 290 for (int i = -17; i <= 17; i++) { 291 for (int j = -59; j <= 59; j++) { 292 for (int k = -59; k <= 59; k++) { 293 if ((i < 0 && j <= 0 && k <= 0) || (i > 0 && j >= 0 && k >= 0) || 294 (i == 0 && ((j < 0 && k <= 0) || (j > 0 && k >= 0) || j == 0))) { 295 String str = (i < 0 || j < 0 || k < 0 ? "-" : "+") + 296 Integer.toString(Math.abs(i) + 100).substring(1) + 297 Integer.toString(Math.abs(j) + 100).substring(1) + 298 Integer.toString(Math.abs(k) + 100).substring(1); 299 ZoneOffset test = ZoneOffset.of(str); 300 doTestOffset(test, i, j, k); 301 } 302 } 303 } 304 } 305 ZoneOffset test1 = ZoneOffset.of("-180000"); 306 doTestOffset(test1, -18, 0, 0); 307 ZoneOffset test2 = ZoneOffset.of("+180000"); 308 doTestOffset(test2, 18, 0, 0); 309 } 310 311 @Test(groups={"tck"}) 312 public void test_factory_string_hours_minutes_seconds_colon() { 313 for (int i = -17; i <= 17; i++) { 314 for (int j = -59; j <= 59; j++) { 315 for (int k = -59; k <= 59; k++) { 316 if ((i < 0 && j <= 0 && k <= 0) || (i > 0 && j >= 0 && k >= 0) || 317 (i == 0 && ((j < 0 && k <= 0) || (j > 0 && k >= 0) || j == 0))) { 318 String str = (i < 0 || j < 0 || k < 0 ? "-" : "+") + 319 Integer.toString(Math.abs(i) + 100).substring(1) + ":" + 320 Integer.toString(Math.abs(j) + 100).substring(1) + ":" + 321 Integer.toString(Math.abs(k) + 100).substring(1); 322 ZoneOffset test = ZoneOffset.of(str); 323 doTestOffset(test, i, j, k); 324 } 325 } 326 } 327 } 328 ZoneOffset test1 = ZoneOffset.of("-18:00:00"); 329 doTestOffset(test1, -18, 0, 0); 330 ZoneOffset test2 = ZoneOffset.of("+18:00:00"); 331 doTestOffset(test2, 18, 0, 0); 332 } 333 334 //----------------------------------------------------------------------- 335 @Test(groups={"tck"}) 336 public void test_factory_int_hours() { 337 for (int i = -18; i <= 18; i++) { 338 ZoneOffset test = ZoneOffset.ofHours(i); 339 doTestOffset(test, i, 0, 0); 340 } 341 } 342 343 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 344 public void test_factory_int_hours_tooBig() { 345 ZoneOffset.ofHours(19); 346 } 347 348 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 349 public void test_factory_int_hours_tooSmall() { 350 ZoneOffset.ofHours(-19); 351 } 352 353 //----------------------------------------------------------------------- 354 @Test(groups={"tck"}) 355 public void test_factory_int_hours_minutes() { 356 for (int i = -17; i <= 17; i++) { 357 for (int j = -59; j <= 59; j++) { 358 if ((i < 0 && j <= 0) || (i > 0 && j >= 0) || i == 0) { 359 ZoneOffset test = ZoneOffset.ofHoursMinutes(i, j); 360 doTestOffset(test, i, j, 0); 361 } 362 } 363 } 364 ZoneOffset test1 = ZoneOffset.ofHoursMinutes(-18, 0); 365 doTestOffset(test1, -18, 0, 0); 366 ZoneOffset test2 = ZoneOffset.ofHoursMinutes(18, 0); 367 doTestOffset(test2, 18, 0, 0); 368 } 369 370 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 371 public void test_factory_int_hours_minutes_tooBig() { 372 ZoneOffset.ofHoursMinutes(19, 0); 373 } 374 375 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 376 public void test_factory_int_hours_minutes_tooSmall() { 377 ZoneOffset.ofHoursMinutes(-19, 0); 378 } 379 380 //----------------------------------------------------------------------- 381 @Test(groups={"tck"}) 382 public void test_factory_int_hours_minutes_seconds() { 383 for (int i = -17; i <= 17; i++) { 384 for (int j = -59; j <= 59; j++) { 385 for (int k = -59; k <= 59; k++) { 386 if ((i < 0 && j <= 0 && k <= 0) || (i > 0 && j >= 0 && k >= 0) || 387 (i == 0 && ((j < 0 && k <= 0) || (j > 0 && k >= 0) || j == 0))) { 388 ZoneOffset test = ZoneOffset.ofHoursMinutesSeconds(i, j, k); 389 doTestOffset(test, i, j, k); 390 } 391 } 392 } 393 } 394 ZoneOffset test1 = ZoneOffset.ofHoursMinutesSeconds(-18, 0, 0); 395 doTestOffset(test1, -18, 0, 0); 396 ZoneOffset test2 = ZoneOffset.ofHoursMinutesSeconds(18, 0, 0); 397 doTestOffset(test2, 18, 0, 0); 398 } 399 400 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 401 public void test_factory_int_hours_minutes_seconds_plusHoursMinusMinutes() { 402 ZoneOffset.ofHoursMinutesSeconds(1, -1, 0); 403 } 404 405 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 406 public void test_factory_int_hours_minutes_seconds_plusHoursMinusSeconds() { 407 ZoneOffset.ofHoursMinutesSeconds(1, 0, -1); 408 } 409 410 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 411 public void test_factory_int_hours_minutes_seconds_minusHoursPlusMinutes() { 412 ZoneOffset.ofHoursMinutesSeconds(-1, 1, 0); 413 } 414 415 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 416 public void test_factory_int_hours_minutes_seconds_minusHoursPlusSeconds() { 417 ZoneOffset.ofHoursMinutesSeconds(-1, 0, 1); 418 } 419 420 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 421 public void test_factory_int_hours_minutes_seconds_zeroHoursMinusMinutesPlusSeconds() { 422 ZoneOffset.ofHoursMinutesSeconds(0, -1, 1); 423 } 424 425 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 426 public void test_factory_int_hours_minutes_seconds_zeroHoursPlusMinutesMinusSeconds() { 427 ZoneOffset.ofHoursMinutesSeconds(0, 1, -1); 428 } 429 430 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 431 public void test_factory_int_hours_minutes_seconds_minutesTooLarge() { 432 ZoneOffset.ofHoursMinutesSeconds(0, 60, 0); 433 } 434 435 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 436 public void test_factory_int_hours_minutes_seconds_minutesTooSmall() { 437 ZoneOffset.ofHoursMinutesSeconds(0, -60, 0); 438 } 439 440 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 441 public void test_factory_int_hours_minutes_seconds_secondsTooLarge() { 442 ZoneOffset.ofHoursMinutesSeconds(0, 0, 60); 443 } 444 445 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 446 public void test_factory_int_hours_minutes_seconds_secondsTooSmall() { 447 ZoneOffset.ofHoursMinutesSeconds(0, 0, 60); 448 } 449 450 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 451 public void test_factory_int_hours_minutes_seconds_hoursTooBig() { 452 ZoneOffset.ofHoursMinutesSeconds(19, 0, 0); 453 } 454 455 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 456 public void test_factory_int_hours_minutes_seconds_hoursTooSmall() { 457 ZoneOffset.ofHoursMinutesSeconds(-19, 0, 0); 458 } 459 460 //----------------------------------------------------------------------- 461 @Test(groups={"tck"}) 462 public void test_factory_ofTotalSeconds() { 463 assertEquals(ZoneOffset.ofTotalSeconds(60 * 60 + 1), ZoneOffset.ofHoursMinutesSeconds(1, 0, 1)); 464 assertEquals(ZoneOffset.ofTotalSeconds(18 * 60 * 60), ZoneOffset.ofHours(18)); 465 assertEquals(ZoneOffset.ofTotalSeconds(-18 * 60 * 60), ZoneOffset.ofHours(-18)); 466 } 467 468 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 469 public void test_factory_ofTotalSeconds_tooLarge() { 470 ZoneOffset.ofTotalSeconds(18 * 60 * 60 + 1); 471 } 472 473 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 474 public void test_factory_ofTotalSeconds_tooSmall() { 475 ZoneOffset.ofTotalSeconds(-18 * 60 * 60 - 1); 476 } 477 478 //----------------------------------------------------------------------- 479 // from() 480 //----------------------------------------------------------------------- 481 @Test(groups={"tck"}) 482 public void test_factory_CalendricalObject() { 483 assertEquals(ZoneOffset.from(ZonedDateTime.of(LocalDateTime.of(LocalDate.of(2007, 7, 15), 484 LocalTime.of(17, 30)), ZoneOffset.ofHours(2))), ZoneOffset.ofHours(2)); 485 } 486 487 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 488 public void test_factory_CalendricalObject_invalid_noDerive() { 489 ZoneOffset.from(LocalTime.of(12, 30)); 490 } 491 492 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 493 public void test_factory_CalendricalObject_null() { 494 ZoneOffset.from((TemporalAccessor) null); 495 } 496 497 //----------------------------------------------------------------------- 498 // getTotalSeconds() 499 //----------------------------------------------------------------------- 500 @Test(groups={"tck"}) 501 public void test_getTotalSeconds() { 502 ZoneOffset offset = ZoneOffset.ofTotalSeconds(60 * 60 + 1); 503 assertEquals(offset.getTotalSeconds(), 60 * 60 + 1); 504 } 505 506 //----------------------------------------------------------------------- 507 // getId() 508 //----------------------------------------------------------------------- 509 @Test(groups={"tck"}) 510 public void test_getId() { 511 ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 0, 0); 512 assertEquals(offset.getId(), "+01:00"); 513 offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 514 assertEquals(offset.getId(), "+01:02:03"); 515 offset = ZoneOffset.UTC; 516 assertEquals(offset.getId(), "Z"); 517 } 518 519 //----------------------------------------------------------------------- 520 // getRules() 521 //----------------------------------------------------------------------- 522 @Test(groups={"tck"}) 523 public void test_getRules() { 524 ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 525 assertEquals(offset.getRules().isFixedOffset(), true); 526 assertEquals(offset.getRules().getOffset((Instant) null), offset); 527 assertEquals(offset.getRules().getDaylightSavings((Instant) null), Duration.ZERO); 528 assertEquals(offset.getRules().getStandardOffset((Instant) null), offset); 529 assertEquals(offset.getRules().nextTransition((Instant) null), null); 530 assertEquals(offset.getRules().previousTransition((Instant) null), null); 531 532 assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, offset), true); 533 assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, ZoneOffset.UTC), false); 534 assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, null), false); 535 assertEquals(offset.getRules().getOffset((LocalDateTime) null), offset); 536 assertEquals(offset.getRules().getValidOffsets((LocalDateTime) null), Arrays.asList(offset)); 537 assertEquals(offset.getRules().getTransition((LocalDateTime) null), null); 538 assertEquals(offset.getRules().getTransitions().size(), 0); 539 assertEquals(offset.getRules().getTransitionRules().size(), 0); 540 } 541 542 //----------------------------------------------------------------------- 543 // get(TemporalField) 544 //----------------------------------------------------------------------- 545 @Test 546 public void test_get_TemporalField() { 547 assertEquals(ZoneOffset.UTC.get(OFFSET_SECONDS), 0); 548 assertEquals(ZoneOffset.ofHours(-2).get(OFFSET_SECONDS), -7200); 549 assertEquals(ZoneOffset.ofHoursMinutesSeconds(0, 1, 5).get(OFFSET_SECONDS), 65); 550 } 551 552 @Test 553 public void test_getLong_TemporalField() { 554 assertEquals(ZoneOffset.UTC.getLong(OFFSET_SECONDS), 0); 555 assertEquals(ZoneOffset.ofHours(-2).getLong(OFFSET_SECONDS), -7200); 556 assertEquals(ZoneOffset.ofHoursMinutesSeconds(0, 1, 5).getLong(OFFSET_SECONDS), 65); 557 } 558 559 //----------------------------------------------------------------------- 560 // query(TemporalQuery) 561 //----------------------------------------------------------------------- 562 @DataProvider(name="query") 563 Object[][] data_query() { 564 return new Object[][] { 565 {ZoneOffset.UTC, Queries.chronology(), null}, 566 {ZoneOffset.UTC, Queries.zoneId(), null}, 567 {ZoneOffset.UTC, Queries.precision(), null}, 568 {ZoneOffset.UTC, Queries.zone(), ZoneOffset.UTC}, 569 {ZoneOffset.UTC, Queries.offset(), ZoneOffset.UTC}, 570 {ZoneOffset.UTC, Queries.localDate(), null}, 571 {ZoneOffset.UTC, Queries.localTime(), null}, 572 }; 573 } 574 575 @Test(dataProvider="query") 576 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 577 assertEquals(temporal.query(query), expected); 578 } 579 580 @Test(dataProvider="query") 581 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 582 assertEquals(query.queryFrom(temporal), expected); 583 } 584 585 @Test(expectedExceptions=NullPointerException.class) 586 public void test_query_null() { 587 ZoneOffset.UTC.query(null); 588 } 589 590 //----------------------------------------------------------------------- 591 // compareTo() 592 //----------------------------------------------------------------------- 593 @Test(groups={"tck"}) 594 public void test_compareTo() { 595 ZoneOffset offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 596 ZoneOffset offset2 = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4); 597 assertTrue(offset1.compareTo(offset2) > 0); 598 assertTrue(offset2.compareTo(offset1) < 0); 599 assertTrue(offset1.compareTo(offset1) == 0); 600 assertTrue(offset2.compareTo(offset2) == 0); 601 } 602 603 //----------------------------------------------------------------------- 604 // equals() / hashCode() 605 //----------------------------------------------------------------------- 606 @Test(groups={"tck"}) 607 public void test_equals() { 608 ZoneOffset offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 609 ZoneOffset offset2 = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4); 610 ZoneOffset offset2b = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4); 611 assertEquals(offset1.equals(offset2), false); 612 assertEquals(offset2.equals(offset1), false); 613 614 assertEquals(offset1.equals(offset1), true); 615 assertEquals(offset2.equals(offset2), true); 616 assertEquals(offset2.equals(offset2b), true); 617 618 assertEquals(offset1.hashCode() == offset1.hashCode(), true); 619 assertEquals(offset2.hashCode() == offset2.hashCode(), true); 620 assertEquals(offset2.hashCode() == offset2b.hashCode(), true); 621 } 622 623 //----------------------------------------------------------------------- 624 // toString() 625 //----------------------------------------------------------------------- 626 @Test(groups={"tck"}) 627 public void test_toString() { 628 ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 0, 0); 629 assertEquals(offset.toString(), "+01:00"); 630 offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 631 assertEquals(offset.toString(), "+01:02:03"); 632 offset = ZoneOffset.UTC; 633 assertEquals(offset.toString(), "Z"); 634 } 635 636 //----------------------------------------------------------------------- 637 //----------------------------------------------------------------------- 638 //----------------------------------------------------------------------- 639 private void doTestOffset(ZoneOffset offset, int hours, int minutes, int seconds) { 640 assertEquals(offset.getTotalSeconds(), hours * 60 * 60 + minutes * 60 + seconds); 641 final String id; 642 if (hours == 0 && minutes == 0 && seconds == 0) { 643 id = "Z"; 644 } else { 645 String str = (hours < 0 || minutes < 0 || seconds < 0) ? "-" : "+"; 646 str += Integer.toString(Math.abs(hours) + 100).substring(1); 647 str += ":"; 648 str += Integer.toString(Math.abs(minutes) + 100).substring(1); 649 if (seconds != 0) { 650 str += ":"; 651 str += Integer.toString(Math.abs(seconds) + 100).substring(1); 652 } 653 id = str; 654 } 655 assertEquals(offset.getId(), id); 656 assertEquals(offset, ZoneOffset.ofHoursMinutesSeconds(hours, minutes, seconds)); 657 if (seconds == 0) { 658 assertEquals(offset, ZoneOffset.ofHoursMinutes(hours, minutes)); 659 if (minutes == 0) { 660 assertEquals(offset, ZoneOffset.ofHours(hours)); 661 } 662 } 663 assertEquals(ZoneOffset.of(id), offset); 664 assertEquals(offset.toString(), id); 665 } 666 667 }