|
|
|
@ -20,6 +20,7 @@ import java.text.ParseException; |
|
|
|
import java.time.Instant; |
|
|
|
import java.time.Instant; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.time.temporal.ChronoUnit; |
|
|
|
import java.time.temporal.ChronoUnit; |
|
|
|
|
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Random; |
|
|
|
import java.util.Random; |
|
|
|
import java.util.stream.Stream; |
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
|
|
@ -50,13 +51,12 @@ class InstantFormatterTests { |
|
|
|
|
|
|
|
|
|
|
|
private final InstantFormatter instantFormatter = new InstantFormatter(); |
|
|
|
private final InstantFormatter instantFormatter = new InstantFormatter(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest |
|
|
|
@ParameterizedTest |
|
|
|
@ArgumentsSource(ISOSerializedInstantProvider.class) |
|
|
|
@ArgumentsSource(ISOSerializedInstantProvider.class) |
|
|
|
void should_parse_an_ISO_formatted_string_representation_of_an_Instant(String input) throws ParseException { |
|
|
|
void should_parse_an_ISO_formatted_string_representation_of_an_Instant(String input) throws ParseException { |
|
|
|
Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from); |
|
|
|
Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from); |
|
|
|
|
|
|
|
Instant actual = instantFormatter.parse(input, Locale.US); |
|
|
|
Instant actual = instantFormatter.parse(input, null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actual).isEqualTo(expected); |
|
|
|
assertThat(actual).isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -64,9 +64,7 @@ class InstantFormatterTests { |
|
|
|
@ArgumentsSource(RFC1123SerializedInstantProvider.class) |
|
|
|
@ArgumentsSource(RFC1123SerializedInstantProvider.class) |
|
|
|
void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant(String input) throws ParseException { |
|
|
|
void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant(String input) throws ParseException { |
|
|
|
Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from); |
|
|
|
Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from); |
|
|
|
|
|
|
|
Instant actual = instantFormatter.parse(input, Locale.US); |
|
|
|
Instant actual = instantFormatter.parse(input, null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actual).isEqualTo(expected); |
|
|
|
assertThat(actual).isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -74,20 +72,18 @@ class InstantFormatterTests { |
|
|
|
@ArgumentsSource(RandomInstantProvider.class) |
|
|
|
@ArgumentsSource(RandomInstantProvider.class) |
|
|
|
void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale(Instant input) { |
|
|
|
void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale(Instant input) { |
|
|
|
String expected = DateTimeFormatter.ISO_INSTANT.format(input); |
|
|
|
String expected = DateTimeFormatter.ISO_INSTANT.format(input); |
|
|
|
|
|
|
|
String actual = instantFormatter.print(input, Locale.US); |
|
|
|
String actual = instantFormatter.print(input, null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actual).isEqualTo(expected); |
|
|
|
assertThat(actual).isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest |
|
|
|
@ParameterizedTest |
|
|
|
@ArgumentsSource(RandomEpochMillisProvider.class) |
|
|
|
@ArgumentsSource(RandomEpochMillisProvider.class) |
|
|
|
void should_parse_into_an_Instant_from_epoch_milli(Instant input) throws ParseException { |
|
|
|
void should_parse_into_an_Instant_from_epoch_milli(Instant input) throws ParseException { |
|
|
|
Instant actual = instantFormatter.parse(Long.toString(input.toEpochMilli()), null); |
|
|
|
Instant actual = instantFormatter.parse(Long.toString(input.toEpochMilli()), Locale.US); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(actual).isEqualTo(input); |
|
|
|
assertThat(actual).isEqualTo(input); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class RandomInstantProvider implements ArgumentsProvider { |
|
|
|
private static class RandomInstantProvider implements ArgumentsProvider { |
|
|
|
|
|
|
|
|
|
|
|
private static final long DATA_SET_SIZE = 10; |
|
|
|
private static final long DATA_SET_SIZE = 10; |
|
|
|
@ -109,6 +105,7 @@ class InstantFormatterTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class ISOSerializedInstantProvider extends RandomInstantProvider { |
|
|
|
private static class ISOSerializedInstantProvider extends RandomInstantProvider { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -117,6 +114,7 @@ class InstantFormatterTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class RFC1123SerializedInstantProvider extends RandomInstantProvider { |
|
|
|
private static class RFC1123SerializedInstantProvider extends RandomInstantProvider { |
|
|
|
|
|
|
|
|
|
|
|
// RFC-1123 supports only 4-digit years
|
|
|
|
// RFC-1123 supports only 4-digit years
|
|
|
|
@ -130,6 +128,8 @@ class InstantFormatterTests { |
|
|
|
.map(DateTimeFormatter.RFC_1123_DATE_TIME.withZone(systemDefault())::format); |
|
|
|
.map(DateTimeFormatter.RFC_1123_DATE_TIME.withZone(systemDefault())::format); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final class RandomEpochMillisProvider implements ArgumentsProvider { |
|
|
|
private static final class RandomEpochMillisProvider implements ArgumentsProvider { |
|
|
|
|
|
|
|
|
|
|
|
private static final long DATA_SET_SIZE = 10; |
|
|
|
private static final long DATA_SET_SIZE = 10; |
|
|
|
|