KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > time > DateUtilsTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.time;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.GregorianCalendar JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.TimeZone JavaDoc;
30
31 import junit.framework.AssertionFailedError;
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35 import junit.textui.TestRunner;
36
37 import org.apache.commons.lang.SystemUtils;
38
39 /**
40  * Unit tests {@link org.apache.commons.lang.time.DateUtils}.
41  *
42  * @author <a HREF="mailto:sergek@lokitech.com">Serge Knystautas</a>
43  * @author <a HREF="mailto:steve@mungoknotwise.com">Steven Caswell</a>
44  */

45 public class DateUtilsTest extends TestCase {
46     DateFormat JavaDoc dateParser = null;
47     DateFormat JavaDoc dateTimeParser = null;
48     DateFormat JavaDoc timeZoneDateParser = null;
49     Date JavaDoc dateAmPm1 = null;
50     Date JavaDoc dateAmPm2 = null;
51     Date JavaDoc dateAmPm3 = null;
52     Date JavaDoc dateAmPm4 = null;
53     Date JavaDoc date0 = null;
54     Date JavaDoc date1 = null;
55     Date JavaDoc date2 = null;
56     Date JavaDoc date3 = null;
57     Date JavaDoc date4 = null;
58     Date JavaDoc date5 = null;
59     Date JavaDoc date6 = null;
60     Date JavaDoc date7 = null;
61     Date JavaDoc date8 = null;
62     Calendar JavaDoc calAmPm1 = null;
63     Calendar JavaDoc calAmPm2 = null;
64     Calendar JavaDoc calAmPm3 = null;
65     Calendar JavaDoc calAmPm4 = null;
66     Calendar JavaDoc cal1 = null;
67     Calendar JavaDoc cal2 = null;
68     Calendar JavaDoc cal3 = null;
69     Calendar JavaDoc cal4 = null;
70     Calendar JavaDoc cal5 = null;
71     Calendar JavaDoc cal6 = null;
72     Calendar JavaDoc cal7 = null;
73     Calendar JavaDoc cal8 = null;
74     TimeZone JavaDoc zone = null;
75     TimeZone JavaDoc defaultZone = null;
76
77     public DateUtilsTest(String JavaDoc name) {
78         super(name);
79     }
80
81     public static void main(String JavaDoc[] args) {
82         TestRunner.run(suite());
83     }
84
85     public static Test suite() {
86         TestSuite suite = new TestSuite(DateUtilsTest.class);
87         suite.setName("DateUtils Tests");
88         return suite;
89     }
90
91     protected void setUp() throws Exception JavaDoc {
92         super.setUp();
93
94         dateParser = new SimpleDateFormat JavaDoc("MMM dd, yyyy", Locale.ENGLISH);
95         dateTimeParser = new SimpleDateFormat JavaDoc("MMM dd, yyyy H:mm:ss.SSS", Locale.ENGLISH);
96
97         dateAmPm1 = dateTimeParser.parse("February 3, 2002 01:10:00.000");
98         dateAmPm2 = dateTimeParser.parse("February 3, 2002 11:10:00.000");
99         dateAmPm3 = dateTimeParser.parse("February 3, 2002 13:10:00.000");
100         dateAmPm4 = dateTimeParser.parse("February 3, 2002 19:10:00.000");
101         date0 = dateTimeParser.parse("February 3, 2002 12:34:56.789");
102         date1 = dateTimeParser.parse("February 12, 2002 12:34:56.789");
103         date2 = dateTimeParser.parse("November 18, 2001 1:23:11.321");
104         defaultZone = TimeZone.getDefault();
105         zone = TimeZone.getTimeZone("MET");
106         TimeZone.setDefault(zone);
107         dateTimeParser.setTimeZone(zone);
108         date3 = dateTimeParser.parse("March 30, 2003 05:30:45.000");
109         date4 = dateTimeParser.parse("March 30, 2003 01:10:00.000");
110         date5 = dateTimeParser.parse("March 30, 2003 01:40:00.000");
111         date6 = dateTimeParser.parse("March 30, 2003 02:10:00.000");
112         date7 = dateTimeParser.parse("March 30, 2003 02:40:00.000");
113         date8 = dateTimeParser.parse("October 26, 2003 05:30:45.000");
114         dateTimeParser.setTimeZone(defaultZone);
115         TimeZone.setDefault(defaultZone);
116         calAmPm1 = Calendar.getInstance();
117         calAmPm1.setTime(dateAmPm1);
118         calAmPm2 = Calendar.getInstance();
119         calAmPm2.setTime(dateAmPm2);
120         calAmPm3 = Calendar.getInstance();
121         calAmPm3.setTime(dateAmPm3);
122         calAmPm4 = Calendar.getInstance();
123         calAmPm4.setTime(dateAmPm4);
124         cal1 = Calendar.getInstance();
125         cal1.setTime(date1);
126         cal2 = Calendar.getInstance();
127         cal2.setTime(date2);
128         TimeZone.setDefault(zone);
129         cal3 = Calendar.getInstance();
130         cal3.setTime(date3);
131         cal4 = Calendar.getInstance();
132         cal4.setTime(date4);
133         cal5 = Calendar.getInstance();
134         cal5.setTime(date5);
135         cal6 = Calendar.getInstance();
136         cal6.setTime(date6);
137         cal7 = Calendar.getInstance();
138         cal7.setTime(date7);
139         cal8 = Calendar.getInstance();
140         cal8.setTime(date8);
141         TimeZone.setDefault(defaultZone);
142     }
143
144     protected void tearDown() throws Exception JavaDoc {
145         super.tearDown();
146     }
147
148     //-----------------------------------------------------------------------
149
public void testConstructor() {
150         assertNotNull(new DateUtils());
151         Constructor JavaDoc[] cons = DateUtils.class.getDeclaredConstructors();
152         assertEquals(1, cons.length);
153         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
154         assertEquals(true, Modifier.isPublic(DateUtils.class.getModifiers()));
155         assertEquals(false, Modifier.isFinal(DateUtils.class.getModifiers()));
156     }
157     
158     //-----------------------------------------------------------------------
159
public void testIsSameDay_Date() {
160         Date JavaDoc date1 = new GregorianCalendar JavaDoc(2004, 6, 9, 13, 45).getTime();
161         Date JavaDoc date2 = new GregorianCalendar JavaDoc(2004, 6, 9, 13, 45).getTime();
162         assertEquals(true, DateUtils.isSameDay(date1, date2));
163         date2 = new GregorianCalendar JavaDoc(2004, 6, 10, 13, 45).getTime();
164         assertEquals(false, DateUtils.isSameDay(date1, date2));
165         date1 = new GregorianCalendar JavaDoc(2004, 6, 10, 13, 45).getTime();
166         assertEquals(true, DateUtils.isSameDay(date1, date2));
167         date2 = new GregorianCalendar JavaDoc(2005, 6, 10, 13, 45).getTime();
168         assertEquals(false, DateUtils.isSameDay(date1, date2));
169         try {
170             DateUtils.isSameDay((Date JavaDoc) null, (Date JavaDoc) null);
171             fail();
172         } catch (IllegalArgumentException JavaDoc ex) {}
173     }
174     
175     //-----------------------------------------------------------------------
176
public void testIsSameDay_Cal() {
177         GregorianCalendar JavaDoc cal1 = new GregorianCalendar JavaDoc(2004, 6, 9, 13, 45);
178         GregorianCalendar JavaDoc cal2 = new GregorianCalendar JavaDoc(2004, 6, 9, 13, 45);
179         assertEquals(true, DateUtils.isSameDay(cal1, cal2));
180         cal2.add(Calendar.DAY_OF_YEAR, 1);
181         assertEquals(false, DateUtils.isSameDay(cal1, cal2));
182         cal1.add(Calendar.DAY_OF_YEAR, 1);
183         assertEquals(true, DateUtils.isSameDay(cal1, cal2));
184         cal2.add(Calendar.YEAR, 1);
185         assertEquals(false, DateUtils.isSameDay(cal1, cal2));
186         try {
187             DateUtils.isSameDay((Calendar JavaDoc) null, (Calendar JavaDoc) null);
188             fail();
189         } catch (IllegalArgumentException JavaDoc ex) {}
190     }
191     
192     //-----------------------------------------------------------------------
193
public void testIsSameInstant_Date() {
194         Date JavaDoc date1 = new GregorianCalendar JavaDoc(2004, 6, 9, 13, 45).getTime();
195         Date JavaDoc date2 = new GregorianCalendar JavaDoc(2004, 6, 9, 13, 45).getTime();
196         assertEquals(true, DateUtils.isSameInstant(date1, date2));
197         date2 = new GregorianCalendar JavaDoc(2004, 6, 10, 13, 45).getTime();
198         assertEquals(false, DateUtils.isSameInstant(date1, date2));
199         date1 = new GregorianCalendar JavaDoc(2004, 6, 10, 13, 45).getTime();
200         assertEquals(true, DateUtils.isSameInstant(date1, date2));
201         date2 = new GregorianCalendar JavaDoc(2005, 6, 10, 13, 45).getTime();
202         assertEquals(false, DateUtils.isSameInstant(date1, date2));
203         try {
204             DateUtils.isSameInstant((Date JavaDoc) null, (Date JavaDoc) null);
205             fail();
206         } catch (IllegalArgumentException JavaDoc ex) {}
207     }
208     
209     //-----------------------------------------------------------------------
210
public void testIsSameInstant_Cal() {
211         GregorianCalendar JavaDoc cal1 = new GregorianCalendar JavaDoc(TimeZone.getTimeZone("GMT+1"));
212         GregorianCalendar JavaDoc cal2 = new GregorianCalendar JavaDoc(TimeZone.getTimeZone("GMT-1"));
213         cal1.set(2004, 6, 9, 13, 45, 0);
214         cal1.set(Calendar.MILLISECOND, 0);
215         cal2.set(2004, 6, 9, 13, 45, 0);
216         cal2.set(Calendar.MILLISECOND, 0);
217         assertEquals(false, DateUtils.isSameInstant(cal1, cal2));
218         
219         cal2.set(2004, 6, 9, 11, 45, 0);
220         assertEquals(true, DateUtils.isSameInstant(cal1, cal2));
221         try {
222             DateUtils.isSameInstant((Calendar JavaDoc) null, (Calendar JavaDoc) null);
223             fail();
224         } catch (IllegalArgumentException JavaDoc ex) {}
225     }
226     
227     //-----------------------------------------------------------------------
228
public void testIsSameLocalTime_Cal() {
229         GregorianCalendar JavaDoc cal1 = new GregorianCalendar JavaDoc(TimeZone.getTimeZone("GMT+1"));
230         GregorianCalendar JavaDoc cal2 = new GregorianCalendar JavaDoc(TimeZone.getTimeZone("GMT-1"));
231         cal1.set(2004, 6, 9, 13, 45, 0);
232         cal1.set(Calendar.MILLISECOND, 0);
233         cal2.set(2004, 6, 9, 13, 45, 0);
234         cal2.set(Calendar.MILLISECOND, 0);
235         assertEquals(true, DateUtils.isSameLocalTime(cal1, cal2));
236         
237         cal2.set(2004, 6, 9, 11, 45, 0);
238         assertEquals(false, DateUtils.isSameLocalTime(cal1, cal2));
239         try {
240             DateUtils.isSameLocalTime((Calendar JavaDoc) null, (Calendar JavaDoc) null);
241             fail();
242         } catch (IllegalArgumentException JavaDoc ex) {}
243     }
244     
245     //-----------------------------------------------------------------------
246
public void testParseDate() throws Exception JavaDoc {
247         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(1972, 11, 3);
248         String JavaDoc dateStr = "1972-12-03";
249         String JavaDoc[] parsers = new String JavaDoc[] {"yyyy'-'DDD", "yyyy'-'MM'-'dd", "yyyyMMdd"};
250         Date JavaDoc date = DateUtils.parseDate(dateStr, parsers);
251         assertEquals(cal.getTime(), date);
252         
253         dateStr = "1972-338";
254         date = DateUtils.parseDate(dateStr, parsers);
255         assertEquals(cal.getTime(), date);
256         
257         dateStr = "19721203";
258         date = DateUtils.parseDate(dateStr, parsers);
259         assertEquals(cal.getTime(), date);
260         
261         try {
262             DateUtils.parseDate("PURPLE", parsers);
263             fail();
264         } catch (ParseException JavaDoc ex) {}
265         try {
266             DateUtils.parseDate("197212AB", parsers);
267             fail();
268         } catch (ParseException JavaDoc ex) {}
269         try {
270             DateUtils.parseDate(null, parsers);
271             fail();
272         } catch (IllegalArgumentException JavaDoc ex) {}
273         try {
274             DateUtils.parseDate(dateStr, null);
275             fail();
276         } catch (IllegalArgumentException JavaDoc ex) {}
277     }
278     
279     //-----------------------------------------------------------------------
280
/**
281      * Tests various values with the round method
282      */

283     public void testRound() throws Exception JavaDoc {
284         // tests for public static Date round(Date date, int field)
285
assertEquals("round year-1 failed",
286                 dateParser.parse("January 1, 2002"),
287                 DateUtils.round(date1, Calendar.YEAR));
288         assertEquals("round year-2 failed",
289                 dateParser.parse("January 1, 2002"),
290                 DateUtils.round(date2, Calendar.YEAR));
291         assertEquals("round month-1 failed",
292                 dateParser.parse("February 1, 2002"),
293                 DateUtils.round(date1, Calendar.MONTH));
294         assertEquals("round month-2 failed",
295                 dateParser.parse("December 1, 2001"),
296                 DateUtils.round(date2, Calendar.MONTH));
297         assertEquals("round semimonth-0 failed",
298                 dateParser.parse("February 1, 2002"),
299                 DateUtils.round(date0, DateUtils.SEMI_MONTH));
300         assertEquals("round semimonth-1 failed",
301                 dateParser.parse("February 16, 2002"),
302                 DateUtils.round(date1, DateUtils.SEMI_MONTH));
303         assertEquals("round semimonth-2 failed",
304                 dateParser.parse("November 16, 2001"),
305                 DateUtils.round(date2, DateUtils.SEMI_MONTH));
306         assertEquals("round date-1 failed",
307                 dateParser.parse("February 13, 2002"),
308                 DateUtils.round(date1, Calendar.DATE));
309         assertEquals("round date-2 failed",
310                 dateParser.parse("November 18, 2001"),
311                 DateUtils.round(date2, Calendar.DATE));
312         assertEquals("round hour-1 failed",
313                 dateTimeParser.parse("February 12, 2002 13:00:00.000"),
314                 DateUtils.round(date1, Calendar.HOUR));
315         assertEquals("round hour-2 failed",
316                 dateTimeParser.parse("November 18, 2001 1:00:00.000"),
317                 DateUtils.round(date2, Calendar.HOUR));
318         assertEquals("round minute-1 failed",
319                 dateTimeParser.parse("February 12, 2002 12:35:00.000"),
320                 DateUtils.round(date1, Calendar.MINUTE));
321         assertEquals("round minute-2 failed",
322                 dateTimeParser.parse("November 18, 2001 1:23:00.000"),
323                 DateUtils.round(date2, Calendar.MINUTE));
324         assertEquals("round second-1 failed",
325                 dateTimeParser.parse("February 12, 2002 12:34:57.000"),
326                 DateUtils.round(date1, Calendar.SECOND));
327         assertEquals("round second-2 failed",
328                 dateTimeParser.parse("November 18, 2001 1:23:11.000"),
329                 DateUtils.round(date2, Calendar.SECOND));
330         assertEquals("truncate ampm-1 failed",
331                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
332                 DateUtils.round(dateAmPm1, Calendar.AM_PM));
333         assertEquals("truncate ampm-2 failed",
334                 dateTimeParser.parse("February 4, 2002 00:00:00.000"),
335                 DateUtils.round(dateAmPm2, Calendar.AM_PM));
336         assertEquals("truncate ampm-3 failed",
337                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
338                 DateUtils.round(dateAmPm3, Calendar.AM_PM));
339         assertEquals("truncate ampm-4 failed",
340                 dateTimeParser.parse("February 4, 2002 12:00:00.000"),
341                 DateUtils.round(dateAmPm4, Calendar.AM_PM));
342
343         // tests for public static Date round(Object date, int field)
344
assertEquals("round year-1 failed",
345                 dateParser.parse("January 1, 2002"),
346                 DateUtils.round((Object JavaDoc) date1, Calendar.YEAR));
347         assertEquals("round year-2 failed",
348                 dateParser.parse("January 1, 2002"),
349                 DateUtils.round((Object JavaDoc) date2, Calendar.YEAR));
350         assertEquals("round month-1 failed",
351                 dateParser.parse("February 1, 2002"),
352                 DateUtils.round((Object JavaDoc) date1, Calendar.MONTH));
353         assertEquals("round month-2 failed",
354                 dateParser.parse("December 1, 2001"),
355                 DateUtils.round((Object JavaDoc) date2, Calendar.MONTH));
356         assertEquals("round semimonth-1 failed",
357                 dateParser.parse("February 16, 2002"),
358                 DateUtils.round((Object JavaDoc) date1, DateUtils.SEMI_MONTH));
359         assertEquals("round semimonth-2 failed",
360                 dateParser.parse("November 16, 2001"),
361                 DateUtils.round((Object JavaDoc) date2, DateUtils.SEMI_MONTH));
362         assertEquals("round date-1 failed",
363                 dateParser.parse("February 13, 2002"),
364                 DateUtils.round((Object JavaDoc) date1, Calendar.DATE));
365         assertEquals("round date-2 failed",
366                 dateParser.parse("November 18, 2001"),
367                 DateUtils.round((Object JavaDoc) date2, Calendar.DATE));
368         assertEquals("round hour-1 failed",
369                 dateTimeParser.parse("February 12, 2002 13:00:00.000"),
370                 DateUtils.round((Object JavaDoc) date1, Calendar.HOUR));
371         assertEquals("round hour-2 failed",
372                 dateTimeParser.parse("November 18, 2001 1:00:00.000"),
373                 DateUtils.round((Object JavaDoc) date2, Calendar.HOUR));
374         assertEquals("round minute-1 failed",
375                 dateTimeParser.parse("February 12, 2002 12:35:00.000"),
376                 DateUtils.round((Object JavaDoc) date1, Calendar.MINUTE));
377         assertEquals("round minute-2 failed",
378                 dateTimeParser.parse("November 18, 2001 1:23:00.000"),
379                 DateUtils.round((Object JavaDoc) date2, Calendar.MINUTE));
380         assertEquals("round second-1 failed",
381                 dateTimeParser.parse("February 12, 2002 12:34:57.000"),
382                 DateUtils.round((Object JavaDoc) date1, Calendar.SECOND));
383         assertEquals("round second-2 failed",
384                 dateTimeParser.parse("November 18, 2001 1:23:11.000"),
385                 DateUtils.round((Object JavaDoc) date2, Calendar.SECOND));
386         assertEquals("round calendar second-1 failed",
387                 dateTimeParser.parse("February 12, 2002 12:34:57.000"),
388                 DateUtils.round((Object JavaDoc) cal1, Calendar.SECOND));
389         assertEquals("round calendar second-2 failed",
390                 dateTimeParser.parse("November 18, 2001 1:23:11.000"),
391                 DateUtils.round((Object JavaDoc) cal2, Calendar.SECOND));
392         assertEquals("truncate ampm-1 failed",
393                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
394                 DateUtils.round((Object JavaDoc) dateAmPm1, Calendar.AM_PM));
395         assertEquals("truncate ampm-2 failed",
396                 dateTimeParser.parse("February 4, 2002 00:00:00.000"),
397                 DateUtils.round((Object JavaDoc) dateAmPm2, Calendar.AM_PM));
398         assertEquals("truncate ampm-3 failed",
399                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
400                 DateUtils.round((Object JavaDoc) dateAmPm3, Calendar.AM_PM));
401         assertEquals("truncate ampm-4 failed",
402                 dateTimeParser.parse("February 4, 2002 12:00:00.000"),
403                 DateUtils.round((Object JavaDoc) dateAmPm4, Calendar.AM_PM));
404
405         try {
406             DateUtils.round((Date JavaDoc) null, Calendar.SECOND);
407             fail();
408         } catch (IllegalArgumentException JavaDoc ex) {}
409         try {
410             DateUtils.round((Calendar JavaDoc) null, Calendar.SECOND);
411             fail();
412         } catch (IllegalArgumentException JavaDoc ex) {}
413         try {
414             DateUtils.round((Object JavaDoc) null, Calendar.SECOND);
415             fail();
416         } catch (IllegalArgumentException JavaDoc ex) {}
417         try {
418             DateUtils.round("", Calendar.SECOND);
419             fail();
420         } catch (ClassCastException JavaDoc ex) {}
421         try {
422             DateUtils.round(date1, -9999);
423             fail();
424         } catch(IllegalArgumentException JavaDoc ex) {}
425
426         assertEquals("truncate ampm-1 failed",
427                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
428                 DateUtils.round((Object JavaDoc) calAmPm1, Calendar.AM_PM));
429         assertEquals("truncate ampm-2 failed",
430                 dateTimeParser.parse("February 4, 2002 00:00:00.000"),
431                 DateUtils.round((Object JavaDoc) calAmPm2, Calendar.AM_PM));
432         assertEquals("truncate ampm-3 failed",
433                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
434                 DateUtils.round((Object JavaDoc) calAmPm3, Calendar.AM_PM));
435         assertEquals("truncate ampm-4 failed",
436                 dateTimeParser.parse("February 4, 2002 12:00:00.000"),
437                 DateUtils.round((Object JavaDoc) calAmPm4, Calendar.AM_PM));
438         
439         // Fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=25560
440
// Test rounding across the beginning of daylight saving time
441
TimeZone.setDefault(zone);
442         dateTimeParser.setTimeZone(zone);
443         assertEquals("round MET date across DST change-over",
444                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
445                 DateUtils.round(date4, Calendar.DATE));
446         assertEquals("round MET date across DST change-over",
447                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
448                 DateUtils.round((Object JavaDoc) cal4, Calendar.DATE));
449         assertEquals("round MET date across DST change-over",
450                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
451                 DateUtils.round(date5, Calendar.DATE));
452         assertEquals("round MET date across DST change-over",
453                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
454                 DateUtils.round((Object JavaDoc) cal5, Calendar.DATE));
455         assertEquals("round MET date across DST change-over",
456                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
457                 DateUtils.round(date6, Calendar.DATE));
458         assertEquals("round MET date across DST change-over",
459                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
460                 DateUtils.round((Object JavaDoc) cal6, Calendar.DATE));
461         assertEquals("round MET date across DST change-over",
462                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
463                 DateUtils.round(date7, Calendar.DATE));
464         assertEquals("round MET date across DST change-over",
465                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
466                 DateUtils.round((Object JavaDoc) cal7, Calendar.DATE));
467         
468         assertEquals("round MET date across DST change-over",
469                 dateTimeParser.parse("March 30, 2003 01:00:00.000"),
470                 DateUtils.round(date4, Calendar.HOUR_OF_DAY));
471         assertEquals("round MET date across DST change-over",
472                 dateTimeParser.parse("March 30, 2003 01:00:00.000"),
473                 DateUtils.round((Object JavaDoc) cal4, Calendar.HOUR_OF_DAY));
474         if (SystemUtils.isJavaVersionAtLeast(1.4f)) {
475             assertEquals("round MET date across DST change-over",
476                     dateTimeParser.parse("March 30, 2003 03:00:00.000"),
477                     DateUtils.round(date5, Calendar.HOUR_OF_DAY));
478             assertEquals("round MET date across DST change-over",
479                     dateTimeParser.parse("March 30, 2003 03:00:00.000"),
480                     DateUtils.round((Object JavaDoc) cal5, Calendar.HOUR_OF_DAY));
481             assertEquals("round MET date across DST change-over",
482                     dateTimeParser.parse("March 30, 2003 03:00:00.000"),
483                     DateUtils.round(date6, Calendar.HOUR_OF_DAY));
484             assertEquals("round MET date across DST change-over",
485                     dateTimeParser.parse("March 30, 2003 03:00:00.000"),
486                     DateUtils.round((Object JavaDoc) cal6, Calendar.HOUR_OF_DAY));
487             assertEquals("round MET date across DST change-over",
488                     dateTimeParser.parse("March 30, 2003 04:00:00.000"),
489                     DateUtils.round(date7, Calendar.HOUR_OF_DAY));
490             assertEquals("round MET date across DST change-over",
491                     dateTimeParser.parse("March 30, 2003 04:00:00.000"),
492                     DateUtils.round((Object JavaDoc) cal7, Calendar.HOUR_OF_DAY));
493         } else {
494             this.warn("Some date rounding tests not run since the current version is " + SystemUtils.JAVA_VERSION);
495         }
496         TimeZone.setDefault(defaultZone);
497         dateTimeParser.setTimeZone(defaultZone);
498     }
499
500     /**
501      * Tests various values with the trunc method
502      */

503     public void testTruncate() throws Exception JavaDoc {
504         // tests public static Date truncate(Date date, int field)
505
assertEquals("truncate year-1 failed",
506                 dateParser.parse("January 1, 2002"),
507                 DateUtils.truncate(date1, Calendar.YEAR));
508         assertEquals("truncate year-2 failed",
509                 dateParser.parse("January 1, 2001"),
510                 DateUtils.truncate(date2, Calendar.YEAR));
511         assertEquals("truncate month-1 failed",
512                 dateParser.parse("February 1, 2002"),
513                 DateUtils.truncate(date1, Calendar.MONTH));
514         assertEquals("truncate month-2 failed",
515                 dateParser.parse("November 1, 2001"),
516                 DateUtils.truncate(date2, Calendar.MONTH));
517         assertEquals("truncate semimonth-1 failed",
518                 dateParser.parse("February 1, 2002"),
519                 DateUtils.truncate(date1, DateUtils.SEMI_MONTH));
520         assertEquals("truncate semimonth-2 failed",
521                 dateParser.parse("November 16, 2001"),
522                 DateUtils.truncate(date2, DateUtils.SEMI_MONTH));
523         assertEquals("truncate date-1 failed",
524                 dateParser.parse("February 12, 2002"),
525                 DateUtils.truncate(date1, Calendar.DATE));
526         assertEquals("truncate date-2 failed",
527                 dateParser.parse("November 18, 2001"),
528                 DateUtils.truncate(date2, Calendar.DATE));
529         assertEquals("truncate hour-1 failed",
530                 dateTimeParser.parse("February 12, 2002 12:00:00.000"),
531                 DateUtils.truncate(date1, Calendar.HOUR));
532         assertEquals("truncate hour-2 failed",
533                 dateTimeParser.parse("November 18, 2001 1:00:00.000"),
534                 DateUtils.truncate(date2, Calendar.HOUR));
535         assertEquals("truncate minute-1 failed",
536                 dateTimeParser.parse("February 12, 2002 12:34:00.000"),
537                 DateUtils.truncate(date1, Calendar.MINUTE));
538         assertEquals("truncate minute-2 failed",
539                 dateTimeParser.parse("November 18, 2001 1:23:00.000"),
540                 DateUtils.truncate(date2, Calendar.MINUTE));
541         assertEquals("truncate second-1 failed",
542                 dateTimeParser.parse("February 12, 2002 12:34:56.000"),
543                 DateUtils.truncate(date1, Calendar.SECOND));
544         assertEquals("truncate second-2 failed",
545                 dateTimeParser.parse("November 18, 2001 1:23:11.000"),
546                 DateUtils.truncate(date2, Calendar.SECOND));
547         assertEquals("truncate ampm-1 failed",
548                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
549                 DateUtils.truncate(dateAmPm1, Calendar.AM_PM));
550         assertEquals("truncate ampm-2 failed",
551                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
552                 DateUtils.truncate(dateAmPm2, Calendar.AM_PM));
553         assertEquals("truncate ampm-3 failed",
554                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
555                 DateUtils.truncate(dateAmPm3, Calendar.AM_PM));
556         assertEquals("truncate ampm-4 failed",
557                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
558                 DateUtils.truncate(dateAmPm4, Calendar.AM_PM));
559
560         // tests public static Date truncate(Object date, int field)
561
assertEquals("truncate year-1 failed",
562                 dateParser.parse("January 1, 2002"),
563                 DateUtils.truncate((Object JavaDoc) date1, Calendar.YEAR));
564         assertEquals("truncate year-2 failed",
565                 dateParser.parse("January 1, 2001"),
566                 DateUtils.truncate((Object JavaDoc) date2, Calendar.YEAR));
567         assertEquals("truncate month-1 failed",
568                 dateParser.parse("February 1, 2002"),
569                 DateUtils.truncate((Object JavaDoc) date1, Calendar.MONTH));
570         assertEquals("truncate month-2 failed",
571                 dateParser.parse("November 1, 2001"),
572                 DateUtils.truncate((Object JavaDoc) date2, Calendar.MONTH));
573         assertEquals("truncate semimonth-1 failed",
574                 dateParser.parse("February 1, 2002"),
575                 DateUtils.truncate((Object JavaDoc) date1, DateUtils.SEMI_MONTH));
576         assertEquals("truncate semimonth-2 failed",
577                 dateParser.parse("November 16, 2001"),
578                 DateUtils.truncate((Object JavaDoc) date2, DateUtils.SEMI_MONTH));
579         assertEquals("truncate date-1 failed",
580                 dateParser.parse("February 12, 2002"),
581                 DateUtils.truncate((Object JavaDoc) date1, Calendar.DATE));
582         assertEquals("truncate date-2 failed",
583                 dateParser.parse("November 18, 2001"),
584                 DateUtils.truncate((Object JavaDoc) date2, Calendar.DATE));
585         assertEquals("truncate hour-1 failed",
586                 dateTimeParser.parse("February 12, 2002 12:00:00.000"),
587                 DateUtils.truncate((Object JavaDoc) date1, Calendar.HOUR));
588         assertEquals("truncate hour-2 failed",
589                 dateTimeParser.parse("November 18, 2001 1:00:00.000"),
590                 DateUtils.truncate((Object JavaDoc) date2, Calendar.HOUR));
591         assertEquals("truncate minute-1 failed",
592                 dateTimeParser.parse("February 12, 2002 12:34:00.000"),
593                 DateUtils.truncate((Object JavaDoc) date1, Calendar.MINUTE));
594         assertEquals("truncate minute-2 failed",
595                 dateTimeParser.parse("November 18, 2001 1:23:00.000"),
596                 DateUtils.truncate((Object JavaDoc) date2, Calendar.MINUTE));
597         assertEquals("truncate second-1 failed",
598                 dateTimeParser.parse("February 12, 2002 12:34:56.000"),
599                 DateUtils.truncate((Object JavaDoc) date1, Calendar.SECOND));
600         assertEquals("truncate second-2 failed",
601                 dateTimeParser.parse("November 18, 2001 1:23:11.000"),
602                 DateUtils.truncate((Object JavaDoc) date2, Calendar.SECOND));
603         assertEquals("truncate ampm-1 failed",
604                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
605                 DateUtils.truncate((Object JavaDoc) dateAmPm1, Calendar.AM_PM));
606         assertEquals("truncate ampm-2 failed",
607                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
608                 DateUtils.truncate((Object JavaDoc) dateAmPm2, Calendar.AM_PM));
609         assertEquals("truncate ampm-3 failed",
610                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
611                 DateUtils.truncate((Object JavaDoc) dateAmPm3, Calendar.AM_PM));
612         assertEquals("truncate ampm-4 failed",
613                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
614                 DateUtils.truncate((Object JavaDoc) dateAmPm4, Calendar.AM_PM));
615         
616         assertEquals("truncate calendar second-1 failed",
617                 dateTimeParser.parse("February 12, 2002 12:34:56.000"),
618                 DateUtils.truncate((Object JavaDoc) cal1, Calendar.SECOND));
619         assertEquals("truncate calendar second-2 failed",
620                 dateTimeParser.parse("November 18, 2001 1:23:11.000"),
621                 DateUtils.truncate((Object JavaDoc) cal2, Calendar.SECOND));
622         
623         assertEquals("truncate ampm-1 failed",
624                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
625                 DateUtils.truncate((Object JavaDoc) calAmPm1, Calendar.AM_PM));
626         assertEquals("truncate ampm-2 failed",
627                 dateTimeParser.parse("February 3, 2002 00:00:00.000"),
628                 DateUtils.truncate((Object JavaDoc) calAmPm2, Calendar.AM_PM));
629         assertEquals("truncate ampm-3 failed",
630                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
631                 DateUtils.truncate((Object JavaDoc) calAmPm3, Calendar.AM_PM));
632         assertEquals("truncate ampm-4 failed",
633                 dateTimeParser.parse("February 3, 2002 12:00:00.000"),
634                 DateUtils.truncate((Object JavaDoc) calAmPm4, Calendar.AM_PM));
635         
636         try {
637             DateUtils.truncate((Date JavaDoc) null, Calendar.SECOND);
638             fail();
639         } catch (IllegalArgumentException JavaDoc ex) {}
640         try {
641             DateUtils.truncate((Calendar JavaDoc) null, Calendar.SECOND);
642             fail();
643         } catch (IllegalArgumentException JavaDoc ex) {}
644         try {
645             DateUtils.truncate((Object JavaDoc) null, Calendar.SECOND);
646             fail();
647         } catch (IllegalArgumentException JavaDoc ex) {}
648         try {
649             DateUtils.truncate("", Calendar.SECOND);
650             fail();
651         } catch (ClassCastException JavaDoc ex) {}
652
653         // Fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=25560
654
// Test truncate across beginning of daylight saving time
655
TimeZone.setDefault(zone);
656         dateTimeParser.setTimeZone(zone);
657         assertEquals("truncate MET date across DST change-over",
658                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
659                 DateUtils.truncate(date3, Calendar.DATE));
660         assertEquals("truncate MET date across DST change-over",
661                 dateTimeParser.parse("March 30, 2003 00:00:00.000"),
662                 DateUtils.truncate((Object JavaDoc) cal3, Calendar.DATE));
663         // Test truncate across end of daylight saving time
664
assertEquals("truncate MET date across DST change-over",
665                 dateTimeParser.parse("October 26, 2003 00:00:00.000"),
666                 DateUtils.truncate(date8, Calendar.DATE));
667         assertEquals("truncate MET date across DST change-over",
668                 dateTimeParser.parse("October 26, 2003 00:00:00.000"),
669                 DateUtils.truncate((Object JavaDoc) cal8, Calendar.DATE));
670         TimeZone.setDefault(defaultZone);
671         dateTimeParser.setTimeZone(defaultZone);
672         
673         // Bug 31395, large dates
674
Date JavaDoc endOfTime = new Date JavaDoc(Long.MAX_VALUE); // fyi: Sun Aug 17 07:12:55 CET 292278994 -- 807 millis
675
GregorianCalendar JavaDoc endCal = new GregorianCalendar JavaDoc();
676         endCal.setTime(endOfTime);
677         try {
678             DateUtils.truncate(endCal, Calendar.DATE);
679             fail();
680         } catch (ArithmeticException JavaDoc ex) {}
681         endCal.set(Calendar.YEAR, 280000001);
682         try {
683             DateUtils.truncate(endCal, Calendar.DATE);
684             fail();
685         } catch (ArithmeticException JavaDoc ex) {}
686         endCal.set(Calendar.YEAR, 280000000);
687         Calendar JavaDoc cal = DateUtils.truncate(endCal, Calendar.DATE);
688         assertEquals(0, cal.get(Calendar.HOUR));
689     }
690
691     /**
692      * Tests the iterator exceptions
693      */

694     public void testIteratorEx() throws Exception JavaDoc {
695         try {
696             DateUtils.iterator(Calendar.getInstance(), -9999);
697         } catch (IllegalArgumentException JavaDoc ex) {}
698         try {
699             DateUtils.iterator((Date JavaDoc) null, DateUtils.RANGE_WEEK_CENTER);
700             fail();
701         } catch (IllegalArgumentException JavaDoc ex) {}
702         try {
703             DateUtils.iterator((Calendar JavaDoc) null, DateUtils.RANGE_WEEK_CENTER);
704             fail();
705         } catch (IllegalArgumentException JavaDoc ex) {}
706         try {
707             DateUtils.iterator((Object JavaDoc) null, DateUtils.RANGE_WEEK_CENTER);
708             fail();
709         } catch (IllegalArgumentException JavaDoc ex) {}
710         try {
711             DateUtils.iterator("", DateUtils.RANGE_WEEK_CENTER);
712             fail();
713         } catch (ClassCastException JavaDoc ex) {}
714     }
715
716     /**
717      * Tests the calendar iterator for week ranges
718      */

719     public void testWeekIterator() throws Exception JavaDoc {
720         Calendar JavaDoc now = Calendar.getInstance();
721         for (int i = 0; i< 7; i++) {
722             Calendar JavaDoc today = DateUtils.truncate(now, Calendar.DATE);
723             Calendar JavaDoc sunday = DateUtils.truncate(now, Calendar.DATE);
724             sunday.add(Calendar.DATE, 1 - sunday.get(Calendar.DAY_OF_WEEK));
725             Calendar JavaDoc monday = DateUtils.truncate(now, Calendar.DATE);
726             if (monday.get(Calendar.DAY_OF_WEEK) == 1) {
727                 //This is sunday... roll back 6 days
728
monday.add(Calendar.DATE, -6);
729             } else {
730                 monday.add(Calendar.DATE, 2 - monday.get(Calendar.DAY_OF_WEEK));
731             }
732             Calendar JavaDoc centered = DateUtils.truncate(now, Calendar.DATE);
733             centered.add(Calendar.DATE, -3);
734             
735             Iterator JavaDoc it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_SUNDAY);
736             assertWeekIterator(it, sunday);
737             it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_MONDAY);
738             assertWeekIterator(it, monday);
739             it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_RELATIVE);
740             assertWeekIterator(it, today);
741             it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER);
742             assertWeekIterator(it, centered);
743             
744             it = DateUtils.iterator((Object JavaDoc) now, DateUtils.RANGE_WEEK_CENTER);
745             assertWeekIterator(it, centered);
746             it = DateUtils.iterator((Object JavaDoc) now.getTime(), DateUtils.RANGE_WEEK_CENTER);
747             assertWeekIterator(it, centered);
748             try {
749                 it.next();
750                 fail();
751             } catch (NoSuchElementException JavaDoc ex) {}
752             it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER);
753             it.next();
754             try {
755                 it.remove();
756             } catch( UnsupportedOperationException JavaDoc ex) {}
757             
758             now.add(Calendar.DATE,1);
759         }
760     }
761             
762     /**
763      * Tests the calendar iterator for month-based ranges
764      */

