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.hc.client5.http.utils;
29  
30  import java.time.Instant;
31  import java.time.LocalDate;
32  import java.time.Month;
33  import java.time.ZoneId;
34  import java.time.format.DateTimeFormatter;
35  import java.time.temporal.ChronoUnit;
36  import java.util.Date;
37  
38  import org.apache.hc.core5.http.HttpHeaders;
39  import org.apache.hc.core5.http.message.BasicHeader;
40  import org.apache.hc.core5.http.message.HeaderGroup;
41  import org.junit.jupiter.api.Assertions;
42  import org.junit.jupiter.api.Test;
43  
44  /**
45   * Unit tests for {@link DateUtils}.
46   */
47  public class TestDateUtils {
48  
49      private static Instant createInstant(final int year, final Month month, final int day) {
50          return LocalDate.of(year, month, day).atStartOfDay(ZoneId.of("GMT")).toInstant();
51      }
52  
53      private static Date createDate(final int year, final Month month, final int day) {
54          final Instant instant = createInstant(year, month, day);
55          return new Date(instant.toEpochMilli());
56      }
57  
58      @Test
59      public void testBasicDateParse() throws Exception {
60          final Instant instant = createInstant(2005, Month.OCTOBER, 14);
61          Assertions.assertEquals(instant, DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1123));
62          Assertions.assertEquals(instant, DateUtils.parseDate("Friday, 14 Oct 2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1123));
63          Assertions.assertEquals(instant, DateUtils.parseDate("Fri, 14-Oct-2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
64          Assertions.assertEquals(instant, DateUtils.parseDate("Friday, 14-Oct-2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
65          Assertions.assertEquals(instant.minus(2, ChronoUnit.HOURS),
66                  DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 CET", DateUtils.FORMATTER_RFC1123));
67          Assertions.assertEquals(instant.minus(2, ChronoUnit.HOURS),
68                  DateUtils.parseDate("Fri, 14-Oct-05 00:00:00 CET", DateUtils.FORMATTER_RFC1036));
69          Assertions.assertEquals(instant, DateUtils.parseStandardDate("Fri, 14 Oct 2005 00:00:00 GMT"));
70      }
71  
72      @Test
73      public void testDateParseMessage() throws Exception {
74          final HeaderGroup message1 = new HeaderGroup();
75          message1.setHeader(new BasicHeader(HttpHeaders.DATE, "Fri, 14 Oct 2005 00:00:00 GMT"));
76          Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14), DateUtils.parseStandardDate(message1, HttpHeaders.DATE));
77  
78          final HeaderGroup message2 = new HeaderGroup();
79          message2.addHeader(new BasicHeader(HttpHeaders.DATE, "Fri, 14 Oct 2005 00:00:00 GMT"));
80          message2.addHeader(new BasicHeader(HttpHeaders.DATE, "Fri, 21 Oct 2005 00:00:00 GMT"));
81          Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14), DateUtils.parseStandardDate(message2, HttpHeaders.DATE));
82      }
83  
84      @Test
85      public void testMalformedDate() {
86          Assertions.assertNull(DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", new DateTimeFormatter[] {}));
87      }
88  
89      @Test
90      public void testInvalidInput() throws Exception {
91          Assertions.assertThrows(NullPointerException.class, () -> DateUtils.parseStandardDate(null));
92          Assertions.assertThrows(NullPointerException.class, () -> DateUtils.formatStandardDate(null));
93      }
94  
95      @Test
96      public void testTwoDigitYearDateParse() throws Exception {
97          Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14),
98                  DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
99      }
100 
101     @Test
102     public void testParseQuotedDate() throws Exception {
103         Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14),
104                 DateUtils.parseDate("'Fri, 14 Oct 2005 00:00:00 GMT'", DateUtils.FORMATTER_RFC1123));
105     }
106 
107     @Test
108     public void testBasicDateFormat() throws Exception {
109         final Instant instant = createInstant(2005, Month.OCTOBER, 14);
110         Assertions.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatStandardDate(instant));
111         Assertions.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1123));
112         Assertions.assertEquals("Fri, 14-Oct-05 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1036));
113         Assertions.assertEquals("Fri Oct 14 00:00:00 2005", DateUtils.formatDate(instant, DateUtils.FORMATTER_ASCTIME));
114     }
115 
116 }