View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.client.utils;
29  
30  import java.util.Calendar;
31  import java.util.Date;
32  
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  /**
37   * Unit tests for {@link DateUtils}.
38   */
39  public class TestDateUtils {
40  
41      @Test
42      public void testBasicDateParse() throws Exception {
43          final Calendar calendar = Calendar.getInstance();
44          calendar.setTimeZone(DateUtils.GMT);
45          calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
46          calendar.set(Calendar.MILLISECOND, 0);
47          final Date date1 = calendar.getTime();
48  
49          final String[] formats = new String[] {
50                  DateUtils.PATTERN_RFC1123
51                  };
52          Date date2 = DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", formats, null);
53          Assert.assertEquals(date1, date2);
54          date2 = DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", formats);
55          Assert.assertEquals(date1, date2);
56          date2 = DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT");
57          Assert.assertEquals(date1, date2);
58      }
59  
60      @Test
61      public void testMalformedDate() {
62          Assert.assertNull(DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", new String[] {}, null));
63      }
64  
65      @Test
66      public void testInvalidInput() throws Exception {
67          try {
68              DateUtils.parseDate(null, null, null);
69              Assert.fail("IllegalArgumentException should habe been thrown");
70          } catch (final IllegalArgumentException ex) {
71              // expected
72          }
73          try {
74              DateUtils.formatDate(null);
75              Assert.fail("IllegalArgumentException should habe been thrown");
76          } catch (final IllegalArgumentException ex) {
77              // expected
78          }
79          try {
80              DateUtils.formatDate(new Date(), null);
81              Assert.fail("IllegalArgumentException should habe been thrown");
82          } catch (final IllegalArgumentException ex) {
83              // expected
84          }
85      }
86  
87      @Test
88      public void testTwoDigitYearDateParse() throws Exception {
89          final Calendar calendar = Calendar.getInstance();
90          calendar.setTimeZone(DateUtils.GMT);
91          calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
92          calendar.set(Calendar.MILLISECOND, 0);
93          Date date1 = calendar.getTime();
94  
95          final String[] formats = new String[] {
96                  DateUtils.PATTERN_RFC1036
97                  };
98          Date date2 = DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", formats, null);
99          Assert.assertEquals(date1, date2);
100 
101         calendar.set(1900, Calendar.JANUARY, 0, 0, 0, 0);
102         calendar.set(Calendar.MILLISECOND, 0);
103         final Date startDate = calendar.getTime();
104 
105         calendar.set(1905, Calendar.OCTOBER, 14, 0, 0, 0);
106         calendar.set(Calendar.MILLISECOND, 0);
107         date1 = calendar.getTime();
108 
109         date2 = DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", formats, startDate);
110         Assert.assertEquals(date1, date2);
111     }
112 
113     @Test
114     public void testParseQuotedDate() throws Exception {
115         final Calendar calendar = Calendar.getInstance();
116         calendar.setTimeZone(DateUtils.GMT);
117         calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
118         calendar.set(Calendar.MILLISECOND, 0);
119         final Date date1 = calendar.getTime();
120 
121         final String[] formats = new String[] {
122                 DateUtils.PATTERN_RFC1123
123                 };
124         final Date date2 = DateUtils.parseDate("'Fri, 14 Oct 2005 00:00:00 GMT'", formats);
125         Assert.assertEquals(date1, date2);
126     }
127 
128     @Test
129     public void testBasicDateFormat() throws Exception {
130         final Calendar calendar = Calendar.getInstance();
131         calendar.setTimeZone(DateUtils.GMT);
132         calendar.set(2005, Calendar.OCTOBER, 14, 0, 0, 0);
133         calendar.set(Calendar.MILLISECOND, 0);
134         final Date date = calendar.getTime();
135 
136         Assert.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(date));
137         Assert.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(date, DateUtils.PATTERN_RFC1123));
138     }
139 
140 }