Browse Source

restored original ConversionService behavior with respect to empty collections/maps (SPR-7728)

3.0.x
Juergen Hoeller 15 years ago
parent
commit
58d68cef98
  1. 5
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java
  2. 5
      org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java
  3. 15
      org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

5
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java

@ -1,5 +1,5 @@ @@ -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 @@ -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,

5
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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;

15
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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 { @@ -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

Loading…
Cancel
Save