• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package org.apache.harmony.tests.java.util;
19 
20 import tests.support.Support_TimeZone;
21 import java.util.Arrays;
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.SimpleTimeZone;
28 import java.util.TimeZone;
29 
30 public class TimeZoneTest extends junit.framework.TestCase {
31 
32     private static final int ONE_HOUR = 3600000;
33 
34     private TimeZone processDefault;
35 
36     /**
37      * java.util.TimeZone#getDefault()
38      */
test_getDefault()39     public void test_getDefault() {
40         assertNotSame("returns identical",
41                 TimeZone.getDefault(), TimeZone.getDefault());
42     }
43 
44     /**
45      * java.util.TimeZone#getDSTSavings()
46      */
test_getDSTSavings()47     public void test_getDSTSavings() {
48         // Test for method int java.util.TimeZone.getDSTSavings()
49 
50         // test on subclass SimpleTimeZone
51         TimeZone st1 = TimeZone.getTimeZone("America/New_York");
52         assertEquals("T1A. Incorrect daylight savings returned",
53                 ONE_HOUR, st1.getDSTSavings());
54 
55         // a SimpleTimeZone with daylight savings different then 1 hour
56         st1 = TimeZone.getTimeZone("Australia/Lord_Howe");
57         assertEquals("T1B. Incorrect daylight savings returned",
58                 1800000, st1.getDSTSavings());
59 
60         // test on subclass Support_TimeZone, an instance with daylight savings
61         TimeZone tz1 = new Support_TimeZone(-5 * ONE_HOUR, true);
62         assertEquals("T2. Incorrect daylight savings returned",
63                 ONE_HOUR, tz1.getDSTSavings());
64 
65         // an instance without daylight savings
66         tz1 = new Support_TimeZone(3 * ONE_HOUR, false);
67         assertEquals("T3. Incorrect daylight savings returned, ",
68                 0, tz1.getDSTSavings());
69     }
70 
71     /**
72      * java.util.TimeZone#getOffset(long)
73      */
test_getOffset_long()74     public void test_getOffset_long() {
75         // Test for method int java.util.TimeZone.getOffset(long time)
76 
77         // test on subclass SimpleTimeZone
78         TimeZone st1 = TimeZone.getTimeZone("EST");
79         long time1 = new GregorianCalendar(1998, Calendar.NOVEMBER, 11)
80                 .getTimeInMillis();
81         assertEquals("T1. Incorrect offset returned",
82                 -(5 * ONE_HOUR), st1.getOffset(time1));
83 
84         long time2 = new GregorianCalendar(1998, Calendar.JUNE, 11)
85                 .getTimeInMillis();
86         st1 = TimeZone.getTimeZone("EST");
87         assertEquals("T2. Incorrect offset returned",
88                 -(5 * ONE_HOUR), st1.getOffset(time2));
89 
90         // test on subclass Support_TimeZone, an instance with daylight savings
91         TimeZone tz1 = new Support_TimeZone(-5 * ONE_HOUR, true);
92         assertEquals("T3. Incorrect offset returned, ",
93                 -(5 * ONE_HOUR), tz1.getOffset(time1));
94         assertEquals("T4. Incorrect offset returned, ",
95                 -(4 * ONE_HOUR), tz1.getOffset(time2));
96 
97         // an instance without daylight savings
98         tz1 = new Support_TimeZone(3 * ONE_HOUR, false);
99         assertEquals("T5. Incorrect offset returned, ",
100                 (3 * ONE_HOUR), tz1.getOffset(time1));
101         assertEquals("T6. Incorrect offset returned, ",
102                 (3 * ONE_HOUR), tz1.getOffset(time2));
103     }
104 
105     /**
106      * java.util.TimeZone#getTimeZone(java.lang.String)
107      */
test_getTimeZoneLjava_lang_String()108     public void test_getTimeZoneLjava_lang_String() {
109         assertEquals("Must return GMT when given an invalid TimeZone id SMT-8.",
110                 "GMT", TimeZone.getTimeZone("SMT-8").getID());
111         assertEquals("Must return GMT when given an invalid TimeZone time GMT+28:70.",
112                 "GMT", TimeZone.getTimeZone("GMT+28:70").getID());
113         assertEquals("Must return GMT when given an invalid TimeZone time GMT+28:30.",
114                 "GMT", TimeZone.getTimeZone("GMT+28:30").getID());
115         assertEquals("Must return GMT when given an invalid TimeZone time GMT+8:70.",
116                 "GMT", TimeZone.getTimeZone("GMT+8:70").getID());
117         assertEquals("Must return GMT when given an invalid TimeZone time GMT+3:.",
118                 "GMT", TimeZone.getTimeZone("GMT+3:").getID());
119         assertEquals("Must return GMT when given an invalid TimeZone time GMT+3:0.",
120                 "GMT", TimeZone.getTimeZone("GMT+3:0").getID());
121         assertEquals("Must return GMT when given an invalid TimeZone time GMT+2360.",
122                 "GMT", TimeZone.getTimeZone("GMT+2360").getID());
123         assertEquals("Must return GMT when given an invalid TimeZone time GMT+892.",
124                 "GMT", TimeZone.getTimeZone("GMT+892").getID());
125         assertEquals("Must return GMT when given an invalid TimeZone time GMT+082.",
126                 "GMT", TimeZone.getTimeZone("GMT+082").getID());
127         assertEquals("Must return GMT when given an invalid TimeZone time GMT+28.",
128                 "GMT", TimeZone.getTimeZone("GMT+28").getID());
129         assertEquals("Must return GMT when given an invalid TimeZone time GMT+30.",
130                 "GMT", TimeZone.getTimeZone("GMT+30").getID());
131         assertEquals("Must return GMT when given TimeZone GMT.",
132                 "GMT", TimeZone.getTimeZone("GMT").getID());
133         assertEquals("Must return GMT when given TimeZone GMT+.",
134                 "GMT", TimeZone.getTimeZone("GMT+").getID());
135         assertEquals("Must return GMT when given TimeZone GMT-.",
136                 "GMT", TimeZone.getTimeZone("GMT-").getID());
137         assertEquals("Must return GMT when given an invalid TimeZone time GMT-8.45.",
138                 "GMT", TimeZone.getTimeZone("GMT-8.45").getID());
139         assertEquals("Must return GMT when given an invalid TimeZone time GMT-123:23.",
140                 "GMT", TimeZone.getTimeZone("GMT-123:23").getID());
141         assertEquals("Must return proper GMT formatted string for GMT+8:30 (eg. GMT+08:20).",
142                 "GMT+08:30", TimeZone.getTimeZone("GMT+8:30").getID());
143         assertEquals("Must return proper GMT formatted string for GMT+3 (eg. GMT+08:20).",
144                 "GMT+03:00", TimeZone.getTimeZone("GMT+3").getID());
145         assertEquals("Must return proper GMT formatted string for GMT+3:02 (eg. GMT+08:20).",
146                 "GMT+03:02", TimeZone.getTimeZone("GMT+3:02").getID());
147         assertEquals("Must return proper GMT formatted string for GMT+2359 (eg. GMT+08:20).",
148                 "GMT+23:59", TimeZone.getTimeZone("GMT+2359").getID());
149         assertEquals("Must return proper GMT formatted string for GMT+520 (eg. GMT+08:20).",
150                 "GMT+05:20", TimeZone.getTimeZone("GMT+520").getID());
151         assertEquals("Must return proper GMT formatted string for GMT+052 (eg. GMT+08:20).",
152                 "GMT+00:52", TimeZone.getTimeZone("GMT+052").getID());
153         // GMT-0 is an available ID in ICU, so replace it with GMT-00
154         assertEquals("Must return proper GMT formatted string for GMT-00 (eg. GMT+08:20).",
155                 "GMT-00:00", TimeZone.getTimeZone("GMT-00").getID());
156     }
157 
158     /**
159      * java.util.TimeZone#setDefault(java.util.TimeZone)
160      */
test_setDefaultLjava_util_TimeZone()161     public void test_setDefaultLjava_util_TimeZone() {
162         // NOTE: Required to get tests passing under vogar. Vogar sets
163         // a hardcoded timezone before running every test, so we have to
164         // set it back to the "real" default before we run the test.
165         TimeZone.setDefault(null);
166 
167         TimeZone oldDefault = TimeZone.getDefault();
168         TimeZone zone = new SimpleTimeZone(45, "TEST");
169         TimeZone.setDefault(zone);
170         assertEquals("timezone not set", zone, TimeZone.getDefault());
171         TimeZone.setDefault(null);
172         assertEquals("default not restored",
173                 oldDefault, TimeZone.getDefault());
174     }
175 
176     /**
177      * java.util.TimeZone#getDisplayName(java.util.Locale)
178      */
test_getDisplayNameLjava_util_Locale()179     public void test_getDisplayNameLjava_util_Locale() {
180         TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai");
181         assertEquals("\u4e2d\u56fd\u6807\u51c6\u65f6\u95f4", timezone
182                 .getDisplayName(Locale.CHINA));
183     }
184 
185     /**
186      * java.util.TimeZone#getDisplayName(boolean, int, java.util.Locale)
187      */
test_getDisplayNameZILjava_util_Locale()188     public void test_getDisplayNameZILjava_util_Locale() {
189         TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai");
190         /* Time zone data was changed in ICU49.2.  Many common short names were removed. */
191         assertEquals("中国标准时间",
192                 timezone.getDisplayName(false, TimeZone.LONG, Locale.CHINA));
193         assertEquals("GMT+08:00",
194                 timezone.getDisplayName(false, TimeZone.SHORT, Locale.CHINA));
195         try {
196             timezone.getDisplayName(false, 100, Locale.CHINA);
197             fail("should throw IllegalArgumentException");
198         } catch (IllegalArgumentException e) {
199             // expected
200         }
201     }
202 
203     /*
204      * Regression for HARMONY-5860
205      */
test_GetTimezoneOffset()206     public void test_GetTimezoneOffset() {
207         // America/Toronto is lazy initialized
208         TimeZone.setDefault(TimeZone.getTimeZone("America/Toronto"));
209         Date date = new Date(07, 2, 24);
210         assertEquals(300, date.getTimezoneOffset());
211         date = new Date(99, 8, 1);
212         assertEquals(240, date.getTimezoneOffset());
213     }
214 
setUp()215     protected void setUp() {
216         processDefault = TimeZone.getDefault();
217     }
218 
tearDown()219     protected void tearDown() {
220         TimeZone.setDefault(processDefault);
221     }
222 
test_getAvailableIDs_I_16947622()223     public void test_getAvailableIDs_I_16947622() {
224         TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
225         int rawOffset = tz.getRawOffset();
226         assertEquals(-8 * 60 * 60 * 1000, rawOffset);
227         List<String> ids = Arrays.asList(TimeZone.getAvailableIDs(rawOffset));
228 
229         // Obviously, for all time zones, the time zone whose raw offset we started with
230         // should be one of the available ids for that offset.
231         assertTrue(ids.toString(), ids.contains("America/Los_Angeles"));
232 
233         // Any one of these might legitimately change its raw offset, though that's
234         // fairly unlikely, and the chances of more than one changing are very slim.
235         assertTrue(ids.toString(), ids.contains("America/Tijuana"));
236         assertTrue(ids.toString(), ids.contains("America/Ensenada")); // Alias for America/Tijuana
237         assertTrue(ids.toString(), ids.contains("America/Vancouver"));
238         assertTrue(ids.toString(), ids.contains("Canada/Pacific")); // Alias for America/Vancouver
239         assertTrue(ids.toString(), ids.contains("Pacific/Pitcairn"));
240     }
241 
test_getAvailableIDs_I()242     public void test_getAvailableIDs_I() {
243         TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
244         int rawoffset = tz.getRawOffset();
245         String[] ids = TimeZone.getAvailableIDs(rawoffset);
246         List<String> idList = Arrays.asList(ids);
247         assertTrue(idList.toString(), idList.contains("Asia/Hong_Kong"));
248     }
249 
250     /**
251      * @add test {@link java.util.TimeZone#getDisplayName()}
252      */
test_getDisplayName()253     public void test_getDisplayName() {
254         TimeZone defaultZone = TimeZone.getDefault();
255         Locale defaulLocal = Locale.getDefault();
256         String defaultName = defaultZone.getDisplayName();
257         String expectedName = defaultZone.getDisplayName(defaulLocal);
258         assertEquals(
259                 "getDispalyName() did not return the default Locale suitable name",
260                 expectedName, defaultName);
261     }
262 
263     /**
264      * @add test {@link java.util.TimeZone#getDisplayName(boolean, int)}
265      */
test_getDisplayName_ZI()266     public void test_getDisplayName_ZI() {
267         TimeZone defaultZone = TimeZone.getDefault();
268         Locale defaultLocale = Locale.getDefault();
269         String actualName = defaultZone.getDisplayName(false, TimeZone.LONG);
270         String expectedName = defaultZone.getDisplayName(false, TimeZone.LONG,
271                 defaultLocale);
272         assertEquals(
273                 "getDisplayName(daylight,style) did not return the default locale suitable name",
274                 expectedName, actualName);
275     }
276 
277     /**
278      * @add test {@link java.util.TimeZone#hasSameRules(TimeZone)}
279      */
test_hasSameRules_Ljava_util_TimeZone()280     public void test_hasSameRules_Ljava_util_TimeZone() {
281         TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
282         int offset = tz.getRawOffset();
283 
284         String[] ids = TimeZone.getAvailableIDs(offset);
285         int i = 0;
286         if (ids.length != 0) {
287             while (true) {
288                 if (!(ids[i].equalsIgnoreCase(tz.getID()))) {
289                     TimeZone sameZone = TimeZone.getTimeZone(ids[i]);
290                     assertTrue(tz.hasSameRules(sameZone));
291                     break;
292                 } else {
293                     i++;
294                 }
295             }
296         }
297         assertFalse("should return false when parameter is null", tz
298                 .hasSameRules(null));
299     }
300 }
301