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.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
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
72 }
73 try {
74 DateUtils.formatDate(null);
75 Assert.fail("IllegalArgumentException should habe been thrown");
76 } catch (final IllegalArgumentException ex) {
77
78 }
79 try {
80 DateUtils.formatDate(new Date(), null);
81 Assert.fail("IllegalArgumentException should habe been thrown");
82 } catch (final IllegalArgumentException ex) {
83
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 }