23
23
24
24
import java .time .Instant ;
25
25
import java .time .ZoneId ;
26
- import java .time .ZonedDateTime ;
27
26
import java .time .format .DateTimeParseException ;
28
27
import java .time .temporal .TemporalAccessor ;
28
+ import java .util .Locale ;
29
29
30
30
import static org .hamcrest .Matchers .containsString ;
31
+ import static org .hamcrest .Matchers .equalTo ;
31
32
import static org .hamcrest .Matchers .is ;
33
+ import static org .hamcrest .Matchers .not ;
34
+ import static org .hamcrest .Matchers .nullValue ;
35
+ import static org .hamcrest .Matchers .sameInstance ;
32
36
33
37
public class DateFormattersTests extends ESTestCase {
34
38
35
39
public void testEpochMilliParser () {
36
40
DateFormatter formatter = DateFormatters .forPattern ("epoch_millis" );
37
-
38
41
DateTimeParseException e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("invalid" ));
39
42
assertThat (e .getMessage (), containsString ("invalid number" ));
40
-
41
- // different zone, should still yield the same output, as epoch is time zone independent
42
- ZoneId zoneId = randomZone ();
43
- DateFormatter zonedFormatter = formatter .withZone (zoneId );
44
-
45
- // test with negative and non negative values
46
- assertThatSameDateTime (formatter , zonedFormatter , randomNonNegativeLong () * -1 );
47
- assertThatSameDateTime (formatter , zonedFormatter , randomNonNegativeLong ());
48
- assertThatSameDateTime (formatter , zonedFormatter , 0 );
49
- assertThatSameDateTime (formatter , zonedFormatter , -1 );
50
- assertThatSameDateTime (formatter , zonedFormatter , 1 );
51
-
52
- // format() output should be equal as well
53
- assertSameFormat (formatter , randomNonNegativeLong () * -1 );
54
- assertSameFormat (formatter , randomNonNegativeLong ());
55
- assertSameFormat (formatter , 0 );
56
- assertSameFormat (formatter , -1 );
57
- assertSameFormat (formatter , 1 );
58
43
}
59
44
60
45
// this is not in the duelling tests, because the epoch second parser in joda time drops the milliseconds after the comma
@@ -83,14 +68,6 @@ public void testEpochSecondParser() {
83
68
assertThat (e .getMessage (), is ("invalid number [abc]" ));
84
69
e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("1234.abc" ));
85
70
assertThat (e .getMessage (), is ("invalid number [1234.abc]" ));
86
-
87
- // different zone, should still yield the same output, as epoch is time zone independent
88
- ZoneId zoneId = randomZone ();
89
- DateFormatter zonedFormatter = formatter .withZone (zoneId );
90
-
91
- assertThatSameDateTime (formatter , zonedFormatter , randomLongBetween (-100_000_000 , 100_000_000 ));
92
- assertSameFormat (formatter , randomLongBetween (-100_000_000 , 100_000_000 ));
93
- assertThat (formatter .format (Instant .ofEpochSecond (1234 , 567_000_000 )), is ("1234.567" ));
94
71
}
95
72
96
73
public void testEpochMilliParsersWithDifferentFormatters () {
@@ -100,16 +77,54 @@ public void testEpochMilliParsersWithDifferentFormatters() {
100
77
assertThat (formatter .pattern (), is ("strict_date_optional_time||epoch_millis" ));
101
78
}
102
79
103
- private void assertThatSameDateTime (DateFormatter formatter , DateFormatter zonedFormatter , long millis ) {
104
- String millisAsString = String .valueOf (millis );
105
- ZonedDateTime formatterZonedDateTime = DateFormatters .toZonedDateTime (formatter .parse (millisAsString ));
106
- ZonedDateTime zonedFormatterZonedDateTime = DateFormatters .toZonedDateTime (zonedFormatter .parse (millisAsString ));
107
- assertThat (formatterZonedDateTime .toInstant ().toEpochMilli (), is (zonedFormatterZonedDateTime .toInstant ().toEpochMilli ()));
80
+ public void testLocales () {
81
+ assertThat (DateFormatters .forPattern ("strict_date_optional_time" ).getLocale (), is (Locale .ROOT ));
82
+ Locale locale = randomLocale (random ());
83
+ assertThat (DateFormatters .forPattern ("strict_date_optional_time" ).withLocale (locale ).getLocale (), is (locale ));
84
+ IllegalArgumentException e =
85
+ expectThrows (IllegalArgumentException .class , () -> DateFormatters .forPattern ("epoch_millis" ).withLocale (locale ));
86
+ assertThat (e .getMessage (), is ("epoch_millis date formatter can only be in locale ROOT" ));
87
+ e = expectThrows (IllegalArgumentException .class , () -> DateFormatters .forPattern ("epoch_second" ).withLocale (locale ));
88
+ assertThat (e .getMessage (), is ("epoch_second date formatter can only be in locale ROOT" ));
89
+ }
90
+
91
+ public void testTimeZones () {
92
+ // zone is null by default due to different behaviours between java8 and above
93
+ assertThat (DateFormatters .forPattern ("strict_date_optional_time" ).getZone (), is (nullValue ()));
94
+ ZoneId zoneId = randomZone ();
95
+ assertThat (DateFormatters .forPattern ("strict_date_optional_time" ).withZone (zoneId ).getZone (), is (zoneId ));
96
+ IllegalArgumentException e =
97
+ expectThrows (IllegalArgumentException .class , () -> DateFormatters .forPattern ("epoch_millis" ).withZone (zoneId ));
98
+ assertThat (e .getMessage (), is ("epoch_millis date formatter can only be in zone offset UTC" ));
99
+ e = expectThrows (IllegalArgumentException .class , () -> DateFormatters .forPattern ("epoch_second" ).withZone (zoneId ));
100
+ assertThat (e .getMessage (), is ("epoch_second date formatter can only be in zone offset UTC" ));
108
101
}
109
102
110
- private void assertSameFormat (DateFormatter formatter , long millis ) {
111
- String millisAsString = String .valueOf (millis );
112
- TemporalAccessor accessor = formatter .parse (millisAsString );
113
- assertThat (millisAsString , is (formatter .format (accessor )));
103
+ public void testEqualsAndHashcode () {
104
+ assertThat (DateFormatters .forPattern ("strict_date_optional_time" ),
105
+ sameInstance (DateFormatters .forPattern ("strict_date_optional_time" )));
106
+ assertThat (DateFormatters .forPattern ("YYYY" ), equalTo (DateFormatters .forPattern ("YYYY" )));
107
+ assertThat (DateFormatters .forPattern ("YYYY" ).hashCode (),
108
+ is (DateFormatters .forPattern ("YYYY" ).hashCode ()));
109
+
110
+ // different timezone, thus not equals
111
+ assertThat (DateFormatters .forPattern ("YYYY" ).withZone (ZoneId .of ("CET" )), not (equalTo (DateFormatters .forPattern ("YYYY" ))));
112
+
113
+ // different locale, thus not equals
114
+ assertThat (DateFormatters .forPattern ("YYYY" ).withLocale (randomLocale (random ())),
115
+ not (equalTo (DateFormatters .forPattern ("YYYY" ))));
116
+
117
+ // different pattern, thus not equals
118
+ assertThat (DateFormatters .forPattern ("YYYY" ), not (equalTo (DateFormatters .forPattern ("YY" ))));
119
+
120
+ DateFormatter epochSecondFormatter = DateFormatters .forPattern ("epoch_second" );
121
+ assertThat (epochSecondFormatter , sameInstance (DateFormatters .forPattern ("epoch_second" )));
122
+ assertThat (epochSecondFormatter , equalTo (DateFormatters .forPattern ("epoch_second" )));
123
+ assertThat (epochSecondFormatter .hashCode (), is (DateFormatters .forPattern ("epoch_second" ).hashCode ()));
124
+
125
+ DateFormatter epochMillisFormatter = DateFormatters .forPattern ("epoch_millis" );
126
+ assertThat (epochMillisFormatter .hashCode (), is (DateFormatters .forPattern ("epoch_millis" ).hashCode ()));
127
+ assertThat (epochMillisFormatter , sameInstance (DateFormatters .forPattern ("epoch_millis" )));
128
+ assertThat (epochMillisFormatter , equalTo (DateFormatters .forPattern ("epoch_millis" )));
114
129
}
115
130
}
0 commit comments