|
17 | 17 |
|
18 | 18 | import static org.springframework.data.r2dbc.testing.Assertions.*;
|
19 | 19 |
|
| 20 | +import io.r2dbc.postgresql.codec.Interval; |
20 | 21 | import lombok.RequiredArgsConstructor;
|
21 | 22 |
|
| 23 | +import java.time.Duration; |
| 24 | +import java.util.ArrayList; |
22 | 25 | import java.util.Arrays;
|
23 | 26 | import java.util.Collections;
|
24 | 27 | import java.util.EnumSet;
|
|
28 | 31 | import org.junit.jupiter.api.Test;
|
29 | 32 |
|
30 | 33 | import org.springframework.core.convert.converter.Converter;
|
| 34 | +import org.springframework.data.convert.ReadingConverter; |
31 | 35 | import org.springframework.data.convert.WritingConverter;
|
32 | 36 | import org.springframework.data.r2dbc.convert.EnumWriteSupport;
|
33 | 37 | import org.springframework.data.r2dbc.dialect.PostgresDialect;
|
|
41 | 45 | */
|
42 | 46 | public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessStrategyTestSupport {
|
43 | 47 |
|
44 |
| - private final ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE); |
| 48 | + private final ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE, |
| 49 | + Arrays.asList(DurationToIntervalConverter.INSTANCE, IntervalToDurationConverter.INSTANCE)); |
45 | 50 |
|
46 | 51 | @Override
|
47 | 52 | protected ReactiveDataAccessStrategy getStrategy() {
|
@@ -116,6 +121,17 @@ void shouldApplyCustomConversionForNull() {
|
116 | 121 | assertThat(outboundRow).containsColumn("my_objects").withColumn("my_objects").isEmpty().hasType(String.class);
|
117 | 122 | }
|
118 | 123 |
|
| 124 | + @Test // gh-796 |
| 125 | + void shouldApplyCustomConversionForEmptyList() { |
| 126 | + |
| 127 | + WithDuration withDuration = new WithDuration(); |
| 128 | + withDuration.durations = new ArrayList<>(); |
| 129 | + |
| 130 | + OutboundRow outboundRow = strategy.getOutboundRow(withDuration); |
| 131 | + |
| 132 | + assertThat(outboundRow).containsColumn("durations").withColumn("durations").hasType(Interval[].class); |
| 133 | + } |
| 134 | + |
119 | 135 | @Test // gh-252, gh-593
|
120 | 136 | void shouldConvertCollectionOfEnumToString() {
|
121 | 137 |
|
@@ -202,6 +218,11 @@ static class WithArray {
|
202 | 218 | List<String> stringList;
|
203 | 219 | }
|
204 | 220 |
|
| 221 | + static class WithDuration { |
| 222 | + |
| 223 | + List<Duration> durations; |
| 224 | + } |
| 225 | + |
205 | 226 | static class WithEnumCollections {
|
206 | 227 |
|
207 | 228 | MyEnum[] enumArray;
|
@@ -242,5 +263,27 @@ public String convert(List<MyObject> myObjects) {
|
242 | 263 | }
|
243 | 264 | }
|
244 | 265 |
|
| 266 | + @WritingConverter |
| 267 | + enum DurationToIntervalConverter implements Converter<Duration, Interval> { |
| 268 | + |
| 269 | + INSTANCE; |
| 270 | + |
| 271 | + @Override |
| 272 | + public Interval convert(Duration duration) { |
| 273 | + return Interval.of(duration); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + @ReadingConverter |
| 278 | + enum IntervalToDurationConverter implements Converter<Interval, Duration> { |
| 279 | + |
| 280 | + INSTANCE; |
| 281 | + |
| 282 | + @Override |
| 283 | + public Duration convert(Interval interval) { |
| 284 | + return interval.getDuration(); |
| 285 | + } |
| 286 | + } |
| 287 | + |
245 | 288 | private static class MyEnumSupport extends EnumWriteSupport<MyEnum> {}
|
246 | 289 | }
|
0 commit comments