001: /*
002: *******************************************************************************
003: * Copyright (C) 2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.tests;
009:
010: import java.util.Date;
011: import java.util.Locale;
012:
013: import com.ibm.icu.text.DateFormat;
014: import com.ibm.icu.util.Calendar;
015: import com.ibm.icu.util.TimeZone;
016: import com.ibm.icu.util.ULocale;
017:
018: public class CalendarTest extends ICUTestCase {
019:
020: /*
021: * Test method for 'com.ibm.icu.util.Calendar.hashCode()'
022: */
023: public void testHashCode() {
024: Calendar cal1 = Calendar.getInstance();
025: Calendar cal2 = Calendar.getInstance();
026: Calendar cal3 = Calendar.getInstance();
027: cal3
028: .setMinimalDaysInFirstWeek(cal3
029: .getMinimalDaysInFirstWeek() + 1);
030: testEHCS(cal1, cal2, cal3);
031: }
032:
033: /*
034: * Test method for 'com.ibm.icu.util.Calendar.Calendar(Calendar)'
035: */
036: public void testCalendar() {
037: // tested implicitly everywhere
038: }
039:
040: /*
041: * Test method for 'com.ibm.icu.util.Calendar.getInstance()'
042: */
043: public void testGetInstance() {
044: // tested by testEHCS
045: }
046:
047: /*
048: * Test method for 'com.ibm.icu.util.Calendar.getInstance(TimeZone)'
049: */
050: public void testGetInstanceTimeZone() {
051: TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
052: Calendar cal = Calendar.getInstance(tz);
053: assertNotNull(cal);
054: assertNotNull(cal.getTime());
055: assertEquals(tz, cal.getTimeZone());
056: }
057:
058: /*
059: * Test method for 'com.ibm.icu.util.Calendar.getInstance(Locale)'
060: */
061: public void testGetInstanceLocale() {
062: Calendar cal = Calendar.getInstance(Locale.US);
063: assertNotNull(cal);
064: assertNotNull(cal.getTime());
065: }
066:
067: /*
068: * Test method for 'com.ibm.icu.util.Calendar.getInstance(ULocale)'
069: */
070: public void testGetInstanceULocale() {
071: Calendar cal = Calendar.getInstance(ULocale.US);
072: assertNotNull(cal);
073: assertNotNull(cal.getTime());
074: }
075:
076: /*
077: * Test method for 'com.ibm.icu.util.Calendar.getInstance(TimeZone, Locale)'
078: */
079: public void testGetInstanceTimeZoneLocale() {
080: TimeZone tz = TimeZone.getTimeZone("America/New_York");
081: Calendar cal = Calendar.getInstance(tz, Locale.US);
082: assertNotNull(cal);
083: assertNotNull(cal.getTime());
084: }
085:
086: /*
087: * Test method for 'com.ibm.icu.util.Calendar.getInstance(TimeZone, ULocale)'
088: */
089: public void testGetInstanceTimeZoneULocale() {
090: TimeZone tz = TimeZone.getTimeZone("America/New_York");
091: Calendar cal = Calendar.getInstance(tz, ULocale.US);
092: assertNotNull(cal);
093: assertNotNull(cal.getTime());
094: }
095:
096: /*
097: * Test method for 'com.ibm.icu.util.Calendar.getAvailableLocales()'
098: */
099: public void testGetAvailableLocales() {
100: assertNotNull(Calendar.getAvailableLocales());
101: }
102:
103: /*
104: * Test method for 'com.ibm.icu.util.Calendar.getAvailableULocales()'
105: */
106: public void testGetAvailableULocales() {
107: assertNotNull(Calendar.getAvailableULocales());
108: }
109:
110: /*
111: * Test method for 'com.ibm.icu.util.Calendar.getTime()'
112: */
113: public void testGetTime() {
114: Calendar cal = Calendar.getInstance();
115: assertNotNull(cal.getTime());
116: }
117:
118: /*
119: * Test method for 'com.ibm.icu.util.Calendar.setTime(Date)'
120: */
121: public void testSetTime() {
122: Calendar cal = Calendar.getInstance();
123: cal.clear();
124: cal.set(2006, 0, 20, 9, 30, 0);
125: Date date = cal.getTime();
126: cal = Calendar.getInstance();
127: cal.setTime(date);
128: assertEquals(date, cal.getTime());
129: }
130:
131: /*
132: * Test method for 'com.ibm.icu.util.Calendar.getTimeInMillis()'
133: */
134: public void testGetTimeInMillis() {
135: Calendar cal = Calendar.getInstance();
136: assertTrue(0 != cal.getTimeInMillis());
137: }
138:
139: /*
140: * Test method for 'com.ibm.icu.util.Calendar.setTimeInMillis(long)'
141: */
142: public void testSetTimeInMillis() {
143: Calendar cal = Calendar.getInstance();
144: cal.clear();
145: cal.set(2006, 0, 20, 9, 30, 0);
146: long millis = cal.getTimeInMillis();
147: Date date = cal.getTime();
148:
149: cal = Calendar.getInstance();
150: cal.setTimeInMillis(millis);
151:
152: assertEquals(date, cal.getTime());
153: }
154:
155: /*
156: * Test method for 'com.ibm.icu.util.Calendar.get(int)'
157: */
158: public void testGet() {
159: Calendar cal = Calendar.getInstance();
160: cal.clear();
161: cal.set(2006, 0, 20, 9, 30, 0);
162: assertEquals(0, cal.get(Calendar.MONTH));
163: assertEquals(20, cal.get(Calendar.DAY_OF_MONTH));
164: assertEquals(30, cal.get(Calendar.MINUTE));
165: }
166:
167: /*
168: * Test method for 'com.ibm.icu.util.Calendar.set(int, int)'
169: */
170: public void testSetIntInt() {
171: Calendar cal = Calendar.getInstance();
172: cal.set(Calendar.YEAR, 1977);
173: assertEquals(1977, cal.get(Calendar.YEAR));
174: }
175:
176: /*
177: * Test method for 'com.ibm.icu.util.Calendar.set(int, int, int)'
178: */
179: public void testSetIntIntInt() {
180: Calendar cal = Calendar.getInstance();
181: cal.set(1997, 9, 15);
182: assertEquals(15, cal.get(Calendar.DATE));
183: }
184:
185: /*
186: * Test method for 'com.ibm.icu.util.Calendar.set(int, int, int, int, int)'
187: */
188: public void testSetIntIntIntIntInt() {
189: Calendar cal = Calendar.getInstance();
190: cal.set(1997, 9, 15, 14, 25);
191: assertEquals(25, cal.get(Calendar.MINUTE));
192: }
193:
194: /*
195: * Test method for 'com.ibm.icu.util.Calendar.set(int, int, int, int, int, int)'
196: */
197: public void testSetIntIntIntIntIntInt() {
198: Calendar cal = Calendar.getInstance();
199: cal.set(1997, 9, 15, 14, 25, 51);
200: assertEquals(51, cal.get(Calendar.SECOND));
201: }
202:
203: /*
204: * Test method for 'com.ibm.icu.util.Calendar.clear()'
205: */
206: public void testClear() {
207: Calendar cal = Calendar.getInstance();
208: cal.set(1997, 9, 15, 14, 25, 51);
209: cal.clear();
210: assertEquals(0, cal.get(Calendar.MONTH));
211: }
212:
213: /*
214: * Test method for 'com.ibm.icu.util.Calendar.clear(int)'
215: */
216: public void testClearInt() {
217: Calendar cal = Calendar.getInstance();
218: cal.set(1997, 9, 15, 14, 25, 51);
219: assertTrue(cal.isSet(Calendar.DAY_OF_MONTH));
220: cal.clear(Calendar.DAY_OF_MONTH);
221: assertFalse(cal.isSet(Calendar.DAY_OF_MONTH));
222: }
223:
224: /*
225: * Test method for 'com.ibm.icu.util.Calendar.isSet(int)'
226: */
227: public void testIsSet() {
228: // see testClearInt
229: }
230:
231: /*
232: * Test method for 'com.ibm.icu.util.Calendar.equals(Object)'
233: */
234: public void testEqualsObject() {
235: // tested by testHashCode
236: }
237:
238: /*
239: * Test method for 'com.ibm.icu.util.Calendar.isEquivalentTo(Calendar)'
240: */
241: public void testIsEquivalentTo() {
242: Calendar cal = Calendar.getInstance();
243: Calendar cal2 = Calendar.getInstance();
244: cal2.set(1994, 6, 21, 8, 7);
245: assertTrue(cal.isEquivalentTo(cal2));
246: cal.setTimeZone(TimeZone.getTimeZone("CST"));
247: cal2.setTimeZone(TimeZone.getTimeZone("PDT"));
248: assertFalse(cal.isEquivalentTo(cal2));
249: }
250:
251: /*
252: * Test method for 'com.ibm.icu.util.Calendar.before(Object)'
253: */
254: public void testBefore() {
255: Calendar cal = Calendar.getInstance();
256: cal.set(Calendar.YEAR, 1990);
257: assertTrue(cal.before(new Date()));
258: assertTrue(cal.before(Calendar.getInstance()));
259: }
260:
261: /*
262: * Test method for 'com.ibm.icu.util.Calendar.after(Object)'
263: */
264: public void testAfter() {
265: Calendar cal = Calendar.getInstance();
266: cal.set(Calendar.YEAR, 3058);
267: assertTrue(cal.after(new Date()));
268: assertTrue(cal.after(Calendar.getInstance()));
269: }
270:
271: /*
272: * Test method for 'com.ibm.icu.util.Calendar.getActualMaximum(int)'
273: */
274: public void testGetActualMaximum() {
275: Calendar cal = Calendar.getInstance(Locale.US);
276: assertEquals(11, cal.getActualMaximum(Calendar.MONTH));
277: }
278:
279: /*
280: * Test method for 'com.ibm.icu.util.Calendar.getActualMinimum(int)'
281: */
282: public void testGetActualMinimum() {
283: Calendar cal = Calendar.getInstance(Locale.US);
284: assertEquals(0, cal.getActualMinimum(Calendar.MONTH));
285: }
286:
287: /*
288: * Test method for 'com.ibm.icu.util.Calendar.roll(int, boolean)'
289: */
290: public void testRollIntBoolean() {
291: Calendar cal = Calendar.getInstance(Locale.US);
292: cal.set(1997, 1, 27);
293: cal.roll(Calendar.DATE, true);
294: assertEquals(28, cal.get(Calendar.DATE));
295: }
296:
297: /*
298: * Test method for 'com.ibm.icu.util.Calendar.roll(int, int)'
299: */
300: public void testRollIntInt() {
301: Calendar cal = Calendar.getInstance(Locale.US);
302: cal.set(1997, 1, 27);
303: cal.roll(Calendar.DATE, 3);
304: assertEquals(2, cal.get(Calendar.DATE));
305: }
306:
307: /*
308: * Test method for 'com.ibm.icu.util.Calendar.add(int, int)'
309: */
310: public void testAdd() {
311: Calendar cal = Calendar.getInstance(Locale.US);
312: cal.set(1997, 1, 27);
313: cal.add(Calendar.DATE, 3);
314: assertEquals(2, cal.get(Calendar.DATE));
315: assertEquals(2, cal.get(Calendar.MONTH));
316: }
317:
318: /*
319: * Test method for 'com.ibm.icu.util.Calendar.getDisplayName(Locale)'
320: */
321: public void testGetDisplayNameLocale() {
322: Calendar cal = Calendar.getInstance();
323: assertEquals("Calendar", cal.getDisplayName(Locale.US));
324: }
325:
326: /*
327: * Test method for 'com.ibm.icu.util.Calendar.getDisplayName(ULocale)'
328: */
329: public void testGetDisplayNameULocale() {
330: Calendar cal = Calendar.getInstance();
331: assertEquals("Calendar", cal.getDisplayName(ULocale.US));
332: }
333:
334: /*
335: * Test method for 'com.ibm.icu.util.Calendar.compareTo(Calendar)'
336: */
337: public void testCompareToCalendar() {
338: Calendar cal = Calendar.getInstance();
339: cal.set(Calendar.YEAR, 1990);
340: assertTrue(0 > cal.compareTo(Calendar.getInstance()));
341: }
342:
343: /*
344: * Test method for 'com.ibm.icu.util.Calendar.compareTo(Object)'
345: */
346: public void testCompareToObject() {
347: Calendar cal = Calendar.getInstance();
348: cal.set(Calendar.YEAR, 1990);
349: assertTrue(0 > cal.compareTo((Object) Calendar.getInstance()));
350: }
351:
352: /*
353: * Test method for 'com.ibm.icu.util.Calendar.getDateTimeFormat(int, int, Locale)'
354: */
355: public void testGetDateTimeFormatIntIntLocale() {
356: Calendar cal = Calendar.getInstance();
357: cal.set(1990, 8, 16, 20, 3);
358: DateFormat df = cal.getDateTimeFormat(DateFormat.LONG,
359: DateFormat.SHORT, Locale.US);
360: assertEquals("September 16, 1990 8:03 PM", df.format(cal));
361: }
362:
363: /*
364: * Test method for 'com.ibm.icu.util.Calendar.getDateTimeFormat(int, int, ULocale)'
365: */
366: public void testGetDateTimeFormatIntIntULocale() {
367: Calendar cal = Calendar.getInstance();
368: cal.set(1990, 8, 16, 20, 3);
369: DateFormat df = cal.getDateTimeFormat(DateFormat.LONG,
370: DateFormat.SHORT, ULocale.US);
371: assertEquals("September 16, 1990 8:03 PM", df.format(cal));
372: }
373:
374: /*
375: * Test method for 'com.ibm.icu.util.Calendar.fieldDifference(Date, int)'
376: */
377: public void testFieldDifference() {
378: Calendar cal = Calendar.getInstance();
379: cal.set(Calendar.DAY_OF_MONTH, 0);
380: Date date = cal.getTime();
381: cal.add(Calendar.DAY_OF_MONTH, 5);
382: assertEquals(-5, cal.fieldDifference(date,
383: Calendar.DAY_OF_MONTH));
384: }
385:
386: /*
387: * Test method for 'com.ibm.icu.util.Calendar.getTimeZone()'
388: */
389: public void testGetTimeZone() {
390: Calendar cal = Calendar.getInstance();
391: assertNotNull(cal.getTimeZone());
392: }
393:
394: /*
395: * Test method for 'com.ibm.icu.util.Calendar.setTimeZone(TimeZone)'
396: */
397: public void testSetTimeZone() {
398: Calendar cal = Calendar.getInstance();
399: TimeZone value1 = cal.getTimeZone();
400: String tzn = "PDT".equals(value1.getID()) ? "CST" : "PDT";
401: TimeZone value2 = TimeZone.getTimeZone(tzn);
402: cal.setTimeZone(value2);
403: TimeZone result = cal.getTimeZone();
404: assertNotEqual(value1, result);
405: assertEquals(value2, result);
406: }
407:
408: /*
409: * Test method for 'com.ibm.icu.util.Calendar.setLenient(boolean)'
410: */
411: public void testSetLenient() {
412: Calendar cal = Calendar.getInstance();
413: boolean lenient = cal.isLenient();
414: cal.setLenient(!lenient);
415: assertFalse(lenient == cal.isLenient());
416:
417: // not testing if it has the expected effect
418: }
419:
420: /*
421: * Test method for 'com.ibm.icu.util.Calendar.isLenient()'
422: */
423: public void testIsLenient() {
424: // tested by testSetLenient
425: }
426:
427: /*
428: * Test method for 'com.ibm.icu.util.Calendar.setFirstDayOfWeek(int)'
429: */
430: public void testSetFirstDayOfWeek() {
431: Calendar cal = Calendar.getInstance();
432: int firstDay = cal.getFirstDayOfWeek();
433: cal.setFirstDayOfWeek(firstDay + 1);
434: assertEquals(firstDay + 1, cal.getFirstDayOfWeek());
435:
436: // don't test functionality
437: }
438:
439: /*
440: * Test method for 'com.ibm.icu.util.Calendar.getFirstDayOfWeek()'
441: */
442: public void testGetFirstDayOfWeek() {
443: // tested by testSetFirstDayOfWeek
444: }
445:
446: /*
447: * Test method for 'com.ibm.icu.util.Calendar.setMinimalDaysInFirstWeek(int)'
448: */
449: public void testSetMinimalDaysInFirstWeek() {
450: Calendar cal = Calendar.getInstance();
451: int firstDay = cal.getMinimalDaysInFirstWeek();
452: cal.setMinimalDaysInFirstWeek(firstDay + 1);
453: assertEquals(firstDay + 1, cal.getMinimalDaysInFirstWeek());
454:
455: // don't test functionality
456: }
457:
458: /*
459: * Test method for 'com.ibm.icu.util.Calendar.getMinimalDaysInFirstWeek()'
460: */
461: public void testGetMinimalDaysInFirstWeek() {
462: // tested by testSetMinimalDaysInFirstWeek
463: }
464:
465: /*
466: * Test method for 'com.ibm.icu.util.Calendar.getMinimum(int)'
467: */
468: public void testGetMinimum() {
469: Calendar cal = Calendar.getInstance();
470: assertEquals(1, cal.getMinimum(Calendar.DAY_OF_WEEK));
471: }
472:
473: /*
474: * Test method for 'com.ibm.icu.util.Calendar.getMaximum(int)'
475: */
476: public void testGetMaximum() {
477: Calendar cal = Calendar.getInstance();
478: assertEquals(7, cal.getMaximum(Calendar.DAY_OF_WEEK));
479: }
480:
481: /*
482: * Test method for 'com.ibm.icu.util.Calendar.getGreatestMinimum(int)'
483: */
484: public void testGetGreatestMinimum() {
485: Calendar cal = Calendar.getInstance();
486: assertEquals(1, cal.getGreatestMinimum(Calendar.DATE));
487: }
488:
489: /*
490: * Test method for 'com.ibm.icu.util.Calendar.getLeastMaximum(int)'
491: */
492: public void testGetLeastMaximum() {
493: Calendar cal = Calendar.getInstance();
494: assertEquals(28, cal.getLeastMaximum(Calendar.DATE));
495: }
496:
497: /*
498: * Test method for 'com.ibm.icu.util.Calendar.getDayOfWeekType(int)'
499: */
500: public void testGetDayOfWeekType() {
501: Calendar cal = Calendar.getInstance(Locale.US);
502: assertEquals(Calendar.WEEKDAY, cal
503: .getDayOfWeekType(Calendar.FRIDAY));
504: assertEquals(Calendar.WEEKEND, cal
505: .getDayOfWeekType(Calendar.SATURDAY));
506: }
507:
508: /*
509: * Test method for 'com.ibm.icu.util.Calendar.getWeekendTransition(int)'
510: */
511: public void testGetWeekendTransition() {
512: Calendar cal = Calendar.getInstance(Locale.US);
513: try {
514: cal.getWeekendTransition(Calendar.WEEKEND_ONSET);
515: fail("expected IllegalArgumentException from getWeekendTransition");
516: } catch (IllegalArgumentException e) {
517: // ok
518: }
519: }
520:
521: /*
522: * Test method for 'com.ibm.icu.util.Calendar.isWeekend(Date)'
523: */
524: public void testIsWeekendDate() {
525: Calendar cal = Calendar.getInstance(Locale.US);
526: cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
527: assertTrue(cal.isWeekend(cal.getTime()));
528: cal.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
529: assertFalse(cal.isWeekend(cal.getTime()));
530: }
531:
532: /*
533: * Test method for 'com.ibm.icu.util.Calendar.isWeekend()'
534: */
535: public void testIsWeekend() {
536: Calendar cal = Calendar.getInstance(Locale.US);
537: cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
538: assertTrue(cal.isWeekend());
539: cal.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
540: assertFalse(cal.isWeekend());
541: }
542:
543: /*
544: * Test method for 'com.ibm.icu.util.Calendar.clone()'
545: */
546: public void testClone() {
547: // tested by testHashCode
548: }
549:
550: /*
551: * Test method for 'com.ibm.icu.util.Calendar.toString()'
552: */
553: public void testToString() {
554: Calendar cal = Calendar.getInstance();
555: assertNotNull(cal.toString());
556: }
557:
558: /*
559: * Test method for 'com.ibm.icu.util.Calendar.getType()'
560: */
561: public void testGetType() {
562: Calendar cal = Calendar.getInstance(Locale.US);
563: assertEquals("gregorian", cal.getType());
564: }
565: }
|