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: package org.apache.commons.lang.time;
018:
019: import java.text.SimpleDateFormat;
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.GregorianCalendar;
023: import java.util.Locale;
024: import java.util.TimeZone;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029: import junit.textui.TestRunner;
030:
031: import org.apache.commons.lang.SerializationUtils;
032:
033: /**
034: * Unit tests {@link org.apache.commons.lang.time.FastDateFormat}.
035: *
036: * @author Sean Schofield
037: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
038: * @author Fredrik Westermarck
039: * @since 2.0
040: * @version $Id: FastDateFormatTest.java 490388 2006-12-26 22:10:10Z bayard $
041: */
042: public class FastDateFormatTest extends TestCase {
043:
044: public FastDateFormatTest(String name) {
045: super (name);
046: }
047:
048: public static void main(String[] args) {
049: TestRunner.run(suite());
050: }
051:
052: public static Test suite() {
053: TestSuite suite = new TestSuite(FastDateFormatTest.class);
054: suite.setName("FastDateFormat Tests");
055:
056: return suite;
057: }
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: }
066:
067: public void test_getInstance() {
068: FastDateFormat format1 = FastDateFormat.getInstance();
069: FastDateFormat format2 = FastDateFormat.getInstance();
070: assertSame(format1, format2);
071: assertEquals(new SimpleDateFormat().toPattern(), format1
072: .getPattern());
073: }
074:
075: public void test_getInstance_String() {
076: FastDateFormat format1 = FastDateFormat
077: .getInstance("MM/DD/yyyy");
078: FastDateFormat format2 = FastDateFormat
079: .getInstance("MM-DD-yyyy");
080: FastDateFormat format3 = FastDateFormat
081: .getInstance("MM-DD-yyyy");
082:
083: assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
084: assertSame(format2, format3);
085: assertEquals("MM/DD/yyyy", format1.getPattern());
086: assertEquals(TimeZone.getDefault(), format1.getTimeZone());
087: assertEquals(TimeZone.getDefault(), format2.getTimeZone());
088: assertEquals(false, format1.getTimeZoneOverridesCalendar());
089: assertEquals(false, format2.getTimeZoneOverridesCalendar());
090: }
091:
092: public void test_getInstance_String_TimeZone() {
093: Locale realDefaultLocale = Locale.getDefault();
094: TimeZone realDefaultZone = TimeZone.getDefault();
095: try {
096: Locale.setDefault(Locale.US);
097: TimeZone.setDefault(TimeZone
098: .getTimeZone("America/New_York"));
099:
100: FastDateFormat format1 = FastDateFormat.getInstance(
101: "MM/DD/yyyy", TimeZone
102: .getTimeZone("Atlantic/Reykjavik"));
103: FastDateFormat format2 = FastDateFormat
104: .getInstance("MM/DD/yyyy");
105: FastDateFormat format3 = FastDateFormat.getInstance(
106: "MM/DD/yyyy", TimeZone.getDefault());
107: FastDateFormat format4 = FastDateFormat.getInstance(
108: "MM/DD/yyyy", TimeZone.getDefault());
109: FastDateFormat format5 = FastDateFormat.getInstance(
110: "MM-DD-yyyy", TimeZone.getDefault());
111: FastDateFormat format6 = FastDateFormat
112: .getInstance("MM-DD-yyyy");
113:
114: assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
115: assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"),
116: format1.getTimeZone());
117: assertEquals(true, format1.getTimeZoneOverridesCalendar());
118: assertEquals(TimeZone.getDefault(), format2.getTimeZone());
119: assertEquals(false, format2.getTimeZoneOverridesCalendar());
120: assertSame(format3, format4);
121: assertTrue(format3 != format5); // -- junit 3.8 version -- assertFalse(format3 == format5);
122: assertTrue(format4 != format6); // -- junit 3.8 version -- assertFalse(format3 == format5);
123:
124: } finally {
125: Locale.setDefault(realDefaultLocale);
126: TimeZone.setDefault(realDefaultZone);
127: }
128: }
129:
130: public void test_getInstance_String_Locale() {
131: Locale realDefaultLocale = Locale.getDefault();
132: try {
133: Locale.setDefault(Locale.US);
134: FastDateFormat format1 = FastDateFormat.getInstance(
135: "MM/DD/yyyy", Locale.GERMANY);
136: FastDateFormat format2 = FastDateFormat
137: .getInstance("MM/DD/yyyy");
138: FastDateFormat format3 = FastDateFormat.getInstance(
139: "MM/DD/yyyy", Locale.GERMANY);
140:
141: assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
142: assertSame(format1, format3);
143: assertSame(Locale.GERMANY, format1.getLocale());
144:
145: } finally {
146: Locale.setDefault(realDefaultLocale);
147: }
148: }
149:
150: public void test_getInstance_String_TimeZone_Locale() {
151: Locale realDefaultLocale = Locale.getDefault();
152: TimeZone realDefaultZone = TimeZone.getDefault();
153: try {
154: Locale.setDefault(Locale.US);
155: TimeZone.setDefault(TimeZone
156: .getTimeZone("America/New_York"));
157:
158: FastDateFormat format1 = FastDateFormat.getInstance(
159: "MM/DD/yyyy", TimeZone
160: .getTimeZone("Atlantic/Reykjavik"),
161: Locale.GERMANY);
162: FastDateFormat format2 = FastDateFormat.getInstance(
163: "MM/DD/yyyy", Locale.GERMANY);
164: FastDateFormat format3 = FastDateFormat
165: .getInstance("MM/DD/yyyy", TimeZone.getDefault(),
166: Locale.GERMANY);
167:
168: assertTrue(format1 != format2); // -- junit 3.8 version -- assertNotSame(format1, format2);
169: assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"),
170: format1.getTimeZone());
171: assertEquals(TimeZone.getDefault(), format2.getTimeZone());
172: assertEquals(TimeZone.getDefault(), format3.getTimeZone());
173: assertEquals(true, format1.getTimeZoneOverridesCalendar());
174: assertEquals(false, format2.getTimeZoneOverridesCalendar());
175: assertEquals(true, format3.getTimeZoneOverridesCalendar());
176: assertEquals(Locale.GERMANY, format1.getLocale());
177: assertEquals(Locale.GERMANY, format2.getLocale());
178: assertEquals(Locale.GERMANY, format3.getLocale());
179:
180: } finally {
181: Locale.setDefault(realDefaultLocale);
182: TimeZone.setDefault(realDefaultZone);
183: }
184: }
185:
186: public void testFormat() {
187: Locale realDefaultLocale = Locale.getDefault();
188: TimeZone realDefaultZone = TimeZone.getDefault();
189: try {
190: Locale.setDefault(Locale.US);
191: TimeZone.setDefault(TimeZone
192: .getTimeZone("America/New_York"));
193: FastDateFormat fdf = null;
194: SimpleDateFormat sdf = null;
195:
196: GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10,
197: 15, 33, 20);
198: GregorianCalendar cal2 = new GregorianCalendar(2003, 6, 10,
199: 9, 00, 00);
200: Date date1 = cal1.getTime();
201: Date date2 = cal2.getTime();
202: long millis1 = date1.getTime();
203: long millis2 = date2.getTime();
204:
205: fdf = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss");
206: sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
207: assertEquals(sdf.format(date1), fdf.format(date1));
208: assertEquals("2003-01-10T15:33:20", fdf.format(date1));
209: assertEquals("2003-01-10T15:33:20", fdf.format(cal1));
210: assertEquals("2003-01-10T15:33:20", fdf.format(millis1));
211: assertEquals("2003-07-10T09:00:00", fdf.format(date2));
212: assertEquals("2003-07-10T09:00:00", fdf.format(cal2));
213: assertEquals("2003-07-10T09:00:00", fdf.format(millis2));
214:
215: fdf = FastDateFormat.getInstance("Z");
216: assertEquals("-0500", fdf.format(date1));
217: assertEquals("-0500", fdf.format(cal1));
218: assertEquals("-0500", fdf.format(millis1));
219:
220: fdf = FastDateFormat.getInstance("Z");
221: assertEquals("-0400", fdf.format(date2));
222: assertEquals("-0400", fdf.format(cal2));
223: assertEquals("-0400", fdf.format(millis2));
224:
225: fdf = FastDateFormat.getInstance("ZZ");
226: assertEquals("-05:00", fdf.format(date1));
227: assertEquals("-05:00", fdf.format(cal1));
228: assertEquals("-05:00", fdf.format(millis1));
229:
230: fdf = FastDateFormat.getInstance("ZZ");
231: assertEquals("-04:00", fdf.format(date2));
232: assertEquals("-04:00", fdf.format(cal2));
233: assertEquals("-04:00", fdf.format(millis2));
234:
235: String pattern = "GGGG GGG GG G yyyy yyy yy y MMMM MMM MM M"
236: + " dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
237: fdf = FastDateFormat.getInstance(pattern);
238: sdf = new SimpleDateFormat(pattern);
239: assertEquals(sdf.format(date1), fdf.format(date1));
240: assertEquals(sdf.format(date2), fdf.format(date2));
241:
242: } finally {
243: Locale.setDefault(realDefaultLocale);
244: TimeZone.setDefault(realDefaultZone);
245: }
246: }
247:
248: /**
249: * Test case for {@link FastDateFormat#getDateInstance(int, java.util.Locale)}.
250: */
251: public void testShortDateStyleWithLocales() {
252: Locale usLocale = Locale.US;
253: Locale swedishLocale = new Locale("sv", "SE");
254: Calendar cal = Calendar.getInstance();
255: cal.set(2004, 1, 3);
256: FastDateFormat fdf = FastDateFormat.getDateInstance(
257: FastDateFormat.SHORT, usLocale);
258: assertEquals("2/3/04", fdf.format(cal));
259:
260: fdf = FastDateFormat.getDateInstance(FastDateFormat.SHORT,
261: swedishLocale);
262: assertEquals("2004-02-03", fdf.format(cal));
263:
264: }
265:
266: /**
267: * Tests that pre-1000AD years get padded with yyyy
268: */
269: public void testLowYearPadding() {
270: Calendar cal = Calendar.getInstance();
271: FastDateFormat format = FastDateFormat
272: .getInstance("yyyy/MM/DD");
273:
274: cal.set(1, 0, 1);
275: assertEquals("0001/01/01", format.format(cal));
276: cal.set(10, 0, 1);
277: assertEquals("0010/01/01", format.format(cal));
278: cal.set(100, 0, 1);
279: assertEquals("0100/01/01", format.format(cal));
280: cal.set(999, 0, 1);
281: assertEquals("0999/01/01", format.format(cal));
282: }
283:
284: /**
285: * Show Bug #39410 is solved
286: */
287: public void testMilleniumBug() {
288: Calendar cal = Calendar.getInstance();
289: FastDateFormat format = FastDateFormat
290: .getInstance("dd.MM.yyyy");
291:
292: cal.set(1000, 0, 1);
293: assertEquals("01.01.1000", format.format(cal));
294: }
295:
296: /**
297: * testLowYearPadding showed that the date was buggy
298: * This test confirms it, getting 366 back as a date
299: */
300: // TODO: Fix this problem
301: public void testSimpleDate() {
302: Calendar cal = Calendar.getInstance();
303: FastDateFormat format = FastDateFormat
304: .getInstance("yyyy/MM/dd");
305:
306: cal.set(2004, 11, 31);
307: assertEquals("2004/12/31", format.format(cal));
308: cal.set(999, 11, 31);
309: assertEquals("0999/12/31", format.format(cal));
310: cal.set(1, 2, 2);
311: assertEquals("0001/03/02", format.format(cal));
312: }
313:
314: public void testLang303() {
315: Calendar cal = Calendar.getInstance();
316: cal.set(2004, 11, 31);
317:
318: FastDateFormat format = FastDateFormat
319: .getInstance("yyyy/MM/dd");
320: String output = format.format(cal);
321:
322: format = (FastDateFormat) SerializationUtils
323: .deserialize(SerializationUtils.serialize(format));
324: assertEquals(output, format.format(cal));
325: }
326: }
|