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