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.lang.reflect.Constructor;
020: import java.lang.reflect.Modifier;
021: import java.util.Calendar;
022: import java.util.Date;
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: /**
032: * TestCase for DateFormatUtils.
033: *
034: * @author Apache Ant - DateUtilsTest
035: * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
036: * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
037: * @author Stephen Colebourne
038: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
039: */
040: public class DateFormatUtilsTest extends TestCase {
041:
042: public static void main(String[] args) {
043: TestRunner.run(suite());
044: }
045:
046: public static Test suite() {
047: TestSuite suite = new TestSuite(DateFormatUtilsTest.class);
048: suite.setName("DateFormatUtils Tests");
049: return suite;
050: }
051:
052: public DateFormatUtilsTest(String s) {
053: super (s);
054: }
055:
056: //-----------------------------------------------------------------------
057: public void testConstructor() {
058: assertNotNull(new DateFormatUtils());
059: Constructor[] cons = DateFormatUtils.class
060: .getDeclaredConstructors();
061: assertEquals(1, cons.length);
062: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
063: assertEquals(true, Modifier.isPublic(DateFormatUtils.class
064: .getModifiers()));
065: assertEquals(false, Modifier.isFinal(DateFormatUtils.class
066: .getModifiers()));
067: }
068:
069: //-----------------------------------------------------------------------
070: public void testFormat() {
071: Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
072: c.set(2005, 0, 1, 12, 0, 0);
073: c.setTimeZone(TimeZone.getDefault());
074: StringBuffer buffer = new StringBuffer();
075: int year = c.get(Calendar.YEAR);
076: int month = c.get(Calendar.MONTH) + 1;
077: int day = c.get(Calendar.DAY_OF_MONTH);
078: int hour = c.get(Calendar.HOUR_OF_DAY);
079: buffer.append(year);
080: buffer.append(month);
081: buffer.append(day);
082: buffer.append(hour);
083: assertEquals(buffer.toString(), DateFormatUtils.format(c
084: .getTime(), "yyyyMdH"));
085:
086: assertEquals(buffer.toString(), DateFormatUtils.format(c
087: .getTime().getTime(), "yyyyMdH"));
088:
089: assertEquals(buffer.toString(), DateFormatUtils.format(c
090: .getTime(), "yyyyMdH", Locale.US));
091:
092: assertEquals(buffer.toString(), DateFormatUtils.format(c
093: .getTime().getTime(), "yyyyMdH", Locale.US));
094: }
095:
096: public void testFormatUTC() {
097: Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
098: c.set(2005, 0, 1, 12, 0, 0);
099: assertEquals("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c
100: .getTime(), DateFormatUtils.ISO_DATETIME_FORMAT
101: .getPattern()));
102:
103: assertEquals("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c
104: .getTime().getTime(),
105: DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
106:
107: assertEquals("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c
108: .getTime(), DateFormatUtils.ISO_DATETIME_FORMAT
109: .getPattern(), Locale.US));
110:
111: assertEquals("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c
112: .getTime().getTime(),
113: DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(),
114: Locale.US));
115: }
116:
117: public void testDateTimeISO() {
118: TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
119: Calendar cal = Calendar.getInstance(timeZone);
120: cal.set(2002, 1, 23, 9, 11, 12);
121: String text = DateFormatUtils.format(cal.getTime(),
122: DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(),
123: timeZone);
124: assertEquals("2002-02-23T09:11:12", text);
125: text = DateFormatUtils.format(cal.getTime().getTime(),
126: DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(),
127: timeZone);
128: assertEquals("2002-02-23T09:11:12", text);
129: text = DateFormatUtils.ISO_DATETIME_FORMAT.format(cal);
130: assertEquals("2002-02-23T09:11:12", text);
131:
132: text = DateFormatUtils.format(cal.getTime(),
133: DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
134: .getPattern(), timeZone);
135: assertEquals("2002-02-23T09:11:12-03:00", text);
136: text = DateFormatUtils.format(cal.getTime().getTime(),
137: DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
138: .getPattern(), timeZone);
139: assertEquals("2002-02-23T09:11:12-03:00", text);
140: text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
141: .format(cal);
142: assertEquals("2002-02-23T09:11:12-03:00", text);
143: }
144:
145: public void testDateISO() {
146: TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
147: Calendar cal = Calendar.getInstance(timeZone);
148: cal.set(2002, 1, 23, 10, 11, 12);
149: String text = DateFormatUtils.format(cal.getTime(),
150: DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
151: assertEquals("2002-02-23", text);
152: text = DateFormatUtils.format(cal.getTime().getTime(),
153: DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
154: assertEquals("2002-02-23", text);
155: text = DateFormatUtils.ISO_DATE_FORMAT.format(cal);
156: assertEquals("2002-02-23", text);
157:
158: text = DateFormatUtils.format(cal.getTime(),
159: DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(),
160: timeZone);
161: assertEquals("2002-02-23-03:00", text);
162: text = DateFormatUtils.format(cal.getTime().getTime(),
163: DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(),
164: timeZone);
165: assertEquals("2002-02-23-03:00", text);
166: text = DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(cal);
167: assertEquals("2002-02-23-03:00", text);
168: }
169:
170: public void testTimeISO() {
171: TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
172: Calendar cal = Calendar.getInstance(timeZone);
173: cal.set(2002, 1, 23, 10, 11, 12);
174: String text = DateFormatUtils.format(cal.getTime(),
175: DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
176: assertEquals("T10:11:12", text);
177: text = DateFormatUtils.format(cal.getTime().getTime(),
178: DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
179: assertEquals("T10:11:12", text);
180: text = DateFormatUtils.ISO_TIME_FORMAT.format(cal);
181: assertEquals("T10:11:12", text);
182:
183: text = DateFormatUtils.format(cal.getTime(),
184: DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(),
185: timeZone);
186: assertEquals("T10:11:12-03:00", text);
187: text = DateFormatUtils.format(cal.getTime().getTime(),
188: DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(),
189: timeZone);
190: assertEquals("T10:11:12-03:00", text);
191: text = DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(cal);
192: assertEquals("T10:11:12-03:00", text);
193: }
194:
195: public void testTimeNoTISO() {
196: TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
197: Calendar cal = Calendar.getInstance(timeZone);
198: cal.set(2002, 1, 23, 10, 11, 12);
199: String text = DateFormatUtils.format(cal.getTime(),
200: DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(),
201: timeZone);
202: assertEquals("10:11:12", text);
203: text = DateFormatUtils.format(cal.getTime().getTime(),
204: DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(),
205: timeZone);
206: assertEquals("10:11:12", text);
207: text = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(cal);
208: assertEquals("10:11:12", text);
209:
210: text = DateFormatUtils.format(cal.getTime(),
211: DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT
212: .getPattern(), timeZone);
213: assertEquals("10:11:12-03:00", text);
214: text = DateFormatUtils.format(cal.getTime().getTime(),
215: DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT
216: .getPattern(), timeZone);
217: assertEquals("10:11:12-03:00", text);
218: text = DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT
219: .format(cal);
220: assertEquals("10:11:12-03:00", text);
221: }
222:
223: public void testSMTP() {
224: TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
225: Calendar cal = Calendar.getInstance(timeZone);
226: cal.set(2003, 5, 8, 10, 11, 12);
227: String text = DateFormatUtils.format(cal.getTime(),
228: DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
229: timeZone, DateFormatUtils.SMTP_DATETIME_FORMAT
230: .getLocale());
231: assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
232: text = DateFormatUtils.format(cal.getTime().getTime(),
233: DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
234: timeZone, DateFormatUtils.SMTP_DATETIME_FORMAT
235: .getLocale());
236: assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
237: text = DateFormatUtils.SMTP_DATETIME_FORMAT.format(cal);
238: assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
239:
240: // format UTC
241: text = DateFormatUtils.formatUTC(cal.getTime().getTime(),
242: DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
243: DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
244: assertEquals("Sun, 08 Jun 2003 13:11:12 +0000", text);
245: }
246:
247: /*
248: public void testLang312() {
249: String pattern = "dd/MM/yyyy";
250: String expected = "19/04/1948";
251: TimeZone timeZone = TimeZone.getTimeZone("CET");
252: Locale locale = Locale.GERMANY;
253:
254: // show Calendar is good
255: Calendar cal = Calendar.getInstance(timeZone, locale);
256: cal.set(1948, 3, 19);
257: assertEquals(expected, DateFormatUtils.format( cal.getTime(), pattern, timeZone, locale ) );
258:
259: Date date = new Date(48, 3, 19);
260:
261: // test JDK
262: java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern, locale);
263: sdf.setTimeZone(timeZone);
264: // There's nothing we can do if the JDK fails, so just going to pring a warning in this case
265: // assertEquals(expected, sdf.format( date ) );
266: if( ! expected.equals( sdf.format( date ) ) ) {
267: System.out.println("WARNING: JDK test failed - testLang312()");
268: }
269:
270: // test Commons
271: assertEquals(expected, DateFormatUtils.format( date, pattern, timeZone, locale ) );
272: }
273: */
274:
275: }
|