test/java/time/test/java/time/TestZoneId.java

Print this page




  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 test.java.time;
  61 
  62 import java.time.*;
  63 
  64 import static org.testng.Assert.assertEquals;
  65 import static org.testng.Assert.assertFalse;
  66 import static org.testng.Assert.assertNotNull;
  67 import static org.testng.Assert.assertSame;
  68 import static org.testng.Assert.assertTrue;
  69 
  70 import java.io.IOException;
  71 import java.lang.reflect.Field;
  72 import java.lang.reflect.Modifier;
  73 import java.util.HashMap;
  74 import java.util.List;
  75 import java.util.Locale;
  76 import java.util.Map;
  77 import java.util.SimpleTimeZone;
  78 import java.util.TimeZone;
  79 
  80 import java.time.temporal.Queries;
  81 import java.time.temporal.TemporalQuery;
  82 import java.time.temporal.TemporalAccessor;
  83 import java.time.temporal.TemporalField;
  84 import java.time.format.TextStyle;
  85 import java.time.zone.ZoneOffsetTransition;
  86 import java.time.zone.ZoneRules;
  87 import java.time.zone.ZoneRulesException;




  88 
  89 import org.testng.annotations.DataProvider;
  90 import org.testng.annotations.Test;
  91 
  92 /**
  93  * Test ZoneId.
  94  */
  95 @Test
  96 public class TestZoneId extends AbstractTest {
  97 
  98     private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris");
  99     public static final String LATEST_TZDB = "2010i";
 100     private static final int OVERLAP = 2;
 101     private static final int GAP = 0;
 102 
 103     //-----------------------------------------------------------------------
 104     // Basics
 105     //-----------------------------------------------------------------------
 106     public void test_immutable() {

 107         Class<ZoneId> cls = ZoneId.class;
 108         assertTrue(Modifier.isPublic(cls.getModifiers()));
 109         Field[] fields = cls.getDeclaredFields();
 110         for (Field field : fields) {
 111             if (Modifier.isStatic(field.getModifiers()) == false) {
 112                 assertTrue(Modifier.isPrivate(field.getModifiers()));
 113                 assertTrue(Modifier.isFinal(field.getModifiers()) ||
 114                         (Modifier.isVolatile(field.getModifiers()) && Modifier.isTransient(field.getModifiers())));
 115             }
 116         }
 117     }
 118 
 119     public void test_serialization_UTC() throws Exception {
 120         ZoneId test = ZoneOffset.UTC;
 121         assertSerializableAndSame(test);
 122     }
 123 
 124     public void test_serialization_fixed() throws Exception {
 125         ZoneId test = ZoneId.of("UTC+01:30");
 126         assertSerializable(test);
 127     }
 128 
 129     public void test_serialization_Europe() throws Exception {
 130         ZoneId test = ZoneId.of("Europe/London");
 131         assertSerializable(test);
 132     }
 133 
 134     public void test_serialization_America() throws Exception {
 135         ZoneId test = ZoneId.of("America/Chicago");
 136         assertSerializable(test);
 137     }
 138 
 139     //-----------------------------------------------------------------------
 140     // UTC
 141     //-----------------------------------------------------------------------
 142     public void test_constant_UTC() {
 143         ZoneId test = ZoneOffset.UTC;
 144         assertEquals(test.getId(), "Z");
 145         assertEquals(test.getText(TextStyle.FULL, Locale.UK), "Z");
 146         assertEquals(test.getRules().isFixedOffset(), true);
 147         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), ZoneOffset.UTC);
 148         checkOffset(test.getRules(), createLDT(2008, 6, 30), ZoneOffset.UTC, 1);
 149         assertSame(test, ZoneId.of("UTC+00"));
 150     }
 151 
 152     //-----------------------------------------------------------------------
 153     // OLD_IDS_PRE_2005
 154     //-----------------------------------------------------------------------
 155     public void test_constant_OLD_IDS_PRE_2005() {
 156         Map<String, String> ids = ZoneId.OLD_IDS_PRE_2005;
 157         assertEquals(ids.get("EST"), "America/Indianapolis");
 158         assertEquals(ids.get("MST"), "America/Phoenix");
 159         assertEquals(ids.get("HST"), "Pacific/Honolulu");
 160         assertEquals(ids.get("ACT"), "Australia/Darwin");
 161         assertEquals(ids.get("AET"), "Australia/Sydney");
 162         assertEquals(ids.get("AGT"), "America/Argentina/Buenos_Aires");
 163         assertEquals(ids.get("ART"), "Africa/Cairo");
 164         assertEquals(ids.get("AST"), "America/Anchorage");
 165         assertEquals(ids.get("BET"), "America/Sao_Paulo");
 166         assertEquals(ids.get("BST"), "Asia/Dhaka");
 167         assertEquals(ids.get("CAT"), "Africa/Harare");
 168         assertEquals(ids.get("CNT"), "America/St_Johns");
 169         assertEquals(ids.get("CST"), "America/Chicago");
 170         assertEquals(ids.get("CTT"), "Asia/Shanghai");
 171         assertEquals(ids.get("EAT"), "Africa/Addis_Ababa");
 172         assertEquals(ids.get("ECT"), "Europe/Paris");
 173         assertEquals(ids.get("IET"), "America/Indiana/Indianapolis");
 174         assertEquals(ids.get("IST"), "Asia/Kolkata");
 175         assertEquals(ids.get("JST"), "Asia/Tokyo");
 176         assertEquals(ids.get("MIT"), "Pacific/Apia");
 177         assertEquals(ids.get("NET"), "Asia/Yerevan");
 178         assertEquals(ids.get("NST"), "Pacific/Auckland");
 179         assertEquals(ids.get("PLT"), "Asia/Karachi");
 180         assertEquals(ids.get("PNT"), "America/Phoenix");
 181         assertEquals(ids.get("PRT"), "America/Puerto_Rico");
 182         assertEquals(ids.get("PST"), "America/Los_Angeles");
 183         assertEquals(ids.get("SST"), "Pacific/Guadalcanal");
 184         assertEquals(ids.get("VST"), "Asia/Ho_Chi_Minh");
 185     }
 186 
 187     @Test(expectedExceptions=UnsupportedOperationException.class)
 188     public void test_constant_OLD_IDS_PRE_2005_immutable() {
 189         Map<String, String> ids = ZoneId.OLD_IDS_PRE_2005;
 190         ids.clear();
 191     }
 192 
 193     //-----------------------------------------------------------------------
 194     // OLD_IDS_POST_2005
 195     //-----------------------------------------------------------------------
 196     public void test_constant_OLD_IDS_POST_2005() {
 197         Map<String, String> ids = ZoneId.OLD_IDS_POST_2005;
 198         assertEquals(ids.get("EST"), "-05:00");
 199         assertEquals(ids.get("MST"), "-07:00");
 200         assertEquals(ids.get("HST"), "-10:00");
 201         assertEquals(ids.get("ACT"), "Australia/Darwin");
 202         assertEquals(ids.get("AET"), "Australia/Sydney");
 203         assertEquals(ids.get("AGT"), "America/Argentina/Buenos_Aires");
 204         assertEquals(ids.get("ART"), "Africa/Cairo");
 205         assertEquals(ids.get("AST"), "America/Anchorage");
 206         assertEquals(ids.get("BET"), "America/Sao_Paulo");
 207         assertEquals(ids.get("BST"), "Asia/Dhaka");
 208         assertEquals(ids.get("CAT"), "Africa/Harare");
 209         assertEquals(ids.get("CNT"), "America/St_Johns");
 210         assertEquals(ids.get("CST"), "America/Chicago");
 211         assertEquals(ids.get("CTT"), "Asia/Shanghai");
 212         assertEquals(ids.get("EAT"), "Africa/Addis_Ababa");
 213         assertEquals(ids.get("ECT"), "Europe/Paris");
 214         assertEquals(ids.get("IET"), "America/Indiana/Indianapolis");
 215         assertEquals(ids.get("IST"), "Asia/Kolkata");
 216         assertEquals(ids.get("JST"), "Asia/Tokyo");
 217         assertEquals(ids.get("MIT"), "Pacific/Apia");
 218         assertEquals(ids.get("NET"), "Asia/Yerevan");
 219         assertEquals(ids.get("NST"), "Pacific/Auckland");
 220         assertEquals(ids.get("PLT"), "Asia/Karachi");
 221         assertEquals(ids.get("PNT"), "America/Phoenix");
 222         assertEquals(ids.get("PRT"), "America/Puerto_Rico");
 223         assertEquals(ids.get("PST"), "America/Los_Angeles");
 224         assertEquals(ids.get("SST"), "Pacific/Guadalcanal");
 225         assertEquals(ids.get("VST"), "Asia/Ho_Chi_Minh");
 226     }
 227 
 228     @Test(expectedExceptions=UnsupportedOperationException.class)
 229     public void test_constant_OLD_IDS_POST_2005_immutable() {
 230         Map<String, String> ids = ZoneId.OLD_IDS_POST_2005;
 231         ids.clear();
 232     }
 233 
 234     //-----------------------------------------------------------------------
 235     // system default
 236     //-----------------------------------------------------------------------
 237     public void test_systemDefault() {
 238         ZoneId test = ZoneId.systemDefault();
 239         assertEquals(test.getId(), TimeZone.getDefault().getID());


 240     }
 241 
 242     @Test(expectedExceptions = DateTimeException.class)
 243     public void test_systemDefault_unableToConvert_badFormat() {
 244         TimeZone current = TimeZone.getDefault();
 245         try {
 246             TimeZone.setDefault(new SimpleTimeZone(127, "Something Weird"));
 247             ZoneId.systemDefault();
 248         } finally {
 249             TimeZone.setDefault(current);
 250         }
 251     }
 252 
 253     @Test(expectedExceptions = ZoneRulesException.class)
 254     public void test_systemDefault_unableToConvert_unknownId() {
 255         TimeZone current = TimeZone.getDefault();
 256         try {
 257             TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
 258             ZoneId.systemDefault();
 259         } finally {
 260             TimeZone.setDefault(current);
 261         }
 262     }
 263 
 264     //-----------------------------------------------------------------------
 265     // mapped factory
 266     //-----------------------------------------------------------------------
 267     public void test_of_string_Map() {
 268         Map<String, String> map = new HashMap<String, String>();
 269         map.put("LONDON", "Europe/London");
 270         map.put("PARIS", "Europe/Paris");
 271         ZoneId test = ZoneId.of("LONDON", map);
 272         assertEquals(test.getId(), "Europe/London");
 273     }
 274 
 275     public void test_of_string_Map_lookThrough() {
 276         Map<String, String> map = new HashMap<String, String>();
 277         map.put("LONDON", "Europe/London");
 278         map.put("PARIS", "Europe/Paris");
 279         ZoneId test = ZoneId.of("Europe/Madrid", map);
 280         assertEquals(test.getId(), "Europe/Madrid");
 281     }
 282 
 283     public void test_of_string_Map_emptyMap() {
 284         Map<String, String> map = new HashMap<String, String>();
 285         ZoneId test = ZoneId.of("Europe/Madrid", map);
 286         assertEquals(test.getId(), "Europe/Madrid");
 287     }
 288 
 289     @Test(expectedExceptions=DateTimeException.class)
 290     public void test_of_string_Map_badFormat() {
 291         Map<String, String> map = new HashMap<String, String>();
 292         ZoneId.of("Not kknown", map);
 293     }
 294 
 295     @Test(expectedExceptions=ZoneRulesException.class)
 296     public void test_of_string_Map_unknown() {
 297         Map<String, String> map = new HashMap<String, String>();
 298         ZoneId.of("Unknown", map);
 299     }
 300 
 301     //-----------------------------------------------------------------------
 302     // regular factory
 303     //-----------------------------------------------------------------------
 304     @DataProvider(name="String_UTC")
 305     Object[][] data_of_string_UTC() {
 306         return new Object[][] {
 307             {""}, {"Z"},
 308             {"+00"},{"+0000"},{"+00:00"},{"+000000"},{"+00:00:00"},
 309             {"-00"},{"-0000"},{"-00:00"},{"-000000"},{"-00:00:00"},
 310         };
 311     }
 312 
 313     @Test(dataProvider="String_UTC")
 314     public void test_of_string_UTC(String id) {
 315         ZoneId test = ZoneId.of("UTC" + id);
 316         assertSame(test, ZoneOffset.UTC);
 317     }
 318 
 319     @Test(dataProvider="String_UTC")
 320     public void test_of_string_GMT(String id) {
 321         ZoneId test = ZoneId.of("GMT" + id);
 322         assertSame(test, ZoneOffset.UTC);
 323     }
 324 
 325     //-----------------------------------------------------------------------
 326     @DataProvider(name="String_Fixed")
 327     Object[][] data_of_string_Fixed() {
 328         return new Object[][] {
 329             {"Z", "Z"},
 330             {"+0", "Z"},
 331             {"+5", "+05:00"},
 332             {"+01", "+01:00"},
 333             {"+0100", "+01:00"},{"+01:00", "+01:00"},
 334             {"+010000", "+01:00"},{"+01:00:00", "+01:00"},
 335             {"+12", "+12:00"},
 336             {"+1234", "+12:34"},{"+12:34", "+12:34"},
 337             {"+123456", "+12:34:56"},{"+12:34:56", "+12:34:56"},
 338             {"-02", "-02:00"},
 339             {"-5", "-05:00"},
 340             {"-0200", "-02:00"},{"-02:00", "-02:00"},
 341             {"-020000", "-02:00"},{"-02:00:00", "-02:00"},
 342         };
 343     }
 344 
 345     @Test(dataProvider="String_Fixed")
 346     public void test_of_string_offset(String input, String id) {
 347         ZoneId test = ZoneId.of(input);
 348         assertEquals(test.getId(), id);
 349         assertEquals(test.getText(TextStyle.FULL, Locale.UK), id);
 350         assertEquals(test.getRules().isFixedOffset(), true);
 351         ZoneOffset offset = ZoneOffset.of(id);
 352         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
 353         checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
 354     }
 355 
 356     @Test(dataProvider="String_Fixed")
 357     public void test_of_string_FixedUTC(String input, String id) {
 358         ZoneId test = ZoneId.of("UTC" + input);
 359         assertEquals(test.getId(), id);
 360         assertEquals(test.getText(TextStyle.FULL, Locale.UK), id);
 361         assertEquals(test.getRules().isFixedOffset(), true);
 362         ZoneOffset offset = ZoneOffset.of(id);
 363         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
 364         checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
 365     }
 366 
 367     @Test(dataProvider="String_Fixed")
 368     public void test_of_string_FixedGMT(String input, String id) {
 369         ZoneId test = ZoneId.of("GMT" + input);
 370         assertEquals(test.getId(), id);
 371         assertEquals(test.getText(TextStyle.FULL, Locale.UK), id);
 372         assertEquals(test.getRules().isFixedOffset(), true);
 373         ZoneOffset offset = ZoneOffset.of(id);
 374         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
 375         checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
 376     }
 377 
 378     //-----------------------------------------------------------------------
 379     @DataProvider(name="String_UTC_Invalid")
 380     Object[][] data_of_string_UTC_invalid() {
 381         return new Object[][] {
 382                 {"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"}, {"G"}, {"H"}, {"I"}, {"J"}, {"K"}, {"L"}, {"M"},
 383                 {"N"}, {"O"}, {"P"}, {"Q"}, {"R"}, {"S"}, {"T"}, {"U"}, {"V"}, {"W"}, {"X"}, {"Y"},
 384                 {"+0:00"}, {"+00:0"}, {"+0:0"},
 385                 {"+000"}, {"+00000"},
 386                 {"+0:00:00"}, {"+00:0:00"}, {"+00:00:0"}, {"+0:0:0"}, {"+0:0:00"}, {"+00:0:0"}, {"+0:00:0"},
 387                 {"+01_00"}, {"+01;00"}, {"+01@00"}, {"+01:AA"},
 388                 {"+19"}, {"+19:00"}, {"+18:01"}, {"+18:00:01"}, {"+1801"}, {"+180001"},
 389                 {"-0:00"}, {"-00:0"}, {"-0:0"},
 390                 {"-000"}, {"-00000"},
 391                 {"-0:00:00"}, {"-00:0:00"}, {"-00:00:0"}, {"-0:0:0"}, {"-0:0:00"}, {"-00:0:0"}, {"-0:00:0"},
 392                 {"-19"}, {"-19:00"}, {"-18:01"}, {"-18:00:01"}, {"-1801"}, {"-180001"},
 393                 {"-01_00"}, {"-01;00"}, {"-01@00"}, {"-01:AA"},
 394                 {"@01:00"},
 395         };
 396     }
 397 
 398     @Test(dataProvider="String_UTC_Invalid", expectedExceptions=DateTimeException.class)
 399     public void test_of_string_UTC_invalid(String id) {
 400         ZoneId.of("UTC" + id);
 401     }
 402 
 403     @Test(dataProvider="String_UTC_Invalid", expectedExceptions=DateTimeException.class)
 404     public void test_of_string_GMT_invalid(String id) {
 405         ZoneId.of("GMT" + id);
 406     }
 407 
 408     //-----------------------------------------------------------------------
 409     @DataProvider(name="String_Invalid")
 410     Object[][] data_of_string_invalid() {
 411         // \u00ef is a random unicode character
 412         return new Object[][] {
 413                 {""}, {":"}, {"#"},
 414                 {"\u00ef"}, {"`"}, {"!"}, {"\""}, {"\u00ef"}, {"$"}, {"^"}, {"&"}, {"*"}, {"("}, {")"}, {"="},
 415                 {"\\"}, {"|"}, {","}, {"<"}, {">"}, {"?"}, {";"}, {"'"}, {"["}, {"]"}, {"{"}, {"}"},
 416                 {"\u00ef:A"}, {"`:A"}, {"!:A"}, {"\":A"}, {"\u00ef:A"}, {"$:A"}, {"^:A"}, {"&:A"}, {"*:A"}, {"(:A"}, {"):A"}, {"=:A"}, {"+:A"},
 417                 {"\\:A"}, {"|:A"}, {",:A"}, {"<:A"}, {">:A"}, {"?:A"}, {";:A"}, {"::A"}, {"':A"}, {"@:A"}, {"~:A"}, {"[:A"}, {"]:A"}, {"{:A"}, {"}:A"},
 418                 {"A:B#\u00ef"}, {"A:B#`"}, {"A:B#!"}, {"A:B#\""}, {"A:B#\u00ef"}, {"A:B#$"}, {"A:B#^"}, {"A:B#&"}, {"A:B#*"},
 419                 {"A:B#("}, {"A:B#)"}, {"A:B#="}, {"A:B#+"},
 420                 {"A:B#\\"}, {"A:B#|"}, {"A:B#,"}, {"A:B#<"}, {"A:B#>"}, {"A:B#?"}, {"A:B#;"}, {"A:B#:"},
 421                 {"A:B#'"}, {"A:B#@"}, {"A:B#~"}, {"A:B#["}, {"A:B#]"}, {"A:B#{"}, {"A:B#}"},
 422         };
 423     }
 424 
 425     @Test(dataProvider="String_Invalid", expectedExceptions=DateTimeException.class)
 426     public void test_of_string_invalid(String id) {
 427         ZoneId.of(id);
 428     }
 429 
 430     //-----------------------------------------------------------------------
 431     public void test_of_string_GMT0() {
 432         ZoneId test = ZoneId.of("GMT0");
 433         assertEquals(test.getId(), "Z");
 434         assertEquals(test.getRules().isFixedOffset(), true);
 435     }
 436 
 437     //-----------------------------------------------------------------------
 438     public void test_of_string_London() {
 439         ZoneId test = ZoneId.of("Europe/London");
 440         assertEquals(test.getId(), "Europe/London");
 441         assertEquals(test.getRules().isFixedOffset(), false);
 442     }
 443 
 444     //-----------------------------------------------------------------------
 445     @Test(expectedExceptions=NullPointerException.class)
 446     public void test_of_string_null() {
 447         ZoneId.of((String) null);
 448     }
 449 
 450     @Test(expectedExceptions=ZoneRulesException.class)
 451     public void test_of_string_unknown_simple() {
 452         ZoneId.of("Unknown");
 453     }
 454 
 455     //-------------------------------------------------------------------------
 456     // TODO: test by deserialization
 457 //    public void test_ofUnchecked_string_invalidNotChecked() {
 458 //        ZoneRegion test = ZoneRegion.ofLenient("Unknown");
 459 //        assertEquals(test.getId(), "Unknown");
 460 //    }
 461 //
 462 //    public void test_ofUnchecked_string_invalidNotChecked_unusualCharacters() {
 463 //        ZoneRegion test = ZoneRegion.ofLenient("QWERTYUIOPASDFGHJKLZXCVBNM~/._+-");
 464 //        assertEquals(test.getId(), "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-");
 465 //    }
 466 
 467     //-----------------------------------------------------------------------
 468     // from(TemporalAccessor)
 469     //-----------------------------------------------------------------------
 470     public void test_factory_from_DateTimeAccessor_zoneId() {
 471         TemporalAccessor mock = new TemporalAccessor() {
 472             @Override
 473             public boolean isSupported(TemporalField field) {
 474                 return false;
 475             }
 476 
 477             @Override
 478             public long getLong(TemporalField field) {
 479                 throw new DateTimeException("Mock");
 480             }
 481 
 482             @Override
 483             public <R> R query(TemporalQuery<R> query) {
 484                 if (query == Queries.zoneId()) {
 485                     return (R) ZONE_PARIS;
 486                 }
 487                 return TemporalAccessor.super.query(query);
 488             }
 489         };
 490         assertEquals(ZoneId.from(mock), ZONE_PARIS);
 491     }
 492 
 493     public void test_factory_from_DateTimeAccessor_offset() {
 494         ZoneOffset offset = ZoneOffset.ofHours(1);
 495         assertEquals(ZoneId.from(offset), offset);
 496     }
 497 
 498     @Test(expectedExceptions=DateTimeException.class)
 499     public void test_factory_from_DateTimeAccessor_invalid_noDerive() {
 500         ZoneId.from(LocalTime.of(12, 30));
 501     }
 502 
 503     @Test(expectedExceptions=NullPointerException.class)
 504     public void test_factory_from_DateTimeAccessor_null() {
 505         ZoneId.from((TemporalAccessor) null);
 506     }
 507 
 508     //-----------------------------------------------------------------------
 509     // Europe/London
 510     //-----------------------------------------------------------------------
 511     public void test_London() {
 512         ZoneId test = ZoneId.of("Europe/London");
 513         assertEquals(test.getId(), "Europe/London");
 514         assertEquals(test.getRules().isFixedOffset(), false);
 515     }
 516 
 517     public void test_London_getOffset() {
 518         ZoneId test = ZoneId.of("Europe/London");
 519         assertEquals(test.getRules().getOffset(createInstant(2008, 1, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
 520         assertEquals(test.getRules().getOffset(createInstant(2008, 2, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
 521         assertEquals(test.getRules().getOffset(createInstant(2008, 3, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
 522         assertEquals(test.getRules().getOffset(createInstant(2008, 4, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 523         assertEquals(test.getRules().getOffset(createInstant(2008, 5, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 524         assertEquals(test.getRules().getOffset(createInstant(2008, 6, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 525         assertEquals(test.getRules().getOffset(createInstant(2008, 7, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 526         assertEquals(test.getRules().getOffset(createInstant(2008, 8, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 527         assertEquals(test.getRules().getOffset(createInstant(2008, 9, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 528         assertEquals(test.getRules().getOffset(createInstant(2008, 10, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));


 982 
 983         assertEquals(trans.hashCode(), otherTrans.hashCode());
 984     }
 985 
 986     //-----------------------------------------------------------------------
 987     // getXxx() isXxx()
 988     //-----------------------------------------------------------------------
 989     public void test_get_Tzdb() {
 990         ZoneId test = ZoneId.of("Europe/London");
 991         assertEquals(test.getId(), "Europe/London");
 992         assertEquals(test.getRules().isFixedOffset(), false);
 993     }
 994 
 995     public void test_get_TzdbFixed() {
 996         ZoneId test = ZoneId.of("+01:30");
 997         assertEquals(test.getId(), "+01:30");
 998         assertEquals(test.getRules().isFixedOffset(), true);
 999     }
1000 
1001     //-----------------------------------------------------------------------
1002     // equals() / hashCode()
1003     //-----------------------------------------------------------------------
1004     public void test_equals() {
1005         ZoneId test1 = ZoneId.of("Europe/London");
1006         ZoneId test2 = ZoneId.of("Europe/Paris");
1007         ZoneId test2b = ZoneId.of("Europe/Paris");
1008         assertEquals(test1.equals(test2), false);
1009         assertEquals(test2.equals(test1), false);
1010 
1011         assertEquals(test1.equals(test1), true);
1012         assertEquals(test2.equals(test2), true);
1013         assertEquals(test2.equals(test2b), true);
1014 
1015         assertEquals(test1.hashCode() == test1.hashCode(), true);
1016         assertEquals(test2.hashCode() == test2.hashCode(), true);
1017         assertEquals(test2.hashCode() == test2b.hashCode(), true);
1018     }
1019 
1020     public void test_equals_null() {
1021         assertEquals(ZoneId.of("Europe/London").equals(null), false);
1022     }
1023 
1024     public void test_equals_notTimeZone() {
1025         assertEquals(ZoneId.of("Europe/London").equals("Europe/London"), false);
1026     }
1027 
1028     //-----------------------------------------------------------------------
1029     // toString()
1030     //-----------------------------------------------------------------------
1031     @DataProvider(name="ToString")
1032     Object[][] data_toString() {
1033         return new Object[][] {
1034             {"Europe/London", "Europe/London"},
1035             {"Europe/Paris", "Europe/Paris"},
1036             {"Europe/Berlin", "Europe/Berlin"},
1037             {"UTC", "Z"},
1038             {"UTC+01:00", "+01:00"},
1039         };
1040     }
1041 
1042     @Test(dataProvider="ToString")
1043     public void test_toString(String id, String expected) {
1044         ZoneId test = ZoneId.of(id);
1045         assertEquals(test.toString(), expected);
1046     }
1047 
1048     //-----------------------------------------------------------------------
1049     //-----------------------------------------------------------------------
1050     //-----------------------------------------------------------------------
1051     private Instant createInstant(int year, int month, int day, ZoneOffset offset) {
1052         return LocalDateTime.of(year, month, day, 0, 0).toInstant(offset);
1053     }
1054 
1055     private Instant createInstant(int year, int month, int day, int hour, int min, int sec, int nano, ZoneOffset offset) {
1056         return LocalDateTime.of(year, month, day, hour, min, sec, nano).toInstant(offset);
1057     }
1058 
1059     private ZonedDateTime createZDT(int year, int month, int day, int hour, int min, int sec, int nano, ZoneId zone) {
1060         return LocalDateTime.of(year, month, day, hour, min, sec, nano).atZone(zone);
1061     }
1062 
1063     private LocalDateTime createLDT(int year, int month, int day) {
1064         return LocalDateTime.of(year, month, day, 0, 0);
1065     }
1066 
1067     private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
1068         List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);


  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 test.java.time;
  61 


  62 import static org.testng.Assert.assertEquals;
  63 import static org.testng.Assert.assertFalse;
  64 import static org.testng.Assert.assertNotNull;
  65 import static org.testng.Assert.assertSame;
  66 import static org.testng.Assert.assertTrue;
  67 

  68 import java.lang.reflect.Field;
  69 import java.lang.reflect.Modifier;
  70 import java.time.DateTimeException;
  71 import java.time.Instant;
  72 import java.time.LocalDateTime;
  73 import java.time.ZoneId;
  74 import java.time.ZoneOffset;
  75 import java.time.ZonedDateTime;





  76 import java.time.format.TextStyle;
  77 import java.time.zone.ZoneOffsetTransition;
  78 import java.time.zone.ZoneRules;
  79 import java.time.zone.ZoneRulesException;
  80 import java.util.List;
  81 import java.util.Locale;
  82 import java.util.SimpleTimeZone;
  83 import java.util.TimeZone;
  84 
  85 import org.testng.annotations.DataProvider;
  86 import org.testng.annotations.Test;
  87 
  88 /**
  89  * Test ZoneId.
  90  */
  91 @Test
  92 public class TestZoneId extends AbstractTest {
  93 


  94     private static final int OVERLAP = 2;
  95     private static final int GAP = 0;
  96 
  97     //-----------------------------------------------------------------------
  98     // Basics
  99     //-----------------------------------------------------------------------
 100     public void test_immutable() {
 101         // cannot use standard test as ZoneId is abstract
 102         Class<ZoneId> cls = ZoneId.class;
 103         assertTrue(Modifier.isPublic(cls.getModifiers()));
 104         Field[] fields = cls.getDeclaredFields();
 105         for (Field field : fields) {
 106             if (Modifier.isStatic(field.getModifiers()) == false) {
 107                 assertTrue(Modifier.isPrivate(field.getModifiers()));
 108                 assertTrue(Modifier.isFinal(field.getModifiers()) ||
 109                         (Modifier.isVolatile(field.getModifiers()) && Modifier.isTransient(field.getModifiers())));
 110             }
 111         }
 112     }
 113 




















 114     //-----------------------------------------------------------------------
 115     // UTC
 116     //-----------------------------------------------------------------------
 117     public void test_constant_UTC() {
 118         ZoneId test = ZoneOffset.UTC;
 119         assertEquals(test.getId(), "Z");
 120         assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), "Z");
 121         assertEquals(test.getRules().isFixedOffset(), true);
 122         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), ZoneOffset.UTC);
 123         checkOffset(test.getRules(), createLDT(2008, 6, 30), ZoneOffset.UTC, 1);
 124         assertSame(test, ZoneId.of("UTC+00"));
 125     }
 126 
 127     //-----------------------------------------------------------------------


















































































 128     // system default
 129     //-----------------------------------------------------------------------
 130     public void test_systemDefault() {
 131         ZoneId test = ZoneId.systemDefault();
 132         assertEquals(test.getId(), TimeZone.getDefault()
 133                                            .getID()
 134                                            .replaceAll("GMT|UTC|UT", "Z"));
 135     }
 136 
 137     @Test(expectedExceptions = DateTimeException.class)
 138     public void test_systemDefault_unableToConvert_badFormat() {
 139         TimeZone current = TimeZone.getDefault();
 140         try {
 141             TimeZone.setDefault(new SimpleTimeZone(127, "Something Weird"));
 142             ZoneId.systemDefault();
 143         } finally {
 144             TimeZone.setDefault(current);
 145         }
 146     }
 147 
 148     @Test(expectedExceptions = ZoneRulesException.class)
 149     public void test_systemDefault_unableToConvert_unknownId() {
 150         TimeZone current = TimeZone.getDefault();
 151         try {
 152             TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
 153             ZoneId.systemDefault();
 154         } finally {
 155             TimeZone.setDefault(current);
 156         }
 157     }
 158 
 159     //-----------------------------------------------------------------------





























































 160     @DataProvider(name="String_Fixed")
 161     Object[][] data_of_string_Fixed() {
 162         return new Object[][] {

 163             {"+0", "Z"},
 164             {"+5", "+05:00"},
 165             {"+01", "+01:00"},
 166             {"+0100", "+01:00"},{"+01:00", "+01:00"},
 167             {"+010000", "+01:00"},{"+01:00:00", "+01:00"},
 168             {"+12", "+12:00"},
 169             {"+1234", "+12:34"},{"+12:34", "+12:34"},
 170             {"+123456", "+12:34:56"},{"+12:34:56", "+12:34:56"},
 171             {"-02", "-02:00"},
 172             {"-5", "-05:00"},
 173             {"-0200", "-02:00"},{"-02:00", "-02:00"},
 174             {"-020000", "-02:00"},{"-02:00:00", "-02:00"},
 175         };
 176     }
 177 
 178     @Test(dataProvider="String_Fixed")
 179     public void test_of_string_offset(String input, String id) {
 180         ZoneId test = ZoneId.of(input);
 181         assertEquals(test.getId(), id);
 182         assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id);
 183         assertEquals(test.getRules().isFixedOffset(), true);
 184         ZoneOffset offset = ZoneOffset.of(id);
 185         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
 186         checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
 187     }
 188 
 189     @Test(dataProvider="String_Fixed")
 190     public void test_of_string_FixedUTC(String input, String id) {
 191         ZoneId test = ZoneId.of("UTC" + input);
 192         assertEquals(test.getId(), id);
 193         assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id);
 194         assertEquals(test.getRules().isFixedOffset(), true);
 195         ZoneOffset offset = ZoneOffset.of(id);
 196         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
 197         checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
 198     }
 199 
 200     @Test(dataProvider="String_Fixed")
 201     public void test_of_string_FixedGMT(String input, String id) {
 202         ZoneId test = ZoneId.of("GMT" + input);
 203         assertEquals(test.getId(), id);
 204         assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id);
 205         assertEquals(test.getRules().isFixedOffset(), true);
 206         ZoneOffset offset = ZoneOffset.of(id);
 207         assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
 208         checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
 209     }
 210 
 211     //-----------------------------------------------------------------------


































































































































 212     // Europe/London
 213     //-----------------------------------------------------------------------
 214     public void test_London() {
 215         ZoneId test = ZoneId.of("Europe/London");
 216         assertEquals(test.getId(), "Europe/London");
 217         assertEquals(test.getRules().isFixedOffset(), false);
 218     }
 219 
 220     public void test_London_getOffset() {
 221         ZoneId test = ZoneId.of("Europe/London");
 222         assertEquals(test.getRules().getOffset(createInstant(2008, 1, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
 223         assertEquals(test.getRules().getOffset(createInstant(2008, 2, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
 224         assertEquals(test.getRules().getOffset(createInstant(2008, 3, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
 225         assertEquals(test.getRules().getOffset(createInstant(2008, 4, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 226         assertEquals(test.getRules().getOffset(createInstant(2008, 5, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 227         assertEquals(test.getRules().getOffset(createInstant(2008, 6, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 228         assertEquals(test.getRules().getOffset(createInstant(2008, 7, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 229         assertEquals(test.getRules().getOffset(createInstant(2008, 8, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 230         assertEquals(test.getRules().getOffset(createInstant(2008, 9, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
 231         assertEquals(test.getRules().getOffset(createInstant(2008, 10, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1));


 685 
 686         assertEquals(trans.hashCode(), otherTrans.hashCode());
 687     }
 688 
 689     //-----------------------------------------------------------------------
 690     // getXxx() isXxx()
 691     //-----------------------------------------------------------------------
 692     public void test_get_Tzdb() {
 693         ZoneId test = ZoneId.of("Europe/London");
 694         assertEquals(test.getId(), "Europe/London");
 695         assertEquals(test.getRules().isFixedOffset(), false);
 696     }
 697 
 698     public void test_get_TzdbFixed() {
 699         ZoneId test = ZoneId.of("+01:30");
 700         assertEquals(test.getId(), "+01:30");
 701         assertEquals(test.getRules().isFixedOffset(), true);
 702     }
 703 
 704     //-----------------------------------------------------------------------















































 705     //-----------------------------------------------------------------------
 706     //-----------------------------------------------------------------------
 707     private Instant createInstant(int year, int month, int day, ZoneOffset offset) {
 708         return LocalDateTime.of(year, month, day, 0, 0).toInstant(offset);
 709     }
 710 
 711     private Instant createInstant(int year, int month, int day, int hour, int min, int sec, int nano, ZoneOffset offset) {
 712         return LocalDateTime.of(year, month, day, hour, min, sec, nano).toInstant(offset);
 713     }
 714 
 715     private ZonedDateTime createZDT(int year, int month, int day, int hour, int min, int sec, int nano, ZoneId zone) {
 716         return LocalDateTime.of(year, month, day, hour, min, sec, nano).atZone(zone);
 717     }
 718 
 719     private LocalDateTime createLDT(int year, int month, int day) {
 720         return LocalDateTime.of(year, month, day, 0, 0);
 721     }
 722 
 723     private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
 724         List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);