|
|
|
|
@ -36,9 +36,14 @@ import org.springframework.util.MultiValueMap;
@@ -36,9 +36,14 @@ import org.springframework.util.MultiValueMap;
|
|
|
|
|
import static org.hamcrest.Matchers.*; |
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Keith Donald |
|
|
|
|
* @author Phil Webb |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
*/ |
|
|
|
|
public class MapToMapConverterTests { |
|
|
|
|
|
|
|
|
|
private GenericConversionService conversionService = new GenericConversionService(); |
|
|
|
|
private final GenericConversionService conversionService = new GenericConversionService(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
@ -54,12 +59,15 @@ public class MapToMapConverterTests {
@@ -54,12 +59,15 @@ public class MapToMapConverterTests {
|
|
|
|
|
map.put("2", "37"); |
|
|
|
|
TypeDescriptor sourceType = TypeDescriptor.forObject(map); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget")); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
try { |
|
|
|
|
conversionService.convert(map, sourceType, targetType); |
|
|
|
|
} catch (ConversionFailedException e) { |
|
|
|
|
assertTrue(e.getCause() instanceof ConverterNotFoundException); |
|
|
|
|
} |
|
|
|
|
catch (ConversionFailedException ex) { |
|
|
|
|
assertTrue(ex.getCause() instanceof ConverterNotFoundException); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
@ -74,6 +82,7 @@ public class MapToMapConverterTests {
@@ -74,6 +82,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>(); |
|
|
|
|
map.put("1", "9"); |
|
|
|
|
map.put("2", "37"); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(Map.class, Map.class)); |
|
|
|
|
assertSame(map, conversionService.convert(map, Map.class)); |
|
|
|
|
} |
|
|
|
|
@ -85,12 +94,15 @@ public class MapToMapConverterTests {
@@ -85,12 +94,15 @@ public class MapToMapConverterTests {
|
|
|
|
|
map.put("2", "37"); |
|
|
|
|
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource")); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget")); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
try { |
|
|
|
|
conversionService.convert(map, sourceType, targetType); |
|
|
|
|
} catch (ConversionFailedException e) { |
|
|
|
|
assertTrue(e.getCause() instanceof ConverterNotFoundException); |
|
|
|
|
} |
|
|
|
|
catch (ConversionFailedException ex) { |
|
|
|
|
assertTrue(ex.getCause() instanceof ConverterNotFoundException); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
@ -107,12 +119,15 @@ public class MapToMapConverterTests {
@@ -107,12 +119,15 @@ public class MapToMapConverterTests {
|
|
|
|
|
map.put("2", Arrays.asList("37", "23")); |
|
|
|
|
TypeDescriptor sourceType = TypeDescriptor.forObject(map); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget")); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
try { |
|
|
|
|
conversionService.convert(map, sourceType, targetType); |
|
|
|
|
} catch (ConversionFailedException e) { |
|
|
|
|
assertTrue(e.getCause() instanceof ConverterNotFoundException); |
|
|
|
|
} |
|
|
|
|
catch (ConversionFailedException ex) { |
|
|
|
|
assertTrue(ex.getCause() instanceof ConverterNotFoundException); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); |
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
@ -130,6 +145,7 @@ public class MapToMapConverterTests {
@@ -130,6 +145,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
map.put("2", Arrays.asList("37", "23")); |
|
|
|
|
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget")); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget")); |
|
|
|
|
|
|
|
|
|
assertFalse(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
try { |
|
|
|
|
conversionService.convert(map, sourceType, targetType); |
|
|
|
|
@ -138,6 +154,7 @@ public class MapToMapConverterTests {
@@ -138,6 +154,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
catch (ConverterNotFoundException ex) { |
|
|
|
|
// expected
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); |
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
@ -153,6 +170,7 @@ public class MapToMapConverterTests {
@@ -153,6 +170,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
Map<String, List<String>> map = new HashMap<String, List<String>>(); |
|
|
|
|
map.put("1", Arrays.asList("9", "12")); |
|
|
|
|
map.put("2", Arrays.asList("37", "23")); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(Map.class, Map.class)); |
|
|
|
|
assertSame(map, conversionService.convert(map, Map.class)); |
|
|
|
|
} |
|
|
|
|
@ -164,6 +182,7 @@ public class MapToMapConverterTests {
@@ -164,6 +182,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
map.put("2", Arrays.asList("37", "23")); |
|
|
|
|
conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); |
|
|
|
|
conversionService.addConverter(new CollectionToObjectConverter(conversionService)); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(Map.class, Map.class)); |
|
|
|
|
assertSame(map, conversionService.convert(map, Map.class)); |
|
|
|
|
} |
|
|
|
|
@ -173,6 +192,7 @@ public class MapToMapConverterTests {
@@ -173,6 +192,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>(); |
|
|
|
|
TypeDescriptor sourceType = TypeDescriptor.forObject(map); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget")); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
assertSame(map, conversionService.convert(map, sourceType, targetType)); |
|
|
|
|
} |
|
|
|
|
@ -180,6 +200,7 @@ public class MapToMapConverterTests {
@@ -180,6 +200,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
@Test |
|
|
|
|
public void emptyMapNoTargetGenericInfo() throws Exception { |
|
|
|
|
Map<String, String> map = new HashMap<String, String>(); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(Map.class, Map.class)); |
|
|
|
|
assertSame(map, conversionService.convert(map, Map.class)); |
|
|
|
|
} |
|
|
|
|
@ -189,6 +210,7 @@ public class MapToMapConverterTests {
@@ -189,6 +210,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>(); |
|
|
|
|
TypeDescriptor sourceType = TypeDescriptor.forObject(map); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget")); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType); |
|
|
|
|
@ -205,6 +227,7 @@ public class MapToMapConverterTests {
@@ -205,6 +227,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)); |
|
|
|
|
TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class, |
|
|
|
|
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)); |
|
|
|
|
|
|
|
|
|
assertTrue(conversionService.canConvert(sourceType, targetType)); |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
Map<String, Integer> result = (Map<String, Integer>) conversionService.convert(map, sourceType, targetType); |
|
|
|
|
@ -220,6 +243,7 @@ public class MapToMapConverterTests {
@@ -220,6 +243,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
source.put("a", Arrays.asList(1, 2, 3)); |
|
|
|
|
source.put("b", Arrays.asList(4, 5, 6)); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget")); |
|
|
|
|
|
|
|
|
|
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType); |
|
|
|
|
assertThat(converted.size(), equalTo(2)); |
|
|
|
|
assertThat(converted.get("a"), equalTo(Arrays.asList("1", "2", "3"))); |
|
|
|
|
@ -234,6 +258,7 @@ public class MapToMapConverterTests {
@@ -234,6 +258,7 @@ public class MapToMapConverterTests {
|
|
|
|
|
source.put("a", 1); |
|
|
|
|
source.put("b", 2); |
|
|
|
|
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget")); |
|
|
|
|
|
|
|
|
|
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType); |
|
|
|
|
assertThat(converted.size(), equalTo(2)); |
|
|
|
|
assertThat(converted.get("a"), equalTo(Arrays.asList("1"))); |
|
|
|
|
@ -249,23 +274,12 @@ public class MapToMapConverterTests {
@@ -249,23 +274,12 @@ public class MapToMapConverterTests {
|
|
|
|
|
EnumMap<MyEnum, Integer> result = new EnumMap<MyEnum, Integer>(MyEnum.class); |
|
|
|
|
result.put(MyEnum.A, 1); |
|
|
|
|
result.put(MyEnum.C, 2); |
|
|
|
|
assertEquals(result, |
|
|
|
|
conversionService.convert(source, TypeDescriptor.forObject(source), new TypeDescriptor(getClass().getField("enumMap")))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("serial") |
|
|
|
|
public static class NoDefaultConstructorMap<K, V> extends HashMap<K, V> { |
|
|
|
|
|
|
|
|
|
public NoDefaultConstructorMap(Map<? extends K, ? extends V> map) { |
|
|
|
|
super(map); |
|
|
|
|
} |
|
|
|
|
assertEquals(result, conversionService.convert(source, |
|
|
|
|
TypeDescriptor.forObject(source), new TypeDescriptor(getClass().getField("enumMap")))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static enum MyEnum {A, B, C} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<Integer, Integer> scalarMapTarget; |
|
|
|
|
|
|
|
|
|
public Map<Integer, List<Integer>> collectionMapTarget; |
|
|
|
|
@ -283,4 +297,16 @@ public class MapToMapConverterTests {
@@ -283,4 +297,16 @@ public class MapToMapConverterTests {
|
|
|
|
|
|
|
|
|
|
public EnumMap<MyEnum, Integer> enumMap; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("serial") |
|
|
|
|
public static class NoDefaultConstructorMap<K, V> extends HashMap<K, V> { |
|
|
|
|
|
|
|
|
|
public NoDefaultConstructorMap(Map<? extends K, ? extends V> map) { |
|
|
|
|
super(map); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public enum MyEnum {A, B, C} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|