From c7fab44e0c8288bb4f647948da886eff1c5e1c51 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 13 Oct 2010 08:13:02 -0400 Subject: [PATCH] INT-1441 reverted back to calling canConvert(), but added check to see if source is empty Collection and target is a Collection and simply returning back what was passed for conversion since empty Collection could be cast to any other typed Collection, added test --- .../util/BeanFactoryTypeConverter.java | 16 ++++++---- .../util/BeanFactoryTypeConverterTests.java | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/util/BeanFactoryTypeConverterTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/util/BeanFactoryTypeConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/util/BeanFactoryTypeConverter.java index d7820b9bdf..7b66b71983 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/util/BeanFactoryTypeConverter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/util/BeanFactoryTypeConverter.java @@ -16,6 +16,7 @@ package org.springframework.integration.util; import java.beans.PropertyEditor; +import java.util.Collection; import org.springframework.beans.BeansException; import org.springframework.beans.SimpleTypeConverter; @@ -27,6 +28,7 @@ import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.expression.TypeConverter; +import org.springframework.util.CollectionUtils; /** * * @author Dave Syer @@ -95,13 +97,15 @@ public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware if (targetType.getType() == Void.class || targetType.getType() == Void.TYPE) { return null; } - try { - return conversionService.convert(value, sourceType, targetType); - } catch (ConversionFailedException e) { - throw e; - } catch (Exception ex){ - // ignore because we have a fallback strategy, see SPR-7548 for more details + if (value instanceof Collection + && CollectionUtils.isEmpty((Collection) value) + && Collection.class.isAssignableFrom(targetType.getObjectType())){ + return value; } + if (conversionService.canConvert(sourceType, targetType)) { + return conversionService.convert(value, sourceType, targetType); + } + if (!String.class.isAssignableFrom(sourceType.getType())) { PropertyEditor editor = delegate.findCustomEditor(sourceType.getType(), null); if (editor != null){ // INT-1441 diff --git a/spring-integration-core/src/test/java/org/springframework/integration/util/BeanFactoryTypeConverterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/util/BeanFactoryTypeConverterTests.java new file mode 100644 index 0000000000..a2b5ae8ddc --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/util/BeanFactoryTypeConverterTests.java @@ -0,0 +1,29 @@ +/** + * + */ +package org.springframework.integration.util; + +import static junit.framework.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.springframework.core.convert.TypeDescriptor; + +/** + * @author Oleg Zhurakousky + * + */ +public class BeanFactoryTypeConverterTests { + + @Test + public void testEmptyCollectionConversion(){ + BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter(); + List sourceObject = new ArrayList(); + // source type doesn't even matter + ArrayList convertedCollection = + (ArrayList) typeConverter.convertValue(sourceObject, null, TypeDescriptor.forObject(new ArrayList())); + assertEquals(sourceObject, convertedCollection); + } +}