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