|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +package test.java.time.zone; |
| 25 | + |
| 26 | +import java.time.ZoneId; |
| 27 | +import java.time.zone.ZoneRules; |
| 28 | +import java.time.zone.ZoneRulesException; |
| 29 | +import java.time.zone.ZoneRulesProvider; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.LinkedHashSet; |
| 32 | +import java.util.NavigableMap; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +import org.testng.annotations.Test; |
| 36 | +import static org.testng.Assert.assertFalse; |
| 37 | +import static org.testng.Assert.assertTrue; |
| 38 | + |
| 39 | +/** |
| 40 | + * @summary Tests for ZoneRulesProvider class. |
| 41 | + * @bug 8299571 |
| 42 | + */ |
| 43 | +@Test |
| 44 | +public class TestZoneRulesProvider { |
| 45 | + private static final Set<String> MY_ZONE_IDS = |
| 46 | + new LinkedHashSet(Arrays.asList(new String[] {"MyID_1", "MyID_2", "CET", "MyID_3"})); |
| 47 | + |
| 48 | + /** |
| 49 | + * Tests whether partially registered zones are cleaned on a provider registration |
| 50 | + * failure, in case a duplicated zone is detected. |
| 51 | + */ |
| 52 | + @Test |
| 53 | + public void test_registerDuplicatedZone() { |
| 54 | + try { |
| 55 | + ZoneRulesProvider.registerProvider(new ZoneRulesProvider() { |
| 56 | + @Override |
| 57 | + protected Set<String> provideZoneIds() { |
| 58 | + return MY_ZONE_IDS; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected ZoneRules provideRules(String zoneId, boolean forCaching) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + }); |
| 71 | + throw new RuntimeException("Registering a provider that duplicates a zone should throw an exception"); |
| 72 | + } catch (ZoneRulesException e) { |
| 73 | + // Ignore. Failure on registration is expected. |
| 74 | + } |
| 75 | + |
| 76 | + MY_ZONE_IDS.stream().forEach(id -> { |
| 77 | + var isCET = id.equals("CET"); |
| 78 | + |
| 79 | + // availability check |
| 80 | + var available = ZoneId.getAvailableZoneIds().contains(id); |
| 81 | + if (available ^ isCET) { |
| 82 | + throw new RuntimeException("Unexpected availability for " + id + ", availability: " + available); |
| 83 | + } |
| 84 | + |
| 85 | + // instantiation check |
| 86 | + try { |
| 87 | + ZoneId.of(id); |
| 88 | + assertTrue(isCET, "ZoneId.of() for the custom id %s should throw ZoneRulesException.".formatted(id)); |
| 89 | + } catch (ZoneRulesException e) { |
| 90 | + assertFalse(isCET, "Not possible to obtain a ZoneId for \"CET\"."); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
0 commit comments