|
|
|
@ -157,6 +157,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToPrimitiveArray() { |
|
|
|
public void convertArrayToPrimitiveArray() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Object[].class, new ArrayToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
int[] result = conversionService.convert(new String[] { "1", "2", "3" }, int[].class); |
|
|
|
int[] result = conversionService.convert(new String[] { "1", "2", "3" }, int[].class); |
|
|
|
assertEquals(1, result[0]); |
|
|
|
assertEquals(1, result[0]); |
|
|
|
@ -174,6 +176,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToListInterface() { |
|
|
|
public void convertArrayToListInterface() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
List<?> result = conversionService.convert(new String[] { "1", "2", "3" }, List.class); |
|
|
|
List<?> result = conversionService.convert(new String[] { "1", "2", "3" }, List.class); |
|
|
|
assertEquals("1", result.get(0)); |
|
|
|
assertEquals("1", result.get(0)); |
|
|
|
assertEquals("2", result.get(1)); |
|
|
|
assertEquals("2", result.get(1)); |
|
|
|
@ -184,6 +188,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToListGenericTypeConversion() throws Exception { |
|
|
|
public void convertArrayToListGenericTypeConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor |
|
|
|
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor |
|
|
|
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); |
|
|
|
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); |
|
|
|
@ -194,6 +200,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToListImpl() { |
|
|
|
public void convertArrayToListImpl() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
LinkedList<?> result = conversionService.convert(new String[] { "1", "2", "3" }, LinkedList.class); |
|
|
|
LinkedList<?> result = conversionService.convert(new String[] { "1", "2", "3" }, LinkedList.class); |
|
|
|
assertEquals("1", result.get(0)); |
|
|
|
assertEquals("1", result.get(0)); |
|
|
|
assertEquals("2", result.get(1)); |
|
|
|
assertEquals("2", result.get(1)); |
|
|
|
@ -202,17 +210,43 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = ConversionFailedException.class) |
|
|
|
@Test(expected = ConversionFailedException.class) |
|
|
|
public void convertArrayToAbstractList() { |
|
|
|
public void convertArrayToAbstractList() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.convert(new String[] { "1", "2", "3" }, AbstractList.class); |
|
|
|
conversionService.convert(new String[] { "1", "2", "3" }, AbstractList.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertArrayToMap() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(conversionService)); |
|
|
|
|
|
|
|
Map result = conversionService.convert(new String[] { "foo=bar", "bar=baz", "baz=boop" }, Map.class); |
|
|
|
|
|
|
|
assertEquals("bar", result.get("foo")); |
|
|
|
|
|
|
|
assertEquals("baz", result.get("bar")); |
|
|
|
|
|
|
|
assertEquals("boop", result.get("baz")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertArrayToMapWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(conversionService)); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToEnumConverterFactory()); |
|
|
|
|
|
|
|
Map result = (Map) conversionService.convert(new String[] { "1=BAR", "2=BAZ" }, TypeDescriptor |
|
|
|
|
|
|
|
.valueOf(String[].class), new TypeDescriptor(getClass().getField("genericMap"))); |
|
|
|
|
|
|
|
assertEquals(FooEnum.BAR, result.get(1)); |
|
|
|
|
|
|
|
assertEquals(FooEnum.BAZ, result.get(2)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToString() { |
|
|
|
public void convertArrayToString() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class); |
|
|
|
String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class); |
|
|
|
assertEquals("1,2,3", result); |
|
|
|
assertEquals("1,2,3", result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToStringWithElementConversion() { |
|
|
|
public void convertArrayToStringWithElementConversion() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
String result = conversionService.convert(new Integer[] { 1, 2, 3 }, String.class); |
|
|
|
String result = conversionService.convert(new Integer[] { 1, 2, 3 }, String.class); |
|
|
|
assertEquals("1,2,3", result); |
|
|
|
assertEquals("1,2,3", result); |
|
|
|
@ -220,12 +254,16 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertEmptyArrayToString() { |
|
|
|
public void convertEmptyArrayToString() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
String result = conversionService.convert(new String[0], String.class); |
|
|
|
String result = conversionService.convert(new String[0], String.class); |
|
|
|
assertEquals("", result); |
|
|
|
assertEquals("", result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToObject() { |
|
|
|
public void convertArrayToObject() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
Object[] array = new Object[] { 3L }; |
|
|
|
Object[] array = new Object[] { 3L }; |
|
|
|
Object result = conversionService.convert(array, Object.class); |
|
|
|
Object result = conversionService.convert(array, Object.class); |
|
|
|
assertEquals(3L, result); |
|
|
|
assertEquals(3L, result); |
|
|
|
@ -233,6 +271,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertArrayToObjectWithElementConversion() { |
|
|
|
public void convertArrayToObjectWithElementConversion() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
String[] array = new String[] { "3" }; |
|
|
|
String[] array = new String[] { "3" }; |
|
|
|
Integer result = conversionService.convert(array, Integer.class); |
|
|
|
Integer result = conversionService.convert(array, Integer.class); |
|
|
|
@ -241,6 +281,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToArray() { |
|
|
|
public void convertCollectionToArray() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
List<String> list = new ArrayList<String>(); |
|
|
|
List<String> list = new ArrayList<String>(); |
|
|
|
list.add("1"); |
|
|
|
list.add("1"); |
|
|
|
list.add("2"); |
|
|
|
list.add("2"); |
|
|
|
@ -253,6 +295,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToArrayWithElementConversion() { |
|
|
|
public void convertCollectionToArrayWithElementConversion() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
List<String> list = new ArrayList<String>(); |
|
|
|
List<String> list = new ArrayList<String>(); |
|
|
|
list.add("1"); |
|
|
|
list.add("1"); |
|
|
|
@ -266,6 +310,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToCollection() throws Exception { |
|
|
|
public void convertCollectionToCollection() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
Set<String> foo = new LinkedHashSet<String>(); |
|
|
|
Set<String> foo = new LinkedHashSet<String>(); |
|
|
|
foo.add("1"); |
|
|
|
foo.add("1"); |
|
|
|
@ -280,6 +326,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToCollectionNull() throws Exception { |
|
|
|
public void convertCollectionToCollectionNull() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
List<Integer> bar = (List<Integer>) conversionService.convert(null, |
|
|
|
List<Integer> bar = (List<Integer>) conversionService.convert(null, |
|
|
|
TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList"))); |
|
|
|
TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList"))); |
|
|
|
assertNull(bar); |
|
|
|
assertNull(bar); |
|
|
|
@ -287,6 +335,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToCollectionNotGeneric() throws Exception { |
|
|
|
public void convertCollectionToCollectionNotGeneric() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
Set<String> foo = new LinkedHashSet<String>(); |
|
|
|
Set<String> foo = new LinkedHashSet<String>(); |
|
|
|
foo.add("1"); |
|
|
|
foo.add("1"); |
|
|
|
@ -301,6 +351,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToCollectionSpecialCaseSourceImpl() throws Exception { |
|
|
|
public void convertCollectionToCollectionSpecialCaseSourceImpl() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
Map map = new LinkedHashMap(); |
|
|
|
Map map = new LinkedHashMap(); |
|
|
|
map.put("1", "1"); |
|
|
|
map.put("1", "1"); |
|
|
|
@ -315,8 +367,39 @@ public class GenericConversionServiceTests { |
|
|
|
assertEquals(new Integer(3), bar.get(2)); |
|
|
|
assertEquals(new Integer(3), bar.get(2)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertCollectionToMap() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
|
|
|
|
List<String> list = new ArrayList<String>(); |
|
|
|
|
|
|
|
list.add("foo=bar"); |
|
|
|
|
|
|
|
list.add("bar=baz"); |
|
|
|
|
|
|
|
list.add("baz=boop"); |
|
|
|
|
|
|
|
Map result = conversionService.convert(list, Map.class); |
|
|
|
|
|
|
|
assertEquals("bar", result.get("foo")); |
|
|
|
|
|
|
|
assertEquals("baz", result.get("bar")); |
|
|
|
|
|
|
|
assertEquals("boop", result.get("baz")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertCollectionToMapWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToEnumConverterFactory()); |
|
|
|
|
|
|
|
List<String> list = new ArrayList<String>(); |
|
|
|
|
|
|
|
list.add("1=BAR"); |
|
|
|
|
|
|
|
list.add("2=BAZ"); |
|
|
|
|
|
|
|
Map result = (Map) conversionService.convert(list, TypeDescriptor.valueOf(List.class), new TypeDescriptor( |
|
|
|
|
|
|
|
getClass().getField("genericMap"))); |
|
|
|
|
|
|
|
assertEquals(FooEnum.BAR, result.get(1)); |
|
|
|
|
|
|
|
assertEquals(FooEnum.BAZ, result.get(2)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToString() { |
|
|
|
public void convertCollectionToString() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
List<String> list = Arrays.asList(new String[] { "foo", "bar" }); |
|
|
|
List<String> list = Arrays.asList(new String[] { "foo", "bar" }); |
|
|
|
String result = conversionService.convert(list, String.class); |
|
|
|
String result = conversionService.convert(list, String.class); |
|
|
|
assertEquals("foo,bar", result); |
|
|
|
assertEquals("foo,bar", result); |
|
|
|
@ -324,6 +407,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToStringWithElementConversion() throws Exception { |
|
|
|
public void convertCollectionToStringWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
List<Integer> list = Arrays.asList(new Integer[] { 3, 5 }); |
|
|
|
List<Integer> list = Arrays.asList(new Integer[] { 3, 5 }); |
|
|
|
String result = (String) conversionService.convert(list, |
|
|
|
String result = (String) conversionService.convert(list, |
|
|
|
@ -333,6 +418,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToObject() { |
|
|
|
public void convertCollectionToObject() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
List<Long> list = Collections.singletonList(3L); |
|
|
|
List<Long> list = Collections.singletonList(3L); |
|
|
|
Long result = conversionService.convert(list, Long.class); |
|
|
|
Long result = conversionService.convert(list, Long.class); |
|
|
|
assertEquals(new Long(3), result); |
|
|
|
assertEquals(new Long(3), result); |
|
|
|
@ -340,6 +427,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertCollectionToObjectWithElementConversion() { |
|
|
|
public void convertCollectionToObjectWithElementConversion() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
List<String> list = Collections.singletonList("3"); |
|
|
|
List<String> list = Collections.singletonList("3"); |
|
|
|
Integer result = conversionService.convert(list, Integer.class); |
|
|
|
Integer result = conversionService.convert(list, Integer.class); |
|
|
|
@ -350,11 +439,12 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertMapToMap() throws Exception { |
|
|
|
public void convertMapToMap() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Map.class, Map.class, new MapToMapConverter(conversionService)); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToEnumConverterFactory()); |
|
|
|
Map<String, String> foo = new HashMap<String, String>(); |
|
|
|
Map<String, String> foo = new HashMap<String, String>(); |
|
|
|
foo.put("1", "BAR"); |
|
|
|
foo.put("1", "BAR"); |
|
|
|
foo.put("2", "BAZ"); |
|
|
|
foo.put("2", "BAZ"); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToEnumConverterFactory()); |
|
|
|
|
|
|
|
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, TypeDescriptor |
|
|
|
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, TypeDescriptor |
|
|
|
.valueOf(Map.class), new TypeDescriptor(getClass().getField("genericMap"))); |
|
|
|
.valueOf(Map.class), new TypeDescriptor(getClass().getField("genericMap"))); |
|
|
|
assertEquals(FooEnum.BAR, map.get(1)); |
|
|
|
assertEquals(FooEnum.BAR, map.get(1)); |
|
|
|
@ -363,10 +453,10 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertMapToStringArray() throws Exception { |
|
|
|
public void convertMapToStringArray() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(conversionService)); |
|
|
|
Map<String, String> foo = new LinkedHashMap<String, String>(); |
|
|
|
Map<String, String> foo = new LinkedHashMap<String, String>(); |
|
|
|
foo.put("1", "BAR"); |
|
|
|
foo.put("1", "BAR"); |
|
|
|
foo.put("2", "BAZ"); |
|
|
|
foo.put("2", "BAZ"); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
|
|
|
String[] result = conversionService.convert(foo, String[].class); |
|
|
|
String[] result = conversionService.convert(foo, String[].class); |
|
|
|
assertEquals("1=BAR", result[0]); |
|
|
|
assertEquals("1=BAR", result[0]); |
|
|
|
assertEquals("2=BAZ", result[1]); |
|
|
|
assertEquals("2=BAZ", result[1]); |
|
|
|
@ -374,19 +464,67 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertMapToStringArrayWithElementConversion() throws Exception { |
|
|
|
public void convertMapToStringArrayWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(conversionService)); |
|
|
|
|
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>(); |
|
|
|
Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>(); |
|
|
|
foo.put(1, FooEnum.BAR); |
|
|
|
foo.put(1, FooEnum.BAR); |
|
|
|
foo.put(2, FooEnum.BAZ); |
|
|
|
foo.put(2, FooEnum.BAZ); |
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
|
|
|
|
String[] result = (String[]) conversionService.convert(foo, new TypeDescriptor(getClass() |
|
|
|
String[] result = (String[]) conversionService.convert(foo, new TypeDescriptor(getClass() |
|
|
|
.getField("genericMap")), TypeDescriptor.valueOf(String[].class)); |
|
|
|
.getField("genericMap")), TypeDescriptor.valueOf(String[].class)); |
|
|
|
assertEquals("1=BAR", result[0]); |
|
|
|
assertEquals("1=BAR", result[0]); |
|
|
|
assertEquals("2=BAZ", result[1]); |
|
|
|
assertEquals("2=BAZ", result[1]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertMapToString() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService)); |
|
|
|
|
|
|
|
Map<String, String> foo = new LinkedHashMap<String, String>(); |
|
|
|
|
|
|
|
foo.put("1", "BAR"); |
|
|
|
|
|
|
|
foo.put("2", "BAZ"); |
|
|
|
|
|
|
|
String result = conversionService.convert(foo, String.class); |
|
|
|
|
|
|
|
assertEquals("1=BAR 2=BAZ", result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertMapToStringWithConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService)); |
|
|
|
|
|
|
|
Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>(); |
|
|
|
|
|
|
|
foo.put(1, FooEnum.BAR); |
|
|
|
|
|
|
|
foo.put(2, FooEnum.BAZ); |
|
|
|
|
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
|
|
|
|
String result = (String) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")), |
|
|
|
|
|
|
|
TypeDescriptor.valueOf(String.class)); |
|
|
|
|
|
|
|
assertEquals("1=BAR 2=BAZ", result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertMapToObject() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService)); |
|
|
|
|
|
|
|
Map<Long, Long> foo = new LinkedHashMap<Long, Long>(); |
|
|
|
|
|
|
|
foo.put(1L, 1L); |
|
|
|
|
|
|
|
foo.put(2L, 2L); |
|
|
|
|
|
|
|
Long result = conversionService.convert(foo, Long.class); |
|
|
|
|
|
|
|
assertEquals(new Long(1), result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<Long, Long> genericMap2 = new HashMap<Long, Long>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertMapToObjectWithConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Map.class, Object.class, new MapToObjectConverter(conversionService)); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
|
|
|
|
Map<Long, Long> foo = new LinkedHashMap<Long, Long>(); |
|
|
|
|
|
|
|
foo.put(1L, 1L); |
|
|
|
|
|
|
|
foo.put(2L, 2L); |
|
|
|
|
|
|
|
Integer result = (Integer) conversionService.convert(foo, |
|
|
|
|
|
|
|
new TypeDescriptor(getClass().getField("genericMap2")), TypeDescriptor.valueOf(Integer.class)); |
|
|
|
|
|
|
|
assertEquals(new Integer(1), result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertStringToArray() { |
|
|
|
public void convertStringToArray() { |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
String[] result = conversionService.convert("1,2,3", String[].class); |
|
|
|
String[] result = conversionService.convert("1,2,3", String[].class); |
|
|
|
assertEquals(3, result.length); |
|
|
|
assertEquals(3, result.length); |
|
|
|
assertEquals("1", result[0]); |
|
|
|
assertEquals("1", result[0]); |
|
|
|
@ -396,6 +534,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertStringToArrayWithElementConversion() { |
|
|
|
public void convertStringToArrayWithElementConversion() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
Integer[] result = conversionService.convert("1,2,3", Integer[].class); |
|
|
|
Integer[] result = conversionService.convert("1,2,3", Integer[].class); |
|
|
|
assertEquals(3, result.length); |
|
|
|
assertEquals(3, result.length); |
|
|
|
@ -406,14 +546,35 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertEmptyStringToArray() { |
|
|
|
public void convertEmptyStringToArray() { |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
String[] result = conversionService.convert("", String[].class); |
|
|
|
String[] result = conversionService.convert("", String[].class); |
|
|
|
assertEquals(0, result.length); |
|
|
|
assertEquals(0, result.length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertObjectToArray() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
|
|
|
|
Object[] result = conversionService.convert(3L, Object[].class); |
|
|
|
|
|
|
|
assertEquals(1, result.length); |
|
|
|
|
|
|
|
assertEquals(3L, result[0]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertObjectToArrayWithElementConversion() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
|
|
|
|
Integer[] result = conversionService.convert(3L, Integer[].class); |
|
|
|
|
|
|
|
assertEquals(1, result.length); |
|
|
|
|
|
|
|
assertEquals(new Integer(3), result[0]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertStringToCollection() { |
|
|
|
public void convertStringToCollection() { |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
List result = conversionService.convert("1,2,3", List.class); |
|
|
|
List result = conversionService.convert("1,2,3", List.class); |
|
|
|
assertEquals(3, result.size()); |
|
|
|
assertEquals(3, result.size()); |
|
|
|
assertEquals("1", result.get(0)); |
|
|
|
assertEquals("1", result.get(0)); |
|
|
|
@ -423,6 +584,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertStringToCollectionWithElementConversion() throws Exception { |
|
|
|
public void convertStringToCollectionWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), |
|
|
|
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), |
|
|
|
new TypeDescriptor(getClass().getField("genericList"))); |
|
|
|
new TypeDescriptor(getClass().getField("genericList"))); |
|
|
|
@ -434,13 +597,16 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertEmptyStringToCollection() { |
|
|
|
public void convertEmptyStringToCollection() { |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter( |
|
|
|
String[] result = conversionService.convert("", String[].class); |
|
|
|
conversionService)); |
|
|
|
assertEquals(0, result.length); |
|
|
|
Collection result = conversionService.convert("", Collection.class); |
|
|
|
|
|
|
|
assertEquals(0, result.size()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertObjectToCollection() { |
|
|
|
public void convertObjectToCollection() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
List<String> result = (List<String>) conversionService.convert(3L, List.class); |
|
|
|
List<String> result = (List<String>) conversionService.convert(3L, List.class); |
|
|
|
assertEquals(1, result.size()); |
|
|
|
assertEquals(1, result.size()); |
|
|
|
assertEquals(3L, result.get(0)); |
|
|
|
assertEquals(3L, result.get(0)); |
|
|
|
@ -448,6 +614,8 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertObjectToCollectionWithElementConversion() throws Exception { |
|
|
|
public void convertObjectToCollectionWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class), |
|
|
|
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class), |
|
|
|
new TypeDescriptor(getClass().getField("genericList"))); |
|
|
|
new TypeDescriptor(getClass().getField("genericList"))); |
|
|
|
@ -455,56 +623,9 @@ public class GenericConversionServiceTests { |
|
|
|
assertEquals(new Integer(3), result.get(0)); |
|
|
|
assertEquals(new Integer(3), result.get(0)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertObjectToArray() { |
|
|
|
|
|
|
|
Object[] result = conversionService.convert(3L, Object[].class); |
|
|
|
|
|
|
|
assertEquals(1, result.length); |
|
|
|
|
|
|
|
assertEquals(3L, result[0]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertObjectToArrayWithElementConversion() { |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
|
|
|
|
Integer[] result = conversionService.convert(3L, Integer[].class); |
|
|
|
|
|
|
|
assertEquals(1, result.length); |
|
|
|
|
|
|
|
assertEquals(new Integer(3), result[0]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertObjectToMap() { |
|
|
|
|
|
|
|
Map result = conversionService.convert("foo=bar bar=baz", Map.class); |
|
|
|
|
|
|
|
assertEquals("bar", result.get("foo")); |
|
|
|
|
|
|
|
assertEquals("baz", result.get("bar")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertObjectToMapWithConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
|
|
|
|
Map result = (Map) conversionService.convert(1L, TypeDescriptor.valueOf(Integer.class), new TypeDescriptor( |
|
|
|
|
|
|
|
getClass().getField("genericMap2"))); |
|
|
|
|
|
|
|
assertEquals(new Long(1), result.get(1L)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertStringArrayToMap() { |
|
|
|
|
|
|
|
Map result = conversionService.convert(new String[] { "foo=bar", "bar=baz", "baz=boop" }, Map.class); |
|
|
|
|
|
|
|
assertEquals("bar", result.get("foo")); |
|
|
|
|
|
|
|
assertEquals("baz", result.get("bar")); |
|
|
|
|
|
|
|
assertEquals("boop", result.get("baz")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertStringArrayToMapWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new StringToEnumConverterFactory()); |
|
|
|
|
|
|
|
Map result = (Map) conversionService.convert(new String[] { "1=BAR", "2=BAZ" }, TypeDescriptor |
|
|
|
|
|
|
|
.valueOf(String[].class), new TypeDescriptor(getClass().getField("genericMap"))); |
|
|
|
|
|
|
|
assertEquals(FooEnum.BAR, result.get(1)); |
|
|
|
|
|
|
|
assertEquals(FooEnum.BAZ, result.get(2)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertStringToMap() { |
|
|
|
public void convertStringToMap() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService)); |
|
|
|
Map result = conversionService.convert("foo=bar bar=baz baz=boop", Map.class); |
|
|
|
Map result = conversionService.convert("foo=bar bar=baz baz=boop", Map.class); |
|
|
|
assertEquals("bar", result.get("foo")); |
|
|
|
assertEquals("bar", result.get("foo")); |
|
|
|
assertEquals("baz", result.get("bar")); |
|
|
|
assertEquals("baz", result.get("bar")); |
|
|
|
@ -513,6 +634,7 @@ public class GenericConversionServiceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertStringToMapWithElementConversion() throws Exception { |
|
|
|
public void convertStringToMapWithElementConversion() throws Exception { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService)); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToEnumConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new StringToEnumConverterFactory()); |
|
|
|
Map result = (Map) conversionService.convert("1=BAR 2=BAZ", TypeDescriptor.valueOf(String.class), |
|
|
|
Map result = (Map) conversionService.convert("1=BAR 2=BAZ", TypeDescriptor.valueOf(String.class), |
|
|
|
@ -522,49 +644,26 @@ public class GenericConversionServiceTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertMapToString() { |
|
|
|
public void convertObjectToMap() { |
|
|
|
Map<String, String> foo = new LinkedHashMap<String, String>(); |
|
|
|
conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService)); |
|
|
|
foo.put("1", "BAR"); |
|
|
|
Map result = conversionService.convert("foo=bar bar=baz", Map.class); |
|
|
|
foo.put("2", "BAZ"); |
|
|
|
assertEquals("bar", result.get("foo")); |
|
|
|
String result = conversionService.convert(foo, String.class); |
|
|
|
assertEquals("baz", result.get("bar")); |
|
|
|
assertEquals("1=BAR 2=BAZ", result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertMapToStringWithConversion() throws Exception { |
|
|
|
|
|
|
|
Map<Integer, FooEnum> foo = new LinkedHashMap<Integer, FooEnum>(); |
|
|
|
|
|
|
|
foo.put(1, FooEnum.BAR); |
|
|
|
|
|
|
|
foo.put(2, FooEnum.BAZ); |
|
|
|
|
|
|
|
conversionService.addConverter(new ObjectToStringConverter()); |
|
|
|
|
|
|
|
String result = (String) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")), |
|
|
|
|
|
|
|
TypeDescriptor.valueOf(String.class)); |
|
|
|
|
|
|
|
assertEquals("1=BAR 2=BAZ", result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertMapToObject() { |
|
|
|
|
|
|
|
Map<Long, Long> foo = new LinkedHashMap<Long, Long>(); |
|
|
|
|
|
|
|
foo.put(1L, 1L); |
|
|
|
|
|
|
|
foo.put(2L, 2L); |
|
|
|
|
|
|
|
Long result = conversionService.convert(foo, Long.class); |
|
|
|
|
|
|
|
assertEquals(new Long(1), result); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Map<Long, Long> genericMap2 = new HashMap<Long, Long>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void convertMapToObjectWithConversion() throws Exception { |
|
|
|
public void convertObjectToMapWithConversion() throws Exception { |
|
|
|
Map<Long, Long> foo = new LinkedHashMap<Long, Long>(); |
|
|
|
conversionService.addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(conversionService)); |
|
|
|
foo.put(1L, 1L); |
|
|
|
|
|
|
|
foo.put(2L, 2L); |
|
|
|
|
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
conversionService.addConverterFactory(new NumberToNumberConverterFactory()); |
|
|
|
Integer result = (Integer) conversionService.convert(foo, |
|
|
|
Map result = (Map) conversionService.convert(1L, TypeDescriptor.valueOf(Integer.class), new TypeDescriptor( |
|
|
|
new TypeDescriptor(getClass().getField("genericMap2")), TypeDescriptor.valueOf(Integer.class)); |
|
|
|
getClass().getField("genericMap2"))); |
|
|
|
assertEquals(new Integer(1), result); |
|
|
|
assertEquals(new Long(1), result.get(1L)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() { |
|
|
|
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() { |
|
|
|
|
|
|
|
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter( |
|
|
|
|
|
|
|
conversionService)); |
|
|
|
try { |
|
|
|
try { |
|
|
|
conversionService.convert("1", Integer[].class); |
|
|
|
conversionService.convert("1", Integer[].class); |
|
|
|
} catch (ConversionFailedException e) { |
|
|
|
} catch (ConversionFailedException e) { |
|
|
|
|