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  package org.apache.commons.lang.time;
18  
19  import java.text.SimpleDateFormat;
20  import java.util.Calendar;
21  import java.util.Date;
22  import java.util.GregorianCalendar;
23  import java.util.Locale;
24  import java.util.TimeZone;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  import junit.textui.TestRunner;
30  
31  import org.apache.commons.lang.SerializationUtils;
32  
33  /**
34   * Unit tests {@link org.apache.commons.lang.time.FastDateFormat}.
35   *
36   * @author Sean Schofield
37   * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
38   * @author Fredrik Westermarck
39   * @since 2.0
40   * @version $Id: FastDateFormatTest.java 590551 2007-10-31 03:58:52Z bayard $
41   */
42  public class FastDateFormatTest extends TestCase {
43  
44      public FastDateFormatTest(String name) {
45          super(name);
46      }
47  
48      public static void main(String[] args) {
49          TestRunner.run(suite());
50      }
51  
52      public static Test suite() {
53          TestSuite suite = new TestSuite(FastDateFormatTest.class);
54          suite.setName("FastDateFormat Tests");
55  
56          return suite;
57      }
58  
59      protected void setUp() throws Exception {
60          super.setUp();
61      }
62  
63      protected void tearDown() throws Exception {
64          super.tearDown();
65      }
66  
67      public void test_getInstance() {
68          FastDateFormat format1 = FastDateFormat.getInstance();
69          FastDateFormat format2 = FastDateFormat.getInstance();
70          assertSame(format1, format2);
71          assertEquals(new SimpleDateFormat().toPattern(), format1.getPattern());
72      }
73  
74      public void test_getInstance_String() {
75          FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy");
76          FastDateFormat format2 = FastDateFormat.getInstance("MM-DD-yyyy");
77          FastDateFormat format3 = FastDateFormat.getInstance("MM-DD-yyyy");
78  
79          assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
80          assertSame(format2, format3);
81          assertEquals("MM/DD/yyyy", format1.getPattern());
82          assertEquals(TimeZone.getDefault(), format1.getTimeZone());
83          assertEquals(TimeZone.getDefault(), format2.getTimeZone());
84          assertEquals(false, format1.getTimeZoneOverridesCalendar());
85          assertEquals(false, format2.getTimeZoneOverridesCalendar());
86      }
87  
88      public void test_getInstance_String_TimeZone() {
89          Locale realDefaultLocale = Locale.getDefault();
90          TimeZone realDefaultZone = TimeZone.getDefault();
91          try {
92              Locale.setDefault(Locale.US);
93              TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
94  
95              FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
96                      TimeZone.getTimeZone("Atlantic/Reykjavik"));
97              FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
98              FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
99              FastDateFormat format4 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
100             FastDateFormat format5 = FastDateFormat.getInstance("MM-DD-yyyy", TimeZone.getDefault());
101             FastDateFormat format6 = FastDateFormat.getInstance("MM-DD-yyyy");
102 
103             assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
104             assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
105             assertEquals(true, format1.getTimeZoneOverridesCalendar());
106             assertEquals(TimeZone.getDefault(), format2.getTimeZone());
107             assertEquals(false, format2.getTimeZoneOverridesCalendar());
108             assertSame(format3, format4);
109             assertTrue(format3 != format5); // -- junit 3.8 version -- assertFalse(format3 == format5);
110             assertTrue(format4 != format6); // -- junit 3.8 version -- assertFalse(format3 == format5);
111 
112         } finally {
113             Locale.setDefault(realDefaultLocale);
114             TimeZone.setDefault(realDefaultZone);
115         }
116     }
117 
118     public void test_getInstance_String_Locale() {
119         Locale realDefaultLocale = Locale.getDefault();
120         try {
121             Locale.setDefault(Locale.US);
122             FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
123             FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
124             FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
125 
126             assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
127             assertSame(format1, format3);
128             assertSame(Locale.GERMANY, format1.getLocale());
129 
130         } finally {
131             Locale.setDefault(realDefaultLocale);
132         }
133     }
134 
135     public void test_changeDefault_Locale_DateInstance() {
136         Locale realDefaultLocale = Locale.getDefault();
137         try {
138             Locale.setDefault(Locale.US);
139             FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY);
140             FastDateFormat format2 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
141             Locale.setDefault(Locale.GERMANY);
142             FastDateFormat format3 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
143 
144             assertSame(Locale.GERMANY, format1.getLocale());
145             assertSame(Locale.US, format2.getLocale());
146             assertSame(Locale.GERMANY, format3.getLocale());
147             assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
148             assertTrue(format2 != format3);
149 
150         } finally {
151             Locale.setDefault(realDefaultLocale);
152         }
153     }
154 
155     public void test_changeDefault_Locale_DateTimeInstance() {
156         Locale realDefaultLocale = Locale.getDefault();
157         try {
158             Locale.setDefault(Locale.US);
159             FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY);
160             FastDateFormat format2 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
161             Locale.setDefault(Locale.GERMANY);
162             FastDateFormat format3 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
163 
164             assertSame(Locale.GERMANY, format1.getLocale());
165             assertSame(Locale.US, format2.getLocale());
166             assertSame(Locale.GERMANY, format3.getLocale());
167             assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
168             assertTrue(format2 != format3);
169 
170         } finally {
171             Locale.setDefault(realDefaultLocale);
172         }
173     }
174 
175     public void test_getInstance_String_TimeZone_Locale() {
176         Locale realDefaultLocale = Locale.getDefault();
177         TimeZone realDefaultZone = TimeZone.getDefault();
178         try {
179             Locale.setDefault(Locale.US);
180             TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
181 
182             FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
183                     TimeZone.getTimeZone("Atlantic/Reykjavik"), Locale.GERMANY);
184             FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
185             FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy",
186                     TimeZone.getDefault(), Locale.GERMANY);
187 
188             assertTrue(format1 != format2); // -- junit 3.8 version -- assertNotSame(format1, format2);
189             assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
190             assertEquals(TimeZone.getDefault(), format2.getTimeZone());
191             assertEquals(TimeZone.getDefault(), format3.getTimeZone());
192             assertEquals(true, format1.getTimeZoneOverridesCalendar());
193             assertEquals(false, format2.getTimeZoneOverridesCalendar());
194             assertEquals(true, format3.getTimeZoneOverridesCalendar());
195             assertEquals(Locale.GERMANY, format1.getLocale());
196             assertEquals(Locale.GERMANY, format2.getLocale());
197             assertEquals(Locale.GERMANY, format3.getLocale());
198 
199         } finally {
200             Locale.setDefault(realDefaultLocale);
201             TimeZone.setDefault(realDefaultZone);
202         }
203     }
204 
205     public void testFormat() {
206         Locale realDefaultLocale = Locale.getDefault();
207         TimeZone realDefaultZone = TimeZone.getDefault();
208         try {
209             Locale.setDefault(Locale.US);
210             TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
211             FastDateFormat fdf = null;
212             SimpleDateFormat sdf = null;
213 
214             GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10, 15, 33, 20);
215             GregorianCalendar cal2 = new GregorianCalendar(2003, 6, 10, 9, 00, 00);
216             Date date1 = cal1.getTime();
217             Date date2 = cal2.getTime();
218             long millis1 = date1.getTime();
219             long millis2 = date2.getTime();
220 
221             fdf = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss");
222             sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
223             assertEquals(sdf.format(date1), fdf.format(date1));
224             assertEquals("2003-01-10T15:33:20", fdf.format(date1));
225             assertEquals("2003-01-10T15:33:20", fdf.format(cal1));
226             assertEquals("2003-01-10T15:33:20", fdf.format(millis1));
227             assertEquals("2003-07-10T09:00:00", fdf.format(date2));
228             assertEquals("2003-07-10T09:00:00", fdf.format(cal2));
229             assertEquals("2003-07-10T09:00:00", fdf.format(millis2));
230 
231             fdf = FastDateFormat.getInstance("Z");
232             assertEquals("-0500", fdf.format(date1));
233             assertEquals("-0500", fdf.format(cal1));
234             assertEquals("-0500", fdf.format(millis1));
235 
236             fdf = FastDateFormat.getInstance("Z");
237             assertEquals("-0400", fdf.format(date2));
238             assertEquals("-0400", fdf.format(cal2));
239             assertEquals("-0400", fdf.format(millis2));
240 
241             fdf = FastDateFormat.getInstance("ZZ");
242             assertEquals("-05:00", fdf.format(date1));
243             assertEquals("-05:00", fdf.format(cal1));
244             assertEquals("-05:00", fdf.format(millis1));
245 
246             fdf = FastDateFormat.getInstance("ZZ");
247             assertEquals("-04:00", fdf.format(date2));
248             assertEquals("-04:00", fdf.format(cal2));
249             assertEquals("-04:00", fdf.format(millis2));
250 
251             String pattern = "GGGG GGG GG G yyyy yyy yy y MMMM MMM MM M" +
252                 " dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
253             fdf = FastDateFormat.getInstance(pattern);
254             sdf = new SimpleDateFormat(pattern);
255             assertEquals(sdf.format(date1), fdf.format(date1));
256             assertEquals(sdf.format(date2), fdf.format(date2));
257 
258         } finally {
259             Locale.setDefault(realDefaultLocale);
260             TimeZone.setDefault(realDefaultZone);
261         }
262     }
263 
264     /**
265      * Test case for {@link FastDateFormat#getDateInstance(int, java.util.Locale)}.
266      */
267     public void testShortDateStyleWithLocales() {
268         Locale usLocale = Locale.US;
269         Locale swedishLocale = new Locale("sv", "SE");
270         Calendar cal = Calendar.getInstance();
271         cal.set(2004, 1, 3);
272         FastDateFormat fdf = FastDateFormat.getDateInstance(FastDateFormat.SHORT, usLocale);
273         assertEquals("2/3/04", fdf.format(cal));
274 
275         fdf = FastDateFormat.getDateInstance(FastDateFormat.SHORT, swedishLocale);
276         assertEquals("2004-02-03", fdf.format(cal));
277 
278     }
279 
280     /**
281      * Tests that pre-1000AD years get padded with yyyy
282      */
283     public void testLowYearPadding() {
284         Calendar cal = Calendar.getInstance();
285         FastDateFormat format = FastDateFormat.getInstance("yyyy/MM/DD");
286 
287         cal.set(1,0,1);
288         assertEquals("0001/01/01", format.format(cal));
289         cal.set(10,0,1);
290         assertEquals("0010/01/01", format.format(cal));
291         cal.set(100,0,1);
292         assertEquals("0100/01/01", format.format(cal));
293         cal.set(999,0,1);
294         assertEquals("0999/01/01", format.format(cal));
295     }
296     /**
297      * Show Bug #39410 is solved
298      */
299     public void testMilleniumBug() {
300         Calendar cal = Calendar.getInstance();
301         FastDateFormat format = FastDateFormat.getInstance("dd.MM.yyyy");
302 
303         cal.set(1000,0,1);
304         assertEquals("01.01.1000", format.format(cal));
305     }
306 
307     /**
308      * testLowYearPadding showed that the date was buggy
309      * This test confirms it, getting 366 back as a date
310      */
311      // TODO: Fix this problem
312     public void testSimpleDate() {
313         Calendar cal = Calendar.getInstance();
314         FastDateFormat format = FastDateFormat.getInstance("yyyy/MM/dd");
315 
316         cal.set(2004,11,31);
317         assertEquals("2004/12/31", format.format(cal));
318         cal.set(999,11,31);
319         assertEquals("0999/12/31", format.format(cal));
320         cal.set(1,2,2);
321         assertEquals("0001/03/02", format.format(cal));
322     }
323 
324     public void testLang303() {
325         Calendar cal = Calendar.getInstance();
326         cal.set(2004,11,31);
327 
328         FastDateFormat format = FastDateFormat.getInstance("yyyy/MM/dd");
329         String output = format.format(cal);
330 
331         format = (FastDateFormat) SerializationUtils.deserialize( SerializationUtils.serialize( format ) );
332         assertEquals(output, format.format(cal));
333     }
334 }