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(1, st.getDSTSavings());
446        try {
447            st.setDSTSavings(0);
448            fail();
449        } catch (IllegalArgumentException expected) {
450        }
451        try {
452            st.setDSTSavings(-1);
453            fail();
454        } catch (IllegalArgumentException expected) {
455        }
456      }
457
458    /**
459     * java.util.SimpleTimeZone#setEndRule(int, int, int)
460     */
461    public void test_setEndRuleIII() {
462        SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
463        st.setStartRule(Calendar.NOVEMBER, 1, 0);
464        st.setEndRule(Calendar.NOVEMBER, 20, 0);
465        assertTrue("StartRule improperly set1", st.useDaylightTime());
466        assertTrue("StartRule improperly set2", st.inDaylightTime(
467                new GregorianCalendar(1998, Calendar.NOVEMBER,
468                        13).getTime()));
469        assertTrue("StartRule improperly set3", !(st
470                .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER,
471                        13).getTime())));
472
473        try {
474            st.setEndRule(13, 20, 0);
475            fail("IllegalArgumentException is not thrown.");
476        } catch(IllegalArgumentException iae) {
477            //expected
478        }
479
480        try {
481            st.setEndRule(1, 32, 0);
482            fail("IllegalArgumentException is not thrown.");
483        } catch(IllegalArgumentException iae) {
484            //expected
485        }
486
487        try {
488            st.setEndRule(1, 30, 10);
489            fail("IllegalArgumentException is not thrown.");
490        } catch(IllegalArgumentException iae) {
491            //expected
492        }
493    }
494
495    /**
496     * java.util.SimpleTimeZone#setEndRule(int, int, int, int)
497     */
498    public void test_setEndRuleIIII() {
499        // Test for method void java.util.SimpleTimeZone.setEndRule(int, int,
500        // int, int)
501        SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
502        // Spec indicates that both end and start must be set or result is
503        // undefined
504        st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
505        st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
506        assertTrue("StartRule improperly set1", st.useDaylightTime());
507        assertTrue("StartRule improperly set2", st
508                .inDaylightTime(new GregorianCalendar(1998, Calendar.NOVEMBER,
509                        13).getTime()));
510        assertTrue("StartRule improperly set3", !(st
511                .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER,
512                        13).getTime())));
513
514        try {
515            st.setEndRule(12, -1, Calendar.SUNDAY, 0);
516            fail("IllegalArgumentException is not thrown.");
517        } catch(IllegalArgumentException iae) {
518            //expected
519        }
520
521        try {
522            st.setEndRule(Calendar.NOVEMBER, 10, Calendar.SUNDAY, 0);
523            fail("IllegalArgumentException is not thrown.");
524        } catch(IllegalArgumentException iae) {
525            //expected
526        }
527
528        try {
529            st.setEndRule(Calendar.NOVEMBER, -1, 8, 0);
530            fail("IllegalArgumentException is not thrown.");
531        } catch(IllegalArgumentException iae) {
532            //expected
533        }
534
535        try {
536            st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, -10);
537            fail("IllegalArgumentException is not thrown.");
538        } catch(IllegalArgumentException iae) {
539            //expected
540        }
541    }
542
543    /**
544     * java.util.SimpleTimeZone#setEndRule(int, int, int, int, boolean)
545     */
546    public void test_setEndRuleIIIIZ() {
547        // Test for method void java.util.SimpleTimeZone.setEndRule(int, int,
548        // int, int, boolean)
549        SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
550        // Spec indicates that both end and start must be set or result is
551        // undefined
552        st.setStartRule(Calendar.NOVEMBER, 8, Calendar.SUNDAY, 1, false);
553        st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, true);
554        assertTrue("StartRule improperly set1", st.useDaylightTime());
555        assertTrue("StartRule improperly set2", st
556                .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER,
557                        7, 12, 0).getTime())));
558        assertTrue("StartRule improperly set3", st
559                .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER,
560                        20, 12, 0).getTime())));
561        assertTrue("StartRule improperly set4", !(st
562                .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER,
563                        6, 12, 0).getTime())));
564        assertTrue("StartRule improperly set5", !(st
565                .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER,
566                        21, 12, 0).getTime())));
567
568        try {
569            st.setEndRule(20, 15, Calendar.SUNDAY, 1, true);
570            fail("IllegalArgumentException is not thrown.");
571        } catch(IllegalArgumentException iae) {
572            //expected
573        }
574
575        try {
576            st.setEndRule(Calendar.NOVEMBER, 35, Calendar.SUNDAY, 1, true);
577            fail("IllegalArgumentException is not thrown.");
578        } catch(IllegalArgumentException iae) {
579            //expected
580        }
581
582        try {
583            st.setEndRule(Calendar.NOVEMBER, 15, 12, 1, true);
584            fail("IllegalArgumentException is not thrown.");
585        } catch(IllegalArgumentException iae) {
586            //expected
587        }
588
589        try {
590            st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, -1, true);
591            fail("IllegalArgumentException is not thrown.");
592        } catch(IllegalArgumentException iae) {
593            //expected
594        }
595    }
596
597    /**
598     * java.util.SimpleTimeZone#setRawOffset(int)
599     */
600    public void test_setRawOffsetI() {
601        // Test for method void java.util.SimpleTimeZone.setRawOffset(int)
602
603        st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST");
604        int off = st1.getRawOffset();
605        st1.setRawOffset(1000);
606        boolean val = st1.getRawOffset() == 1000;
607        st1.setRawOffset(off);
608        assertTrue("Incorrect offset set", val);
609    }
610
611    /**
612     * java.util.SimpleTimeZone#setStartRule(int, int, int)
613     */
614    public void test_setStartRuleIII() {
615        // Test for method void java.util.SimpleTimeZone.setStartRule(int, int,
616        // int)
617        SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
618        // Spec indicates that both end and start must be set or result is
619        // undefined
620        st.setStartRule(Calendar.NOVEMBER, 1, 1);
621        st.setEndRule(Calendar.DECEMBER, 1, 1);
622        assertTrue("StartRule improperly set", st.useDaylightTime());
623        assertTrue("StartRule improperly set", st
624                .inDaylightTime((new GregorianCalendar(1998, Calendar.NOVEMBER,
625                        13).getTime())));
626        assertTrue("StartRule improperly set", !(st
627                .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER,
628                        13).getTime())));
629
630        try {
631            st.setStartRule(13, 20, 0);
632            fail("IllegalArgumentException is not thrown.");
633        } catch(IllegalArgumentException iae) {
634            //expected
635        }
636
637        try {
638            st.setStartRule(1, 32, 0);
639            fail("IllegalArgumentException is not thrown.");
640        } catch(IllegalArgumentException iae) {
641            //expected
642        }
643
644        try {
645            st.setStartRule(1, 30, 10);
646            fail("IllegalArgumentException is not thrown.");
647        } catch(IllegalArgumentException iae) {
648            //expected
649        }
650    }
651
652    /**
653     * java.util.SimpleTimeZone#setStartRule(int, int, int, int)
654     */
655    public void test_setStartRuleIIII() {
656        // Test for method void java.util.SimpleTimeZone.setStartRule(int, int,
657        // int, int)
658        SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
659        // Spec indicates that both end and start must be set or result is
660        // undefined
661        st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
662        st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
663        assertTrue("StartRule improperly set1", st.useDaylightTime());
664        assertTrue("StartRule improperly set2", st
665                .inDaylightTime((new GregorianCalendar(1998, Calendar.NOVEMBER,
666                        13).getTime())));
667        assertTrue("StartRule improperly set3", !(st
668                .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER,
669                        13).getTime())));
670
671        try {
672            st.setStartRule(12, -1, Calendar.SUNDAY, 0);
673            fail("IllegalArgumentException is not thrown.");
674        } catch(IllegalArgumentException iae) {
675            //expected
676        }
677
678        try {
679            st.setStartRule(Calendar.NOVEMBER, 10, Calendar.SUNDAY, 0);
680            fail("IllegalArgumentException is not thrown.");
681        } catch(IllegalArgumentException iae) {
682            //expected
683        }
684
685        try {
686            st.setStartRule(Calendar.NOVEMBER, -1, 8, 0);
687            fail("IllegalArgumentException is not thrown.");
688        } catch(IllegalArgumentException iae) {
689            //expected
690        }
691
692        try {
693            st.setStartRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, -10);
694            fail("IllegalArgumentException is not thrown.");
695        } catch(IllegalArgumentException iae) {
696            //expected
697        }
698    }
699
700    /**
701     * java.util.SimpleTimeZone#setStartRule(int, int, int, int, boolean)
702     */
703    public void test_setStartRuleIIIIZ() {
704        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
705        // Test for method void java.util.SimpleTimeZone.setStartRule(int, int,
706        // int, int, boolean)
707        SimpleTimeZone st = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST");
708        // Spec indicates that both end and start must be set or result is
709        // undefined
710        st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 1, true);
711        st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, false);
712        assertTrue("StartRule improperly set1", st.useDaylightTime());
713        assertTrue("StartRule improperly set2", st
714                .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER,
715                        7, 12, 0).getTime())));
716        assertTrue("StartRule improperly set3", st
717                .inDaylightTime((new GregorianCalendar(1999, Calendar.NOVEMBER,
718                        13, 12, 0).getTime())));
719        assertTrue("StartRule improperly set4", !(st
720                .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER,
721                        6, 12, 0).getTime())));
722        assertTrue("StartRule improperly set5", !(st
723                .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER,
724                        14, 12, 0).getTime())));
725
726        try {
727            st.setStartRule(20, 15, Calendar.SUNDAY, 1, true);
728            fail("IllegalArgumentException is not thrown.");
729        } catch(IllegalArgumentException iae) {
730            //expected
731        }
732
733        try {
734            st.setStartRule(Calendar.NOVEMBER, 35, Calendar.SUNDAY, 1, true);
735            fail("IllegalArgumentException is not thrown.");
736        } catch(IllegalArgumentException iae) {
737            //expected
738        }
739
740        try {
741            st.setStartRule(Calendar.NOVEMBER, 15, 12, 1, true);
742            fail("IllegalArgumentException is not thrown.");
743        } catch(IllegalArgumentException iae) {
744            //expected
745        }
746
747        try {
748            st.setStartRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, -1, true);
749            fail("IllegalArgumentException is not thrown.");
750        } catch(IllegalArgumentException iae) {
751            //expected
752        }
753    }
754
755    /**
756     * java.util.SimpleTimeZone#setStartYear(int)
757     */
758    public void test_setStartYearI() {
759        // Test for method void java.util.SimpleTimeZone.setStartYear(int)
760        SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
761        st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
762        st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
763        st.setStartYear(1999);
764        assertTrue("set year improperly set1", !(st
765                .inDaylightTime(new GregorianCalendar(1999, Calendar.JULY, 12)
766                        .getTime())));
767        assertTrue("set year improperly set2", !(st
768                .inDaylightTime(new GregorianCalendar(1998, Calendar.OCTOBER,
769                        13).getTime())));
770        assertTrue("set year improperly set3", (st
771                .inDaylightTime(new GregorianCalendar(1999, Calendar.NOVEMBER,
772                        13).getTime())));
773    }
774
775    /**
776     * java.util.SimpleTimeZone#toString()
777     */
778    public void test_toString() {
779        // Test for method java.lang.String java.util.SimpleTimeZone.toString()
780        String string = TimeZone.getTimeZone("EST").toString();
781        assertNotNull("toString() returned null", string);
782        assertTrue("toString() is empty", string.length() != 0);
783    }
784
785    /**
786     * java.util.SimpleTimeZone#useDaylightTime()
787     */
788    public void test_useDaylightTime() {
789        // Test for method boolean java.util.SimpleTimeZone.useDaylightTime()
790        SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
791        assertTrue("useDaylightTime returned incorrect value", !st
792                .useDaylightTime());
793        // Spec indicates that both end and start must be set or result is
794        // undefined
795        st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
796        st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
797        assertTrue("useDaylightTime returned incorrect value", st
798                .useDaylightTime());
799    }
800
801    public void test_getOffsetJ() {
802        Calendar cal = Calendar.getInstance();
803        cal.set(1998, Calendar.NOVEMBER, 11, 0, 0);
804        st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST");
805
806        assertTrue("Incorrect offset returned", st1.getOffset(cal.getTimeInMillis()) ==
807            -(5 * 60 * 60 * 1000));
808
809        st1 = new SimpleTimeZone(TimeZone.getTimeZone("EST").getRawOffset(), "EST");
810        cal.set(1998, Calendar.JUNE, 11, 0, 0);
811        assertEquals("Incorrect offset returned", -(5 * 60 * 60 * 1000), st1
812                .getOffset(cal.getTimeInMillis()));
813
814        // Regression for HARMONY-5459
815        st1 = new SimpleTimeZone(TimeZone.getDefault().getRawOffset(), TimeZone.getDefault().getID());
816        int fourHours = 4*60*60*1000;
817        st1.setRawOffset(fourHours);
818        cal.set(2099, 01, 1, 0, 0);
819
820        assertEquals(fourHours, st1.getOffset(cal.getTimeInMillis()));
821
822    }
823
824
825    /**
826     * Sets up the fixture, for example, open a network connection. This method
827     * is called before a test is executed.
828     */
829    protected void setUp() {
830    }
831
832    /**
833     * Tears down the fixture, for example, close a network connection. This
834     * method is called after a test is executed.
835     */
836    protected void tearDown() {
837    }
838}
839