From 58d68cef986aaaf4b6a296b234f8ca4744d357e3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 13 Jun 2011 22:03:00 +0000 Subject: [PATCH] restored original ConversionService behavior with respect to empty collections/maps (SPR-7728) --- .../support/CollectionToCollectionConverter.java | 5 +---- .../core/convert/support/MapToMapConverter.java | 5 +---- .../support/GenericConversionServiceTests.java | 15 ++++++--------- 3 files changed, 8 insertions(+), 17 deletions(-) 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 906bc423e90..3e1aeefb76e 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,9 +58,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, 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 172bda410a3..6b1cf4ec5b3 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,9 +59,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 71ff37da78f..d941ec832ea 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,18 +24,19 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import static org.junit.Assert.*; import org.junit.Test; import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; -import org.springframework.core.io.Resource; import org.springframework.core.io.DescriptiveResource; +import org.springframework.core.io.Resource; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; +import static org.junit.Assert.*; + /** * @author Keith Donald * @author Juergen Hoeller @@ -271,9 +272,7 @@ public class GenericConversionServiceTests { GenericConversionService service = ConversionServiceFactory.createDefaultConversionService(); List list = Collections.emptyList(); List result = service.convert(list, List.class); - assertSame(list, result); - result = service.convert(list, list.getClass()); - assertSame(list, result); + assertTrue(result.isEmpty()); } @Test @@ -281,9 +280,7 @@ public class GenericConversionServiceTests { GenericConversionService service = ConversionServiceFactory.createDefaultConversionService(); Map map = Collections.emptyMap(); Map result = service.convert(map, Map.class); - assertSame(map, result); - result = service.convert(map, map.getClass()); - assertSame(map, result); + assertTrue(result.isEmpty()); } @Test