001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.GregorianCalendar;
023: import java.util.SimpleTimeZone;
024: import java.util.TimeZone;
025:
026: public class SimpleTimeZoneTest extends junit.framework.TestCase {
027:
028: SimpleTimeZone st1;
029:
030: SimpleTimeZone st2;
031:
032: /**
033: * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String)
034: */
035: public void test_ConstructorILjava_lang_String() {
036: // Test for method java.util.SimpleTimeZone(int, java.lang.String)
037:
038: SimpleTimeZone st = new SimpleTimeZone(1000, "TEST");
039: assertEquals("Incorrect TZ constructed", "TEST", st.getID());
040: assertTrue("Incorrect TZ constructed: "
041: + "returned wrong offset", st.getRawOffset() == 1000);
042: assertTrue("Incorrect TZ constructed"
043: + "using daylight savings", !st.useDaylightTime());
044: }
045:
046: /**
047: * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String,
048: * int, int, int, int, int, int, int, int)
049: */
050: public void test_ConstructorILjava_lang_StringIIIIIIII() {
051: // Test for method java.util.SimpleTimeZone(int, java.lang.String, int,
052: // int, int, int, int, int, int, int)
053: SimpleTimeZone st = new SimpleTimeZone(1000, "TEST",
054: Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0,
055: Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
056: assertTrue("Incorrect TZ constructed", st
057: .inDaylightTime(new GregorianCalendar(1998,
058: Calendar.NOVEMBER, 13).getTime()));
059: assertTrue("Incorrect TZ constructed", !(st
060: .inDaylightTime(new GregorianCalendar(1998,
061: Calendar.OCTOBER, 13).getTime())));
062: assertEquals("Incorrect TZ constructed", "TEST", st.getID());
063: assertEquals("Incorrect TZ constructed", 1000, st
064: .getRawOffset());
065: assertTrue("Incorrect TZ constructed", st.useDaylightTime());
066: }
067:
068: /**
069: * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String,
070: * int, int, int, int, int, int, int, int, int)
071: */
072: public void test_ConstructorILjava_lang_StringIIIIIIIII() {
073: // Test for method java.util.SimpleTimeZone(int, java.lang.String, int,
074: // int, int, int, int, int, int, int, int)
075: SimpleTimeZone st = new SimpleTimeZone(1000, "TEST",
076: Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0,
077: Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0,
078: 1000 * 60 * 60);
079: assertTrue("Incorrect TZ constructed", st
080: .inDaylightTime(new GregorianCalendar(1998,
081: Calendar.NOVEMBER, 13).getTime()));
082: assertTrue("Incorrect TZ constructed", !(st
083: .inDaylightTime(new GregorianCalendar(1998,
084: Calendar.OCTOBER, 13).getTime())));
085: assertEquals("Incorrect TZ constructed", "TEST", st.getID());
086: assertEquals("Incorrect TZ constructed", 1000, st
087: .getRawOffset());
088: assertTrue("Incorrect TZ constructed", st.useDaylightTime());
089: assertTrue("Incorrect TZ constructed",
090: st.getDSTSavings() == 1000 * 60 * 60);
091: }
092:
093: /**
094: * @tests java.util.SimpleTimeZone#SimpleTimeZone(int, java.lang.String,
095: * int, int, int, int, int, int, int, int, int, int, int)
096: */
097: public void test_ConstructorILjava_lang_StringIIIIIIIIIII() {
098: // Test for method java.util.SimpleTimeZone(int, java.lang.String, int,
099: // int, int, int, int, int, int, int, int, int, int)
100: // TODO : Implement test
101: //Regression for HARMONY-1241
102: assertNotNull(new SimpleTimeZone(TimeZone.LONG, "Europe/Paris",
103: SimpleTimeZone.STANDARD_TIME,
104: SimpleTimeZone.STANDARD_TIME, SimpleTimeZone.UTC_TIME,
105: SimpleTimeZone.WALL_TIME, SimpleTimeZone.WALL_TIME,
106: TimeZone.SHORT, SimpleTimeZone.STANDARD_TIME,
107: TimeZone.LONG, SimpleTimeZone.UTC_TIME,
108: SimpleTimeZone.STANDARD_TIME, TimeZone.LONG));
109: //seems RI doesn't check the startTimeMode and endTimeMode at all
110: //this behavior is contradicts with spec
111: assertNotNull(new SimpleTimeZone(TimeZone.LONG, "Europe/Paris",
112: SimpleTimeZone.STANDARD_TIME,
113: SimpleTimeZone.STANDARD_TIME, SimpleTimeZone.UTC_TIME,
114: SimpleTimeZone.WALL_TIME, Integer.MAX_VALUE,
115: TimeZone.SHORT, SimpleTimeZone.STANDARD_TIME,
116: TimeZone.LONG, SimpleTimeZone.UTC_TIME,
117: Integer.MIN_VALUE, TimeZone.LONG));
118: }
119:
120: /**
121: * @tests java.util.SimpleTimeZone#clone()
122: */
123: public void test_clone() {
124: // Test for method java.lang.Object java.util.SimpleTimeZone.clone()
125: SimpleTimeZone st1 = new SimpleTimeZone(1000, "TEST",
126: Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0,
127: Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
128: SimpleTimeZone stA = new SimpleTimeZone(1, "Gah");
129: assertTrue("Clone resulted in same reference",
130: st1.clone() != st1);
131: assertTrue("Clone resulted in unequal object",
132: ((SimpleTimeZone) st1.clone()).equals(st1));
133: assertTrue("Clone resulted in same reference",
134: stA.clone() != stA);
135: assertTrue("Clone resulted in unequal object",
136: ((SimpleTimeZone) stA.clone()).equals(stA));
137: }
138:
139: /**
140: * @tests java.util.SimpleTimeZone#equals(java.lang.Object)
141: */
142: public void test_equalsLjava_lang_Object() {
143: // Test for method boolean
144: // java.util.SimpleTimeZone.equals(java.lang.Object)
145:
146: st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL,
147: 1, -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1,
148: Calendar.SUNDAY, 2 * 3600000);
149: assertTrue("Equalty test1 failed", TimeZone.getTimeZone("EST")
150: .equals(st1));
151: assertTrue("Equalty test2 failed", !(TimeZone
152: .getTimeZone("CST").equals(st1)));
153: }
154:
155: /**
156: * @tests java.util.SimpleTimeZone#getDSTSavings()
157: */
158: public void test_getDSTSavings() {
159: // Test for method int java.util.SimpleTimeZone.getDSTSavings()
160: st1 = new SimpleTimeZone(0, "TEST");
161:
162: assertEquals("Non-zero default daylight savings", 0, st1
163: .getDSTSavings());
164: st1.setStartRule(0, 1, 1, 1);
165: st1.setEndRule(11, 1, 1, 1);
166:
167: assertEquals("Incorrect default daylight savings", 3600000, st1
168: .getDSTSavings());
169: st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL,
170: 1, -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1,
171: Calendar.SUNDAY, 2 * 3600000, 7200000);
172: assertEquals("Incorrect daylight savings from constructor",
173: 7200000, st1.getDSTSavings());
174:
175: }
176:
177: /**
178: * @tests java.util.SimpleTimeZone#getOffset(int, int, int, int, int, int)
179: */
180: public void test_getOffsetIIIIII() {
181: // Test for method int java.util.SimpleTimeZone.getOffset(int, int, int,
182: // int, int, int)
183: TimeZone st1 = TimeZone.getTimeZone("EST");
184: assertTrue("Incorrect offset returned", st1.getOffset(
185: GregorianCalendar.AD, 1998, Calendar.NOVEMBER, 11,
186: Calendar.WEDNESDAY, 0) == -(5 * 60 * 60 * 1000));
187:
188: st1 = TimeZone.getTimeZone("EST");
189: assertEquals("Incorrect offset returned",
190: -(5 * 60 * 60 * 1000), st1.getOffset(
191: GregorianCalendar.AD, 1998, Calendar.JUNE, 11,
192: Calendar.THURSDAY, 0));
193:
194: // Regression for HARMONY-5459
195: st1 = (TimeZone) TimeZone.getDefault();
196: int fourHours = 4 * 60 * 60 * 1000;
197: st1.setRawOffset(fourHours);
198: assertEquals(fourHours, st1.getOffset(1, 2099, 01, 1, 5, 0));
199:
200: }
201:
202: /**
203: * @tests java.util.SimpleTimeZone#getRawOffset()
204: */
205: public void test_getRawOffset() {
206: // Test for method int java.util.SimpleTimeZone.getRawOffset()
207: st1 = (SimpleTimeZone) TimeZone.getTimeZone("EST");
208: assertTrue("Incorrect offset returned",
209: st1.getRawOffset() == -(5 * 60 * 60 * 1000));
210:
211: }
212:
213: /**
214: * @tests java.util.SimpleTimeZone#hashCode()
215: */
216: public void test_hashCode() {
217: // Test for method int java.util.SimpleTimeZone.hashCode()
218: // For lack of a better test.
219: st1 = new SimpleTimeZone(-5 * 3600000, "EST", Calendar.APRIL,
220: 1, -Calendar.SUNDAY, 2 * 3600000, Calendar.OCTOBER, -1,
221: Calendar.SUNDAY, 2 * 3600000);
222: assertTrue("Returned different hashcodes", TimeZone
223: .getTimeZone("EST").hashCode() == st1.hashCode());
224: }
225:
226: /**
227: * @tests java.util.SimpleTimeZone#hasSameRules(java.util.TimeZone)
228: */
229: public void test_hasSameRulesLjava_util_TimeZone() {
230: // Test for method boolean
231: // java.util.SimpleTimeZone.hasSameRules(java.util.TimeZone)
232: SimpleTimeZone st = new SimpleTimeZone(1000, "TEST",
233: Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0,
234: Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
235: SimpleTimeZone sameAsSt = new SimpleTimeZone(1000, "REST",
236: Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0,
237: Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
238: SimpleTimeZone notSameAsSt = new SimpleTimeZone(1000, "PEST",
239: Calendar.NOVEMBER, 2, Calendar.SUNDAY, 0,
240: Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
241: assertTrue("Time zones have same rules but return false", st
242: .hasSameRules(sameAsSt));
243: assertTrue("Time zones have different rules but return true",
244: !st.hasSameRules(notSameAsSt));
245: }
246:
247: /**
248: * @tests java.util.SimpleTimeZone#inDaylightTime(java.util.Date)
249: */
250: public void test_inDaylightTimeLjava_util_Date() {
251: // Test for method boolean
252: // java.util.SimpleTimeZone.inDaylightTime(java.util.Date)
253: TimeZone zone = TimeZone.getTimeZone("EST");
254: GregorianCalendar gc = new GregorianCalendar(1998,
255: Calendar.JUNE, 11);
256: assertFalse("Returned incorrect daylight value1", zone
257: .inDaylightTime(gc.getTime()));
258: gc = new GregorianCalendar(1998, Calendar.NOVEMBER, 11);
259: assertFalse("Returned incorrect daylight value1", zone
260: .inDaylightTime(gc.getTime()));
261: gc = new GregorianCalendar(zone);
262: gc.set(1999, Calendar.APRIL, 4, 1, 59, 59);
263: assertTrue("Returned incorrect daylight value3", !(zone
264: .inDaylightTime(gc.getTime())));
265: Date date = new Date(gc.getTime().getTime() + 1000);
266: assertFalse("Returned incorrect daylight value4", zone
267: .inDaylightTime(date));
268: gc.set(1999, Calendar.OCTOBER, 31, 1, 0, 0);
269: assertTrue("Returned incorrect daylight value5", !(zone
270: .inDaylightTime(gc.getTime())));
271: date = new Date(gc.getTime().getTime() - 1000);
272: assertFalse("Returned incorrect daylight value6", zone
273: .inDaylightTime(date));
274:
275: assertTrue("Returned incorrect daylight value7", !zone
276: .inDaylightTime(new Date(891752400000L + 7200000 - 1)));
277: assertFalse("Returned incorrect daylight value8", zone
278: .inDaylightTime(new Date(891752400000L + 7200000)));
279: assertFalse("Returned incorrect daylight value9", zone
280: .inDaylightTime(new Date(909288000000L + 7200000 - 1)));
281: assertTrue("Returned incorrect daylight value10", !zone
282: .inDaylightTime(new Date(909288000000L + 7200000)));
283: }
284:
285: /**
286: * @tests java.util.SimpleTimeZone#setDSTSavings(int)
287: */
288: public void test_setDSTSavingsI() {
289: // Test for method void java.util.SimpleTimeZone.setDSTSavings(int)
290: SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
291: st.setStartRule(0, 1, 1, 1);
292: st.setEndRule(11, 1, 1, 1);
293: st.setDSTSavings(1);
294: assertEquals("Daylight savings amount not set", 1, st
295: .getDSTSavings());
296: }
297:
298: /**
299: * @tests java.util.SimpleTimeZone#setEndRule(int, int, int)
300: */
301: public void test_setEndRuleIII() {
302: // Test for method void java.util.SimpleTimeZone.setEndRule(int, int,
303: // int)
304: assertTrue("Used to test", true);
305: }
306:
307: /**
308: * @tests java.util.SimpleTimeZone#setEndRule(int, int, int, int)
309: */
310: public void test_setEndRuleIIII() {
311: // Test for method void java.util.SimpleTimeZone.setEndRule(int, int,
312: // int, int)
313: SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
314: // Spec indicates that both end and start must be set or result is
315: // undefined
316: st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
317: st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
318: assertTrue("StartRule improperly set1", st.useDaylightTime());
319: assertTrue("StartRule improperly set2", st
320: .inDaylightTime(new GregorianCalendar(1998,
321: Calendar.NOVEMBER, 13).getTime()));
322: assertTrue("StartRule improperly set3", !(st
323: .inDaylightTime(new GregorianCalendar(1998,
324: Calendar.OCTOBER, 13).getTime())));
325: }
326:
327: /**
328: * @tests java.util.SimpleTimeZone#setEndRule(int, int, int, int, boolean)
329: */
330: public void test_setEndRuleIIIIZ() {
331: // Test for method void java.util.SimpleTimeZone.setEndRule(int, int,
332: // int, int, boolean)
333: SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
334: // Spec indicates that both end and start must be set or result is
335: // undefined
336: st
337: .setStartRule(Calendar.NOVEMBER, 8, Calendar.SUNDAY, 1,
338: false);
339: st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, true);
340: assertTrue("StartRule improperly set1", st.useDaylightTime());
341: assertTrue("StartRule improperly set2", st
342: .inDaylightTime((new GregorianCalendar(1999,
343: Calendar.NOVEMBER, 7, 12, 0).getTime())));
344: assertTrue("StartRule improperly set3", st
345: .inDaylightTime((new GregorianCalendar(1999,
346: Calendar.NOVEMBER, 20, 12, 0).getTime())));
347: assertTrue("StartRule improperly set4", !(st
348: .inDaylightTime(new GregorianCalendar(1999,
349: Calendar.NOVEMBER, 6, 12, 0).getTime())));
350: assertTrue("StartRule improperly set5", !(st
351: .inDaylightTime(new GregorianCalendar(1999,
352: Calendar.NOVEMBER, 21, 12, 0).getTime())));
353: }
354:
355: /**
356: * @tests java.util.SimpleTimeZone#setRawOffset(int)
357: */
358: public void test_setRawOffsetI() {
359: // Test for method void java.util.SimpleTimeZone.setRawOffset(int)
360:
361: st1 = (SimpleTimeZone) TimeZone.getTimeZone("EST");
362: int off = st1.getRawOffset();
363: st1.setRawOffset(1000);
364: boolean val = st1.getRawOffset() == 1000;
365: st1.setRawOffset(off);
366: assertTrue("Incorrect offset set", val);
367: }
368:
369: /**
370: * @tests java.util.SimpleTimeZone#setStartRule(int, int, int)
371: */
372: public void test_setStartRuleIII() {
373: // Test for method void java.util.SimpleTimeZone.setStartRule(int, int,
374: // int)
375: SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
376: // Spec indicates that both end and start must be set or result is
377: // undefined
378: st.setStartRule(Calendar.NOVEMBER, 1, 1);
379: st.setEndRule(Calendar.DECEMBER, 1, 1);
380: assertTrue("StartRule improperly set", st.useDaylightTime());
381: assertTrue("StartRule improperly set", st
382: .inDaylightTime((new GregorianCalendar(1998,
383: Calendar.NOVEMBER, 13).getTime())));
384: assertTrue("StartRule improperly set", !(st
385: .inDaylightTime(new GregorianCalendar(1998,
386: Calendar.OCTOBER, 13).getTime())));
387:
388: }
389:
390: /**
391: * @tests java.util.SimpleTimeZone#setStartRule(int, int, int, int)
392: */
393: public void test_setStartRuleIIII() {
394: // Test for method void java.util.SimpleTimeZone.setStartRule(int, int,
395: // int, int)
396: SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
397: // Spec indicates that both end and start must be set or result is
398: // undefined
399: st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
400: st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
401: assertTrue("StartRule improperly set1", st.useDaylightTime());
402: assertTrue("StartRule improperly set2", st
403: .inDaylightTime((new GregorianCalendar(1998,
404: Calendar.NOVEMBER, 13).getTime())));
405: assertTrue("StartRule improperly set3", !(st
406: .inDaylightTime(new GregorianCalendar(1998,
407: Calendar.OCTOBER, 13).getTime())));
408: }
409:
410: /**
411: * @tests java.util.SimpleTimeZone#setStartRule(int, int, int, int, boolean)
412: */
413: public void test_setStartRuleIIIIZ() {
414: // Test for method void java.util.SimpleTimeZone.setStartRule(int, int,
415: // int, int, boolean)
416: SimpleTimeZone st = new SimpleTimeZone(0, "Test");
417: // Spec indicates that both end and start must be set or result is
418: // undefined
419: st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 1, true);
420: st.setEndRule(Calendar.NOVEMBER, 15, Calendar.SUNDAY, 1, false);
421: assertTrue("StartRule improperly set1", st.useDaylightTime());
422: assertTrue("StartRule improperly set2", st
423: .inDaylightTime((new GregorianCalendar(1999,
424: Calendar.NOVEMBER, 7, 12, 0).getTime())));
425: assertTrue("StartRule improperly set3", st
426: .inDaylightTime((new GregorianCalendar(1999,
427: Calendar.NOVEMBER, 13, 12, 0).getTime())));
428: assertTrue("StartRule improperly set4", !(st
429: .inDaylightTime(new GregorianCalendar(1999,
430: Calendar.NOVEMBER, 6, 12, 0).getTime())));
431: assertTrue("StartRule improperly set5", !(st
432: .inDaylightTime(new GregorianCalendar(1999,
433: Calendar.NOVEMBER, 14, 12, 0).getTime())));
434: }
435:
436: /**
437: * @tests java.util.SimpleTimeZone#setStartYear(int)
438: */
439: public void test_setStartYearI() {
440: // Test for method void java.util.SimpleTimeZone.setStartYear(int)
441: SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
442: st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
443: st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
444: st.setStartYear(1999);
445: assertTrue("set year improperly set1", !(st
446: .inDaylightTime(new GregorianCalendar(1999,
447: Calendar.JULY, 12).getTime())));
448: assertTrue("set year improperly set2", !(st
449: .inDaylightTime(new GregorianCalendar(1998,
450: Calendar.OCTOBER, 13).getTime())));
451: assertTrue("set year improperly set3", (st
452: .inDaylightTime(new GregorianCalendar(1999,
453: Calendar.NOVEMBER, 13).getTime())));
454: }
455:
456: /**
457: * @tests java.util.SimpleTimeZone#toString()
458: */
459: public void test_toString() {
460: // Test for method java.lang.String java.util.SimpleTimeZone.toString()
461: String string = TimeZone.getTimeZone("EST").toString();
462: assertNotNull("toString() returned null", string);
463: assertTrue("toString() is empty", string.length() != 0);
464: }
465:
466: /**
467: * @tests java.util.SimpleTimeZone#useDaylightTime()
468: */
469: public void test_useDaylightTime() {
470: // Test for method boolean java.util.SimpleTimeZone.useDaylightTime()
471: SimpleTimeZone st = new SimpleTimeZone(1000, "Test_TZ");
472: assertTrue("useDaylightTime returned incorrect value", !st
473: .useDaylightTime());
474: // Spec indicates that both end and start must be set or result is
475: // undefined
476: st.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 0);
477: st.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 0);
478: assertTrue("useDaylightTime returned incorrect value", st
479: .useDaylightTime());
480: }
481:
482: /**
483: * Sets up the fixture, for example, open a network connection. This method
484: * is called before a test is executed.
485: */
486: protected void setUp() {
487: }
488:
489: /**
490: * Tears down the fixture, for example, close a network connection. This
491: * method is called after a test is executed.
492: */
493: protected void tearDown() {
494: }
495: }
|