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