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.Calendar; 21 import java.util.Date; 22 import java.util.GregorianCalendar; 23 import java.util.SimpleTimeZone; 24 import java.util.TimeZone; 25 26 public class SimpleTimeZoneTest extends junit.framework.TestCase { 27 28 SimpleTimeZone st1; 29 30 SimpleTimeZone st2; 31 32 /** 33 * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String) 34 */ 35 public void test_ConstructorILjava_lang_String() { 36 // Test for method java.util.SimpleTimeZone(int, java.lang.String) 37 38 SimpleTimeZone st = new SimpleTimeZone(1000, "TEST"); 39 assertEquals("Incorrect TZ constructed", "TEST", st.getID()); 40 assertTrue("Incorrect TZ constructed: " + "returned wrong offset", st 41 .getRawOffset() == 1000); 42 assertTrue("Incorrect TZ constructed" + "using daylight savings", !st 43 .useDaylightTime()); 44 } 45 46 /** 47 * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String, 48 * int, int, int, int, int, int, int, int) 49 */ 50 public void test_ConstructorILjava_lang_StringIIIIIIII() { 51 // Test for method java.util.SimpleTimeZone(int, java.lang.String, int, 52 // int, int, int, int, int, int, int) 53 SimpleTimeZone st = new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 54 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 55 0); 56 assertTrue("Incorrect TZ constructed", st 57 .inDaylightTime(new GregorianCalendar(1998, Calendar.NOVEMBER, 58 13).getTime())); 59 assertTrue("Incorrect TZ constructed", !(st 60 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 61 13).getTime()))); 62 assertEquals("Incorrect TZ constructed", "TEST", st.getID()); 63 assertEquals("Incorrect TZ constructed", 1000, st.getRawOffset()); 64 assertTrue("Incorrect TZ constructed", st.useDaylightTime()); 65 } 66 67 /** 68 * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String, 69 * int, int, int, int, int, int, int, int, int) 70 */ 71 public void test_ConstructorILjava_lang_StringIIIIIIIII() { 72 // Test for method java.util.SimpleTimeZone(int, java.lang.String, int, 73 // int, int, int, int, int, int, int, int) 74 SimpleTimeZone st = new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 75 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 76 0, 1000 * 60 * 60); 77 assertTrue("Incorrect TZ constructed", st 78 .inDaylightTime(new GregorianCalendar(1998, Calendar.NOVEMBER, 79 13).getTime())); 80 assertTrue("Incorrect TZ constructed", !(st 81 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 82 13).getTime()))); 83 assertEquals("Incorrect TZ constructed", "TEST", st.getID()); 84 assertEquals("Incorrect TZ constructed", 1000, st.getRawOffset()); 85 assertTrue("Incorrect TZ constructed", st.useDaylightTime()); 86 assertTrue("Incorrect TZ constructed", 87 st.getDSTSavings() == 1000 * 60 * 60); 88 } 89 90 /** 91 * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String, 92 * int, int, int, int, int, int, int, int, int, int, int) 93 */ 94 public void test_ConstructorILjava_lang_StringIIIIIIIIIII() { 95 // Test for method java.util.SimpleTimeZone(int, java.lang.String, int, 96 // int, int, int, int, int, int, int, int, int, int) 97 // TODO : Implement test 98 //Regression for HARMONY-1241 99 assertNotNull(new SimpleTimeZone( 100 TimeZone.LONG, 101 "Europe/Paris", 102 SimpleTimeZone.STANDARD_TIME, 103 SimpleTimeZone.STANDARD_TIME, 104 SimpleTimeZone.UTC_TIME, 105 SimpleTimeZone.WALL_TIME, 106 SimpleTimeZone.WALL_TIME, 107 TimeZone.SHORT, 108 SimpleTimeZone.STANDARD_TIME, 109 TimeZone.LONG, 110 SimpleTimeZone.UTC_TIME, 111 SimpleTimeZone.STANDARD_TIME, 112 TimeZone.LONG)); 113 //seems RI doesn't check the startTimeMode and endTimeMode at all 114 //this behavior is contradicts with spec 115 assertNotNull(new SimpleTimeZone( 116 TimeZone.LONG, 117 "Europe/Paris", 118 SimpleTimeZone.STANDARD_TIME, 119 SimpleTimeZone.STANDARD_TIME, 120 SimpleTimeZone.UTC_TIME, 121 SimpleTimeZone.WALL_TIME, 122 Integer.MAX_VALUE, 123 TimeZone.SHORT, 124 SimpleTimeZone.STANDARD_TIME, 125 TimeZone.LONG, 126 SimpleTimeZone.UTC_TIME, 127 Integer.MIN_VALUE, 128 TimeZone.LONG)); 129 } 130 131 /** 132 * @tests java.util.SimpleTimeZone#clone() 133 */ 134 public void test_clone() { 135 // Test for method java.lang.Object java.util.SimpleTimeZone.clone() 136 SimpleTimeZone st1 = new SimpleTimeZone(1000, "TEST", 137 Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, 138 -1, Calendar.SUNDAY, 0); 139 SimpleTimeZone stA = new SimpleTimeZone(1, "Gah"); 140 assertTrue("Clone resulted in same reference", st1.clone() != st1); 141 assertTrue("Clone resulted in unequal object", ((SimpleTimeZone) st1 142 .clone()).equals(st1)); 143 assertTrue("Clone resulted in same reference", stA.clone() != stA); 144 assertTrue("Clone resulted in unequal object", ((SimpleTimeZone) stA 145 .clone()).equals(stA)); 146 } 147 148 /** 149 * @tests java.util.SimpleTimeZone#equals(java.lang.Object) 150 */ 151 public void test_equalsLjava_lang_Object() { 152 // Test for method boolean 153 // java.util.SimpleTimeZone.equals(java.lang.Object) 154 155 st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL, 1, 156 -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1, 157 Calendar.SUNDAY, 2 * 3600000); 158 assertTrue("Equalty test1 failed", TimeZone.getTimeZone("EST").equals( 159 st1)); 160 assertTrue("Equalty test2 failed", !(TimeZone.getTimeZone("CST") 161 .equals(st1))); 162 } 163 164 /** 165 * @tests java.util.SimpleTimeZone#getDSTSavings() 166 */ 167 public void test_getDSTSavings() { 168 // Test for method int java.util.SimpleTimeZone.getDSTSavings() 169 st1 = new SimpleTimeZone(0, "TEST"); 170 171 assertEquals("Non-zero default daylight savings", 172 0, st1.getDSTSavings()); 173 st1.setStartRule(0, 1, 1, 1); 174 st1.setEndRule(11, 1, 1, 1); 175 176 assertEquals("Incorrect default daylight savings", 177 3600000, st1.getDSTSavings()); 178 st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL, 1, 179 -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1, 180 Calendar.SUNDAY, 2 * 3600000, 7200000); 181 assertEquals("Incorrect daylight savings from constructor", 7200000, st1 182 .getDSTSavings()); 183 184 } 185 186 /** 187 * @tests java.util.SimpleTimeZone#getOffset(int, int, int, int, int, int) 188 */ 189 public void test_getOffsetIIIIII() { 190 // Test for method int java.util.SimpleTimeZone.getOffset(int, int, int, 191 // int, int, int) 192 TimeZone st1 = TimeZone.getTimeZone("EST"); 193 assertTrue("Incorrect offset returned", st1.getOffset( 194 GregorianCalendar.AD, 1998, Calendar.NOVEMBER, 11, 195 Calendar.WEDNESDAY, 0) == -(5 * 60 * 60 * 1000)); 196 197 st1 = TimeZone.getTimeZone("EST"); 198 assertEquals("Incorrect offset returned", -(5 * 60 * 60 * 1000), st1 199 .getOffset(GregorianCalendar.AD, 1998, Calendar.JUNE, 11, 200 Calendar.THURSDAY, 0)); 201 202 // Regression for HARMONY-5459 203 st1 = (TimeZone)TimeZone.getDefault(); 204 int fourHours = 4*60*60*1000; 205 st1.setRawOffset(fourHours); 206 assertEquals(fourHours, st1.getOffset(1, 2099, 01, 1, 5, 0)); 207 208 } 209 210 /** 211 * @tests java.util.SimpleTimeZone#getRawOffset() 212 */ 213 public void test_getRawOffset() { 214 // Test for method int java.util.SimpleTimeZone.getRawOffset() 215 st1 = (SimpleTimeZone) TimeZone.getTimeZone("EST"); 216 assertTrue("Incorrect offset returned", 217 st1.getRawOffset() == -(5 * 60 * 60 * 1000)); 218 219 } 220 221 /** 222 * @tests java.util.SimpleTimeZone#hashCode() 223 */ 224 public void test_hashCode() { 225 // Test for method int java.util.SimpleTimeZone.hashCode() 226 // For lack of a better test. 227 st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL, 1, 228 -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1, 229 Calendar.SUNDAY, 2 * 3600000); 230 assertTrue("Returned different hashcodes", TimeZone.getTimeZone("EST") 231 .hashCode() == st1.hashCode()); 232 } 233 234 /** 235 * @tests java.util.SimpleTimeZone#hasSameRules(java.util.TimeZone) 236 */ 237 public void test_hasSameRulesLjava_util_TimeZone() { 238 // Test for method boolean 239 // java.util.SimpleTimeZone.hasSameRules(java.util.TimeZone) 240 SimpleTimeZone st = new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 241 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 242 0); 243 SimpleTimeZone sameAsSt = new SimpleTimeZone(1000, "REST", 244 Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, 245 -1, Calendar.SUNDAY, 0); 246 SimpleTimeZone notSameAsSt = new SimpleTimeZone(1000, "PEST", 247 Calendar.NOVEMBER, 2, Calendar.SUNDAY, 0, Calendar.NOVEMBER, 248 -1, Calendar.SUNDAY, 0); 249 assertTrue("Time zones have same rules but return false", st 250 .hasSameRules(sameAsSt)); 251 assertTrue("Time zones have different rules but return true", !st 252 .hasSameRules(notSameAsSt)); 253 } 254 255 /** 256 * @tests java.util.SimpleTimeZone#inDaylightTime(java.util.Date) 257 */ 258 public void test_inDaylightTimeLjava_util_Date() { 259 // Test for method boolean 260 // java.util.SimpleTimeZone.inDaylightTime(java.util.Date) 261 TimeZone zone = TimeZone.getTimeZone("EST"); 262 GregorianCalendar gc = new GregorianCalendar(1998, Calendar.JUNE, 11); 263 assertFalse("Returned incorrect daylight value1", zone.inDaylightTime(gc 264 .getTime())); 265 gc = new GregorianCalendar(1998, Calendar.NOVEMBER, 11); 266 assertFalse("Returned incorrect daylight value1", zone.inDaylightTime(gc 267 .getTime())); 268 gc = new GregorianCalendar(zone); 269 gc.set(1999, Calendar.APRIL, 4, 1, 59, 59); 270 assertTrue("Returned incorrect daylight value3", !(zone 271 .inDaylightTime(gc.getTime()))); 272 Date date = new Date(gc.getTime().getTime() + 1000); 273 assertFalse("Returned incorrect daylight value4", zone 274 .inDaylightTime(date)); 275 gc.set(1999, Calendar.OCTOBER, 31, 1, 0, 0); 276 assertTrue("Returned incorrect daylight value5", !(zone 277 .inDaylightTime(gc.getTime()))); 278 date = new Date(gc.getTime().getTime() - 1000); 279 assertFalse("Returned incorrect daylight value6", zone 280 .inDaylightTime(date)); 281 282 assertTrue("Returned incorrect daylight value7", !zone 283 .inDaylightTime(new Date(891752400000L + 7200000 - 1))); 284 assertFalse("Returned incorrect daylight value8", zone 285 .inDaylightTime(new Date(891752400000L + 7200000))); 286 assertFalse("Returned incorrect daylight value9", zone 287 .inDaylightTime(new Date(909288000000L + 7200000 - 1))); 288 assertTrue("Returned incorrect daylight value10", !zone 289 .inDaylightTime(new Date(909288000000L + 7200000))); 290 } 291 292 /** 293 * @tests java.util.SimpleTimeZone#setDSTSavings(int) 294 */ 295 public void test_setDSTSavingsI() { 296 // Test for method void java.util.SimpleTimeZone.setDSTSavings(int) 297 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 298 st.setStartRule(0, 1, 1, 1); 299 st.setEndRule(11, 1, 1, 1); 300 st.setDSTSavings(1); 301 assertEquals("Daylight savings amount not set", 1, st.getDSTSavings()); 302 } 303 304 /** 305 * @tests java.util.SimpleTimeZone#setEndRule(int, int, int) 306 */ 307 public void test_setEndRuleIII() { 308 // Test for method void java.util.SimpleTimeZone.setEndRule(int, int, 309 // int) 310 assertTrue("Used to test", true); 311 } 312 313 /** 314 * @tests java.util.SimpleTimeZone#setEndRule(int, int, int, int) 315 */ 316 public void test_setEndRuleIIII() { 317 // Test for method void java.util.SimpleTimeZone.setEndRule(int, int, 318 // int, int) 319 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 320 // Spec indicates that both end and start must be set or result is 321 // undefined 322 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 323 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 324 assertTrue("StartRule improperly set1", st.useDaylightTime()); 325 assertTrue("StartRule improperly set2", st 326 .inDaylightTime(new GregorianCalendar(1998, Calendar.NOVEMBER, 327 13).getTime())); 328 assertTrue("StartRule improperly set3", !(st 329 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 330 13).getTime()))); 331 } 332 333 /** 334 * @tests java.util.SimpleTimeZone#setEndRule(int, int, int, int, boolean) 335 */ 336 public void test_setEndRuleIIIIZ() { 337 // Test for method void java.util.SimpleTimeZone.setEndRule(int, int, 338 // int, int, boolean) 339 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 340 // Spec indicates that both end and start must be set or result is 341 // undefined 342 st.setStartRule(Calendar.NOVEMBER, 8, Calendar.SUNDAY, 1, false); 343 st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, true); 344 assertTrue("StartRule improperly set1", st.useDaylightTime()); 345 assertTrue("StartRule improperly set2", st 346 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 347 7, 12, 0).getTime()))); 348 assertTrue("StartRule improperly set3", st 349 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 350 20, 12, 0).getTime()))); 351 assertTrue("StartRule improperly set4", !(st 352 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 353 6, 12, 0).getTime()))); 354 assertTrue("StartRule improperly set5", !(st 355 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 356 21, 12, 0).getTime()))); 357 } 358 359 /** 360 * @tests java.util.SimpleTimeZone#setRawOffset(int) 361 */ 362 public void test_setRawOffsetI() { 363 // Test for method void java.util.SimpleTimeZone.setRawOffset(int) 364 365 st1 = (SimpleTimeZone) TimeZone.getTimeZone("EST"); 366 int off = st1.getRawOffset(); 367 st1.setRawOffset(1000); 368 boolean val = st1.getRawOffset() == 1000; 369 st1.setRawOffset(off); 370 assertTrue("Incorrect offset set", val); 371 } 372 373 /** 374 * @tests java.util.SimpleTimeZone#setStartRule(int, int, int) 375 */ 376 public void test_setStartRuleIII() { 377 // Test for method void java.util.SimpleTimeZone.setStartRule(int, int, 378 // int) 379 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 380 // Spec indicates that both end and start must be set or result is 381 // undefined 382 st.setStartRule(Calendar.NOVEMBER, 1, 1); 383 st.setEndRule(Calendar.DECEMBER, 1, 1); 384 assertTrue("StartRule improperly set", st.useDaylightTime()); 385 assertTrue("StartRule improperly set", st 386 .inDaylightTime((new GregorianCalendar(1998, Calendar.NOVEMBER, 387 13).getTime()))); 388 assertTrue("StartRule improperly set", !(st 389 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 390 13).getTime()))); 391 392 } 393 394 /** 395 * @tests java.util.SimpleTimeZone#setStartRule(int, int, int, int) 396 */ 397 public void test_setStartRuleIIII() { 398 // Test for method void java.util.SimpleTimeZone.setStartRule(int, int, 399 // int, int) 400 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 401 // Spec indicates that both end and start must be set or result is 402 // undefined 403 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 404 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 405 assertTrue("StartRule improperly set1", st.useDaylightTime()); 406 assertTrue("StartRule improperly set2", st 407 .inDaylightTime((new GregorianCalendar(1998, Calendar.NOVEMBER, 408 13).getTime()))); 409 assertTrue("StartRule improperly set3", !(st 410 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 411 13).getTime()))); 412 } 413 414 /** 415 * @tests java.util.SimpleTimeZone#setStartRule(int, int, int, int, boolean) 416 */ 417 public void test_setStartRuleIIIIZ() { 418 // Test for method void java.util.SimpleTimeZone.setStartRule(int, int, 419 // int, int, boolean) 420 SimpleTimeZone st = new SimpleTimeZone(0, "Test"); 421 // Spec indicates that both end and start must be set or result is 422 // undefined 423 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 1, true); 424 st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, false); 425 assertTrue("StartRule improperly set1", st.useDaylightTime()); 426 assertTrue("StartRule improperly set2", st 427 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 428 7, 12, 0).getTime()))); 429 assertTrue("StartRule improperly set3", st 430 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 431 13, 12, 0).getTime()))); 432 assertTrue("StartRule improperly set4", !(st 433 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 434 6, 12, 0).getTime()))); 435 assertTrue("StartRule improperly set5", !(st 436 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 437 14, 12, 0).getTime()))); 438 } 439 440 /** 441 * @tests java.util.SimpleTimeZone#setStartYear(int) 442 */ 443 public void test_setStartYearI() { 444 // Test for method void java.util.SimpleTimeZone.setStartYear(int) 445 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 446 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 447 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 448 st.setStartYear(1999); 449 assertTrue("set year improperly set1", !(st 450 .inDaylightTime(new GregorianCalendar(1999, Calendar.JULY, 12) 451 .getTime()))); 452 assertTrue("set year improperly set2", !(st 453 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 454 13).getTime()))); 455 assertTrue("set year improperly set3", (st 456 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 457 13).getTime()))); 458 } 459 460 /** 461 * @tests java.util.SimpleTimeZone#toString() 462 */ 463 public void test_toString() { 464 // Test for method java.lang.String java.util.SimpleTimeZone.toString() 465 String string = TimeZone.getTimeZone("EST").toString(); 466 assertNotNull("toString() returned null", string); 467 assertTrue("toString() is empty", string.length() != 0); 468 } 469 470 /** 471 * @tests java.util.SimpleTimeZone#useDaylightTime() 472 */ 473 public void test_useDaylightTime() { 474 // Test for method boolean java.util.SimpleTimeZone.useDaylightTime() 475 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 476 assertTrue("useDaylightTime returned incorrect value", !st 477 .useDaylightTime()); 478 // Spec indicates that both end and start must be set or result is 479 // undefined 480 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 481 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 482 assertTrue("useDaylightTime returned incorrect value", st 483 .useDaylightTime()); 484 } 485 486 /** 487 * Sets up the fixture, for example, open a network connection. This method 488 * is called before a test is executed. 489 */ 490 protected void setUp() { 491 } 492 493 /** 494 * Tears down the fixture, for example, close a network connection. This 495 * method is called after a test is executed. 496 */ 497 protected void tearDown() { 498 } 499 } 500