Home » commons-lang-2.4-src » org.apache.commons » lang » time » [javadoc | source]
    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.lang.reflect.Constructor;
   19   import java.lang.reflect.Modifier;
   20   import java.util.Calendar;
   21   import java.util.TimeZone;
   22   
   23   import junit.framework.Test;
   24   import junit.framework.TestCase;
   25   import junit.framework.TestSuite;
   26   import junit.textui.TestRunner;
   27   
   28   /**
   29    * TestCase for DateFormatUtils.
   30    *
   31    * @author Apache Ant - DateUtilsTest
   32    * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
   33    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   34    * @author Stephen Colebourne
   35    * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
   36    */
   37   public class DateFormatUtilsTest extends TestCase {
   38   
   39       public static void main(String[] args) {
   40           TestRunner.run(suite());
   41       }
   42   
   43       public static Test suite() {
   44           TestSuite suite = new TestSuite(DateFormatUtilsTest.class);
   45           suite.setName("DateFormatUtils Tests");
   46           return suite;
   47       }
   48   
   49       public DateFormatUtilsTest(String s) {
   50           super(s);
   51       }
   52   
   53       //-----------------------------------------------------------------------
   54       public void testConstructor() {
   55           assertNotNull(new DateFormatUtils());
   56           Constructor[] cons = DateFormatUtils.class.getDeclaredConstructors();
   57           assertEquals(1, cons.length);
   58           assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
   59           assertEquals(true, Modifier.isPublic(DateFormatUtils.class.getModifiers()));
   60           assertEquals(false, Modifier.isFinal(DateFormatUtils.class.getModifiers()));
   61       }
   62       
   63       //-----------------------------------------------------------------------
   64       public void testDateTimeISO(){
   65           TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
   66           Calendar cal = Calendar.getInstance(timeZone);
   67           cal.set(2002,1,23,9,11,12);
   68           String text = DateFormatUtils.format(cal.getTime(), 
   69                           DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
   70           assertEquals("2002-02-23T09:11:12", text);
   71           text = DateFormatUtils.format(cal.getTime().getTime(), 
   72                         DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
   73           assertEquals("2002-02-23T09:11:12", text);
   74           text = DateFormatUtils.ISO_DATETIME_FORMAT.format(cal);
   75           assertEquals("2002-02-23T09:11:12", text);
   76           
   77           text = DateFormatUtils.format(cal.getTime(), 
   78                         DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
   79           assertEquals("2002-02-23T09:11:12-03:00", text);
   80           text = DateFormatUtils.format(cal.getTime().getTime(), 
   81                         DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
   82           assertEquals("2002-02-23T09:11:12-03:00", text);
   83           text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(cal);
   84           assertEquals("2002-02-23T09:11:12-03:00", text);
   85       }
   86   
   87       public void testDateISO(){
   88           TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
   89           Calendar cal = Calendar.getInstance(timeZone);
   90           cal.set(2002,1,23,10,11,12);
   91           String text = DateFormatUtils.format(cal.getTime(), 
   92                           DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
   93           assertEquals("2002-02-23", text);
   94           text = DateFormatUtils.format(cal.getTime().getTime(), 
   95                           DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
   96           assertEquals("2002-02-23", text);
   97           text = DateFormatUtils.ISO_DATE_FORMAT.format(cal);
   98           assertEquals("2002-02-23", text);
   99           
  100           text = DateFormatUtils.format(cal.getTime(), 
  101                         DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
  102           assertEquals("2002-02-23-03:00", text);
  103           text = DateFormatUtils.format(cal.getTime().getTime(), 
  104                         DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
  105           assertEquals("2002-02-23-03:00", text);
  106           text = DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(cal);
  107           assertEquals("2002-02-23-03:00", text);
  108       }
  109   
  110       public void testTimeISO(){
  111           TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
  112           Calendar cal = Calendar.getInstance(timeZone);
  113           cal.set(2002,1,23,10,11,12);
  114           String text = DateFormatUtils.format(cal.getTime(), 
  115                           DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
  116           assertEquals("T10:11:12", text);
  117           text = DateFormatUtils.format(cal.getTime().getTime(), 
  118                           DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
  119           assertEquals("T10:11:12", text);
  120           text = DateFormatUtils.ISO_TIME_FORMAT.format(cal);
  121           assertEquals("T10:11:12", text);
  122           
  123           text = DateFormatUtils.format(cal.getTime(), 
  124                         DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
  125           assertEquals("T10:11:12-03:00", text);
  126           text = DateFormatUtils.format(cal.getTime().getTime(), 
  127                         DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
  128           assertEquals("T10:11:12-03:00", text);
  129           text = DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(cal);
  130           assertEquals("T10:11:12-03:00", text);
  131       }
  132   
  133       public void testTimeNoTISO(){
  134           TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
  135           Calendar cal = Calendar.getInstance(timeZone);
  136           cal.set(2002,1,23,10,11,12);
  137           String text = DateFormatUtils.format(cal.getTime(), 
  138                           DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
  139           assertEquals("10:11:12", text);
  140           text = DateFormatUtils.format(cal.getTime().getTime(), 
  141                           DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
  142           assertEquals("10:11:12", text);
  143           text = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(cal);
  144           assertEquals("10:11:12", text);
  145           
  146           text = DateFormatUtils.format(cal.getTime(), 
  147                         DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
  148           assertEquals("10:11:12-03:00", text);
  149           text = DateFormatUtils.format(cal.getTime().getTime(), 
  150                         DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
  151           assertEquals("10:11:12-03:00", text);
  152           text = DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(cal);
  153           assertEquals("10:11:12-03:00", text);
  154       }
  155   
  156       public void testSMTP(){
  157           TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
  158           Calendar cal = Calendar.getInstance(timeZone);
  159           cal.set(2003,5,8,10,11,12);
  160           String text = DateFormatUtils.format(cal.getTime(), 
  161                           DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
  162                           DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
  163           assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
  164           text = DateFormatUtils.format(cal.getTime().getTime(), 
  165                           DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
  166                           DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
  167           assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
  168           text = DateFormatUtils.SMTP_DATETIME_FORMAT.format(cal);
  169           assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
  170           
  171           // format UTC
  172           text = DateFormatUtils.formatUTC(cal.getTime().getTime(), 
  173                           DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
  174                           DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
  175           assertEquals("Sun, 08 Jun 2003 13:11:12 +0000", text);
  176       }
  177   
  178   }

Save This Page
Home » commons-lang-2.4-src » org.apache.commons » lang » time » [javadoc | source]