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 18package tests.api.java.util; 19 20import java.util.Calendar; 21import java.util.Date; 22import java.util.GregorianCalendar; 23import java.util.SimpleTimeZone; 24import java.util.TimeZone; 25 26public class SimpleTimeZoneTest extends junit.framework.TestCase { 27 28 SimpleTimeZone st1; 29 30 SimpleTimeZone st2; 31 32 /** 33 * 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 * 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 try { 67 new SimpleTimeZone(1000, "TEST", 12, 68 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 69 0); 70 fail("IllegalArgumentException expected"); 71 } catch (IllegalArgumentException e) { 72 //expected 73 } 74 75 try { 76 new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 77 10, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 78 0); 79 fail("IllegalArgumentException expected"); 80 } catch (IllegalArgumentException e) { 81 //expected 82 } 83 84 try { 85 new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 86 1, 10, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 87 0); 88 fail("IllegalArgumentException expected"); 89 } catch (IllegalArgumentException e) { 90 //expected 91 } 92 93 try { 94 new SimpleTimeZone(1000, "TEST", Calendar.DECEMBER, 95 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -10, Calendar.SUNDAY, 96 0); 97 fail("IllegalArgumentException expected"); 98 } catch (IllegalArgumentException e) { 99 //expected 100 } 101 } 102 103 /** 104 * java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String, 105 * int, int, int, int, int, int, int, int, int) 106 */ 107 public void test_ConstructorILjava_lang_StringIIIIIIIII() { 108 // Test for method java.util.SimpleTimeZone(int, java.lang.String, int, 109 // int, int, int, int, int, int, int, int) 110 SimpleTimeZone st = new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 111 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 112 0, 1000 * 60 * 60); 113 assertTrue("Incorrect TZ constructed", st 114 .inDaylightTime(new GregorianCalendar(1998, Calendar.NOVEMBER, 115 13).getTime())); 116 assertTrue("Incorrect TZ constructed", !(st 117 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 118 13).getTime()))); 119 assertEquals("Incorrect TZ constructed", "TEST", st.getID()); 120 assertEquals("Incorrect TZ constructed", 1000, st.getRawOffset()); 121 assertTrue("Incorrect TZ constructed", st.useDaylightTime()); 122 assertTrue("Incorrect TZ constructed", 123 st.getDSTSavings() == 1000 * 60 * 60); 124 125 try { 126 new SimpleTimeZone(1000, "TEST", 12, 127 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 128 0, 1000 * 60 * 60); 129 fail("IllegalArgumentException expected"); 130 } catch (IllegalArgumentException e) { 131 //expected 132 } 133 134 try { 135 new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 136 10, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 137 0, 1000 * 60 * 60); 138 fail("IllegalArgumentException expected"); 139 } catch (IllegalArgumentException e) { 140 //expected 141 } 142 143 try { 144 new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 145 1, 10, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 146 0, 1000 * 60 * 60); 147 fail("IllegalArgumentException expected"); 148 } catch (IllegalArgumentException e) { 149 //expected 150 } 151 152 try { 153 new SimpleTimeZone(1000, "TEST", Calendar.DECEMBER, 154 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -10, Calendar.SUNDAY, 155 0, 1000 * 60 * 60); 156 fail("IllegalArgumentException expected"); 157 } catch (IllegalArgumentException e) { 158 //expected 159 } 160 } 161 162 /** 163 * java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String, 164 * int, int, int, int, int, int, int, int, int, int, int) 165 */ 166 public void test_ConstructorILjava_lang_StringIIIIIIIIIII() { 167 // Test for method java.util.SimpleTimeZone(int, java.lang.String, int, 168 // int, int, int, int, int, int, int, int, int, int) 169 // TODO : Implement test 170 //Regression for HARMONY-1241 171 assertNotNull(new SimpleTimeZone( 172 TimeZone.LONG, 173 "Europe/Paris", 174 SimpleTimeZone.STANDARD_TIME, 175 SimpleTimeZone.STANDARD_TIME, 176 SimpleTimeZone.UTC_TIME, 177 SimpleTimeZone.WALL_TIME, 178 SimpleTimeZone.WALL_TIME, 179 TimeZone.SHORT, 180 SimpleTimeZone.STANDARD_TIME, 181 TimeZone.LONG, 182 SimpleTimeZone.UTC_TIME, 183 SimpleTimeZone.STANDARD_TIME, 184 TimeZone.LONG)); 185 //seems RI doesn't check the startTimeMode and endTimeMode at all 186 //this behavior is contradicts with spec 187 assertNotNull(new SimpleTimeZone( 188 TimeZone.LONG, 189 "Europe/Paris", 190 SimpleTimeZone.STANDARD_TIME, 191 SimpleTimeZone.STANDARD_TIME, 192 SimpleTimeZone.UTC_TIME, 193 SimpleTimeZone.WALL_TIME, 194 Integer.MAX_VALUE, 195 TimeZone.SHORT, 196 SimpleTimeZone.STANDARD_TIME, 197 TimeZone.LONG, 198 SimpleTimeZone.UTC_TIME, 199 Integer.MIN_VALUE, 200 TimeZone.LONG)); 201 202 try { 203 new SimpleTimeZone(1000, "TEST", 12, 204 1, Calendar.SUNDAY, 0, Integer.MAX_VALUE, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 205 0, Integer.MAX_VALUE, 1000 * 60 * 60); 206 fail("IllegalArgumentException expected"); 207 } catch (IllegalArgumentException e) { 208 //expected 209 } 210 211 try { 212 new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 213 10, Calendar.SUNDAY, 0, Integer.MAX_VALUE, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 214 0, Integer.MAX_VALUE, 1000 * 60 * 60); 215 fail("IllegalArgumentException expected"); 216 } catch (IllegalArgumentException e) { 217 //expected 218 } 219 220 try { 221 new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 222 1, 10, 0, Calendar.NOVEMBER, Integer.MAX_VALUE, -1, Calendar.SUNDAY, 223 0, Integer.MAX_VALUE, 1000 * 60 * 60); 224 fail("IllegalArgumentException expected"); 225 } catch (IllegalArgumentException e) { 226 //expected 227 } 228 229 try { 230 new SimpleTimeZone(1000, "TEST", Calendar.DECEMBER, 231 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, Integer.MAX_VALUE, -10, Calendar.SUNDAY, 232 0, Integer.MAX_VALUE, 1000 * 60 * 60); 233 fail("IllegalArgumentException expected"); 234 } catch (IllegalArgumentException e) { 235 //expected 236 } 237 } 238 239 /** 240 * java.util.SimpleTimeZone#clone() 241 */ 242 public void test_clone() { 243 // Test for method java.lang.Object java.util.SimpleTimeZone.clone() 244 SimpleTimeZone st1 = new SimpleTimeZone(1000, "TEST", 245 Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, 246 -1, Calendar.SUNDAY, 0); 247 SimpleTimeZone stA = new SimpleTimeZone(1, "Gah"); 248 assertTrue("Clone resulted in same reference", st1.clone() != st1); 249 assertTrue("Clone resulted in unequal object", ((SimpleTimeZone) st1 250 .clone()).equals(st1)); 251 assertTrue("Clone resulted in same reference", stA.clone() != stA); 252 assertTrue("Clone resulted in unequal object", ((SimpleTimeZone) stA 253 .clone()).equals(stA)); 254 } 255 256 /** 257 * java.util.SimpleTimeZone#equals(java.lang.Object) 258 */ 259 public void test_equalsLjava_lang_Object() { 260 // Test for method boolean 261 // java.util.SimpleTimeZone.equals(java.lang.Object) 262 TimeZone tz = TimeZone.getTimeZone("EST"); 263 st1 = new SimpleTimeZone(tz.getRawOffset(), "EST"); 264 st2 = new SimpleTimeZone(0, "EST"); 265 assertFalse(st1.equals(st2)); 266 st1.setRawOffset(st2.getRawOffset()); 267 assertTrue(st1.equals(st2)); 268 } 269 270 /** 271 * java.util.SimpleTimeZone#getDSTSavings() 272 */ 273 public void test_getDSTSavings() { 274 // Test for method int java.util.SimpleTimeZone.getDSTSavings() 275 st1 = new SimpleTimeZone(0, "TEST"); 276 277 assertEquals("Non-zero default daylight savings", 278 0, st1.getDSTSavings()); 279 st1.setStartRule(0, 1, 1, 1); 280 st1.setEndRule(11, 1, 1, 1); 281 282 assertEquals("Incorrect default daylight savings", 283 3600000, st1.getDSTSavings()); 284 st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL, 1, 285 -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1, 286 Calendar.SUNDAY, 2 * 3600000, 7200000); 287 assertEquals("Incorrect daylight savings from constructor", 7200000, st1 288 .getDSTSavings()); 289 290 } 291 292 /** 293 * java.util.SimpleTimeZone#getOffset(int, int, int, int, int, int) 294 */ 295 public void test_getOffsetIIIIII() { 296 // Test for method int java.util.SimpleTimeZone.getOffset(int, int, int, 297 // int, int, int) 298// TimeZone st1 = TimeZone.getTimeZone("EST"); 299 st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST"); 300 assertTrue("Incorrect offset returned", st1.getOffset( 301 GregorianCalendar.AD, 1998, Calendar.NOVEMBER, 11, 302 Calendar.WEDNESDAY, 0) == -(5 * 60 * 60 * 1000)); 303 304 st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST"); 305 assertEquals("Incorrect offset returned", -(5 * 60 * 60 * 1000), st1 306 .getOffset(GregorianCalendar.AD, 1998, Calendar.JUNE, 11, 307 Calendar.THURSDAY, 0)); 308 309 // Regression for HARMONY-5459 310 st1 = new SimpleTimeZone(TimeZone.getDefault().getRawOffset(), TimeZone.getDefault().getID()); 311 int fourHours = 4*60*60*1000; 312 st1.setRawOffset(fourHours); 313 assertEquals(fourHours, st1.getOffset(1, 2099, 01, 1, 5, 0)); 314 315 try { 316 st1.getOffset(-1, 2099, 01, 1, 5, 0); 317 fail("IllegalArgumentException expected"); 318 } catch (IllegalArgumentException e) { 319 //expected 320 } 321 322 try { 323 st1.getOffset(1, 2099, 15, 1, 5, 0); 324 fail("IllegalArgumentException expected"); 325 } catch (IllegalArgumentException e) { 326 //expected 327 } 328 329 try { 330 st1.getOffset(1, 2099, 01, 100, 5, 0); 331 fail("IllegalArgumentException expected"); 332 } catch (IllegalArgumentException e) { 333 //expected 334 } 335 336 try { 337 st1.getOffset(1, 2099, 01, 1, 50, 0); 338 fail("IllegalArgumentException expected"); 339 } catch (IllegalArgumentException e) { 340 //expected 341 } 342 343 try { 344 st1.getOffset(1, 2099, 01, 1, 5, -10); 345 fail("IllegalArgumentException expected"); 346 } catch (IllegalArgumentException e) { 347 //expected 348 } 349 } 350 351 /** 352 * java.util.SimpleTimeZone#getRawOffset() 353 */ 354 public void test_getRawOffset() { 355 // Test for method int java.util.SimpleTimeZone.getRawOffset() 356 st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST"); 357 assertTrue("Incorrect offset returned", 358 st1.getRawOffset() == -(5 * 60 * 60 * 1000)); 359 360 } 361 362 /** 363 * java.util.SimpleTimeZone#hashCode() 364 */ 365 public void test_hashCode() { 366 // Test for method int java.util.SimpleTimeZone.hashCode() 367 // For lack of a better test. 368 st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL, 1, 369 -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1, 370 Calendar.SUNDAY, 2 * 3600000); 371 assertTrue(TimeZone.getTimeZone("EST").hashCode() != 0); 372 assertTrue(st1.hashCode() != 0); 373 } 374 375 /** 376 * java.util.SimpleTimeZone#hasSameRules(java.util.TimeZone) 377 */ 378 public void test_hasSameRulesLjava_util_TimeZone() { 379 // Test for method boolean 380 // java.util.SimpleTimeZone.hasSameRules(java.util.TimeZone) 381 SimpleTimeZone st = new SimpleTimeZone(1000, "TEST", Calendar.NOVEMBER, 382 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, -1, Calendar.SUNDAY, 383 0); 384 SimpleTimeZone sameAsSt = new SimpleTimeZone(1000, "REST", 385 Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0, Calendar.NOVEMBER, 386 -1, Calendar.SUNDAY, 0); 387 SimpleTimeZone notSameAsSt = new SimpleTimeZone(1000, "PEST", 388 Calendar.NOVEMBER, 2, Calendar.SUNDAY, 0, Calendar.NOVEMBER, 389 -1, Calendar.SUNDAY, 0); 390 assertTrue("Time zones have same rules but return false", st 391 .hasSameRules(sameAsSt)); 392 assertTrue("Time zones have different rules but return true", !st 393 .hasSameRules(notSameAsSt)); 394 } 395 396 /** 397 * java.util.SimpleTimeZone#inDaylightTime(java.util.Date) 398 */ 399 public void test_inDaylightTimeLjava_util_Date() { 400 // Test for method boolean 401 // java.util.SimpleTimeZone.inDaylightTime(java.util.Date) 402 TimeZone tz = TimeZone.getTimeZone("EST"); 403 SimpleTimeZone zone = new SimpleTimeZone(tz.getRawOffset(), "EST", 404 Calendar.APRIL, 1, -Calendar.SUNDAY, 7200000, Calendar.OCTOBER, -1, Calendar.SUNDAY, 7200000, 3600000); 405 GregorianCalendar gc = new GregorianCalendar(1998, Calendar.JUNE, 11); 406 407 assertTrue("Returned incorrect daylight value1", zone.inDaylightTime(gc 408 .getTime())); 409 gc = new GregorianCalendar(1998, Calendar.NOVEMBER, 11); 410 assertTrue("Returned incorrect daylight value2", !(zone 411 .inDaylightTime(gc.getTime()))); 412 gc = new GregorianCalendar(zone); 413 gc.set(1999, Calendar.APRIL, 4, 1, 59, 59); 414 assertTrue("Returned incorrect daylight value3", !(zone 415 .inDaylightTime(gc.getTime()))); 416 Date date = new Date(gc.getTime().getTime() + 1000); 417 assertTrue("Returned incorrect daylight value4", zone 418 .inDaylightTime(date)); 419 gc.set(1999, Calendar.OCTOBER, 31, 1, 0, 0); 420 assertTrue("Returned incorrect daylight value5", !(zone 421 .inDaylightTime(gc.getTime()))); 422 date = new Date(gc.getTime().getTime() - 1000); 423 assertTrue("Returned incorrect daylight value6", zone 424 .inDaylightTime(date)); 425 426 assertTrue("Returned incorrect daylight value7", !zone 427 .inDaylightTime(new Date(891752400000L + 7200000 - 1))); 428 assertTrue("Returned incorrect daylight value8", zone 429 .inDaylightTime(new Date(891752400000L + 7200000))); 430 assertTrue("Returned incorrect daylight value9", zone 431 .inDaylightTime(new Date(909288000000L + 7200000 - 1))); 432 assertTrue("Returned incorrect daylight value10", !zone 433 .inDaylightTime(new Date(909288000000L + 7200000))); 434 } 435 436 /** 437 * java.util.SimpleTimeZone#setDSTSavings(int) 438 */ 439 public void test_setDSTSavingsI() { 440 // Test for method void java.util.SimpleTimeZone.setDSTSavings(int) 441 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 442 st.setStartRule(0, 1, 1, 1); 443 st.setEndRule(11, 1, 1, 1); 444 st.setDSTSavings(1); 445 assertEquals("Daylight savings amount not set", 1, st.getDSTSavings()); 446 } 447 448 /** 449 * java.util.SimpleTimeZone#setEndRule(int, int, int) 450 */ 451 public void test_setEndRuleIII() { 452 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 453 st.setStartRule(Calendar.NOVEMBER, 1, 0); 454 st.setEndRule(Calendar.NOVEMBER, 20, 0); 455 assertTrue("StartRule improperly set1", st.useDaylightTime()); 456 assertTrue("StartRule improperly set2", st.inDaylightTime( 457 new GregorianCalendar(1998, Calendar.NOVEMBER, 458 13).getTime())); 459 assertTrue("StartRule improperly set3", !(st 460 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 461 13).getTime()))); 462 463 try { 464 st.setEndRule(13, 20, 0); 465 fail("IllegalArgumentException is not thrown."); 466 } catch(IllegalArgumentException iae) { 467 //expected 468 } 469 470 try { 471 st.setEndRule(1, 32, 0); 472 fail("IllegalArgumentException is not thrown."); 473 } catch(IllegalArgumentException iae) { 474 //expected 475 } 476 477 try { 478 st.setEndRule(1, 30, 10); 479 fail("IllegalArgumentException is not thrown."); 480 } catch(IllegalArgumentException iae) { 481 //expected 482 } 483 } 484 485 /** 486 * java.util.SimpleTimeZone#setEndRule(int, int, int, int) 487 */ 488 public void test_setEndRuleIIII() { 489 // Test for method void java.util.SimpleTimeZone.setEndRule(int, int, 490 // int, int) 491 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 492 // Spec indicates that both end and start must be set or result is 493 // undefined 494 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 495 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 496 assertTrue("StartRule improperly set1", st.useDaylightTime()); 497 assertTrue("StartRule improperly set2", st 498 .inDaylightTime(new GregorianCalendar(1998, Calendar.NOVEMBER, 499 13).getTime())); 500 assertTrue("StartRule improperly set3", !(st 501 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 502 13).getTime()))); 503 504 try { 505 st.setEndRule(12, -1, Calendar.SUNDAY, 0); 506 fail("IllegalArgumentException is not thrown."); 507 } catch(IllegalArgumentException iae) { 508 //expected 509 } 510 511 try { 512 st.setEndRule(Calendar.NOVEMBER, 10, Calendar.SUNDAY, 0); 513 fail("IllegalArgumentException is not thrown."); 514 } catch(IllegalArgumentException iae) { 515 //expected 516 } 517 518 try { 519 st.setEndRule(Calendar.NOVEMBER, -1, 8, 0); 520 fail("IllegalArgumentException is not thrown."); 521 } catch(IllegalArgumentException iae) { 522 //expected 523 } 524 525 try { 526 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, -10); 527 fail("IllegalArgumentException is not thrown."); 528 } catch(IllegalArgumentException iae) { 529 //expected 530 } 531 } 532 533 /** 534 * java.util.SimpleTimeZone#setEndRule(int, int, int, int, boolean) 535 */ 536 public void test_setEndRuleIIIIZ() { 537 // Test for method void java.util.SimpleTimeZone.setEndRule(int, int, 538 // int, int, boolean) 539 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 540 // Spec indicates that both end and start must be set or result is 541 // undefined 542 st.setStartRule(Calendar.NOVEMBER, 8, Calendar.SUNDAY, 1, false); 543 st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, true); 544 assertTrue("StartRule improperly set1", st.useDaylightTime()); 545 assertTrue("StartRule improperly set2", st 546 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 547 7, 12, 0).getTime()))); 548 assertTrue("StartRule improperly set3", st 549 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 550 20, 12, 0).getTime()))); 551 assertTrue("StartRule improperly set4", !(st 552 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 553 6, 12, 0).getTime()))); 554 assertTrue("StartRule improperly set5", !(st 555 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 556 21, 12, 0).getTime()))); 557 558 try { 559 st.setEndRule(20, 15, Calendar.SUNDAY, 1, true); 560 fail("IllegalArgumentException is not thrown."); 561 } catch(IllegalArgumentException iae) { 562 //expected 563 } 564 565 try { 566 st.setEndRule(Calendar.NOVEMBER, 35, Calendar.SUNDAY, 1, true); 567 fail("IllegalArgumentException is not thrown."); 568 } catch(IllegalArgumentException iae) { 569 //expected 570 } 571 572 try { 573 st.setEndRule(Calendar.NOVEMBER, 15, 12, 1, true); 574 fail("IllegalArgumentException is not thrown."); 575 } catch(IllegalArgumentException iae) { 576 //expected 577 } 578 579 try { 580 st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, -1, true); 581 fail("IllegalArgumentException is not thrown."); 582 } catch(IllegalArgumentException iae) { 583 //expected 584 } 585 } 586 587 /** 588 * java.util.SimpleTimeZone#setRawOffset(int) 589 */ 590 public void test_setRawOffsetI() { 591 // Test for method void java.util.SimpleTimeZone.setRawOffset(int) 592 593 st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST"); 594 int off = st1.getRawOffset(); 595 st1.setRawOffset(1000); 596 boolean val = st1.getRawOffset() == 1000; 597 st1.setRawOffset(off); 598 assertTrue("Incorrect offset set", val); 599 } 600 601 /** 602 * java.util.SimpleTimeZone#setStartRule(int, int, int) 603 */ 604 public void test_setStartRuleIII() { 605 // Test for method void java.util.SimpleTimeZone.setStartRule(int, int, 606 // int) 607 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 608 // Spec indicates that both end and start must be set or result is 609 // undefined 610 st.setStartRule(Calendar.NOVEMBER, 1, 1); 611 st.setEndRule(Calendar.DECEMBER, 1, 1); 612 assertTrue("StartRule improperly set", st.useDaylightTime()); 613 assertTrue("StartRule improperly set", st 614 .inDaylightTime((new GregorianCalendar(1998, Calendar.NOVEMBER, 615 13).getTime()))); 616 assertTrue("StartRule improperly set", !(st 617 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 618 13).getTime()))); 619 620 try { 621 st.setStartRule(13, 20, 0); 622 fail("IllegalArgumentException is not thrown."); 623 } catch(IllegalArgumentException iae) { 624 //expected 625 } 626 627 try { 628 st.setStartRule(1, 32, 0); 629 fail("IllegalArgumentException is not thrown."); 630 } catch(IllegalArgumentException iae) { 631 //expected 632 } 633 634 try { 635 st.setStartRule(1, 30, 10); 636 fail("IllegalArgumentException is not thrown."); 637 } catch(IllegalArgumentException iae) { 638 //expected 639 } 640 } 641 642 /** 643 * java.util.SimpleTimeZone#setStartRule(int, int, int, int) 644 */ 645 public void test_setStartRuleIIII() { 646 // Test for method void java.util.SimpleTimeZone.setStartRule(int, int, 647 // int, int) 648 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 649 // Spec indicates that both end and start must be set or result is 650 // undefined 651 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 652 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 653 assertTrue("StartRule improperly set1", st.useDaylightTime()); 654 assertTrue("StartRule improperly set2", st 655 .inDaylightTime((new GregorianCalendar(1998, Calendar.NOVEMBER, 656 13).getTime()))); 657 assertTrue("StartRule improperly set3", !(st 658 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 659 13).getTime()))); 660 661 try { 662 st.setStartRule(12, -1, Calendar.SUNDAY, 0); 663 fail("IllegalArgumentException is not thrown."); 664 } catch(IllegalArgumentException iae) { 665 //expected 666 } 667 668 try { 669 st.setStartRule(Calendar.NOVEMBER, 10, Calendar.SUNDAY, 0); 670 fail("IllegalArgumentException is not thrown."); 671 } catch(IllegalArgumentException iae) { 672 //expected 673 } 674 675 try { 676 st.setStartRule(Calendar.NOVEMBER, -1, 8, 0); 677 fail("IllegalArgumentException is not thrown."); 678 } catch(IllegalArgumentException iae) { 679 //expected 680 } 681 682 try { 683 st.setStartRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, -10); 684 fail("IllegalArgumentException is not thrown."); 685 } catch(IllegalArgumentException iae) { 686 //expected 687 } 688 } 689 690 /** 691 * java.util.SimpleTimeZone#setStartRule(int, int, int, int, boolean) 692 */ 693 public void test_setStartRuleIIIIZ() { 694 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 695 // Test for method void java.util.SimpleTimeZone.setStartRule(int, int, 696 // int, int, boolean) 697 SimpleTimeZone st = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST"); 698 // Spec indicates that both end and start must be set or result is 699 // undefined 700 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 1, true); 701 st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, false); 702 assertTrue("StartRule improperly set1", st.useDaylightTime()); 703 assertTrue("StartRule improperly set2", st 704 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 705 7, 12, 0).getTime()))); 706 assertTrue("StartRule improperly set3", st 707 .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER, 708 13, 12, 0).getTime()))); 709 assertTrue("StartRule improperly set4", !(st 710 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 711 6, 12, 0).getTime()))); 712 assertTrue("StartRule improperly set5", !(st 713 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 714 14, 12, 0).getTime()))); 715 716 try { 717 st.setStartRule(20, 15, Calendar.SUNDAY, 1, true); 718 fail("IllegalArgumentException is not thrown."); 719 } catch(IllegalArgumentException iae) { 720 //expected 721 } 722 723 try { 724 st.setStartRule(Calendar.NOVEMBER, 35, Calendar.SUNDAY, 1, true); 725 fail("IllegalArgumentException is not thrown."); 726 } catch(IllegalArgumentException iae) { 727 //expected 728 } 729 730 try { 731 st.setStartRule(Calendar.NOVEMBER, 15, 12, 1, true); 732 fail("IllegalArgumentException is not thrown."); 733 } catch(IllegalArgumentException iae) { 734 //expected 735 } 736 737 try { 738 st.setStartRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, -1, true); 739 fail("IllegalArgumentException is not thrown."); 740 } catch(IllegalArgumentException iae) { 741 //expected 742 } 743 } 744 745 /** 746 * java.util.SimpleTimeZone#setStartYear(int) 747 */ 748 public void test_setStartYearI() { 749 // Test for method void java.util.SimpleTimeZone.setStartYear(int) 750 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 751 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 752 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 753 st.setStartYear(1999); 754 assertTrue("set year improperly set1", !(st 755 .inDaylightTime(new GregorianCalendar(1999, Calendar.JULY, 12) 756 .getTime()))); 757 assertTrue("set year improperly set2", !(st 758 .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER, 759 13).getTime()))); 760 assertTrue("set year improperly set3", (st 761 .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER, 762 13).getTime()))); 763 } 764 765 /** 766 * java.util.SimpleTimeZone#toString() 767 */ 768 public void test_toString() { 769 // Test for method java.lang.String java.util.SimpleTimeZone.toString() 770 String string = TimeZone.getTimeZone("EST").toString(); 771 assertNotNull("toString() returned null", string); 772 assertTrue("toString() is empty", string.length() != 0); 773 } 774 775 /** 776 * java.util.SimpleTimeZone#useDaylightTime() 777 */ 778 public void test_useDaylightTime() { 779 // Test for method boolean java.util.SimpleTimeZone.useDaylightTime() 780 SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ"); 781 assertTrue("useDaylightTime returned incorrect value", !st 782 .useDaylightTime()); 783 // Spec indicates that both end and start must be set or result is 784 // undefined 785 st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0); 786 st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0); 787 assertTrue("useDaylightTime returned incorrect value", st 788 .useDaylightTime()); 789 } 790 791 public void test_getOffsetJ() { 792 Calendar cal = Calendar.getInstance(); 793 cal.set(1998, Calendar.NOVEMBER, 11, 0, 0); 794 st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST"); 795 796 assertTrue("Incorrect offset returned", st1.getOffset(cal.getTimeInMillis()) == 797 -(5 * 60 * 60 * 1000)); 798 799 st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST"); 800 cal.set(1998, Calendar.JUNE, 11, 0, 0); 801 assertEquals("Incorrect offset returned", -(5 * 60 * 60 * 1000), st1 802 .getOffset(cal.getTimeInMillis())); 803 804 // Regression for HARMONY-5459 805 st1 = new SimpleTimeZone(TimeZone.getDefault().getRawOffset(), TimeZone.getDefault().getID()); 806 int fourHours = 4*60*60*1000; 807 st1.setRawOffset(fourHours); 808 cal.set(2099, 01, 1, 0, 0); 809 810 assertEquals(fourHours, st1.getOffset(cal.getTimeInMillis())); 811 812 } 813 814 815 /** 816 * Sets up the fixture, for example, open a network connection. This method 817 * is called before a test is executed. 818 */ 819 protected void setUp() { 820 } 821 822 /** 823 * Tears down the fixture, for example, close a network connection. This 824 * method is called after a test is executed. 825 */ 826 protected void tearDown() { 827 } 828} 829