You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/10/15 20:52:48 UTC

svn commit: r321444 - in /jakarta/httpclient/trunk/http-common/src: java/org/apache/http/util/DateUtils.java test/org/apache/http/util/TestAllUtil.java test/org/apache/http/util/TestDateUtils.java

Author: olegk
Date: Sat Oct 15 11:52:40 2005
New Revision: 321444

URL: http://svn.apache.org/viewcvs?rev=321444&view=rev
Log:
Test cases for the DateUtils class

Added:
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java   (with props)
Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/DateUtils.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/DateUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/DateUtils.java?rev=321444&r1=321443&r2=321444&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/DateUtils.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/DateUtils.java Sat Oct 15 11:52:40 2005
@@ -31,11 +31,8 @@
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.Arrays;
 import java.util.Calendar;
-import java.util.Collection;
 import java.util.Date;
-import java.util.Iterator;
 import java.util.Locale;
 import java.util.TimeZone;
 
@@ -65,19 +62,23 @@
      */
     public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
 
-    private static final Collection DEFAULT_PATTERNS = Arrays.asList(
-    		new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
+    private static final String[] DEFAULT_PATTERNS = new String[] { 
+    	PATTERN_ASCTIME, 
+    	PATTERN_RFC1036, 
+    	PATTERN_RFC1123 };
     
     private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
     
+    public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
+    
     static {
         Calendar calendar = Calendar.getInstance();
-        calendar.set(2000, Calendar.JANUARY, 1, 0, 0);
+        calendar.setTimeZone(GMT);
+        calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
         DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); 
     }
     
-    public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
-    
     /**
      * Parses a date value.  The formats used for parsing the date value are retrieved from
      * the default http params.
@@ -103,7 +104,7 @@
      * 
      * @throws DateParseException if none of the dataFormats could parse the dateValue
      */
-    public static Date parseDate(String dateValue, Collection dateFormats) 
+    public static Date parseDate(final String dateValue, String[] dateFormats) 
         throws DateParseException {
         return parseDate(dateValue, dateFormats, null);
     }
@@ -124,7 +125,7 @@
      */
     public static Date parseDate(
         String dateValue, 
-        Collection dateFormats,
+        String[] dateFormats,
         Date startDate 
     ) throws DateParseException {
         
@@ -147,16 +148,13 @@
         }
         
         SimpleDateFormat dateParser = null;        
-        Iterator formatIter = dateFormats.iterator();
-        
-        while (formatIter.hasNext()) {
-            String format = (String) formatIter.next();            
+        for (int i = 0; i < dateFormats.length; i++) {
             if (dateParser == null) {
-                dateParser = new SimpleDateFormat(format, Locale.US);
+                dateParser = new SimpleDateFormat(dateFormats[i], Locale.US);
                 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
                 dateParser.set2DigitYearStart(startDate);
             } else {
-                dateParser.applyPattern(format);                    
+                dateParser.applyPattern(dateFormats[i]);                    
             }
             try {
                 return dateParser.parse(dateValue);
@@ -204,6 +202,7 @@
     }
     
     /** This class should not be instantiated. */    
-    private DateUtils() { }
+    private DateUtils() { 
+    }
     
 }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java?rev=321444&r1=321443&r2=321444&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java Sat Oct 15 11:52:40 2005
@@ -46,6 +46,7 @@
         suite.addTest(TestHeadersParser.suite());
         suite.addTest(TestByteArrayBuffer.suite());
         suite.addTest(TestCharArrayBuffer.suite());
+        suite.addTest(TestDateUtils.suite());
         return suite;
     }
 

Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java?rev=321444&view=auto
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java Sat Oct 15 11:52:40 2005
@@ -0,0 +1,158 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * 
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.util;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link DateUtils}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestDateUtils extends TestCase {
+
+    public TestDateUtils(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestDateUtils.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestDateUtils.class);
+    }
+
+    public void testBasicDateParse() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeZone(DateUtils.GMT);
+        calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        Date date1 = calendar.getTime(); 
+        
+    	String[] formats = new String[] {
+    			DateUtils.PATTERN_RFC1123		
+    			};
+    	Date date2 = DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", formats, null);
+    	assertEquals(date1, date2);
+    	date2 = DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", formats);
+    	assertEquals(date1, date2);
+    	date2 = DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT");
+    	assertEquals(date1, date2);
+    }
+
+    public void testInvalidInput() throws Exception {
+        try {
+        	DateUtils.parseDate(null, null, null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+        	// expected
+        }
+        try {
+        	DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", new String[] {}, null);
+            fail("DateParseException should habe been thrown");
+        } catch (DateParseException ex) {
+        	// expected
+        }
+        try {
+        	DateUtils.formatDate(null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+        	// expected
+        }
+        try {
+        	DateUtils.formatDate(new Date(), null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+        	// expected
+        }
+    }
+    
+    public void testTwoDigitYearDateParse() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeZone(DateUtils.GMT);
+        calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        Date date1 = calendar.getTime(); 
+        
+    	String[] formats = new String[] {
+    			DateUtils.PATTERN_RFC1036		
+    			};
+    	Date date2 = DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", formats, null);
+    	assertEquals(date1, date2);
+
+        calendar.set(1900, Calendar.JANUARY, 0, 0, 0, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        Date startDate = calendar.getTime(); 
+
+        calendar.set(1905, Calendar.OCTOBER, 14, 0, 0, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        date1 = calendar.getTime(); 
+    	
+    	date2 = DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", formats, startDate);
+    	assertEquals(date1, date2);
+    }
+
+    public void testParseQuotedDate() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeZone(DateUtils.GMT);
+        calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        Date date1 = calendar.getTime(); 
+        
+    	String[] formats = new String[] {
+    			DateUtils.PATTERN_RFC1123		
+    			};
+    	Date date2 = DateUtils.parseDate("'Fri, 14 Oct 2005 00:00:00 GMT'", formats);
+    	assertEquals(date1, date2);
+    }
+
+    public void testBasicDateFormat() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeZone(DateUtils.GMT);
+        calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        Date date = calendar.getTime(); 
+        
+        assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(date));
+        assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(date, DateUtils.PATTERN_RFC1123));
+    }
+
+    public void testConstructor() {
+        new DateParseException();
+        new DateParseException("Oppsie");
+    }
+}

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestDateUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain