|
22 | 22 | import java.util.Random;
|
23 | 23 | import java.util.stream.Stream;
|
24 | 24 |
|
25 |
| -import org.junit.jupiter.api.DisplayName; |
26 |
| -import org.junit.jupiter.api.DisplayNameGeneration; |
27 |
| -import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; |
28 |
| -import org.junit.jupiter.api.extension.ExtensionContext; |
29 |
| -import org.junit.jupiter.params.ParameterizedTest; |
30 |
| -import org.junit.jupiter.params.provider.Arguments; |
31 |
| -import org.junit.jupiter.params.provider.ArgumentsProvider; |
32 |
| -import org.junit.jupiter.params.provider.ArgumentsSource; |
| 25 | +import org.junit.Test; |
33 | 26 |
|
34 | 27 | import static java.time.Instant.MAX;
|
35 | 28 | import static java.time.Instant.MIN;
|
36 | 29 | import static java.time.ZoneId.systemDefault;
|
37 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.junit.Assert.assertEquals; |
38 | 31 |
|
39 | 32 | /**
|
40 | 33 | * Unit tests for {@link InstantFormatter}.
|
|
43 | 36 | * @author Sam Brannen
|
44 | 37 | * @since 5.1.12
|
45 | 38 | */
|
46 |
| -@DisplayName("InstantFormatter unit tests") |
47 |
| -@DisplayNameGeneration(ReplaceUnderscores.class) |
48 |
| -class InstantFormatterTests { |
| 39 | +public class InstantFormatterTests { |
49 | 40 |
|
50 | 41 | private final InstantFormatter instantFormatter = new InstantFormatter();
|
51 | 42 |
|
52 |
| - @ParameterizedTest |
53 |
| - @ArgumentsSource(ISOSerializedInstantProvider.class) |
54 |
| - void should_parse_an_ISO_formatted_string_representation_of_an_Instant(String input) throws ParseException { |
55 |
| - Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from); |
56 | 43 |
|
57 |
| - Instant actual = instantFormatter.parse(input, null); |
| 44 | + @Test |
| 45 | + public void should_parse_an_ISO_formatted_string_representation_of_an_Instant() { |
| 46 | + new ISOSerializedInstantProvider().provideArguments().forEach(input -> { |
| 47 | + try { |
| 48 | + Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from); |
58 | 49 |
|
59 |
| - assertThat(actual).isEqualTo(expected); |
| 50 | + Instant actual = instantFormatter.parse(input, null); |
| 51 | + |
| 52 | + assertEquals(expected, actual); |
| 53 | + } |
| 54 | + catch (ParseException ex) { |
| 55 | + throw new RuntimeException(ex); |
| 56 | + } |
| 57 | + }); |
60 | 58 | }
|
61 | 59 |
|
62 |
| - @ParameterizedTest |
63 |
| - @ArgumentsSource(RFC1123SerializedInstantProvider.class) |
64 |
| - void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant(String input) throws ParseException { |
65 |
| - Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from); |
| 60 | + @Test |
| 61 | + public void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant() { |
| 62 | + new RFC1123SerializedInstantProvider().provideArguments().forEach(input -> { |
| 63 | + try { |
| 64 | + Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from); |
66 | 65 |
|
67 |
| - Instant actual = instantFormatter.parse(input, null); |
| 66 | + Instant actual = instantFormatter.parse(input, null); |
68 | 67 |
|
69 |
| - assertThat(actual).isEqualTo(expected); |
| 68 | + assertEquals(expected, actual); |
| 69 | + } |
| 70 | + catch (ParseException ex) { |
| 71 | + throw new RuntimeException(ex); |
| 72 | + } |
| 73 | + }); |
70 | 74 | }
|
71 | 75 |
|
72 |
| - @ParameterizedTest |
73 |
| - @ArgumentsSource(RandomInstantProvider.class) |
74 |
| - void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale(Instant input) { |
75 |
| - String expected = DateTimeFormatter.ISO_INSTANT.format(input); |
| 76 | + @Test |
| 77 | + public void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale() { |
| 78 | + new RandomInstantProvider().randomInstantStream(MIN, MAX).forEach(instant -> { |
| 79 | + String expected = DateTimeFormatter.ISO_INSTANT.format(instant); |
| 80 | + |
| 81 | + String actual = instantFormatter.print(instant, null); |
| 82 | + |
| 83 | + assertEquals(expected, actual); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + private static class RandomInstantProvider { |
| 89 | + |
| 90 | + private static final long DATA_SET_SIZE = 10; |
76 | 91 |
|
77 |
| - String actual = instantFormatter.print(input, null); |
| 92 | + private static final Random random = new Random(); |
78 | 93 |
|
79 |
| - assertThat(actual).isEqualTo(expected); |
| 94 | + Stream<Instant> randomInstantStream(Instant min, Instant max) { |
| 95 | + return Stream.concat(Stream.of(Instant.now()), // make sure that the data set includes current instant |
| 96 | + random.longs(min.getEpochSecond(), max.getEpochSecond()).limit(DATA_SET_SIZE).mapToObj(Instant::ofEpochSecond)); |
| 97 | + } |
80 | 98 | }
|
81 | 99 |
|
82 | 100 | private static class ISOSerializedInstantProvider extends RandomInstantProvider {
|
83 | 101 |
|
84 |
| - @Override |
85 |
| - Stream<?> provideArguments() { |
| 102 | + Stream<String> provideArguments() { |
86 | 103 | return randomInstantStream(MIN, MAX).map(DateTimeFormatter.ISO_INSTANT::format);
|
87 | 104 | }
|
88 | 105 | }
|
89 | 106 |
|
90 | 107 | private static class RFC1123SerializedInstantProvider extends RandomInstantProvider {
|
91 | 108 |
|
92 | 109 | // RFC-1123 supports only 4-digit years
|
93 |
| - private final Instant min = Instant.parse("0000-01-01T00:00:00.00Z"); |
| 110 | + private static final Instant min = Instant.parse("0000-01-01T00:00:00.00Z"); |
| 111 | + |
| 112 | + private static final Instant max = Instant.parse("9999-12-31T23:59:59.99Z"); |
94 | 113 |
|
95 |
| - private final Instant max = Instant.parse("9999-12-31T23:59:59.99Z"); |
96 | 114 |
|
97 |
| - @Override |
98 |
| - Stream<?> provideArguments() { |
| 115 | + Stream<String> provideArguments() { |
99 | 116 | return randomInstantStream(min, max)
|
100 | 117 | .map(DateTimeFormatter.RFC_1123_DATE_TIME.withZone(systemDefault())::format);
|
101 | 118 | }
|
102 | 119 | }
|
103 | 120 |
|
104 |
| - private static class RandomInstantProvider implements ArgumentsProvider { |
105 |
| - |
106 |
| - private static final long DATA_SET_SIZE = 10; |
107 |
| - |
108 |
| - private static final Random random = new Random(); |
109 |
| - |
110 |
| - Stream<?> provideArguments() { |
111 |
| - return randomInstantStream(MIN, MAX); |
112 |
| - } |
113 |
| - |
114 |
| - @Override |
115 |
| - public final Stream<? extends Arguments> provideArguments(ExtensionContext context) { |
116 |
| - return provideArguments().map(Arguments::of).limit(DATA_SET_SIZE); |
117 |
| - } |
118 |
| - |
119 |
| - Stream<Instant> randomInstantStream(Instant min, Instant max) { |
120 |
| - return Stream.concat(Stream.of(Instant.now()), // make sure that the data set includes current instant |
121 |
| - random.longs(min.getEpochSecond(), max.getEpochSecond()).mapToObj(Instant::ofEpochSecond)); |
122 |
| - } |
123 |
| - } |
124 |
| - |
125 | 121 | }
|
0 commit comments