string to collection and array converters now are conditional and apply target element type match

This commit is contained in:
Keith Donald
2011-06-06 22:48:00 +00:00
parent 38041acc87
commit a1a7c32052
5 changed files with 50 additions and 12 deletions

View File

@@ -759,4 +759,5 @@ public class DefaultConversionTests {
return new TestEntity(id);
}
}
}

View File

@@ -16,16 +16,17 @@
package org.springframework.core.convert.support;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -528,4 +529,25 @@ public class GenericConversionServiceTests {
public static Map<String, ?> wildcardMap;
@Test
public void stringToArrayCanConvert() {
conversionService.addConverter(new StringToArrayConverter(conversionService));
assertFalse(conversionService.canConvert(String.class, Integer[].class));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(String.class, Integer[].class));
}
@Test
public void stringToCollectionCanConvert() throws Exception {
conversionService.addConverter(new StringToCollectionConverter(conversionService));
assertTrue(conversionService.canConvert(String.class, Collection.class));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("stringToCollection"));
assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
}
public Collection<Integer> stringToCollection;
}