• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.zone;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertNotNull;
36 import static org.testng.Assert.assertTrue;
37 import static org.testng.Assert.fail;
38 
39 import java.util.Collections;
40 import java.util.HashSet;
41 import java.util.NavigableMap;
42 import java.util.Set;
43 import java.util.TreeMap;
44 
45 import org.testng.annotations.Test;
46 import org.threeten.bp.ZoneOffset;
47 
48 /**
49  * Test ZoneRulesProvider.
50  */
51 @Test
52 public class TestZoneRulesProvider {
53 
54     //-----------------------------------------------------------------------
55     // getAvailableZoneIds()
56     //-----------------------------------------------------------------------
57     @Test
test_getAvailableGroupIds()58     public void test_getAvailableGroupIds() {
59         Set<String> zoneIds = ZoneRulesProvider.getAvailableZoneIds();
60         assertEquals(zoneIds.contains("Europe/London"), true);
61         try {
62             zoneIds.clear();
63             fail();
64         } catch (UnsupportedOperationException ex) {
65             // ignore
66         }
67         Set<String> zoneIds2 = ZoneRulesProvider.getAvailableZoneIds();
68         assertEquals(zoneIds2.contains("Europe/London"), true);
69     }
70 
71     //-----------------------------------------------------------------------
72     // getRules(String)
73     //-----------------------------------------------------------------------
74     @Test
test_getRules_String()75     public void test_getRules_String() {
76         ZoneRules rules = ZoneRulesProvider.getRules("Europe/London", false);
77         assertNotNull(rules);
78         ZoneRules rules2 = ZoneRulesProvider.getRules("Europe/London", false);
79         assertEquals(rules2, rules);
80     }
81 
82     @Test(expectedExceptions=ZoneRulesException.class)
test_getRules_String_unknownId()83     public void test_getRules_String_unknownId() {
84         ZoneRulesProvider.getRules("Europe/Lon", false);
85     }
86 
87     @Test(expectedExceptions=NullPointerException.class)
test_getRules_String_null()88     public void test_getRules_String_null() {
89         ZoneRulesProvider.getRules(null, false);
90     }
91 
92     //-----------------------------------------------------------------------
93     // getVersions(String)
94     //-----------------------------------------------------------------------
95     @Test
test_getVersions_String()96     public void test_getVersions_String() {
97         NavigableMap<String, ZoneRules> versions = ZoneRulesProvider.getVersions("Europe/London");
98         assertTrue(versions.size() >= 1);
99         ZoneRules rules = ZoneRulesProvider.getRules("Europe/London", false);
100         assertEquals(versions.lastEntry().getValue(), rules);
101 
102         NavigableMap<String, ZoneRules> copy = new TreeMap<String, ZoneRules>(versions);
103         versions.clear();
104         assertEquals(versions.size(), 0);
105         NavigableMap<String, ZoneRules> versions2 = ZoneRulesProvider.getVersions("Europe/London");
106         assertEquals(versions2, copy);
107     }
108 
109     @Test(expectedExceptions=ZoneRulesException.class)
test_getVersions_String_unknownId()110     public void test_getVersions_String_unknownId() {
111         ZoneRulesProvider.getVersions("Europe/Lon");
112     }
113 
114     @Test(expectedExceptions=NullPointerException.class)
test_getVersions_String_null()115     public void test_getVersions_String_null() {
116         ZoneRulesProvider.getVersions(null);
117     }
118 
119     //-----------------------------------------------------------------------
120     // refresh()
121     //-----------------------------------------------------------------------
122     @Test
test_refresh()123     public void test_refresh() {
124         assertEquals(ZoneRulesProvider.refresh(), false);
125     }
126 
127     //-----------------------------------------------------------------------
128     // registerProvider()
129     //-----------------------------------------------------------------------
130     @Test
test_registerProvider()131     public void test_registerProvider() {
132         Set<String> pre = ZoneRulesProvider.getAvailableZoneIds();
133         assertEquals(pre.contains("FooLocation"), false);
134         ZoneRulesProvider.registerProvider(new MockTempProvider());
135         Set<String> post = ZoneRulesProvider.getAvailableZoneIds();
136         assertEquals(post.contains("FooLocation"), true);
137 
138         assertEquals(ZoneRulesProvider.getRules("FooLocation", false), ZoneOffset.of("+01:45").getRules());
139     }
140 
141     static class MockTempProvider extends ZoneRulesProvider {
142         final ZoneRules rules = ZoneOffset.of("+01:45").getRules();
143         @Override
provideZoneIds()144         public Set<String> provideZoneIds() {
145             return new HashSet<String>(Collections.singleton("FooLocation"));
146         }
147         @Override
provideVersions(String zoneId)148         protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
149             NavigableMap<String, ZoneRules> result = new TreeMap<String, ZoneRules>();
150             result.put("BarVersion", rules);
151             return result;
152         }
153         @Override
provideRules(String zoneId, boolean forCaching)154         protected ZoneRules provideRules(String zoneId, boolean forCaching) {
155             if (zoneId.equals("FooLocation")) {
156                 return rules;
157             }
158             throw new ZoneRulesException("Invalid");
159         }
160     }
161 
162 }
163