18
18
import static java .time .ZoneOffset .UTC ;
19
19
import static java .time .format .DateTimeFormatter .ISO_INSTANT ;
20
20
import static java .time .format .DateTimeFormatter .ISO_OFFSET_DATE_TIME ;
21
+ import static java .time .format .DateTimeFormatter .RFC_1123_DATE_TIME ;
22
+ import static org .assertj .core .api .Assertions .assertThat ;
21
23
import static org .junit .Assert .assertEquals ;
22
24
import static org .junit .Assert .assertTrue ;
23
25
import static software .amazon .awssdk .utils .DateUtils .ALTERNATE_ISO_8601_DATE_FORMAT ;
26
+ import static software .amazon .awssdk .utils .DateUtils .RFC_822_DATE_TIME ;
24
27
25
28
import java .text .ParseException ;
26
29
import java .text .SimpleDateFormat ;
@@ -72,6 +75,80 @@ public void formatIso8601Date() throws ParseException {
72
75
assertEquals (expectedDate , actualDate );
73
76
}
74
77
78
+ @ Test
79
+ public void formatRfc822Date_DateWithTwoDigitDayOfMonth_ReturnsFormattedString () throws ParseException {
80
+ String string = DateUtils .formatRfc822Date (INSTANT );
81
+ Instant parsedDateAsInstant = LONG_DATE_FORMAT .parse (string ).toInstant ();
82
+ assertThat (parsedDateAsInstant ).isEqualTo (INSTANT );
83
+ }
84
+
85
+ @ Test
86
+ public void formatRfc822Date_DateWithSingleDigitDayOfMonth_ReturnsFormattedString () throws ParseException {
87
+ Instant INSTANT_SINGLE_DIGIT_DAY_OF_MONTH = Instant .ofEpochMilli (1399484606000L );;
88
+ String string = DateUtils .formatRfc822Date (INSTANT_SINGLE_DIGIT_DAY_OF_MONTH );
89
+ Instant parsedDateAsInstant = LONG_DATE_FORMAT .parse (string ).toInstant ();
90
+ assertThat (parsedDateAsInstant ).isEqualTo (INSTANT_SINGLE_DIGIT_DAY_OF_MONTH );
91
+ }
92
+
93
+ @ Test
94
+ public void formatRfc822Date_DateWithSingleDigitDayOfMonth_ReturnsStringWithZeroLeadingDayOfMonth () throws ParseException {
95
+ final Instant INSTANT_SINGLE_DIGIT_DAY_OF_MONTH = Instant .ofEpochMilli (1399484606000L );;
96
+ String string = DateUtils .formatRfc822Date (INSTANT_SINGLE_DIGIT_DAY_OF_MONTH );
97
+ String expectedString = "Wed, 07 May 2014 17:43:26 GMT" ;
98
+ assertThat (string ).isEqualTo (expectedString );
99
+ }
100
+
101
+ @ Test
102
+ public void parseRfc822Date_DateWithTwoDigitDayOfMonth_ReturnsInstantObject () throws ParseException {
103
+ String formattedDate = LONG_DATE_FORMAT .format (Date .from (INSTANT ));
104
+ Instant parsedInstant = DateUtils .parseRfc822Date (formattedDate );
105
+ assertThat (parsedInstant ).isEqualTo (INSTANT );
106
+ }
107
+
108
+ @ Test
109
+ public void parseRfc822Date_DateWithSingleDigitDayOfMonth_ReturnsInstantObject () throws ParseException {
110
+ final Instant INSTANT_SINGLE_DIGIT_DAY_OF_MONTH = Instant .ofEpochMilli (1399484606000L );;
111
+ String formattedDate = LONG_DATE_FORMAT .format (Date .from (INSTANT_SINGLE_DIGIT_DAY_OF_MONTH ));
112
+ Instant parsedInstant = DateUtils .parseRfc822Date (formattedDate );
113
+ assertThat (parsedInstant ).isEqualTo (INSTANT_SINGLE_DIGIT_DAY_OF_MONTH );
114
+ }
115
+
116
+ @ Test
117
+ public void parseRfc822Date_DateWithInvalidDayOfMonth_IsParsedWithSmartResolverStyle () {
118
+ String badDateString = "Wed, 31 Apr 2014 17:43:26 GMT" ;
119
+ String validDateString = "Wed, 30 Apr 2014 17:43:26 GMT" ;
120
+ Instant badDateParsedInstant = DateUtils .parseRfc822Date (badDateString );
121
+ Instant validDateParsedInstant = DateUtils .parseRfc1123Date (validDateString );
122
+ assertThat (badDateParsedInstant ).isEqualTo (validDateParsedInstant );
123
+ }
124
+
125
+ @ Test
126
+ public void parseRfc822Date_DateWithInvalidDayOfMonth_MatchesRfc1123Behavior () {
127
+ String dateString = "Wed, 31 Apr 2014 17:43:26 GMT" ;
128
+ Instant parsedInstantFromRfc822Parser = DateUtils .parseRfc822Date (dateString );
129
+ Instant parsedInstantFromRfc1123arser = DateUtils .parseRfc1123Date (dateString );
130
+ assertThat (parsedInstantFromRfc822Parser ).isEqualTo (parsedInstantFromRfc1123arser );
131
+ }
132
+
133
+ @ Test
134
+ public void parseRfc822Date_DateWithDayOfMonthLessThan10th_MatchesRfc1123Behavior () {
135
+ String rfc822DateString = "Wed, 02 Apr 2014 17:43:26 GMT" ;
136
+ String rfc1123DateString = "Wed, 2 Apr 2014 17:43:26 GMT" ;
137
+ Instant parsedInstantFromRfc822Parser = DateUtils .parseRfc822Date (rfc822DateString );
138
+ Instant parsedInstantFromRfc1123arser = DateUtils .parseRfc1123Date (rfc1123DateString );
139
+ assertThat (parsedInstantFromRfc822Parser ).isEqualTo (parsedInstantFromRfc1123arser );
140
+ }
141
+
142
+ @ Test
143
+ public void resolverStyleOfRfc822FormatterMatchesRfc1123Formatter () {
144
+ assertThat (RFC_822_DATE_TIME .getResolverStyle ()).isSameAs (RFC_1123_DATE_TIME .getResolverStyle ());
145
+ }
146
+
147
+ @ Test
148
+ public void chronologyOfRfc822FormatterMatchesRfc1123Formatter () {
149
+ assertThat (RFC_822_DATE_TIME .getChronology ()).isSameAs (RFC_1123_DATE_TIME .getChronology ());
150
+ }
151
+
75
152
@ Test
76
153
public void formatRfc1123Date () throws ParseException {
77
154
String string = DateUtils .formatRfc1123Date (INSTANT );
@@ -84,7 +161,7 @@ public void formatRfc1123Date() throws ParseException {
84
161
}
85
162
86
163
@ Test
87
- public void parseRfc822Date () throws ParseException {
164
+ public void parseRfc1123Date () throws ParseException {
88
165
String formatted = LONG_DATE_FORMAT .format (Date .from (INSTANT ));
89
166
Instant expected = LONG_DATE_FORMAT .parse (formatted ).toInstant ();
90
167
Instant actual = DateUtils .parseRfc1123Date (formatted );
0 commit comments