19
19
import static org .assertj .core .api .SoftAssertions .*;
20
20
import static org .mockito .Mockito .*;
21
21
22
+ import java .nio .ByteBuffer ;
22
23
import java .sql .Array ;
23
24
import java .sql .Timestamp ;
24
25
import java .time .Instant ;
28
29
import java .time .OffsetDateTime ;
29
30
import java .time .ZoneOffset ;
30
31
import java .time .ZonedDateTime ;
32
+ import java .util .Collections ;
31
33
import java .util .Date ;
32
34
import java .util .List ;
33
35
import java .util .Map ;
34
36
import java .util .UUID ;
35
37
36
38
import org .assertj .core .api .SoftAssertions ;
37
39
import org .junit .jupiter .api .Test ;
40
+ import org .springframework .core .convert .converter .Converter ;
38
41
import org .springframework .data .annotation .Id ;
39
42
import org .springframework .data .jdbc .core .mapping .AggregateReference ;
40
43
import org .springframework .data .jdbc .core .mapping .JdbcMappingContext ;
50
53
* Unit tests for {@link MappingJdbcConverter}.
51
54
*
52
55
* @author Mark Paluch
56
+ * @author Jens Schauder
53
57
*/
54
58
public class MappingJdbcConverterUnitTests {
55
59
60
+ public static final UUID UUID = java .util .UUID .fromString ("87a48aa8-a071-705e-54a9-e52fe3a012f1" );
61
+ public static final byte [] BYTES_REPRESENTING_UUID = { -121 , -92 , -118 , -88 , -96 , 113 , 112 , 94 , 84 , -87 , -27 , 47 , -29 ,
62
+ -96 , 18 , -15 };
63
+
56
64
JdbcMappingContext context = new JdbcMappingContext ();
57
65
StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory ();
58
66
MappingJdbcConverter converter = new MappingJdbcConverter ( //
@@ -61,7 +69,7 @@ public class MappingJdbcConverterUnitTests {
61
69
throw new UnsupportedOperationException ();
62
70
}, //
63
71
new JdbcCustomConversions (), //
64
- typeFactory //
72
+ typeFactory //
65
73
);
66
74
67
75
@ Test // DATAJDBC-104, DATAJDBC-1384
@@ -152,6 +160,38 @@ void accessesCorrectValuesForOneToOneRelationshipWithIdenticallyNamedIdPropertie
152
160
assertThat (result ).isEqualTo (new WithOneToOne ("one" , new Referenced (23L )));
153
161
}
154
162
163
+ @ Test // GH-1750
164
+ void readByteArrayToAggregateReferenceWithCustomConverter () {
165
+
166
+ JdbcMappingContext context = new JdbcMappingContext ();
167
+ StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory ();
168
+ Converter <byte [], UUID > customConverter = new ByteArrayToUuid ();
169
+ MappingJdbcConverter converter = new MappingJdbcConverter ( //
170
+ context , //
171
+ (identifier , path ) -> {
172
+ throw new UnsupportedOperationException ();
173
+ }, //
174
+ new JdbcCustomConversions (Collections .singletonList (customConverter )), //
175
+ typeFactory //
176
+ );
177
+
178
+ SoftAssertions .assertSoftly (softly -> {
179
+ checkReadConversion (softly , converter , "uuidRef" , AggregateReference .to (UUID ));
180
+ checkReadConversion (softly , converter , "uuid" , UUID );
181
+ });
182
+
183
+ }
184
+
185
+ private static void checkReadConversion (SoftAssertions softly , MappingJdbcConverter converter , String propertyName ,
186
+ Object expected ) {
187
+
188
+ RelationalPersistentProperty property = converter .getMappingContext ().getRequiredPersistentEntity (DummyEntity .class )
189
+ .getRequiredPersistentProperty (propertyName );
190
+ Object value = converter .readValue (BYTES_REPRESENTING_UUID , property .getTypeInformation () //
191
+ );
192
+
193
+ softly .assertThat (value ).isEqualTo (expected );
194
+ }
155
195
156
196
private void checkConversionToTimestampAndBack (SoftAssertions softly , RelationalPersistentEntity <?> persistentEntity ,
157
197
String propertyName , Object value ) {
@@ -187,6 +227,7 @@ private static class DummyEntity {
187
227
private final Timestamp timestamp ;
188
228
private final AggregateReference <DummyEntity , Long > reference ;
189
229
private final UUID uuid ;
230
+ private final AggregateReference <ReferencedByUuid , UUID > uuidRef ;
190
231
191
232
// DATAJDBC-259
192
233
private final List <String > listOfString ;
@@ -196,8 +237,9 @@ private static class DummyEntity {
196
237
197
238
private DummyEntity (Long id , SomeEnum someEnum , LocalDateTime localDateTime , LocalDate localDate ,
198
239
LocalTime localTime , ZonedDateTime zonedDateTime , OffsetDateTime offsetDateTime , Instant instant , Date date ,
199
- Timestamp timestamp , AggregateReference <DummyEntity , Long > reference , UUID uuid , List <String > listOfString ,
200
- String [] arrayOfString , List <OtherEntity > listOfEntity , OtherEntity [] arrayOfEntity ) {
240
+ Timestamp timestamp , AggregateReference <DummyEntity , Long > reference , UUID uuid ,
241
+ AggregateReference <ReferencedByUuid , UUID > uuidRef , List <String > listOfString , String [] arrayOfString ,
242
+ List <OtherEntity > listOfEntity , OtherEntity [] arrayOfEntity ) {
201
243
this .id = id ;
202
244
this .someEnum = someEnum ;
203
245
this .localDateTime = localDateTime ;
@@ -210,6 +252,7 @@ private DummyEntity(Long id, SomeEnum someEnum, LocalDateTime localDateTime, Loc
210
252
this .timestamp = timestamp ;
211
253
this .reference = reference ;
212
254
this .uuid = uuid ;
255
+ this .uuidRef = uuidRef ;
213
256
this .listOfString = listOfString ;
214
257
this .arrayOfString = arrayOfString ;
215
258
this .listOfEntity = listOfEntity ;
@@ -299,9 +342,23 @@ public Array createArray(Object[] value) {
299
342
}
300
343
}
301
344
302
- record WithOneToOne (@ Id String id ,@ MappedCollection (idColumn = "renamed" ) Referenced referenced ){}
345
+ record WithOneToOne (@ Id String id , @ MappedCollection (idColumn = "renamed" ) Referenced referenced ) {
346
+ }
303
347
304
348
record Referenced (@ Id Long id ) {
305
349
}
306
350
351
+ record ReferencedByUuid (@ Id UUID id ) {
352
+ }
353
+
354
+ class ByteArrayToUuid implements Converter <byte [], UUID > {
355
+ @ Override
356
+ public UUID convert (byte [] source ) {
357
+
358
+ ByteBuffer byteBuffer = ByteBuffer .wrap (source );
359
+ long high = byteBuffer .getLong ();
360
+ long low = byteBuffer .getLong ();
361
+ return new UUID (high , low );
362
+ }
363
+ }
307
364
}
0 commit comments