Skip to content

Commit 5423856

Browse files
committed
added unit test for the problem
1 parent 9d4d272 commit 5423856

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

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

+43-17
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

@@ -91,6 +93,9 @@ void testTargetTypesForPropertyType() {
9193
checkTargetType(softly, entity, "date", Date.class);
9294
checkTargetType(softly, entity, "timestamp", Timestamp.class);
9395
checkTargetType(softly, entity, "uuid", UUID.class);
96+
checkTargetType(softly, entity, "reference", Long.class);
97+
checkTargetType(softly, entity, "enumIdReference", String.class);
98+
checkTargetType(softly, entity, "customIdReference", Long.class);
9499

95100
softly.assertAll();
96101
}
@@ -216,22 +221,21 @@ private void checkTargetType(SoftAssertions softly, RelationalPersistentEntity<?
216221
}
217222

218223
@SuppressWarnings("unused")
219-
private static class DummyEntity {
220-
221-
@Id private final Long id;
222-
private final SomeEnum someEnum;
223-
private final LocalDateTime localDateTime;
224-
private final LocalDate localDate;
225-
private final LocalTime localTime;
226-
private final ZonedDateTime zonedDateTime;
227-
private final OffsetDateTime offsetDateTime;
228-
private final Instant instant;
229-
private final Date date;
230-
private final Timestamp timestamp;
231-
private final AggregateReference<DummyEntity, Long> reference;
232-
private final UUID uuid;
233-
private final AggregateReference<ReferencedByUuid, UUID> uuidRef;
234-
private final Optional<UUID> optionalUuid;
224+
private record DummyEntity(
225+
@Id Long id,
226+
SomeEnum someEnum,
227+
LocalDateTime localDateTime,
228+
LocalDate localDate,
229+
LocalTime localTime,
230+
ZonedDateTime zonedDateTime,
231+
OffsetDateTime offsetDateTime,
232+
Instant instant,
233+
Date date,
234+
Timestamp timestamp,
235+
AggregateReference<DummyEntity, Long> reference,
236+
UUID uuid,
237+
AggregateReference<ReferencedByUuid, UUID> uuidRef,
238+
Optional<UUID> optionalUuid,
235239

236240
// DATAJDBC-259
237241
private final List<String> listOfString;
@@ -337,6 +341,18 @@ private enum SomeEnum {
337341
@SuppressWarnings("unused")
338342
private static class OtherEntity {}
339343

344+
private static class EnumIdEntity {
345+
@Id SomeEnum id;
346+
}
347+
348+
private static class CustomIdEntity {
349+
@Id CustomId id;
350+
}
351+
352+
private record CustomId(Long id) {
353+
354+
}
355+
340356
private static class StubbedJdbcTypeFactory implements JdbcTypeFactory {
341357
Object[] arraySource;
342358

@@ -366,4 +382,14 @@ public UUID convert(byte[] source) {
366382
return new UUID(high, low);
367383
}
368384
}
385+
386+
@WritingConverter
387+
private enum CustomIdToLong implements Converter<CustomId, Long> {
388+
INSTANCE;
389+
390+
@Override
391+
public Long convert(CustomId source) {
392+
return source.id;
393+
}
394+
}
369395
}

0 commit comments

Comments
 (0)