765     public void testMonthIterator() throws Exception JavaDoc {
766         Iterator JavaDoc it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_SUNDAY);
767         assertWeekIterator(it,
768                 dateParser.parse("January 27, 2002"),
769                 dateParser.parse("March 2, 2002"));
770
771         it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_MONDAY);
772         assertWeekIterator(it,
773                 dateParser.parse("January 28, 2002"),
774                 dateParser.parse("March 3, 2002"));
775
776         it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_SUNDAY);
777         assertWeekIterator(it,
778                 dateParser.parse("October 28, 2001"),
779                 dateParser.parse("December 1, 2001"));
780
781         it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_MONDAY);
782         assertWeekIterator(it,
783                 dateParser.parse("October 29, 2001"),
784                 dateParser.parse("December 2, 2001"));
785     }
786
787     /**
788      * This checks that this is a 7 element iterator of Calendar objects
789      * that are dates (no time), and exactly 1 day spaced after each other.
790      */

791     private static void assertWeekIterator(Iterator JavaDoc it, Calendar JavaDoc start) {
792         Calendar JavaDoc end = (Calendar JavaDoc) start.clone();
793         end.add(Calendar.DATE, 6);
794
795         assertWeekIterator(it, start, end);
796     }
797
798     /**
799      * Convenience method for when working with Date objects
800      */

