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

Polishing

上级 387c8a81
......@@ -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 {
@Override
......@@ -330,25 +349,6 @@ public class CollectionToCollectionConverterTests {
}
public static 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;
public enum MyEnum {A, B, C}
}
......@@ -313,7 +313,7 @@ public class DefaultConversionServiceTests {
@Test
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("2", result.get(1));
assertEquals("3", result.get(2));
......@@ -322,7 +322,7 @@ public class DefaultConversionServiceTests {
@Test
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
@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")));
assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1));
......@@ -344,7 +344,7 @@ public class DefaultConversionServiceTests {
ConverterRegistry registry = (conversionService);
registry.addConverter(new ColorConverter());
@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),
new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
assertEquals(2, colors.size());
......@@ -354,7 +354,7 @@ public class DefaultConversionServiceTests {
@Test
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("2", result.get(1));
assertEquals("3", result.get(2));
......@@ -371,13 +371,13 @@ public class DefaultConversionServiceTests {
@Test
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);
}
@Test
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);
}
......@@ -422,21 +422,21 @@ public class DefaultConversionServiceTests {
@Test
public void convertArrayToObject() {
Object[] array = new Object[] { 3L };
Object[] array = new Object[] {3L};
Object result = conversionService.convert(array, Long.class);
assertEquals(3L, result);
}
@Test
public void convertArrayToObjectWithElementConversion() {
String[] array = new String[] { "3" };
String[] array = new String[] {"3"};
Integer result = conversionService.convert(array, Integer.class);
assertEquals(new Integer(3), result);
}
@Test
public void convertArrayToObjectAssignableTargetType() {
Long[] array = new Long[] { 3L };
Long[] array = new Long[] {3L};
Long[] result = (Long[]) conversionService.convert(array, Object.class);
assertArrayEquals(array, result);
}
......@@ -849,14 +849,14 @@ public class DefaultConversionServiceTests {
}
});
char[] converted = conversionService.convert("abc", char[].class);
assertThat(converted, equalTo(new char[] { 'a', 'b', 'c' }));
assertThat(converted, equalTo(new char[] {'a', 'b', 'c'}));
}
@Test
@SuppressWarnings("unchecked")
public void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly() {
String[][] grid = new String[][] { new String[] { "1", "2", "3", "4" }, new String[] { "5", "6", "7", "8" },
new String[] { "9", "10", "11", "12" } };
String[][] grid = new String[][] {new String[] {"1", "2", "3", "4"}, new String[] {"5", "6", "7", "8"},
new String[] {"9", "10", "11", "12"}};
List<String[]> converted = conversionService.convert(grid, List.class);
String[][] convertedBack = conversionService.convert(converted, String[][].class);
assertArrayEquals(grid, convertedBack);
......@@ -865,16 +865,15 @@ public class DefaultConversionServiceTests {
@Test
public void convertCannotOptimizeArray() {
conversionService.addConverter(new Converter<Byte, Byte>() {
@Override
public Byte convert(Byte source) {
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);
assertNotSame(byteArray, converted);
assertTrue(Arrays.equals(new byte[] { 2, 3, 4 }, converted));
assertTrue(Arrays.equals(new byte[] {2, 3, 4}, converted));
}
@Test
......@@ -938,6 +937,7 @@ public class DefaultConversionServiceTests {
public enum Foo {
BAR, BAZ
}
......@@ -964,7 +964,12 @@ public class DefaultConversionServiceTests {
public class ColorConverter implements Converter<String, Color> {
@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 {
private static class SSN {
static int constructorCount = 0;
static int toStringCount = 0;
static int toStringCount = 0;
static void reset() {
constructorCount = 0;
......
......@@ -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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
@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 {
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 {
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 {
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 {
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 {
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 {
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 {
private final StreamConverter streamConverter = new StreamConverter(this.conversionService);
@Before
public void setup() {
this.conversionService.addConverter(new CollectionToCollectionConverter(this.conversionService));
......@@ -57,13 +58,15 @@ public class StreamConverterTests {
this.conversionService.addConverter(this.streamConverter);
}
@Test
public void convertFromStreamToList() throws NoSuchFieldException {
this.conversionService.addConverter(Number.class, String.class, new ObjectToStringConverter());
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("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);
@SuppressWarnings("unchecked")
List<String> content = (List<String>) result;
......@@ -79,7 +82,8 @@ public class StreamConverterTests {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("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());
Long[] content = (Long[]) result;
assertEquals(Long.valueOf(1L), content[0]);
......@@ -93,7 +97,8 @@ public class StreamConverterTests {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("rawList")); ;
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);
@SuppressWarnings("unchecked")
List<Object> content = (List<Object>) result;
......@@ -120,7 +125,8 @@ public class StreamConverterTests {
List<String> stream = Arrays.asList("1", "2", "3");
TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("streamOfIntegers")); ;
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);
@SuppressWarnings("unchecked")
Stream<Integer> content = (Stream<Integer>) result;
......@@ -139,7 +145,8 @@ public class StreamConverterTests {
});
TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans")); ;
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);
@SuppressWarnings("unchecked")
Stream<Boolean> content = (Stream<Boolean>) result;
......@@ -152,7 +159,8 @@ public class StreamConverterTests {
List<String> stream = Arrays.asList("1", "2", "3");
TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("rawStream")); ;
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);
@SuppressWarnings("unchecked")
Stream<Object> content = (Stream<Object>) result;
......@@ -175,6 +183,7 @@ public class StreamConverterTests {
new TypeDescriptor(Types.class.getField("arrayOfLongs")));
}
@SuppressWarnings({ "rawtypes" })
static class Types {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册