提交 64bd8b7f 编写于 作者: J Juergen Hoeller

Polishing

上级 387c8a81
...@@ -262,6 +262,25 @@ public class CollectionToCollectionConverterTests { ...@@ -262,6 +262,25 @@ public class CollectionToCollectionConverterTests {
} }
public ArrayList<Integer> scalarListTarget;
public List<Integer> emptyListTarget;
public LinkedList<Integer> emptyListDifferentTarget;
public List<List<List<Integer>>> objectToCollection;
public List<String> strings;
public List<?> list = Collections.emptyList();
public Collection<?> wildcardCollection = Collections.emptyList();
public List<Resource> resources;
public EnumSet<MyEnum> enumSet;
public static abstract class BaseResource implements Resource { public static abstract class BaseResource implements Resource {
@Override @Override
...@@ -330,25 +349,6 @@ public class CollectionToCollectionConverterTests { ...@@ -330,25 +349,6 @@ public class CollectionToCollectionConverterTests {
} }
public static enum MyEnum {A, B, C} public enum MyEnum {A, B, C}
public ArrayList<Integer> scalarListTarget;
public List<Integer> emptyListTarget;
public LinkedList<Integer> emptyListDifferentTarget;
public List<List<List<Integer>>> objectToCollection;
public List<String> strings;
public List<?> list = Collections.emptyList();
public Collection<?> wildcardCollection = Collections.emptyList();
public List<Resource> resources;
public EnumSet<MyEnum> enumSet;
} }
...@@ -313,7 +313,7 @@ public class DefaultConversionServiceTests { ...@@ -313,7 +313,7 @@ public class DefaultConversionServiceTests {
@Test @Test
public void convertArrayToCollectionInterface() { public void convertArrayToCollectionInterface() {
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));
assertEquals("3", result.get(2)); assertEquals("3", result.get(2));
...@@ -322,7 +322,7 @@ public class DefaultConversionServiceTests { ...@@ -322,7 +322,7 @@ public class DefaultConversionServiceTests {
@Test @Test
public void convertArrayToCollectionGenericTypeConversion() throws Exception { public void convertArrayToCollectionGenericTypeConversion() throws Exception {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
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")));
assertEquals(new Integer("1"), result.get(0)); assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1)); assertEquals(new Integer("2"), result.get(1));
...@@ -344,7 +344,7 @@ public class DefaultConversionServiceTests { ...@@ -344,7 +344,7 @@ public class DefaultConversionServiceTests {
ConverterRegistry registry = (conversionService); ConverterRegistry registry = (conversionService);
registry.addConverter(new ColorConverter()); registry.addConverter(new ColorConverter());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, List<Color> colors = (List<Color>) conversionService.convert(new String[] {"ffffff", "#000000"},
TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(String[].class),
new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0))); new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
assertEquals(2, colors.size()); assertEquals(2, colors.size());
...@@ -354,7 +354,7 @@ public class DefaultConversionServiceTests { ...@@ -354,7 +354,7 @@ public class DefaultConversionServiceTests {
@Test @Test
public void convertArrayToCollectionImpl() { public void convertArrayToCollectionImpl() {
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));
assertEquals("3", result.get(2)); assertEquals("3", result.get(2));
...@@ -371,13 +371,13 @@ public class DefaultConversionServiceTests { ...@@ -371,13 +371,13 @@ public class DefaultConversionServiceTests {
@Test @Test
public void convertArrayToString() { public void convertArrayToString() {
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() {
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);
} }
...@@ -422,21 +422,21 @@ public class DefaultConversionServiceTests { ...@@ -422,21 +422,21 @@ public class DefaultConversionServiceTests {
@Test @Test
public void convertArrayToObject() { public void convertArrayToObject() {
Object[] array = new Object[] { 3L }; Object[] array = new Object[] {3L};
Object result = conversionService.convert(array, Long.class); Object result = conversionService.convert(array, Long.class);
assertEquals(3L, result); assertEquals(3L, result);
} }
@Test @Test
public void convertArrayToObjectWithElementConversion() { public void convertArrayToObjectWithElementConversion() {
String[] array = new String[] { "3" }; String[] array = new String[] {"3"};
Integer result = conversionService.convert(array, Integer.class); Integer result = conversionService.convert(array, Integer.class);
assertEquals(new Integer(3), result); assertEquals(new Integer(3), result);
} }
@Test @Test
public void convertArrayToObjectAssignableTargetType() { public void convertArrayToObjectAssignableTargetType() {
Long[] array = new Long[] { 3L }; Long[] array = new Long[] {3L};
Long[] result = (Long[]) conversionService.convert(array, Object.class); Long[] result = (Long[]) conversionService.convert(array, Object.class);
assertArrayEquals(array, result); assertArrayEquals(array, result);
} }
...@@ -849,14 +849,14 @@ public class DefaultConversionServiceTests { ...@@ -849,14 +849,14 @@ public class DefaultConversionServiceTests {
} }
}); });
char[] converted = conversionService.convert("abc", char[].class); char[] converted = conversionService.convert("abc", char[].class);
assertThat(converted, equalTo(new char[] { 'a', 'b', 'c' })); assertThat(converted, equalTo(new char[] {'a', 'b', 'c'}));
} }
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly() { public void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly() {
String[][] grid = new String[][] { new String[] { "1", "2", "3", "4" }, new String[] { "5", "6", "7", "8" }, String[][] grid = new String[][] {new String[] {"1", "2", "3", "4"}, new String[] {"5", "6", "7", "8"},
new String[] { "9", "10", "11", "12" } }; new String[] {"9", "10", "11", "12"}};
List<String[]> converted = conversionService.convert(grid, List.class); List<String[]> converted = conversionService.convert(grid, List.class);
String[][] convertedBack = conversionService.convert(converted, String[][].class); String[][] convertedBack = conversionService.convert(converted, String[][].class);
assertArrayEquals(grid, convertedBack); assertArrayEquals(grid, convertedBack);
...@@ -865,16 +865,15 @@ public class DefaultConversionServiceTests { ...@@ -865,16 +865,15 @@ public class DefaultConversionServiceTests {
@Test @Test
public void convertCannotOptimizeArray() { public void convertCannotOptimizeArray() {
conversionService.addConverter(new Converter<Byte, Byte>() { conversionService.addConverter(new Converter<Byte, Byte>() {
@Override @Override
public Byte convert(Byte source) { public Byte convert(Byte source) {
return (byte) (source + 1); return (byte) (source + 1);
} }
}); });
byte[] byteArray = new byte[] { 1, 2, 3 }; byte[] byteArray = new byte[] {1, 2, 3};
byte[] converted = conversionService.convert(byteArray, byte[].class); byte[] converted = conversionService.convert(byteArray, byte[].class);
assertNotSame(byteArray, converted); assertNotSame(byteArray, converted);
assertTrue(Arrays.equals(new byte[] { 2, 3, 4 }, converted)); assertTrue(Arrays.equals(new byte[] {2, 3, 4}, converted));
} }
@Test @Test
...@@ -938,6 +937,7 @@ public class DefaultConversionServiceTests { ...@@ -938,6 +937,7 @@ public class DefaultConversionServiceTests {
public enum Foo { public enum Foo {
BAR, BAZ BAR, BAZ
} }
...@@ -964,7 +964,12 @@ public class DefaultConversionServiceTests { ...@@ -964,7 +964,12 @@ public class DefaultConversionServiceTests {
public class ColorConverter implements Converter<String, Color> { public class ColorConverter implements Converter<String, Color> {
@Override @Override
public Color convert(String source) { if (!source.startsWith("#")) source = "#" + source; return Color.decode(source); } public Color convert(String source) {
if (!source.startsWith("#")) {
source = "#" + source;
}
return Color.decode(source);
}
} }
...@@ -1031,8 +1036,8 @@ public class DefaultConversionServiceTests { ...@@ -1031,8 +1036,8 @@ public class DefaultConversionServiceTests {
private static class SSN { private static class SSN {
static int constructorCount = 0; static int constructorCount = 0;
static int toStringCount = 0;
static int toStringCount = 0;
static void reset() { static void reset() {
constructorCount = 0; constructorCount = 0;
......
...@@ -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.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/**
* @author Keith Donald
* @author Phil Webb
* @author Juergen Hoeller
*/
public class MapToMapConverterTests { public class MapToMapConverterTests {
private GenericConversionService conversionService = new GenericConversionService(); private final GenericConversionService conversionService = new GenericConversionService();
@Before @Before
...@@ -54,12 +59,15 @@ public class MapToMapConverterTests { ...@@ -54,12 +59,15 @@ public class MapToMapConverterTests {
map.put("2", "37"); map.put("2", "37");
TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
try { try {
conversionService.convert(map, sourceType, targetType); 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()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") @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<String, String> map = new HashMap<String, String>();
map.put("1", "9"); map.put("1", "9");
map.put("2", "37"); map.put("2", "37");
assertTrue(conversionService.canConvert(Map.class, Map.class)); assertTrue(conversionService.canConvert(Map.class, Map.class));
assertSame(map, conversionService.convert(map, 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"); map.put("2", "37");
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource")); TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
try { try {
conversionService.convert(map, sourceType, targetType); 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()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
...@@ -107,12 +119,15 @@ public class MapToMapConverterTests { ...@@ -107,12 +119,15 @@ public class MapToMapConverterTests {
map.put("2", Arrays.asList("37", "23")); map.put("2", Arrays.asList("37", "23"));
TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
try { try {
conversionService.convert(map, sourceType, targetType); 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.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType)); 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")); map.put("2", Arrays.asList("37", "23"));
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget")); TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
assertFalse(conversionService.canConvert(sourceType, targetType)); assertFalse(conversionService.canConvert(sourceType, targetType));
try { try {
conversionService.convert(map, sourceType, targetType); conversionService.convert(map, sourceType, targetType);
...@@ -138,6 +154,7 @@ public class MapToMapConverterTests { ...@@ -138,6 +154,7 @@ public class MapToMapConverterTests {
catch (ConverterNotFoundException ex) { catch (ConverterNotFoundException ex) {
// expected // expected
} }
conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType)); 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<String, List<String>> map = new HashMap<String, List<String>>();
map.put("1", Arrays.asList("9", "12")); map.put("1", Arrays.asList("9", "12"));
map.put("2", Arrays.asList("37", "23")); map.put("2", Arrays.asList("37", "23"));
assertTrue(conversionService.canConvert(Map.class, Map.class)); assertTrue(conversionService.canConvert(Map.class, Map.class));
assertSame(map, conversionService.convert(map, 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")); map.put("2", Arrays.asList("37", "23"));
conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverter(new CollectionToObjectConverter(conversionService)); conversionService.addConverter(new CollectionToObjectConverter(conversionService));
assertTrue(conversionService.canConvert(Map.class, Map.class)); assertTrue(conversionService.canConvert(Map.class, Map.class));
assertSame(map, conversionService.convert(map, 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>(); Map<String, String> map = new HashMap<String, String>();
TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
assertSame(map, conversionService.convert(map, sourceType, targetType)); assertSame(map, conversionService.convert(map, sourceType, targetType));
} }
...@@ -180,6 +200,7 @@ public class MapToMapConverterTests { ...@@ -180,6 +200,7 @@ public class MapToMapConverterTests {
@Test @Test
public void emptyMapNoTargetGenericInfo() throws Exception { public void emptyMapNoTargetGenericInfo() throws Exception {
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
assertTrue(conversionService.canConvert(Map.class, Map.class)); assertTrue(conversionService.canConvert(Map.class, Map.class));
assertSame(map, conversionService.convert(map, 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>(); Map<String, String> map = new HashMap<String, String>();
TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType); 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.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class, TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class,
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)); TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Integer> result = (Map<String, Integer>) conversionService.convert(map, sourceType, targetType); 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("a", Arrays.asList(1, 2, 3));
source.put("b", Arrays.asList(4, 5, 6)); source.put("b", Arrays.asList(4, 5, 6));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType); MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size(), equalTo(2)); assertThat(converted.size(), equalTo(2));
assertThat(converted.get("a"), equalTo(Arrays.asList("1", "2", "3"))); 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("a", 1);
source.put("b", 2); source.put("b", 2);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType); MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size(), equalTo(2)); assertThat(converted.size(), equalTo(2));
assertThat(converted.get("a"), equalTo(Arrays.asList("1"))); 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); EnumMap<MyEnum, Integer> result = new EnumMap<MyEnum, Integer>(MyEnum.class);
result.put(MyEnum.A, 1); result.put(MyEnum.A, 1);
result.put(MyEnum.C, 2); 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) { assertEquals(result, conversionService.convert(source,
super(map); TypeDescriptor.forObject(source), new TypeDescriptor(getClass().getField("enumMap"))));
}
} }
public static enum MyEnum {A, B, C}
public Map<Integer, Integer> scalarMapTarget; public Map<Integer, Integer> scalarMapTarget;
public Map<Integer, List<Integer>> collectionMapTarget; 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; 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}
} }
...@@ -49,6 +49,7 @@ public class StreamConverterTests { ...@@ -49,6 +49,7 @@ public class StreamConverterTests {
private final StreamConverter streamConverter = new StreamConverter(this.conversionService); private final StreamConverter streamConverter = new StreamConverter(this.conversionService);
@Before @Before
public void setup() { public void setup() {
this.conversionService.addConverter(new CollectionToCollectionConverter(this.conversionService)); this.conversionService.addConverter(new CollectionToCollectionConverter(this.conversionService));
...@@ -57,13 +58,15 @@ public class StreamConverterTests { ...@@ -57,13 +58,15 @@ public class StreamConverterTests {
this.conversionService.addConverter(this.streamConverter); this.conversionService.addConverter(this.streamConverter);
} }
@Test @Test
public void convertFromStreamToList() throws NoSuchFieldException { public void convertFromStreamToList() throws NoSuchFieldException {
this.conversionService.addConverter(Number.class, String.class, new ObjectToStringConverter()); this.conversionService.addConverter(Number.class, String.class, new ObjectToStringConverter());
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream(); Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("listOfStrings")); ; TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("listOfStrings")); ;
Object result = this.conversionService.convert(stream, listOfStrings); Object result = this.conversionService.convert(stream, listOfStrings);
assertNotNull("converted object must not be null", result);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a list", result instanceof List); assertTrue("Converted object must be a list", result instanceof List);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<String> content = (List<String>) result; List<String> content = (List<String>) result;
...@@ -79,7 +82,8 @@ public class StreamConverterTests { ...@@ -79,7 +82,8 @@ public class StreamConverterTests {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream(); Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs")); ; TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs")); ;
Object result = this.conversionService.convert(stream, arrayOfLongs); Object result = this.conversionService.convert(stream, arrayOfLongs);
assertNotNull("converted object must not be null", result);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be an array", result.getClass().isArray()); assertTrue("Converted object must be an array", result.getClass().isArray());
Long[] content = (Long[]) result; Long[] content = (Long[]) result;
assertEquals(Long.valueOf(1L), content[0]); assertEquals(Long.valueOf(1L), content[0]);
...@@ -93,7 +97,8 @@ public class StreamConverterTests { ...@@ -93,7 +97,8 @@ public class StreamConverterTests {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream(); Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("rawList")); ; TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("rawList")); ;
Object result = this.conversionService.convert(stream, listOfStrings); Object result = this.conversionService.convert(stream, listOfStrings);
assertNotNull("converted object must not be null", result);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a list", result instanceof List); assertTrue("Converted object must be a list", result instanceof List);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Object> content = (List<Object>) result; List<Object> content = (List<Object>) result;
...@@ -120,7 +125,8 @@ public class StreamConverterTests { ...@@ -120,7 +125,8 @@ public class StreamConverterTests {
List<String> stream = Arrays.asList("1", "2", "3"); List<String> stream = Arrays.asList("1", "2", "3");
TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("streamOfIntegers")); ; TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("streamOfIntegers")); ;
Object result = this.conversionService.convert(stream, streamOfInteger); Object result = this.conversionService.convert(stream, streamOfInteger);
assertNotNull("converted object must not be null", result);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a stream", result instanceof Stream); assertTrue("Converted object must be a stream", result instanceof Stream);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Stream<Integer> content = (Stream<Integer>) result; Stream<Integer> content = (Stream<Integer>) result;
...@@ -139,7 +145,8 @@ public class StreamConverterTests { ...@@ -139,7 +145,8 @@ public class StreamConverterTests {
}); });
TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans")); ; TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans")); ;
Object result = this.conversionService.convert(stream, streamOfBoolean); Object result = this.conversionService.convert(stream, streamOfBoolean);
assertNotNull("converted object must not be null", result);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a stream", result instanceof Stream); assertTrue("Converted object must be a stream", result instanceof Stream);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Stream<Boolean> content = (Stream<Boolean>) result; Stream<Boolean> content = (Stream<Boolean>) result;
...@@ -152,7 +159,8 @@ public class StreamConverterTests { ...@@ -152,7 +159,8 @@ public class StreamConverterTests {
List<String> stream = Arrays.asList("1", "2", "3"); List<String> stream = Arrays.asList("1", "2", "3");
TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("rawStream")); ; TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("rawStream")); ;
Object result = this.conversionService.convert(stream, streamOfInteger); Object result = this.conversionService.convert(stream, streamOfInteger);
assertNotNull("converted object must not be null", result);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a stream", result instanceof Stream); assertTrue("Converted object must be a stream", result instanceof Stream);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Stream<Object> content = (Stream<Object>) result; Stream<Object> content = (Stream<Object>) result;
...@@ -175,6 +183,7 @@ public class StreamConverterTests { ...@@ -175,6 +183,7 @@ public class StreamConverterTests {
new TypeDescriptor(Types.class.getField("arrayOfLongs"))); new TypeDescriptor(Types.class.getField("arrayOfLongs")));
} }
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({ "rawtypes" })
static class Types { static class Types {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册