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.lang.reflect.Constructor;
20  import java.lang.reflect.Modifier;
21  import java.util.Calendar;
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   * TestCase for DateFormatUtils.
32   *
33   * @author Apache Ant - DateUtilsTest
34   * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
35   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
36   * @author Stephen Colebourne
37   * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
38   */
39  public class DateFormatUtilsTest extends TestCase {
40  
41      public static void main(String[] args) {
42          TestRunner.run(suite());
43      }
44  
45      public static Test suite() {
46          TestSuite suite = new TestSuite(DateFormatUtilsTest.class);
47          suite.setName("DateFormatUtils Tests");
48          return suite;
49      }
50  
51      public DateFormatUtilsTest(String s) {
52          super(s);
53      }
54  
55      //-----------------------------------------------------------------------
56      public void testConstructor() {
57          assertNotNull(new DateFormatUtils());
58          Constructor[] cons = DateFormatUtils.class.getDeclaredConstructors();
59          assertEquals(1, cons.length);
60          assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
61          assertEquals(true, Modifier.isPublic(DateFormatUtils.class.getModifiers()));
62          assertEquals(false, Modifier.isFinal(DateFormatUtils.class.getModifiers()));
63      }
64      
65      //-----------------------------------------------------------------------
66      public void testFormat() {
67          Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
68          c.set(2005,0,1,12,0,0);
69          c.setTimeZone(TimeZone.getDefault());
70          StringBuffer buffer = new StringBuffer ();
71          int year = c.get(Calendar.YEAR);
72          int month = c.get(Calendar.MONTH) + 1;
73          int day = c.get(Calendar.DAY_OF_MONTH);
74          int hour = c.get(Calendar.HOUR_OF_DAY);
75          buffer.append (year);
76          buffer.append(month);
77          buffer.append(day);
78          buffer.append(hour);
79          assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
80          
81          assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH"));
82          
83          assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
84          
85          assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH", Locale.US));
86      }
87      
88      //-----------------------------------------------------------------------
89      public void testFormatCalendar() {
90          Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
91          c.set(2005,0,1,12,0,0);
92          c.setTimeZone(TimeZone.getDefault());
93          StringBuffer buffer = new StringBuffer ();
94          int year = c.get(Calendar.YEAR);
95          int month = c.get(Calendar.MONTH) + 1;
96          int day = c.get(Calendar.DAY_OF_MONTH);
97          int hour = c.get(Calendar.HOUR_OF_DAY);
98          buffer.append (year);
99          buffer.append(month);
100         buffer.append(day);
101         buffer.append(hour);
102         assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH"));
103         
104         assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
105         
106         assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH", Locale.US));
107         
108         assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
109     }
110     
111     public void testFormatUTC() {
112         Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
113         c.set(2005,0,1,12,0,0);
114         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
115         
116         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
117         
118         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
119         
120         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
121     }
122     
123     public void testDateTimeISO(){
124         TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
125         Calendar cal = Calendar.getInstance(timeZone);
126         cal.set(2002,1,23,9,11,12);
127         String text = DateFormatUtils.format(cal.getTime(), 
128                         DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
129         assertEquals("2002-02-23T09:11:12", text);
130         text = DateFormatUtils.format(cal.getTime().getTime(), 
131                       DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
132         assertEquals("2002-02-23T09:11:12", text);
133         text = DateFormatUtils.ISO_DATETIME_FORMAT.format(cal);
134         assertEquals("2002-02-23T09:11:12", text);
135         
136         text = DateFormatUtils.format(cal.getTime(), 
137                       DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
138         assertEquals("2002-02-23T09:11:12-03:00", text);
139         text = DateFormatUtils.format(cal.getTime().getTime(), 
140                       DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
141         assertEquals("2002-02-23T09:11:12-03:00", text);
142         text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(cal);
143         assertEquals("2002-02-23T09:11:12-03:00", text);
144     }
145 
146     public void testDateISO(){
147         TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
148         Calendar cal = Calendar.getInstance(timeZone);
149         cal.set(2002,1,23,10,11,12);
150         String text = DateFormatUtils.format(cal.getTime(), 
151                         DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
152         assertEquals("2002-02-23", text);
153         text = DateFormatUtils.format(cal.getTime().getTime(), 
154                         DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
155         assertEquals("2002-02-23", text);
156         text = DateFormatUtils.ISO_DATE_FORMAT.format(cal);
157         assertEquals("2002-02-23", text);
158         
159         text = DateFormatUtils.format(cal.getTime(), 
160                       DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
161         assertEquals("2002-02-23-03:00", text);
162         text = DateFormatUtils.format(cal.getTime().getTime(), 
163                       DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
164         assertEquals("2002-02-23-03:00", text);
165         text = DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(cal);
166         assertEquals("2002-02-23-03:00", text);
167     }
168 
169     public void testTimeISO(){
170         TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
171         Calendar cal = Calendar.getInstance(timeZone);
172         cal.set(2002,1,23,10,11,12);
173         String text = DateFormatUtils.format(cal.getTime(), 
174                         DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
175         assertEquals("T10:11:12", text);
176         text = DateFormatUtils.format(cal.getTime().getTime(), 
177                         DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
178         assertEquals("T10:11:12", text);
179         text = DateFormatUtils.ISO_TIME_FORMAT.format(cal);
180         assertEquals("T10:11:12", text);
181         
182         text = DateFormatUtils.format(cal.getTime(), 
183                       DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
184         assertEquals("T10:11:12-03:00", text);
185         text = DateFormatUtils.format(cal.getTime().getTime(), 
186                       DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
187         assertEquals("T10:11:12-03:00", text);
188         text = DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(cal);
189         assertEquals("T10:11:12-03:00", text);
190     }
191 
192     public void testTimeNoTISO(){
193         TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
194         Calendar cal = Calendar.getInstance(timeZone);
195         cal.set(2002,1,23,10,11,12);
196         String text = DateFormatUtils.format(cal.getTime(), 
197                         DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
198         assertEquals("10:11:12", text);
199         text = DateFormatUtils.format(cal.getTime().getTime(), 
200                         DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
201         assertEquals("10:11:12", text);
202         text = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(cal);
203         assertEquals("10:11:12", text);
204         
205         text = DateFormatUtils.format(cal.getTime(), 
206                       DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
207         assertEquals("10:11:12-03:00", text);
208         text = DateFormatUtils.format(cal.getTime().getTime(), 
209                       DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
210         assertEquals("10:11:12-03:00", text);
211         text = DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(cal);
212         assertEquals("10:11:12-03:00", text);
213     }
214 
215     public void testSMTP(){
216         TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
217         Calendar cal = Calendar.getInstance(timeZone);
218         cal.set(2003,5,8,10,11,12);
219         String text = DateFormatUtils.format(cal.getTime(), 
220                         DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
221                         DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
222         assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
223         text = DateFormatUtils.format(cal.getTime().getTime(), 
224                         DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
225                         DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
226         assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
227         text = DateFormatUtils.SMTP_DATETIME_FORMAT.format(cal);
228         assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
229         
230         // format UTC
231         text = DateFormatUtils.formatUTC(cal.getTime().getTime(), 
232                         DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
233                         DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
234         assertEquals("Sun, 08 Jun 2003 13:11:12 +0000", text);
235     }
236 
237     /*
238     public void testLang312() {
239         String pattern = "dd/MM/yyyy";
240         String expected = "19/04/1948";
241         TimeZone timeZone = TimeZone.getTimeZone("CET");
242         Locale locale = Locale.GERMANY;
243 
244         // show Calendar is good
245         Calendar cal = Calendar.getInstance(timeZone, locale);
246         cal.set(1948, 3, 19);
247         assertEquals(expected, DateFormatUtils.format( cal.getTime(), pattern, timeZone, locale ) );
248 
249         Date date = new Date(48, 3, 19);
250 
251         // test JDK
252         java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern, locale);
253         sdf.setTimeZone(timeZone);
254 // There's nothing we can do if the JDK fails, so just going to pring a warning in this case
255 //        assertEquals(expected, sdf.format( date ) );
256         if( ! expected.equals( sdf.format( date ) ) ) {
257             System.out.println("WARNING: JDK test failed - testLang312()");
258         }
259 
260         // test Commons
261         assertEquals(expected, DateFormatUtils.format( date, pattern, timeZone, locale ) );
262     }
263     */
264 
265 }