Skip to content

Commit f45b9be

Browse files
committed
added unit test for the problem
1 parent 9063963 commit f45b9be

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverterUnitTests.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18+
import static java.util.Arrays.*;
1819
import static org.assertj.core.api.Assertions.*;
1920
import static org.assertj.core.api.SoftAssertions.*;
2021
import static org.mockito.Mockito.*;
@@ -40,6 +41,7 @@
4041
import org.junit.jupiter.api.Test;
4142
import org.springframework.core.convert.converter.Converter;
4243
import org.springframework.data.annotation.Id;
44+
import org.springframework.data.convert.WritingConverter;
4345
import org.springframework.data.jdbc.core.mapping.AggregateReference;
4446
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4547
import org.springframework.data.jdbc.core.mapping.JdbcValue;
@@ -70,7 +72,7 @@ class MappingJdbcConverterUnitTests {
7072
(identifier, path) -> {
7173
throw new UnsupportedOperationException();
7274
}, //
73-
new JdbcCustomConversions(), //
75+
new JdbcCustomConversions(List.of(CustomIdToLong.INSTANCE)), //
7476
typeFactory //
7577
);
7678

@@ -92,6 +94,9 @@ void testTargetTypesForPropertyType() {
9294
checkTargetType(softly, entity, "date", Date.class);
9395
checkTargetType(softly, entity, "timestamp", Timestamp.class);
9496
checkTargetType(softly, entity, "uuid", UUID.class);
97+
checkTargetType(softly, entity, "reference", Long.class);
98+
checkTargetType(softly, entity, "enumIdReference", String.class);
99+
checkTargetType(softly, entity, "customIdReference", Long.class);
95100
});
96101
}
97102

@@ -234,6 +239,8 @@ private record DummyEntity(
234239
Date date,
235240
Timestamp timestamp,
236241
AggregateReference<DummyEntity, Long> reference,
242+
AggregateReference<EnumIdEntity, SomeEnum> enumIdReference,
243+
AggregateReference<CustomIdEntity, SomeEnum> customIdReference,
237244
UUID uuid,
238245
AggregateReference<ReferencedByUuid, UUID> uuidRef,
239246
Optional<UUID> optionalUuid,
@@ -255,6 +262,18 @@ private enum SomeEnum {
255262
private static class OtherEntity {
256263
}
257264

265+
private static class EnumIdEntity {
266+
@Id SomeEnum id;
267+
}
268+
269+
private static class CustomIdEntity {
270+
@Id CustomId id;
271+
}
272+
273+
private record CustomId(Long id) {
274+
275+
}
276+
258277
private static class StubbedJdbcTypeFactory implements JdbcTypeFactory {
259278
Object[] arraySource;
260279

@@ -284,4 +303,14 @@ public UUID convert(byte[] source) {
284303
return new UUID(high, low);
285304
}
286305
}
306+
307+
@WritingConverter
308+
private enum CustomIdToLong implements Converter<CustomId, Long> {
309+
INSTANCE;
310+
311+
@Override
312+
public Long convert(CustomId source) {
313+
return source.id;
314+
}
315+
}
287316
}

0 commit comments

Comments
 (0)