From c45f4b1caf17a189fca80afda8a9ef0209ba9573 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Fri, 7 Jan 2011 14:21:54 +0000 Subject: [PATCH] spr-7728: empty collection conversion can return value not assignable to targetType git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3883 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../CollectionToCollectionConverter.java | 3 -- .../convert/support/MapToMapConverter.java | 3 -- .../GenericConversionServiceTests.java | 26 ++++++++++ .../core/convert/support/Spr7728Tests.java | 52 +++++++++++++++++++ 4 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java index 1451376409c..de84a99769f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java @@ -62,9 +62,6 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert return null; } Collection sourceCollection = (Collection) source; - if (sourceCollection.isEmpty()) { - return sourceCollection; - } Collection target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size()); for (Object sourceElement : sourceCollection) { Object targetElement = this.conversionService.convert(sourceElement, sourceType.getElementTypeDescriptor().applyIndexedObject(sourceElement), targetType.getElementTypeDescriptor()); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java index 95dd66c64f8..f8db9c9b0cb 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java @@ -67,9 +67,6 @@ final class MapToMapConverter implements ConditionalGenericConverter { return null; } Map sourceMap = (Map) source; - if (sourceMap.isEmpty()) { - return sourceMap; - } Map targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size()); for (Object entry : sourceMap.entrySet()) { Map.Entry sourceMapEntry = (Map.Entry) entry; 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 acb23e98bb0..9f8c61dc97c 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 @@ -399,6 +399,19 @@ public class GenericConversionServiceTests { public List emptyListTarget; + @Test + public void emptyListToListDifferentTargetType() throws Exception { + conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + List list = new ArrayList(); + TypeDescriptor sourceType = TypeDescriptor.forObject(list); + TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListDifferentTarget")); + assertTrue(conversionService.canConvert(sourceType, targetType)); + assertEquals(LinkedList.class, conversionService.convert(list, sourceType, targetType).getClass()); + } + + public LinkedList emptyListDifferentTarget; + @Test public void emptyListToArray() throws Exception { conversionService.addConverter(new CollectionToArrayConverter(conversionService)); @@ -433,6 +446,19 @@ public class GenericConversionServiceTests { } public Map emptyMapTarget; + + @Test + public void emptyMapToMapDifferentTargetType() throws Exception { + conversionService.addConverter(new MapToMapConverter(conversionService)); + conversionService.addConverterFactory(new StringToNumberConverterFactory()); + Map map = new HashMap(); + TypeDescriptor sourceType = TypeDescriptor.forObject(map); + TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget")); + assertTrue(conversionService.canConvert(sourceType, targetType)); + assertEquals(map, conversionService.convert(map, sourceType, targetType)); + } + + public Map emptyMapDifferentTarget; private interface MyBaseInterface { diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java new file mode 100644 index 00000000000..54aadf3cafd --- /dev/null +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java @@ -0,0 +1,52 @@ +package org.springframework.core.convert.support; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Vector; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.convert.TypeDescriptor; + +public class Spr7728Tests +{ + private CollectionToCollectionConverter theConverter; + private Vector theSrcVector; + private TypeDescriptor theSrcType; + private TypeDescriptor theTargetType; + + @Before + public void setup() + { + theSrcVector = new Vector(); + theSrcType = TypeDescriptor.forObject(theSrcVector); + theTargetType = TypeDescriptor.forObject(new ArrayList()); + theConverter = new CollectionToCollectionConverter(new GenericConversionService()); + } + + @Test + public void convertEmptyVector_shouldReturnEmptyArrayList() + throws Exception + { + theSrcVector.add("Element"); + + testCollectionConversionToArrayList(theSrcVector); + } + + @Test + public void convertNonEmptyVector_shouldReturnNonEmptyArrayList() + throws Exception + { + testCollectionConversionToArrayList(theSrcVector); + } + + private void testCollectionConversionToArrayList(Collection aSource) + { + Object myConverted = theConverter.convert(aSource, theSrcType, theTargetType); + + Assert.assertTrue(myConverted instanceof ArrayList); + Assert.assertEquals(aSource.size(), ((ArrayList) myConverted).size()); + } + +} \ No newline at end of file