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.luni.tests.java.util; 19 20 import java.util.Arrays; 21 import java.util.Calendar; 22 import java.util.Date; 23 import java.util.Formatter; 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 import tests.support.Support_TimeZone; 31 32 public class TimeZoneTest extends junit.framework.TestCase { 33 34 private static final int ONE_HOUR = 3600000; 35 36 /** 37 * @tests 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 * @tests 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("EST"); 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 * @tests 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 * @tests 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 * @tests java.util.TimeZone#setDefault(java.util.TimeZone) 160 */ test_setDefaultLjava_util_TimeZone()161 public void test_setDefaultLjava_util_TimeZone() { 162 TimeZone oldDefault = TimeZone.getDefault(); 163 TimeZone zone = new SimpleTimeZone(45, "TEST"); 164 TimeZone.setDefault(zone); 165 assertEquals("timezone not set", zone, TimeZone.getDefault()); 166 TimeZone.setDefault(null); 167 assertEquals("default not restored", 168 oldDefault, TimeZone.getDefault()); 169 } 170 171 /** 172 * @tests java.util.TimeZone#getDisplayName(java.util.Locale) 173 */ test_getDisplayNameLjava_util_Locale()174 public void test_getDisplayNameLjava_util_Locale() { 175 TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai"); 176 assertEquals("\u4e2d\u56fd\u6807\u51c6\u65f6\u95f4", timezone 177 .getDisplayName(Locale.CHINA)); 178 } 179 180 /** 181 * @tests java.util.TimeZone#getDisplayName(boolean, int, java.util.Locale) 182 */ test_getDisplayNameZILjava_util_Locale()183 public void test_getDisplayNameZILjava_util_Locale() { 184 TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai"); 185 assertEquals("\u683c\u6797\u5c3c\u6cbb\u6807\u51c6\u65f6\u95f4+0800", 186 timezone.getDisplayName(false, TimeZone.SHORT, Locale.CHINA)); 187 try { 188 timezone.getDisplayName(false, 100, Locale.CHINA); 189 fail("should throw IllegalArgumentException"); 190 } catch (IllegalArgumentException e) { 191 // expected 192 } 193 } 194 195 /* 196 * Regression for HARMONY-5860 197 */ test_GetTimezoneOffset()198 public void test_GetTimezoneOffset() { 199 // America/Toronto is lazy initialized 200 TimeZone.setDefault(TimeZone.getTimeZone("America/Toronto")); 201 Date date = new Date(07, 2, 24); 202 assertEquals(300, date.getTimezoneOffset()); 203 date = new Date(99, 8, 1); 204 assertEquals(240, date.getTimezoneOffset()); 205 } 206 setUp()207 protected void setUp() { 208 } 209 tearDown()210 protected void tearDown() { 211 } 212 213 /** 214 * @add test {@link java.util.TimeZone#getAvailableIDs(int)} 215 */ test_getAvailableIDs_I()216 public void test_getAvailableIDs_I() { 217 TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); 218 int rawoffset = tz.getRawOffset(); 219 String[] ids = TimeZone.getAvailableIDs(rawoffset); 220 List<String> idList = Arrays.asList(ids); 221 assertTrue("Asia/shanghai and Hongkong should have the same rawoffset", 222 idList.contains("Hongkong")); 223 } 224 225 /** 226 * @add test {@link java.util.TimeZone#getDisplayName()} 227 */ test_getDisplayName()228 public void test_getDisplayName() { 229 TimeZone defaultZone = TimeZone.getDefault(); 230 Locale defaulLocal = Locale.getDefault(); 231 String defaultName = defaultZone.getDisplayName(); 232 String expectedName = defaultZone.getDisplayName(defaulLocal); 233 assertEquals( 234 "getDispalyName() did not return the default Locale suitable name", 235 expectedName, defaultName); 236 } 237 238 /** 239 * @add test {@link java.util.TimeZone#getDisplayName(boolean, int)} 240 */ test_getDisplayName_ZI()241 public void test_getDisplayName_ZI() { 242 TimeZone defaultZone = TimeZone.getDefault(); 243 Locale defaultLocale = Locale.getDefault(); 244 String actualName = defaultZone.getDisplayName(false, TimeZone.LONG); 245 String expectedName = defaultZone.getDisplayName(false, TimeZone.LONG, 246 defaultLocale); 247 assertEquals( 248 "getDisplayName(daylight,style) did not return the default locale suitable name", 249 expectedName, actualName); 250 } 251 252 /** 253 * @add test {@link java.util.TimeZone#hasSameRules(TimeZone)} 254 */ test_hasSameRules_Ljava_util_TimeZone()255 public void test_hasSameRules_Ljava_util_TimeZone() { 256 TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); 257 int offset = tz.getRawOffset(); 258 259 String[] ids = TimeZone.getAvailableIDs(offset); 260 int i = 0; 261 if (ids.length != 0) { 262 while (true) { 263 if (!(ids[i].equalsIgnoreCase(tz.getID()))) { 264 TimeZone sameZone = TimeZone.getTimeZone(ids[i]); 265 assertTrue(tz.hasSameRules(sameZone)); 266 break; 267 } else { 268 i++; 269 } 270 } 271 } 272 assertFalse("should return false when parameter is null", tz 273 .hasSameRules(null)); 274 } 275 } 276