commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie TestDateParser.java TestCookieAll.java
Date Fri, 24 Dec 2004 20:36:13 GMT
olegk       2004/12/24 12:36:13

  Modified:    httpclient/src/java/org/apache/commons/httpclient/util
                        DateUtil.java
               httpclient/src/test/org/apache/commons/httpclient/cookie
                        TestCookieAll.java
  Added:       httpclient/src/test/org/apache/commons/httpclient/cookie
                        TestDateParser.java
  Log:
  PR #32835 (cookies > 20 years invalidated)
  
  Contributed by Oleg Kalnichevski
  Reviewed by Michael Becke
  
  Revision  Changes    Path
  1.2       +38 -5     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateUtil.java
  
  Index: DateUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateUtil.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DateUtil.java	6 Nov 2004 19:15:42 -0000	1.1
  +++ DateUtil.java	24 Dec 2004 20:36:13 -0000	1.2
  @@ -32,6 +32,7 @@
   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;
  @@ -73,6 +74,14 @@
       private static final Collection DEFAULT_PATTERNS = Arrays.asList(
       		new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
       
  +    private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
  +    
  +    static {
  +        Calendar calendar = Calendar.getInstance();
  +        calendar.set(2000, Calendar.JANUARY, 1, 0, 0);
  +        DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); 
  +    }
  +    
       /**
        * Parses a date value.  The formats used for parsing the date value are retrieved
from
        * the default http params.
  @@ -85,7 +94,22 @@
        * supported date formats
        */
       public static Date parseDate(String dateValue) throws DateParseException {
  -        return parseDate(dateValue, null);
  +        return parseDate(dateValue, null, null);
  +    }
  +    
  +    /**
  +     * Parses the date value using the given date formats.
  +     * 
  +     * @param dateValue the date value to parse
  +     * @param dateFormats the date formats to use
  +     * 
  +     * @return the parsed date
  +     * 
  +     * @throws DateParseException if none of the dataFormats could parse the dateValue
  +     */
  +    public static Date parseDate(String dateValue, Collection dateFormats) 
  +        throws DateParseException {
  +        return parseDate(dateValue, dateFormats, null);
       }
       
       /**
  @@ -93,6 +117,10 @@
        * 
        * @param dateValue the date value to parse
        * @param dateFormats the date formats to use
  +     * @param startDate During parsing, two digit years will be placed in the range 
  +     * <code>startDate</code> to <code>startDate + 100 years</code>.
This value may 
  +     * be <code>null</code>. When <code>null</code> is given as
a parameter, year 
  +     * <code>2000</code> will be used. 
        * 
        * @return the parsed date
        * 
  @@ -100,7 +128,8 @@
        */
       public static Date parseDate(
           String dateValue, 
  -        Collection dateFormats
  +        Collection dateFormats,
  +        Date startDate 
       ) throws DateParseException {
           
           if (dateValue == null) {
  @@ -109,6 +138,9 @@
           if (dateFormats == null) {
           	dateFormats = DEFAULT_PATTERNS;
           }
  +        if (startDate == null) {
  +            startDate = DEFAULT_TWO_DIGIT_YEAR_START;
  +        }
           // trim single quotes around date if present
           // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279
           if (dateValue.length() > 1 
  @@ -126,6 +158,7 @@
               if (dateParser == null) {
                   dateParser = new SimpleDateFormat(format, Locale.US);
                   dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
  +                dateParser.set2DigitYearStart(startDate);
               } else {
                   dateParser.applyPattern(format);                    
               }
  
  
  
  1.3       +5 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java
  
  Index: TestCookieAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestCookieAll.java	25 Apr 2004 12:25:09 -0000	1.2
  +++ TestCookieAll.java	24 Dec 2004 20:36:13 -0000	1.3
  @@ -48,6 +48,7 @@
           suite.addTest(TestCookieNetscapeDraft.suite());
           suite.addTest(TestCookieIgnoreSpec.suite());
           suite.addTest(TestCookiePolicy.suite());
  +        suite.addTest(TestDateParser.suite());
           return suite;
       }
   
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestDateParser.java
  
  Index: TestDateParser.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestDateParser.java,v
1.1 2004/12/24 20:36:13 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/12/24 20:36:13 $
   * ====================================================================
   *
   *  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.commons.httpclient.cookie;
  
  import java.util.ArrayList;
  import java.util.Calendar;
  import java.util.List;
  
  import org.apache.commons.httpclient.util.DateUtil;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  
  /**
   * Test cases for expiry date parsing
   *
   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
   * 
   * @version $Revision: 1.1 $
   */
  public class TestDateParser extends TestCase {
  
      // ------------------------------------------------------------ Constructor
  
      public TestDateParser(String name) {
          super(name);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestDateParser.class);
      }
  
      private static final String PATTERN = "EEE, dd-MMM-yy HH:mm:ss zzz";
      private static final List PATTERNS = new ArrayList();
      
      static {
          PATTERNS.add(PATTERN);
      }
      
      public void testFourDigitYear() throws Exception {
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-2004 24:00:00 CET", PATTERNS));
          assertEquals(2004, calendar.get(Calendar.YEAR));
      }
  
      public void testThreeDigitYear() throws Exception {
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-994 24:00:00 CET", PATTERNS));
          assertEquals(994, calendar.get(Calendar.YEAR));
      }
  
      public void testTwoDigitYear() throws Exception {
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-04 24:00:00 CET", PATTERNS));
          assertEquals(2004, calendar.get(Calendar.YEAR));
  
          calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-94 24:00:00 CET", PATTERNS));
          assertEquals(2094, calendar.get(Calendar.YEAR));
      }
  
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message