1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 }