general polishing
This commit is contained in:
parent
bb93f5967f
commit
b64945988b
|
|
@ -25,16 +25,16 @@ package org.springframework.core.convert.converter;
|
|||
*/
|
||||
public interface ConverterRegistry {
|
||||
|
||||
/**
|
||||
* Add a generic converter to this registry.
|
||||
*/
|
||||
void addConverter(GenericConverter converter);
|
||||
|
||||
/**
|
||||
* Add a plain converter to this registry.
|
||||
*/
|
||||
void addConverter(Converter<?, ?> converter);
|
||||
|
||||
/**
|
||||
* Add a generic converter to this registry.
|
||||
*/
|
||||
void addConverter(GenericConverter converter);
|
||||
|
||||
/**
|
||||
* Add a ranged converter factory to this registry.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -46,29 +46,39 @@ public abstract class ConversionServiceFactory {
|
|||
* Populate the given ConversionService instance with all applicable default converters.
|
||||
*/
|
||||
public static void addDefaultConverters(GenericConversionService conversionService) {
|
||||
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new ArrayToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new ArrayToStringConverter(conversionService));
|
||||
conversionService.addConverter(new ArrayToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToStringConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new MapToMapConverter(conversionService));
|
||||
conversionService.addConverter(new PropertiesToStringConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new ArrayToStringConverter(conversionService));
|
||||
conversionService.addConverter(new StringToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new StringToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new StringToPropertiesConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new ArrayToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new ObjectToArrayConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new CollectionToStringConverter(conversionService));
|
||||
conversionService.addConverter(new StringToCollectionConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
|
||||
conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
|
||||
conversionService.addConverterFactory(new CharacterToNumberFactory());
|
||||
conversionService.addConverter(new NumberToCharacterConverter());
|
||||
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
|
||||
|
||||
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
|
||||
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
|
||||
conversionService.addConverter(new MapToMapConverter(conversionService));
|
||||
|
||||
conversionService.addConverter(new PropertiesToStringConverter());
|
||||
conversionService.addConverter(new StringToPropertiesConverter());
|
||||
|
||||
conversionService.addConverter(new StringToBooleanConverter());
|
||||
conversionService.addConverter(new StringToCharacterConverter());
|
||||
conversionService.addConverterFactory(new StringToEnumConverterFactory());
|
||||
conversionService.addConverter(new StringToCharacterConverter());
|
||||
conversionService.addConverter(new StringToLocaleConverter());
|
||||
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||
conversionService.addConverterFactory(new StringToEnumConverterFactory());
|
||||
|
||||
conversionService.addConverter(new NumberToCharacterConverter());
|
||||
conversionService.addConverterFactory(new CharacterToNumberFactory());
|
||||
|
||||
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
|
||||
|
||||
conversionService.addConverter(new ObjectToStringConverter());
|
||||
conversionService.addConverter(new ObjectToObjectConverter());
|
||||
conversionService.addConverter(new IdToEntityConverter(conversionService));
|
||||
|
|
|
|||
|
|
@ -18,93 +18,28 @@ package org.springframework.core.convert.support;
|
|||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* Converts from a Map to a String by storing each source Map entry into the String as a property (name=value pair).
|
||||
* Converts from a Properties to a String by calling {@link Properties#store(java.io.OutputStream, String)}.
|
||||
* Decodes with the ISO-8859-1 charset before returning the String.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
* @see Properties#store(java.io.OutputStream, String)
|
||||
*/
|
||||
final class PropertiesToStringConverter implements ConditionalGenericConverter {
|
||||
final class PropertiesToStringConverter implements Converter<Properties, String> {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public PropertiesToStringConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(Properties.class, String.class));
|
||||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return this.conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType)
|
||||
&& this.conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (source == null) {
|
||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||
}
|
||||
Map<?, ?> sourceMap = (Map<?, ?>) source;
|
||||
if (sourceMap.size() == 0) {
|
||||
return "";
|
||||
}
|
||||
else {
|
||||
TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor();
|
||||
TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor();
|
||||
if (sourceKeyType == TypeDescriptor.NULL || sourceValueType == TypeDescriptor.NULL) {
|
||||
TypeDescriptor[] sourceEntryTypes = ConversionUtils.getMapEntryTypes(sourceMap);
|
||||
sourceKeyType = sourceEntryTypes[0];
|
||||
sourceValueType = sourceEntryTypes[1];
|
||||
}
|
||||
boolean keysCompatible = false;
|
||||
if (sourceKeyType != TypeDescriptor.NULL && sourceKeyType.isAssignableTo(targetType)) {
|
||||
keysCompatible = true;
|
||||
}
|
||||
boolean valuesCompatible = false;
|
||||
if (sourceValueType != TypeDescriptor.NULL && sourceValueType.isAssignableTo(targetType)) {
|
||||
valuesCompatible = true;
|
||||
}
|
||||
Properties props = new Properties();
|
||||
if (keysCompatible && valuesCompatible) {
|
||||
for (Object entry : sourceMap.entrySet()) {
|
||||
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||
props.setProperty((String) mapEntry.getKey(), (String) mapEntry.getValue());
|
||||
}
|
||||
return store(props);
|
||||
}
|
||||
else {
|
||||
MapEntryConverter converter = new MapEntryConverter(sourceKeyType, sourceValueType, targetType,
|
||||
targetType, keysCompatible, valuesCompatible, this.conversionService);
|
||||
for (Object entry : sourceMap.entrySet()) {
|
||||
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
|
||||
Object key = converter.convertKey(mapEntry.getKey());
|
||||
Object value = converter.convertValue(mapEntry.getValue());
|
||||
props.setProperty((String) key, (String) value);
|
||||
}
|
||||
return store(props);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String store(Properties props) {
|
||||
public String convert(Properties source) {
|
||||
try {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
props.store(os, null);
|
||||
source.store(os, null);
|
||||
return os.toString("ISO-8859-1");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// Should never happen.
|
||||
throw new IllegalArgumentException("Failed to store [" + props + "] into String", ex);
|
||||
throw new IllegalArgumentException("Failed to store [" + source + "] into String", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,55 +17,29 @@
|
|||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* Converts a String to a Map by loading the list of properties (name=value pairs) encoded in the String.
|
||||
*
|
||||
* Converts a String to a Properties by calling Properties#load(java.io.InputStream).
|
||||
* Uses ISO-8559-1 encoding required by Properties.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
* @see Properties#load(java.io.InputStream)
|
||||
*/
|
||||
final class StringToPropertiesConverter implements ConditionalGenericConverter {
|
||||
final class StringToPropertiesConverter implements Converter<String, Properties> {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public StringToPropertiesConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(String.class, Properties.class));
|
||||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return this.conversionService.canConvert(sourceType, targetType.getMapKeyTypeDescriptor()) &&
|
||||
this.conversionService.canConvert(sourceType, targetType.getMapValueTypeDescriptor());
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (source == null) {
|
||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||
}
|
||||
String string = (String) source;
|
||||
return this.conversionService.convert(loadProperties(string), TypeDescriptor.valueOf(Properties.class), targetType);
|
||||
}
|
||||
|
||||
private Properties loadProperties(String string) {
|
||||
public Properties convert(String source) {
|
||||
try {
|
||||
Properties props = new Properties();
|
||||
// Must use the ISO-8859-1 encoding because Properties.load(stream) expects it.
|
||||
props.load(new ByteArrayInputStream(string.getBytes("ISO-8859-1")));
|
||||
props.load(new ByteArrayInputStream(source.getBytes("ISO-8859-1")));
|
||||
return props;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Should never happen.
|
||||
throw new IllegalArgumentException("Failed to parse [" + string + "] into Properties", ex);
|
||||
throw new IllegalArgumentException("Failed to parse [" + source + "] into Properties", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@
|
|||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.AbstractList;
|
||||
|
|
@ -33,17 +37,12 @@ import java.util.Map;
|
|||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
|
|
@ -68,6 +67,11 @@ public class DefaultConversionTests {
|
|||
conversionService.convert("invalid", Character.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCharacterToString() {
|
||||
assertEquals("3", conversionService.convert('3', String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBooleanTrue() {
|
||||
assertEquals(Boolean.valueOf(true), conversionService.convert("true", Boolean.class));
|
||||
|
|
@ -94,46 +98,92 @@ public class DefaultConversionTests {
|
|||
conversionService.convert("invalid", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanToString() {
|
||||
assertEquals("true", conversionService.convert(true, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToByte() throws Exception {
|
||||
assertEquals(Byte.valueOf("1"), conversionService.convert("1", Byte.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteToString() {
|
||||
assertEquals("65", conversionService.convert(new String("A").getBytes()[0], String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToShort() {
|
||||
assertEquals(Short.valueOf("1"), conversionService.convert("1", Short.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortToString() {
|
||||
short three = 3;
|
||||
assertEquals("3", conversionService.convert(three, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToInteger() {
|
||||
assertEquals(Integer.valueOf("1"), conversionService.convert("1", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntegerToString() {
|
||||
assertEquals("3", conversionService.convert(3, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToLong() {
|
||||
assertEquals(Long.valueOf("1"), conversionService.convert("1", Long.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongToString() {
|
||||
assertEquals("3", conversionService.convert(3L, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToFloat() {
|
||||
assertEquals(Float.valueOf("1.0"), conversionService.convert("1.0", Float.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatToString() {
|
||||
assertEquals("1.0", conversionService.convert(new Float("1.0"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToDouble() {
|
||||
assertEquals(Double.valueOf("1.0"), conversionService.convert("1.0", Double.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleToString() {
|
||||
assertEquals("1.0", conversionService.convert(new Double("1.0"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBigInteger() {
|
||||
assertEquals(new BigInteger("1"), conversionService.convert("1", BigInteger.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBigDouble() {
|
||||
public void testBigIntegerToString() {
|
||||
assertEquals("100", conversionService.convert(new BigInteger("100"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBigDecimal() {
|
||||
assertEquals(new BigDecimal("1.0"), conversionService.convert("1.0", BigDecimal.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalToString() {
|
||||
assertEquals("100.00", conversionService.convert(new BigDecimal("100.00"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToNumber() {
|
||||
assertEquals(new BigDecimal("1.0"), conversionService.convert("1.0", Number.class));
|
||||
|
|
@ -153,7 +203,12 @@ public class DefaultConversionTests {
|
|||
public void testStringToEnumEmptyString() {
|
||||
assertEquals(null, conversionService.convert("", Foo.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEnumToString() {
|
||||
assertEquals("BAR", conversionService.convert(Foo.BAR, String.class));
|
||||
}
|
||||
|
||||
public static enum Foo {
|
||||
BAR, BAZ;
|
||||
}
|
||||
|
|
@ -165,7 +220,6 @@ public class DefaultConversionTests {
|
|||
|
||||
@Test
|
||||
public void testNumberToNumber() {
|
||||
Converter<Number, Long> c = new NumberToNumberConverterFactory().getConverter(Long.class);
|
||||
assertEquals(Long.valueOf(1), conversionService.convert(Integer.valueOf(1), Long.class));
|
||||
}
|
||||
|
||||
|
|
@ -202,113 +256,16 @@ public class DefaultConversionTests {
|
|||
public void testNumberToCharacter() {
|
||||
assertEquals(Character.valueOf('A'), conversionService.convert(Integer.valueOf(65), Character.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectToString() {
|
||||
assertEquals("3", conversionService.convert(3, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectValueOFMethod() {
|
||||
assertEquals(new Integer(3), conversionService.convert("3", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectConstructor() {
|
||||
assertEquals(new SSN("123456789"), conversionService.convert("123456789", SSN.class));
|
||||
assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class));
|
||||
public void testCharacterToNumber() {
|
||||
assertEquals(new Integer(65), conversionService.convert('A', Integer.class));
|
||||
}
|
||||
|
||||
@Test(expected=ConverterNotFoundException.class)
|
||||
public void convertObjectToObjectNoValueOFMethodOrConstructor() {
|
||||
conversionService.convert(new Long(3), SSN.class);
|
||||
}
|
||||
|
||||
private static class SSN {
|
||||
private String value;
|
||||
|
||||
public SSN(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof SSN)) {
|
||||
return false;
|
||||
}
|
||||
SSN ssn = (SSN) o;
|
||||
return this.value.equals(ssn.value);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return value.hashCode();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethod() {
|
||||
TestEntity e = conversionService.convert(1L, TestEntity.class);
|
||||
assertEquals(new Long(1), e.getId());
|
||||
}
|
||||
// collection conversion
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethodWithNull() {
|
||||
TestEntity e = (TestEntity) conversionService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(TestEntity.class));
|
||||
assertNull(e);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethodWithIdConversion() {
|
||||
TestEntity e = conversionService.convert("1", TestEntity.class);
|
||||
assertEquals(new Long(1), e.getId());
|
||||
}
|
||||
|
||||
public static class TestEntity {
|
||||
|
||||
private Long id;
|
||||
|
||||
public TestEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public static TestEntity findTestEntity(Long id) {
|
||||
return new TestEntity(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToArray() {
|
||||
Integer[] result = conversionService.convert(new String[] { "1", "2", "3" }, Integer[].class);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToPrimitiveArray() {
|
||||
int[] result = conversionService.convert(new String[] { "1", "2", "3" }, int[].class);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToArrayAssignable() {
|
||||
int[] result = conversionService.convert(new int[] { 1, 2, 3 }, int[].class);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToListInterface() {
|
||||
public void convertArrayToCollectionInterface() {
|
||||
List<?> result = conversionService.convert(new String[] { "1", "2", "3" }, List.class);
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
|
|
@ -318,7 +275,7 @@ public class DefaultConversionTests {
|
|||
public List<Integer> genericList = new ArrayList<Integer>();
|
||||
|
||||
@Test
|
||||
public void convertArrayToListGenericTypeConversion() throws Exception {
|
||||
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
|
||||
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor
|
||||
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
|
||||
assertEquals(new Integer("1"), result.get(0));
|
||||
|
|
@ -327,7 +284,7 @@ public class DefaultConversionTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToListImpl() {
|
||||
public void convertArrayToCollectionImpl() {
|
||||
LinkedList<?> result = conversionService.convert(new String[] { "1", "2", "3" }, LinkedList.class);
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
|
|
@ -335,7 +292,7 @@ public class DefaultConversionTests {
|
|||
}
|
||||
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
public void convertArrayToAbstractList() {
|
||||
public void convertArrayToAbstractCollection() {
|
||||
conversionService.convert(new String[] { "1", "2", "3" }, AbstractList.class);
|
||||
}
|
||||
|
||||
|
|
@ -361,6 +318,39 @@ public class DefaultConversionTests {
|
|||
assertEquals("", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToArray() {
|
||||
String[] result = conversionService.convert("1,2,3", String[].class);
|
||||
assertEquals(3, result.length);
|
||||
assertEquals("1", result[0]);
|
||||
assertEquals("2", result[1]);
|
||||
assertEquals("3", result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToArrayWithElementConversion() {
|
||||
Integer[] result = conversionService.convert("1,2,3", Integer[].class);
|
||||
assertEquals(3, result.length);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToPrimitiveArrayWithElementConversion() {
|
||||
int[] result = conversionService.convert("1,2,3", int[].class);
|
||||
assertEquals(3, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertEmptyStringToArray() {
|
||||
String[] result = conversionService.convert("", String[].class);
|
||||
assertEquals(0, result.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToObject() {
|
||||
Object[] array = new Object[] { 3L };
|
||||
|
|
@ -375,6 +365,20 @@ public class DefaultConversionTests {
|
|||
assertEquals(new Integer(3), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToArray() {
|
||||
Object[] result = conversionService.convert(3L, Object[].class);
|
||||
assertEquals(1, result.length);
|
||||
assertEquals(3L, result[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToArrayWithElementConversion() {
|
||||
Integer[] result = conversionService.convert(3L, Integer[].class);
|
||||
assertEquals(1, result.length);
|
||||
assertEquals(new Integer(3), result[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToArray() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
|
|
@ -399,6 +403,99 @@ public class DefaultConversionTests {
|
|||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToString() {
|
||||
List<String> list = Arrays.asList(new String[] { "foo", "bar" });
|
||||
String result = conversionService.convert(list, String.class);
|
||||
assertEquals("foo,bar", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToStringWithElementConversion() throws Exception {
|
||||
List<Integer> list = Arrays.asList(new Integer[] { 3, 5 });
|
||||
String result = (String) conversionService.convert(list,
|
||||
new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.STRING);
|
||||
assertEquals("3,5", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToCollection() {
|
||||
List result = conversionService.convert("1,2,3", List.class);
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
assertEquals("3", result.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToCollectionWithElementConversion() throws Exception {
|
||||
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.STRING,
|
||||
new TypeDescriptor(getClass().getField("genericList")));
|
||||
assertEquals(3, result.size());
|
||||
assertEquals(new Integer(1), result.get(0));
|
||||
assertEquals(new Integer(2), result.get(1));
|
||||
assertEquals(new Integer(3), result.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertEmptyStringToCollection() {
|
||||
Collection result = conversionService.convert("", Collection.class);
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToObject() {
|
||||
List<Long> list = Collections.singletonList(3L);
|
||||
Long result = conversionService.convert(list, Long.class);
|
||||
assertEquals(new Long(3), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToObjectWithElementConversion() {
|
||||
List<String> list = Collections.singletonList("3");
|
||||
Integer result = conversionService.convert(list, Integer.class);
|
||||
assertEquals(new Integer(3), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToCollection() {
|
||||
List<String> result = (List<String>) conversionService.convert(3L, List.class);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(3L, result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToCollectionWithElementConversion() throws Exception {
|
||||
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class),
|
||||
new TypeDescriptor(getClass().getField("genericList")));
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(new Integer(3), result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToArray() {
|
||||
Integer[] result = conversionService.convert(new String[] { "1", "2", "3" }, Integer[].class);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToPrimitiveArray() {
|
||||
int[] result = conversionService.convert(new String[] { "1", "2", "3" }, int[].class);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToArrayAssignable() {
|
||||
int[] result = conversionService.convert(new int[] { 1, 2, 3 }, int[].class);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToCollection() throws Exception {
|
||||
Set<String> foo = new LinkedHashSet<String>();
|
||||
|
|
@ -447,35 +544,6 @@ public class DefaultConversionTests {
|
|||
assertEquals(new Integer(3), bar.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToString() {
|
||||
List<String> list = Arrays.asList(new String[] { "foo", "bar" });
|
||||
String result = conversionService.convert(list, String.class);
|
||||
assertEquals("foo,bar", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToStringWithElementConversion() throws Exception {
|
||||
List<Integer> list = Arrays.asList(new Integer[] { 3, 5 });
|
||||
String result = (String) conversionService.convert(list,
|
||||
new TypeDescriptor(getClass().getField("genericList")), TypeDescriptor.STRING);
|
||||
assertEquals("3,5", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToObject() {
|
||||
List<Long> list = Collections.singletonList(3L);
|
||||
Long result = conversionService.convert(list, Long.class);
|
||||
assertEquals(new Long(3), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCollectionToObjectWithElementConversion() {
|
||||
List<String> list = Collections.singletonList("3");
|
||||
Integer result = conversionService.convert(list, Integer.class);
|
||||
assertEquals(new Integer(3), result);
|
||||
}
|
||||
|
||||
public Map<Integer, FooEnum> genericMap = new HashMap<Integer, FooEnum>();
|
||||
|
||||
@Test
|
||||
|
|
@ -499,43 +567,6 @@ public class DefaultConversionTests {
|
|||
assertTrue(result.contains("2=BAZ"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertPropertiesToStringWithConversion() throws Exception {
|
||||
Properties foo = new Properties();
|
||||
foo.put(1, FooEnum.BAR);
|
||||
foo.put(2, FooEnum.BAZ);
|
||||
String result = conversionService.convert(foo, String.class);
|
||||
assertTrue(result.contains("1=BAR"));
|
||||
assertTrue(result.contains("2=BAZ"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToArray() {
|
||||
String[] result = conversionService.convert("1,2,3", String[].class);
|
||||
assertEquals(3, result.length);
|
||||
assertEquals("1", result[0]);
|
||||
assertEquals("2", result[1]);
|
||||
assertEquals("3", result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToArrayWithElementConversion() {
|
||||
Integer[] result = conversionService.convert("1,2,3", Integer[].class);
|
||||
assertEquals(3, result.length);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToPrimitiveArrayWithElementConversion() {
|
||||
int[] result = conversionService.convert("1,2,3", int[].class);
|
||||
assertEquals(3, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToProperties() {
|
||||
Properties result = conversionService.convert("a=b\nc=2\nd=", Properties.class);
|
||||
|
|
@ -552,83 +583,121 @@ public class DefaultConversionTests {
|
|||
assertEquals("baz", result.get("bar"));
|
||||
assertEquals("boop", result.get("baz"));
|
||||
}
|
||||
|
||||
// generic object conversion
|
||||
|
||||
@Test
|
||||
public void convertEmptyStringToArray() {
|
||||
String[] result = conversionService.convert("", String[].class);
|
||||
assertEquals(0, result.length);
|
||||
public void convertObjectToStringValueOfMethodPresent() {
|
||||
assertEquals("123456789", conversionService.convert(ISBN.valueOf("123456789"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToArray() {
|
||||
Object[] result = conversionService.convert(3L, Object[].class);
|
||||
assertEquals(1, result.length);
|
||||
assertEquals(3L, result[0]);
|
||||
public void convertObjectToStringStringConstructorPresent() {
|
||||
assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToArrayWithElementConversion() {
|
||||
Integer[] result = conversionService.convert(3L, Integer[].class);
|
||||
assertEquals(1, result.length);
|
||||
assertEquals(new Integer(3), result[0]);
|
||||
@Ignore
|
||||
public void convertObjectToObjectValueOFMethod() {
|
||||
assertEquals(ISBN.valueOf("123456789"), conversionService.convert("123456789", ISBN.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToCollection() {
|
||||
List result = conversionService.convert("1,2,3", List.class);
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
assertEquals("3", result.get(2));
|
||||
public void convertObjectToObjectConstructor() {
|
||||
assertEquals(new SSN("123456789"), conversionService.convert("123456789", SSN.class));
|
||||
assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToCollectionWithElementConversion() throws Exception {
|
||||
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.STRING,
|
||||
new TypeDescriptor(getClass().getField("genericList")));
|
||||
assertEquals(3, result.size());
|
||||
assertEquals(new Integer(1), result.get(0));
|
||||
assertEquals(new Integer(2), result.get(1));
|
||||
assertEquals(new Integer(3), result.get(2));
|
||||
@Test(expected=ConverterNotFoundException.class)
|
||||
public void convertObjectToObjectNoValueOFMethodOrConstructor() {
|
||||
conversionService.convert(new Long(3), SSN.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertEmptyStringToCollection() {
|
||||
Collection result = conversionService.convert("", Collection.class);
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToCollection() {
|
||||
List<String> result = (List<String>) conversionService.convert(3L, List.class);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(3L, result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToCollectionWithElementConversion() throws Exception {
|
||||
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class),
|
||||
new TypeDescriptor(getClass().getField("genericList")));
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(new Integer(3), result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testUnmodifiableListConversion() {
|
||||
List<String> stringList = new ArrayList<String>();
|
||||
stringList.add("foo");
|
||||
stringList.add("bar");
|
||||
|
||||
List<String> frozenList = Collections.unmodifiableList(stringList);
|
||||
private static class SSN {
|
||||
private String value;
|
||||
|
||||
List<String> converted = conversionService.convert(frozenList, List.class);
|
||||
|
||||
// The converted list should contain all the elements in the original list
|
||||
Assert.assertEquals(frozenList, converted);
|
||||
// Would fail since CollectionToCollectionConverter does not create a copy if source list (including elements) are compatible with target list -
|
||||
// TODO is this optimization a suitable default?
|
||||
// Assert.assertNotSame(frozenList, converted);
|
||||
public SSN(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof SSN)) {
|
||||
return false;
|
||||
}
|
||||
SSN ssn = (SSN) o;
|
||||
return this.value.equals(ssn.value);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return value.hashCode();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ISBN {
|
||||
|
||||
private String value;
|
||||
|
||||
private ISBN(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof ISBN)) {
|
||||
return false;
|
||||
}
|
||||
ISBN isbn = (ISBN) o;
|
||||
return this.value.equals(isbn.value);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return value.hashCode();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static ISBN valueOf(String value) {
|
||||
return new ISBN(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethod() {
|
||||
TestEntity e = conversionService.convert(1L, TestEntity.class);
|
||||
assertEquals(new Long(1), e.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethodWithNull() {
|
||||
TestEntity e = (TestEntity) conversionService.convert(null, TypeDescriptor.STRING, TypeDescriptor.valueOf(TestEntity.class));
|
||||
assertNull(e);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethodWithIdConversion() {
|
||||
TestEntity e = conversionService.convert("1", TestEntity.class);
|
||||
assertEquals(new Long(1), e.getId());
|
||||
}
|
||||
|
||||
public static class TestEntity {
|
||||
|
||||
private Long id;
|
||||
|
||||
public TestEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public static TestEntity findTestEntity(Long id) {
|
||||
return new TestEntity(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue