From c693f216f10b35017ee089b2f6693e14b8ca8e31 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Mon, 21 Sep 2009 22:59:48 +0000 Subject: [PATCH] Beefed up test suite --- .../support/ArrayToArrayGenericConverter.java | 2 +- .../ArrayToCollectionGenericConverter.java | 2 +- .../ArrayToObjectGenericConverter.java | 2 +- .../CollectionToArrayGenericConverter.java | 2 +- ...ollectionToCollectionGenericConverter.java | 11 ++-- .../CollectionToObjectGenericConverter.java | 10 +++- .../ObjectToArrayGenericConverter.java | 2 +- .../ObjectToCollectionGenericConverter.java | 2 +- .../GenericConversionServiceTests.java | 55 +++++++++++++++++-- 9 files changed, 70 insertions(+), 18 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java index a29792901e5..58cd87a4890 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java @@ -37,7 +37,7 @@ final class ArrayToArrayGenericConverter implements GenericConverter { Object target = Array.newInstance(targetElementType.getType(), Array.getLength(source)); GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); + throw new ConverterNotFoundException(sourceElementType, targetElementType); } for (int i = 0; i < Array.getLength(target); i++) { Array.set(target, i, converter.convert(Array.get(source, i), sourceElementType, targetElementType)); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java index 645a1fab148..e3c1f72b664 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java @@ -42,7 +42,7 @@ final class ArrayToCollectionGenericConverter implements GenericConverter { } else { GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); + throw new ConverterNotFoundException(sourceElementType, targetElementType); } for (int i = 0; i < length; i++) { collection.add(converter.convert(Array.get(source, i), sourceElementType, targetElementType)); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java index f8da38a3cdc..af24d61307a 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java @@ -39,7 +39,7 @@ final class ArrayToObjectGenericConverter implements GenericConverter { } else { GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); + throw new ConverterNotFoundException(sourceElementType, targetType); } return converter.convert(Array.get(source, 0), sourceElementType, targetType); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java index 369093e155b..b2023f50349 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java @@ -46,7 +46,7 @@ final class CollectionToArrayGenericConverter implements GenericConverter { } else { GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); + throw new ConverterNotFoundException(sourceElementType, targetElementType); } for (Iterator it = sourceCollection.iterator(); it.hasNext(); i++) { Array.set(array, i, converter.convert(it.next(), sourceElementType, targetElementType)); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java index b36295050ab..50e77d2a497 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java @@ -28,7 +28,7 @@ final class CollectionToCollectionGenericConverter implements GenericConverter { public CollectionToCollectionGenericConverter(GenericConversionService conversionService) { this.conversionService = conversionService; } - + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { Collection sourceCollection = (Collection) source; TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); @@ -40,7 +40,8 @@ final class CollectionToCollectionGenericConverter implements GenericConverter { if (sourceType.isAssignableTo(targetType)) { return sourceCollection; } else { - Collection targetCollection = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size()); + Collection targetCollection = CollectionFactory.createCollection(targetType.getType(), sourceCollection + .size()); targetCollection.addAll(sourceCollection); return targetCollection; } @@ -48,14 +49,14 @@ final class CollectionToCollectionGenericConverter implements GenericConverter { Collection targetCollection = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size()); GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); - } + throw new ConverterNotFoundException(sourceElementType, targetElementType); + } for (Object element : sourceCollection) { targetCollection.add(converter.convert(element, sourceElementType, targetElementType)); } return targetCollection; } - + private TypeDescriptor getElementType(Collection collection) { for (Object element : collection) { if (element != null) { diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java index 10f30c8f77e..58b2a40ea8e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java @@ -33,15 +33,19 @@ final class CollectionToObjectGenericConverter implements GenericConverter { if (sourceCollection.size() == 0) { return null; } else { + Object firstElement = sourceCollection.iterator().next(); TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); + if (sourceElementType == TypeDescriptor.NULL && firstElement != null) { + sourceElementType = TypeDescriptor.valueOf(firstElement.getClass()); + } if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetType)) { - return sourceCollection.iterator().next(); + return firstElement; } else { GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); + throw new ConverterNotFoundException(sourceElementType, targetType); } - return converter.convert(sourceCollection.iterator().next(), sourceElementType, targetType); + return converter.convert(firstElement, sourceElementType, targetType); } } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java index 8e6cfc6541d..c691930dd57 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java @@ -36,7 +36,7 @@ final class ObjectToArrayGenericConverter implements GenericConverter { } else { GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); + throw new ConverterNotFoundException(sourceType, targetElementType); } Array.set(target, 0, converter.convert(source, sourceType, targetElementType)); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java index 24549490687..1cd19d77fbe 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java @@ -37,7 +37,7 @@ final class ObjectToCollectionGenericConverter implements GenericConverter { } else { GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType); if (converter == null) { - throw new ConverterNotFoundException(sourceType, targetType); + throw new ConverterNotFoundException(sourceType, targetElementType); } target.add(converter.convert(source, sourceType, targetElementType)); } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index bd8b1bf24e7..1293759454a 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue; import java.util.AbstractList; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.LinkedList; @@ -145,7 +146,7 @@ public class GenericConversionServiceTests { assertEquals(2, result[1]); assertEquals(3, result[2]); } - + @Test public void convertArrayToArrayAssignable() { int[] result = converter.convert(new int[] { 1, 2, 3 }, int[].class); @@ -182,7 +183,7 @@ public class GenericConversionServiceTests { assertEquals("3", result.get(2)); } - @Test(expected=ConversionFailedException.class) + @Test(expected = ConversionFailedException.class) public void convertArrayToAbstractList() { converter.convert(new String[] { "1", "2", "3" }, AbstractList.class); } @@ -241,6 +242,37 @@ public class GenericConversionServiceTests { assertEquals(map.get(2), FooEnum.BAZ); } + @Test + public void convertObjectToCollection() { + List result = (List) converter.convert("test", List.class); + assertEquals(1, result.size()); + assertEquals("test", result.get(0)); + } + + @Test + public void convertObjectToCollectionWithElementConversion() throws Exception { + converter.addConverterFactory(new StringToNumberConverterFactory()); + List result = (List) converter.convert("3", TypeDescriptor.valueOf(String.class), + new TypeDescriptor(getClass().getField("genericList"))); + assertEquals(1, result.size()); + assertEquals(new Integer(3), result.get(0)); + } + + @Test + public void convertCollectionToObject() { + List list = Collections.singletonList("test"); + String result = converter.convert(list, String.class); + assertEquals("test", result); + } + + @Test + public void convertCollectionToObjectWithElementConversion() { + converter.addConverterFactory(new StringToNumberConverterFactory()); + List list = Collections.singletonList("3"); + Integer result = converter.convert(list, Integer.class); + assertEquals(new Integer(3), result); + } + @Test public void convertObjectToArray() { String[] result = converter.convert("test", String[].class); @@ -256,6 +288,21 @@ public class GenericConversionServiceTests { assertEquals(new Integer(1), result[0]); } + @Test + public void convertArrayToObject() { + String[] array = new String[] { "test" }; + String result = converter.convert(array, String.class); + assertEquals("test", result); + } + + @Test + public void convertArrayToObjectWithElementConversion() { + converter.addConverterFactory(new StringToNumberConverterFactory()); + String[] array = new String[] { "3" }; + Integer result = converter.convert(array, Integer.class); + assertEquals(new Integer(3), result); + } + @Test @Ignore public void convertStringToArrayWithElementConversion() { @@ -275,7 +322,7 @@ public class GenericConversionServiceTests { assertTrue(e.getCause() instanceof ConverterNotFoundException); } } - + @Test public void parent() { GenericConversionService parent = new GenericConversionService(); @@ -284,7 +331,7 @@ public class GenericConversionServiceTests { try { converter.convert("3", Integer.class); } catch (ConverterNotFoundException e) { - + } }