801     private static void assertWeekIterator(Iterator JavaDoc it, Date JavaDoc start, Date JavaDoc end) {
802         Calendar JavaDoc calStart = Calendar.getInstance();
803         calStart.setTime(start);
804         Calendar JavaDoc calEnd = Calendar.getInstance();
805         calEnd.setTime(end);
806
807         assertWeekIterator(it, calStart, calEnd);
808     }
809
810     /**
811      * This checks that this is a 7 divisble iterator of Calendar objects
812      * that are dates (no time), and exactly 1 day spaced after each other
813      * (in addition to the proper start and stop dates)
814      */

815     private static void assertWeekIterator(Iterator JavaDoc it, Calendar JavaDoc start, Calendar JavaDoc end) {
816         Calendar JavaDoc cal = (Calendar JavaDoc) it.next();
817         assertEquals("", start, cal, 0);
818         Calendar JavaDoc last = null;
819         int count = 1;
820         while (it.hasNext()) {
821             //Check this is just a date (no time component)
822
assertEquals("", cal, DateUtils.truncate(cal, Calendar.DATE), 0);
823
824             last = cal;
825             cal = (Calendar JavaDoc) it.next();
826             count++;
827
828             //Check that this is one day more than the last date
829
last.add(Calendar.DATE, 1);
830             assertEquals("", last, cal, 0);
831         }
832         if (count % 7 != 0) {
833             throw new AssertionFailedError("There were " + count + " days in this iterator");
834         }
835         assertEquals("", end, cal, 0);
836     }
837
838     /**
839      * Used to check that Calendar objects are close enough
840      * delta is in milliseconds
841      */

842     private static void assertEquals(String JavaDoc message, Calendar JavaDoc cal1, Calendar JavaDoc cal2, long delta) {
843         if (Math.abs(cal1.getTime().getTime() - cal2.getTime().getTime()) > delta) {
844             throw new AssertionFailedError(
845                     message + " expected " + cal1.getTime() + " but got " + cal2.getTime());
846         }
847     }
848
849     void warn(String JavaDoc msg) {
850         System.err.println(msg);
851     }
852 }
853
854
Popular Tags