diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java index c65ba7d3d862e1d39c9699b6826e218aa7b85ed3..86b5415875dd93462532c3595b85505534ca1e9a 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java @@ -262,6 +262,25 @@ public class CollectionToCollectionConverterTests { } + public ArrayList scalarListTarget; + + public List emptyListTarget; + + public LinkedList emptyListDifferentTarget; + + public List>> objectToCollection; + + public List strings; + + public List list = Collections.emptyList(); + + public Collection wildcardCollection = Collections.emptyList(); + + public List resources; + + public EnumSet 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 scalarListTarget; - - public List emptyListTarget; - - public LinkedList emptyListDifferentTarget; - - public List>> objectToCollection; - - public List strings; - - public List list = Collections.emptyList(); - - public Collection wildcardCollection = Collections.emptyList(); - - public List resources; - - public EnumSet enumSet; + public enum MyEnum {A, B, C} } diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java index 92d1d0d777bbd6cdcbf2a19ef5d403938f5464c5..3d2a87d09f67e70659a6207a0e838deff40c4772 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java @@ -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 result = (List) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor + List result = (List) 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 colors = (List) conversionService.convert(new String[] { "ffffff", "#000000" }, + List colors = (List) 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 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() { - @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 { @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; diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java index 510f4f532c57a58a7740974176747531f8e16297..3757f5543a905013dd8be1221b18c33adec90531 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java @@ -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 map = new HashMap(); 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> map = new HashMap>(); 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 map = new HashMap(); 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 map = new HashMap(); + assertTrue(conversionService.canConvert(Map.class, Map.class)); assertSame(map, conversionService.convert(map, Map.class)); } @@ -189,6 +210,7 @@ public class MapToMapConverterTests { Map map = new HashMap(); TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget")); + assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") LinkedHashMap result = (LinkedHashMap) 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 result = (Map) 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 converted = (MultiValueMap) 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 converted = (MultiValueMap) 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 result = new EnumMap(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 extends HashMap { - public NoDefaultConstructorMap(Map 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 scalarMapTarget; public Map> collectionMapTarget; @@ -283,4 +297,16 @@ public class MapToMapConverterTests { public EnumMap enumMap; + + @SuppressWarnings("serial") + public static class NoDefaultConstructorMap extends HashMap { + + public NoDefaultConstructorMap(Map map) { + super(map); + } + } + + + public enum MyEnum {A, B, C} + } diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java index 6b09204e84119e28cc4e2bca924f3a4d6bb357c0..2da31bf147a9391cc63ccb669ece1f7d54f8d646 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java @@ -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 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 content = (List) result; @@ -79,7 +82,8 @@ public class StreamConverterTests { Stream 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 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 content = (List) result; @@ -120,7 +125,8 @@ public class StreamConverterTests { List 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 content = (Stream) 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 content = (Stream) result; @@ -152,7 +159,8 @@ public class StreamConverterTests { List 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 content = (Stream) result; @@ -175,6 +183,7 @@ public class StreamConverterTests { new TypeDescriptor(Types.class.getField("arrayOfLongs"))); } + @SuppressWarnings({ "rawtypes" }) static class Types {