polish
This commit is contained in:
@@ -34,13 +34,13 @@ public class TypeDescriptorTests {
|
||||
|
||||
@Test
|
||||
public void testWrapperType() {
|
||||
TypeDescriptor desc = TypeDescriptor.valueOf(int.class);
|
||||
BindingPoint desc = BindingPoint.valueOf(int.class);
|
||||
assertEquals(Integer.class, desc.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listDescriptors() throws Exception {
|
||||
TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfString"));
|
||||
BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("listOfString"));
|
||||
assertFalse(typeDescriptor.isArray());
|
||||
assertEquals(List.class,typeDescriptor.getType());
|
||||
assertEquals(String.class,typeDescriptor.getElementType());
|
||||
@@ -50,7 +50,7 @@ public class TypeDescriptorTests {
|
||||
|
||||
@Test
|
||||
public void arrayTypeDescriptors() throws Exception {
|
||||
TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("intArray"));
|
||||
BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("intArray"));
|
||||
assertTrue(typeDescriptor.isArray());
|
||||
assertEquals(Integer.TYPE,typeDescriptor.getElementType());
|
||||
assertEquals("int[]",typeDescriptor.asString());
|
||||
@@ -58,14 +58,14 @@ public class TypeDescriptorTests {
|
||||
|
||||
@Test
|
||||
public void buildingArrayTypeDescriptors() throws Exception {
|
||||
TypeDescriptor typeDescriptor = new TypeDescriptor(new int[0].getClass());
|
||||
BindingPoint typeDescriptor = new BindingPoint(new int[0].getClass());
|
||||
assertTrue(typeDescriptor.isArray());
|
||||
assertEquals(Integer.TYPE,typeDescriptor.getElementType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void complexTypeDescriptors() throws Exception {
|
||||
TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString"));
|
||||
BindingPoint typeDescriptor = new BindingPoint(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString"));
|
||||
assertTrue(typeDescriptor.isArray());
|
||||
assertEquals(List.class,typeDescriptor.getElementType());
|
||||
// TODO asc notice that the type of the list elements is lost: typeDescriptor.getElementType() should return a TypeDescriptor
|
||||
|
||||
@@ -3,16 +3,16 @@ package org.springframework.core.convert.support;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.BindingPoint;
|
||||
import org.springframework.core.convert.support.ArrayToArray;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.DefaultTypeConverter;
|
||||
|
||||
public class ArrayToArrayTests {
|
||||
|
||||
@Test
|
||||
public void testArrayToArrayConversion() {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToArray c = new ArrayToArray(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Integer[].class), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
ArrayToArray c = new ArrayToArray(BindingPoint.valueOf(String[].class), BindingPoint.valueOf(Integer[].class), service);
|
||||
Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
|
||||
@@ -9,16 +9,16 @@ import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.BindingPoint;
|
||||
import org.springframework.core.convert.support.ArrayToCollection;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.DefaultTypeConverter;
|
||||
|
||||
public class ArrayToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testArrayToCollectionConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("bindTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("bindTarget")), service);
|
||||
List result = (List) c.execute(new String[] { "1", "2", "3" });
|
||||
assertEquals(new Integer(1), result.get(0));
|
||||
assertEquals(new Integer(2), result.get(1));
|
||||
@@ -27,32 +27,32 @@ public class ArrayToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testArrayToSetConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("setTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("setTarget")), service);
|
||||
Set result = (Set) c.execute(new String[] { "1" });
|
||||
assertEquals("1", result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayToSortedSetConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("sortedSetTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("sortedSetTarget")), service);
|
||||
SortedSet result = (SortedSet) c.execute(new String[] { "1" });
|
||||
assertEquals(new Integer(1), result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayToCollectionImplConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("implTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("implTarget")), service);
|
||||
LinkedList result = (LinkedList) c.execute(new String[] { "1" });
|
||||
assertEquals("1", result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayToNonGenericCollectionConversionNullElement() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("listTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
ArrayToCollection c = new ArrayToCollection(BindingPoint.valueOf(String[].class), new BindingPoint(getClass().getField("listTarget")), service);
|
||||
List result = (List) c.execute(new Integer[] { null, new Integer(1) });
|
||||
assertEquals(null, result.get(0));
|
||||
assertEquals(new Integer(1), result.get(1));
|
||||
|
||||
@@ -6,17 +6,17 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.BindingPoint;
|
||||
import org.springframework.core.convert.support.CollectionToArray;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.DefaultTypeConverter;
|
||||
|
||||
public class CollectionToArrayTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToArrayConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToArray c = new CollectionToArray(new TypeDescriptor(getClass().getField("bindTarget")),
|
||||
TypeDescriptor.valueOf(Integer[].class), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToArray c = new CollectionToArray(new BindingPoint(getClass().getField("bindTarget")),
|
||||
BindingPoint.valueOf(Integer[].class), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
@@ -28,8 +28,8 @@ public class CollectionToArrayTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToArrayConversionNoGenericInfo() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToArray c = new CollectionToArray(BindingPoint.valueOf(Collection.class), BindingPoint
|
||||
.valueOf(Integer[].class), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
@@ -42,8 +42,8 @@ public class CollectionToArrayTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToArray c = new CollectionToArray(BindingPoint.valueOf(Collection.class), BindingPoint
|
||||
.valueOf(Integer[].class), service);
|
||||
bindTarget.add(null);
|
||||
bindTarget.add("1");
|
||||
|
||||
@@ -8,17 +8,17 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.BindingPoint;
|
||||
import org.springframework.core.convert.support.CollectionToCollection;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.DefaultTypeConverter;
|
||||
|
||||
public class CollectionToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(new TypeDescriptor(getClass().getField("bindTarget")),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToCollection c = new CollectionToCollection(new BindingPoint(getClass().getField("bindTarget")),
|
||||
new BindingPoint(getClass().getField("integerTarget")), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
@@ -30,9 +30,9 @@ public class CollectionToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfo() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
TypeDescriptor.valueOf(List.class), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
|
||||
BindingPoint.valueOf(List.class), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
@@ -44,9 +44,9 @@ public class CollectionToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfoSource() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
|
||||
new BindingPoint(getClass().getField("integerTarget")), service);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
bindTarget.add("3");
|
||||
@@ -58,9 +58,9 @@ public class CollectionToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfoSourceNullValues() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
|
||||
new BindingPoint(getClass().getField("integerTarget")), service);
|
||||
bindTarget.add(null);
|
||||
bindTarget.add("1");
|
||||
bindTarget.add("2");
|
||||
@@ -76,9 +76,9 @@ public class CollectionToCollectionTests {
|
||||
|
||||
@Test
|
||||
public void testCollectionToCollectionConversionNoGenericInfoSourceEmpty() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
|
||||
new TypeDescriptor(getClass().getField("integerTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
CollectionToCollection c = new CollectionToCollection(BindingPoint.valueOf(Collection.class),
|
||||
new BindingPoint(getClass().getField("integerTarget")), service);
|
||||
List result = (List) c.execute(bindTarget);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.core.convert.converters;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -28,7 +28,7 @@ import org.springframework.core.convert.support.StringToShort;
|
||||
/**
|
||||
* Tests for the default converters in the converters package.
|
||||
s */
|
||||
public class DefaultConverterTests {
|
||||
public class DefaultTypeConverterTests {
|
||||
|
||||
@Test
|
||||
public void testStringToByte() throws Exception {
|
||||
@@ -21,7 +21,6 @@ import static junit.framework.Assert.fail;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -29,43 +28,35 @@ import java.util.Map;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.ConversionException;
|
||||
import org.springframework.core.convert.BindingPoint;
|
||||
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;
|
||||
|
||||
public class GenericConversionServiceTests {
|
||||
public class GenericTypeConverterTests {
|
||||
|
||||
private GenericConversionService service = new GenericConversionService();
|
||||
private GenericTypeConverter converter = new GenericTypeConverter();
|
||||
|
||||
@Test
|
||||
public void executeConversion() {
|
||||
service.addConverter(new StringToInteger());
|
||||
assertEquals(new Integer(3), service.convert("3", type(Integer.class)));
|
||||
converter.addConverter(new StringToInteger());
|
||||
assertEquals(new Integer(3), converter.convert("3", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeConversionNullSource() {
|
||||
assertEquals(null, service.convert(null, type(Integer.class)));
|
||||
assertEquals(null, converter.convert(null, Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeCompatibleSource() {
|
||||
assertEquals(false, service.convert(false, type(boolean.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void converterConvert() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
|
||||
Integer three = (Integer) executor.execute("3");
|
||||
assertEquals(3, three.intValue());
|
||||
assertEquals(Boolean.FALSE, converter.convert(false, boolean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertExecutorNotFound() {
|
||||
public void converterNotFound() {
|
||||
try {
|
||||
service.convert("3", type(Integer.class));
|
||||
converter.convert("3", Integer.class);
|
||||
fail("Should have thrown an exception");
|
||||
} catch (ConverterNotFoundException e) {
|
||||
}
|
||||
@@ -74,14 +65,10 @@ public class GenericConversionServiceTests {
|
||||
@Test
|
||||
public void addConverterNoSourceTargetClassInfoAvailable() {
|
||||
try {
|
||||
service.addConverter(new Converter() {
|
||||
converter.addConverter(new Converter() {
|
||||
public Object convert(Object source) throws Exception {
|
||||
return source;
|
||||
}
|
||||
|
||||
public Object convertBack(Object target) throws Exception {
|
||||
return target;
|
||||
}
|
||||
});
|
||||
fail("Should have failed");
|
||||
} catch (IllegalArgumentException e) {
|
||||
@@ -91,51 +78,40 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertNull() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
|
||||
assertNull(executor.execute(null));
|
||||
assertNull(converter.convert(null, Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWrongTypeArgument() {
|
||||
service.addConverter(new StringToInteger());
|
||||
converter.addConverter(new StringToInteger());
|
||||
try {
|
||||
service.convert("BOGUS", type(Integer.class));
|
||||
converter.convert("BOGUS", Integer.class);
|
||||
fail("Should have failed");
|
||||
} catch (ConversionException e) {
|
||||
} catch (ConversionFailedException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSuperSourceType() {
|
||||
service.addConverter(new Converter<CharSequence, Integer>() {
|
||||
converter.addConverter(new Converter<CharSequence, Integer>() {
|
||||
public Integer convert(CharSequence source) throws Exception {
|
||||
return Integer.valueOf(source.toString());
|
||||
}
|
||||
|
||||
public CharSequence convertBack(Integer target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
});
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
|
||||
Integer result = (Integer) executor.execute("3");
|
||||
Integer result = converter.convert("3", Integer.class);
|
||||
assertEquals(new Integer(3), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertNoSuperTargetType() {
|
||||
service.addConverter(new Converter<CharSequence, Number>() {
|
||||
converter.addConverter(new Converter<CharSequence, Number>() {
|
||||
public Integer convert(CharSequence source) throws Exception {
|
||||
return Integer.valueOf(source.toString());
|
||||
}
|
||||
|
||||
public CharSequence convertBack(Number target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
});
|
||||
try {
|
||||
service.convert("3", type(Integer.class));
|
||||
converter.convert("3", Integer.class);
|
||||
fail("Should have failed");
|
||||
} catch (ConverterNotFoundException e) {
|
||||
|
||||
@@ -144,17 +120,15 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertObjectToPrimitive() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(int.class));
|
||||
Integer three = (Integer) executor.execute("3");
|
||||
converter.addConverter(new StringToInteger());
|
||||
Integer three = converter.convert("3", int.class);
|
||||
assertEquals(3, three.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToArray() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(Integer[].class));
|
||||
Integer[] result = (Integer[]) executor.execute(new String[] { "1", "2", "3" });
|
||||
converter.addConverter(new StringToInteger());
|
||||
Integer[] result = converter.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]);
|
||||
@@ -162,9 +136,8 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertArrayToPrimitiveArray() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(int[].class));
|
||||
int[] result = (int[]) executor.execute(new String[] { "1", "2", "3" });
|
||||
converter.addConverter(new StringToInteger());
|
||||
int[] result = (int[]) converter.convert(new String[] { "1", "2", "3" }, int[].class);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
@@ -172,8 +145,7 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertArrayToListInterface() {
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(List.class));
|
||||
List result = (List) executor.execute(new String[] { "1", "2", "3" });
|
||||
List<?> result = converter.convert(new String[] { "1", "2", "3" }, List.class);
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
assertEquals("3", result.get(2));
|
||||
@@ -183,10 +155,8 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertArrayToListGenericTypeConversion() throws Exception {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, new TypeDescriptor(getClass()
|
||||
.getDeclaredField("genericList")));
|
||||
List result = (List) executor.execute(new String[] { "1", "2", "3" });
|
||||
converter.addConverter(new StringToInteger());
|
||||
List<Integer> result = converter.convert(new String[] { "1", "2", "3" }, new BindingPoint<List<Integer>>(getClass().getDeclaredField("genericList")));
|
||||
assertEquals(new Integer("1"), result.get(0));
|
||||
assertEquals(new Integer("2"), result.get(1));
|
||||
assertEquals(new Integer("3"), result.get(2));
|
||||
@@ -194,8 +164,7 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertArrayToListImpl() {
|
||||
ConversionExecutor executor = service.getConversionExecutor(String[].class, type(LinkedList.class));
|
||||
LinkedList result = (LinkedList) executor.execute(new String[] { "1", "2", "3" });
|
||||
LinkedList<?> result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class);
|
||||
assertEquals("1", result.get(0));
|
||||
assertEquals("2", result.get(1));
|
||||
assertEquals("3", result.get(2));
|
||||
@@ -204,7 +173,7 @@ public class GenericConversionServiceTests {
|
||||
@Test
|
||||
public void convertArrayToAbstractList() {
|
||||
try {
|
||||
service.getConversionExecutor(String[].class, type(AbstractList.class));
|
||||
converter.convert(new String[] { "1", "2", "3" }, AbstractList.class);
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
||||
}
|
||||
@@ -212,12 +181,11 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertListToArray() {
|
||||
ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(String[].class));
|
||||
List list = new ArrayList();
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("1");
|
||||
list.add("2");
|
||||
list.add("3");
|
||||
String[] result = (String[]) executor.execute(list);
|
||||
String[] result = (String[]) converter.convert(list, String[].class);
|
||||
assertEquals("1", result[0]);
|
||||
assertEquals("2", result[1]);
|
||||
assertEquals("3", result[2]);
|
||||
@@ -225,13 +193,12 @@ public class GenericConversionServiceTests {
|
||||
|
||||
@Test
|
||||
public void convertListToArrayWithComponentConversion() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(Integer[].class));
|
||||
List list = new ArrayList();
|
||||
converter.addConverter(new StringToInteger());
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("1");
|
||||
list.add("2");
|
||||
list.add("3");
|
||||
Integer[] result = (Integer[]) executor.execute(list);
|
||||
Integer[] result = converter.convert(list, Integer[].class);
|
||||
assertEquals(new Integer(1), result[0]);
|
||||
assertEquals(new Integer(2), result[1]);
|
||||
assertEquals(new Integer(3), result[2]);
|
||||
@@ -245,16 +212,17 @@ public class GenericConversionServiceTests {
|
||||
Map<String, String> foo = new HashMap<String, String>();
|
||||
foo.put("1", "BAR");
|
||||
foo.put("2", "BAZ");
|
||||
service.addConverter(new StringToInteger());
|
||||
service.addConverter(new StringToEnumFactory().getConverter(FooEnum.class));
|
||||
service.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
|
||||
converter.addConverter(new StringToInteger());
|
||||
converter.addConverter(new StringToEnumFactory().getConverter(FooEnum.class));
|
||||
Map<String, FooEnum> map = converter.convert(foo, new BindingPoint<Map<String, FooEnum>>(getClass().getField("genericMap")));
|
||||
assertEquals(map.get(1), FooEnum.BAR);
|
||||
assertEquals(map.get(2), FooEnum.BAZ);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void convertObjectToArray() {
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(String[].class));
|
||||
String[] result = (String[]) executor.execute("1,2,3");
|
||||
String[] result = (String[]) converter.convert("1,2,3", String[].class);
|
||||
assertEquals(1, result.length);
|
||||
assertEquals("1,2,3", result[0]);
|
||||
}
|
||||
@@ -262,9 +230,8 @@ public class GenericConversionServiceTests {
|
||||
@Ignore
|
||||
@Test
|
||||
public void convertObjectToArrayWithElementConversion() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer[].class));
|
||||
Integer[] result = (Integer[]) executor.execute("123");
|
||||
converter.addConverter(new StringToInteger());
|
||||
Integer[] result = converter.convert("123", Integer[].class);
|
||||
assertEquals(1, result.length);
|
||||
assertEquals(new Integer(123), result[0]);
|
||||
}
|
||||
@@ -273,8 +240,4 @@ public class GenericConversionServiceTests {
|
||||
BAR, BAZ
|
||||
}
|
||||
|
||||
private TypeDescriptor type(Class<?> clazz) {
|
||||
return TypeDescriptor.valueOf(clazz);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,29 +6,29 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.BindingPoint;
|
||||
import org.springframework.core.convert.support.DefaultTypeConverter;
|
||||
import org.springframework.core.convert.support.MapToMap;
|
||||
|
||||
public class MapToMapTests {
|
||||
|
||||
@Test
|
||||
public void testMapToMapConversion() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
MapToMap c = new MapToMap(new TypeDescriptor(getClass().getField("source")),
|
||||
new TypeDescriptor(getClass().getField("bindTarget")), service);
|
||||
DefaultTypeConverter converter = new DefaultTypeConverter();
|
||||
MapToMap c = new MapToMap(new BindingPoint<Map<String, String>>(getClass().getField("source")),
|
||||
new BindingPoint<Map<String, FooEnum>>(getClass().getField("bindTarget")), converter);
|
||||
source.put("1", "BAR");
|
||||
source.put("2", "BAZ");
|
||||
Map result = (Map) c.execute(source);
|
||||
Map<String, FooEnum> result = (Map<String, FooEnum>) c.execute(source);
|
||||
assertEquals(FooEnum.BAR, result.get(1));
|
||||
assertEquals(FooEnum.BAZ, result.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapToMapConversionNoGenericInfoOnSource() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class),
|
||||
new TypeDescriptor(getClass().getField("bindTarget")), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
MapToMap c = new MapToMap(BindingPoint.valueOf(Map.class),
|
||||
new BindingPoint(getClass().getField("bindTarget")), service);
|
||||
source.put("1", "BAR");
|
||||
source.put("2", "BAZ");
|
||||
Map result = (Map) c.execute(source);
|
||||
@@ -38,9 +38,9 @@ public class MapToMapTests {
|
||||
|
||||
@Test
|
||||
public void testMapToMapConversionNoGenericInfo() throws Exception {
|
||||
DefaultConversionService service = new DefaultConversionService();
|
||||
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class),
|
||||
TypeDescriptor.valueOf(Map.class), service);
|
||||
DefaultTypeConverter service = new DefaultTypeConverter();
|
||||
MapToMap c = new MapToMap(BindingPoint.valueOf(Map.class),
|
||||
BindingPoint.valueOf(Map.class), service);
|
||||
source.put("1", "BAR");
|
||||
source.put("2", "BAZ");
|
||||
Map result = (Map) c.execute(source);
|
||||
|
||||
Reference in New Issue
Block a user