Skip to content

Commit 14e4488

Browse files
DATAREDIS-492 - Polishing.
Added some more tests and fix read operation. Original Pull Request: #189
1 parent 2492fbe commit 14e4488

File tree

3 files changed

+148
-33
lines changed

3 files changed

+148
-33
lines changed

src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.redis.core.convert;
1717

18+
import java.lang.reflect.Array;
1819
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collection;
@@ -231,14 +232,39 @@ public void doWithPersistentProperty(KeyValuePersistentProperty persistentProper
231232
else if (persistentProperty.isCollectionLike()) {
232233

233234
if (conversionService.canConvert(byte[].class, persistentProperty.getComponentType())) {
234-
accessor.setProperty(persistentProperty,
235-
readCollectionOfSimpleTypes(currentPath, persistentProperty.getType(),
236-
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(), source));
235+
236+
Object targetValue = null;
237+
if (persistentProperty.getType().isArray()) {
238+
239+
List<Object> list = (List<Object>) readCollectionOfSimpleTypes(currentPath, ArrayList.class,
240+
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(), source);
241+
242+
targetValue = list.toArray((Object[]) Array.newInstance(
243+
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(), list.size()));
244+
} else {
245+
targetValue = readCollectionOfSimpleTypes(currentPath, persistentProperty.getType(),
246+
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(), source);
247+
}
248+
249+
accessor.setProperty(persistentProperty, targetValue);
237250
} else {
238-
accessor.setProperty(persistentProperty,
239-
readCollectionOfComplexTypes(currentPath, persistentProperty.getType(),
240-
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(),
241-
source.getBucket()));
251+
252+
Object targetValue = null;
253+
if (persistentProperty.getType().isArray()) {
254+
255+
List<Object> list = (List<Object>) readCollectionOfComplexTypes(currentPath, ArrayList.class,
256+
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(),
257+
source.getBucket());
258+
259+
targetValue = list.toArray((Object[]) Array.newInstance(
260+
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(), list.size()));
261+
} else {
262+
263+
targetValue = readCollectionOfComplexTypes(currentPath, persistentProperty.getType(),
264+
persistentProperty.getTypeInformation().getComponentType().getActualType().getType(),
265+
source.getBucket());
266+
}
267+
accessor.setProperty(persistentProperty, targetValue);
242268
}
243269

244270
} else if (persistentProperty.isEntity() && !conversionService.canConvert(byte[].class,
@@ -417,11 +443,11 @@ public void doWithPersistentProperty(KeyValuePersistentProperty persistentProper
417443
if (property == null || Iterable.class.isAssignableFrom(property.getClass())) {
418444

419445
writeCollection(keyspace, propertyStringPath, (Iterable<?>) property,
420-
persistentProperty.getTypeInformation().getComponentType(), sink);
446+
persistentProperty.getTypeInformation().getComponentType(), sink);
421447
} else if (property.getClass().isArray()) {
422448

423449
writeCollection(keyspace, propertyStringPath, Arrays.asList((Object[]) property),
424-
persistentProperty.getTypeInformation().getComponentType(), sink);
450+
persistentProperty.getTypeInformation().getComponentType(), sink);
425451
} else {
426452

427453
throw new RuntimeException("Don't know how to handle " + property.getClass() + " type collection");

src/test/java/org/springframework/data/redis/core/convert/ConversionTestEntities.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,11 @@ public static class Size {
160160
int length;
161161
}
162162

163+
public static class WithArrays {
164+
165+
Object[] arrayOfObject;
166+
String[] arrayOfSimpleTypes;
167+
Species[] arrayOfCompexTypes;
168+
}
169+
163170
}

src/test/java/org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.Map;
4848
import java.util.UUID;
4949

50+
import org.hamcrest.core.IsEqual;
5051
import org.junit.Before;
5152
import org.junit.Test;
5253
import org.junit.runner.RunWith;
@@ -66,6 +67,7 @@
6667
import org.springframework.data.redis.core.convert.ConversionTestEntities.Species;
6768
import org.springframework.data.redis.core.convert.ConversionTestEntities.TaVeren;
6869
import org.springframework.data.redis.core.convert.ConversionTestEntities.TheWheelOfTime;
70+
import org.springframework.data.redis.core.convert.ConversionTestEntities.WithArrays;
6971
import org.springframework.data.redis.core.convert.KeyspaceConfiguration.KeyspaceSettings;
7072
import org.springframework.data.redis.core.mapping.RedisMappingContext;
7173
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@@ -437,27 +439,6 @@ public void writeAppendsMapWithSimpleKeyCorrectly() {
437439
.containingUtf8String("physicalAttributes.[eye-color]", "grey"));
438440
}
439441

440-
/**
441-
* @see DATAREDIS-492
442-
*/
443-
@Test
444-
public void writeHandlesArraysProperly() {
445-
446-
this.converter = new MappingRedisConverter(null, null, resolverMock);
447-
this.converter
448-
.setCustomConversions(new CustomConversions(Collections.singletonList(new ListToByteConverter())));
449-
this.converter.afterPropertiesSet();
450-
451-
Map<String, Object> innerMap = new LinkedHashMap<String, Object>();
452-
innerMap.put("address", "tyrionl@netflix.com");
453-
innerMap.put("when", new String[]{"pipeline.failed"});
454-
455-
Map<String, Object> map = new LinkedHashMap<String, Object>();
456-
map.put("email", Collections.singletonList(innerMap));
457-
458-
RedisData target = write(map);
459-
}
460-
461442
/**
462443
* @see DATAREDIS-425
463444
*/
@@ -1348,13 +1329,115 @@ public void readShouldConsiderMapConvertersForValuesInList() {
13481329
assertThat(target.species.get(0).name, is("trolloc"));
13491330
}
13501331

1332+
/**
1333+
* @see DATAREDIS-492
1334+
*/
1335+
@Test
1336+
public void writeHandlesArraysProperly() {
1337+
1338+
this.converter = new MappingRedisConverter(null, null, resolverMock);
1339+
this.converter.setCustomConversions(new CustomConversions(Collections.singletonList(new ListToByteConverter())));
1340+
this.converter.afterPropertiesSet();
1341+
1342+
Map<String, Object> innerMap = new LinkedHashMap<String, Object>();
1343+
innerMap.put("address", "tyrionl@netflix.com");
1344+
innerMap.put("when", new String[] { "pipeline.failed" });
1345+
1346+
Map<String, Object> map = new LinkedHashMap<String, Object>();
1347+
map.put("email", Collections.singletonList(innerMap));
1348+
1349+
RedisData target = write(map);
1350+
}
1351+
1352+
/**
1353+
* @see DATAREDIS-492
1354+
*/
1355+
@Test
1356+
public void writeHandlesArraysOfSimpleTypeProperly() {
1357+
1358+
WithArrays source = new WithArrays();
1359+
source.arrayOfSimpleTypes = new String[] { "rand", "mat", "perrin" };
1360+
1361+
assertThat(write(source).getBucket(),
1362+
isBucket().containingUtf8String("arrayOfSimpleTypes.[0]", "rand")
1363+
.containingUtf8String("arrayOfSimpleTypes.[1]", "mat")
1364+
.containingUtf8String("arrayOfSimpleTypes.[2]", "perrin"));
1365+
}
1366+
1367+
/**
1368+
* @see DATAREDIS-492
1369+
*/
1370+
@Test
1371+
public void readHandlesArraysOfSimpleTypeProperly() {
1372+
1373+
Map<String, String> source = new LinkedHashMap<String, String>();
1374+
source.put("arrayOfSimpleTypes.[0]", "rand");
1375+
source.put("arrayOfSimpleTypes.[1]", "mat");
1376+
source.put("arrayOfSimpleTypes.[2]", "perrin");
1377+
1378+
WithArrays target = read(WithArrays.class, source);
1379+
1380+
assertThat(target.arrayOfSimpleTypes, IsEqual.equalTo(new String[] { "rand", "mat", "perrin" }));
1381+
}
1382+
1383+
/**
1384+
* @see DATAREDIS-492
1385+
*/
1386+
@Test
1387+
public void writeHandlesArraysOfComplexTypeProperly() {
1388+
1389+
WithArrays source = new WithArrays();
1390+
1391+
Species trolloc = new Species();
1392+
trolloc.name = "trolloc";
1393+
1394+
Species myrddraal = new Species();
1395+
myrddraal.name = "myrddraal";
1396+
myrddraal.alsoKnownAs = Arrays.asList("halfmen", "fades", "neverborn");
1397+
1398+
source.arrayOfCompexTypes = new Species[] { trolloc, myrddraal };
1399+
1400+
assertThat(write(source).getBucket(),
1401+
isBucket().containingUtf8String("arrayOfCompexTypes.[0].name", "trolloc") //
1402+
.containingUtf8String("arrayOfCompexTypes.[1].name", "myrddraal") //
1403+
.containingUtf8String("arrayOfCompexTypes.[1].alsoKnownAs.[0]", "halfmen") //
1404+
.containingUtf8String("arrayOfCompexTypes.[1].alsoKnownAs.[1]", "fades") //
1405+
.containingUtf8String("arrayOfCompexTypes.[1].alsoKnownAs.[2]", "neverborn"));
1406+
}
1407+
1408+
/**
1409+
* @see DATAREDIS-492
1410+
*/
1411+
@Test
1412+
public void readHandlesArraysOfComplexTypeProperly() {
1413+
1414+
Map<String, String> source = new LinkedHashMap<String, String>();
1415+
source.put("arrayOfCompexTypes.[0].name", "trolloc");
1416+
source.put("arrayOfCompexTypes.[1].name", "myrddraal");
1417+
source.put("arrayOfCompexTypes.[1].alsoKnownAs.[0]", "halfmen");
1418+
source.put("arrayOfCompexTypes.[1].alsoKnownAs.[1]", "fades");
1419+
source.put("arrayOfCompexTypes.[1].alsoKnownAs.[2]", "neverborn");
1420+
1421+
WithArrays target = read(WithArrays.class, source);
1422+
1423+
assertThat(target.arrayOfCompexTypes[0], notNullValue());
1424+
assertThat(target.arrayOfCompexTypes[0].name, is("trolloc"));
1425+
assertThat(target.arrayOfCompexTypes[1], notNullValue());
1426+
assertThat(target.arrayOfCompexTypes[1].name, is("myrddraal"));
1427+
assertThat(target.arrayOfCompexTypes[1].alsoKnownAs, contains("halfmen", "fades", "neverborn"));
1428+
}
1429+
13511430
private RedisData write(Object source) {
13521431

13531432
RedisData rdo = new RedisData();
13541433
converter.write(source, rdo);
13551434
return rdo;
13561435
}
13571436

1437+
private <T> T read(Class<T> type, Map<String, String> source) {
1438+
return converter.read(type, new RedisData(Bucket.newBucketFromStringMap(source)));
1439+
}
1440+
13581441
@WritingConverter
13591442
static class AddressToBytesConverter implements Converter<Address, byte[]> {
13601443

@@ -1408,8 +1491,8 @@ static class ListToByteConverter implements Converter<List, byte[]> {
14081491

14091492
mapper = new ObjectMapper();
14101493
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
1411-
.withFieldVisibility(Visibility.ANY).withGetterVisibility(Visibility.NONE)
1412-
.withSetterVisibility(Visibility.NONE).withCreatorVisibility(Visibility.NONE));
1494+
.withFieldVisibility(Visibility.ANY).withGetterVisibility(Visibility.NONE)
1495+
.withSetterVisibility(Visibility.NONE).withCreatorVisibility(Visibility.NONE));
14131496

14141497
serializer = new Jackson2JsonRedisSerializer<List>(List.class);
14151498
serializer.setObjectMapper(mapper);
@@ -1426,7 +1509,6 @@ public byte[] convert(List source) {
14261509
}
14271510
}
14281511

1429-
14301512
@ReadingConverter
14311513
static class MapToSpeciesConverter implements Converter<Map<String, byte[]>, Species> {
14321514

0 commit comments

Comments
 (0)