converter system refactoring from community input
This commit is contained in:
@@ -8,21 +8,22 @@ import java.math.BigInteger;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.converter.NumberToCharacter;
|
||||
import org.springframework.core.convert.converter.NumberToNumber;
|
||||
import org.springframework.core.convert.converter.ObjectToString;
|
||||
import org.springframework.core.convert.converter.StringToBigDecimal;
|
||||
import org.springframework.core.convert.converter.StringToBigInteger;
|
||||
import org.springframework.core.convert.converter.StringToBoolean;
|
||||
import org.springframework.core.convert.converter.StringToByte;
|
||||
import org.springframework.core.convert.converter.StringToCharacter;
|
||||
import org.springframework.core.convert.converter.StringToDouble;
|
||||
import org.springframework.core.convert.converter.StringToEnum;
|
||||
import org.springframework.core.convert.converter.StringToFloat;
|
||||
import org.springframework.core.convert.converter.StringToInteger;
|
||||
import org.springframework.core.convert.converter.StringToLocale;
|
||||
import org.springframework.core.convert.converter.StringToLong;
|
||||
import org.springframework.core.convert.converter.StringToShort;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.NumberToCharacter;
|
||||
import org.springframework.core.convert.support.NumberToNumberFactory;
|
||||
import org.springframework.core.convert.support.ObjectToString;
|
||||
import org.springframework.core.convert.support.StringToBigDecimal;
|
||||
import org.springframework.core.convert.support.StringToBigInteger;
|
||||
import org.springframework.core.convert.support.StringToBoolean;
|
||||
import org.springframework.core.convert.support.StringToByte;
|
||||
import org.springframework.core.convert.support.StringToCharacter;
|
||||
import org.springframework.core.convert.support.StringToDouble;
|
||||
import org.springframework.core.convert.support.StringToEnumFactory;
|
||||
import org.springframework.core.convert.support.StringToFloat;
|
||||
import org.springframework.core.convert.support.StringToInteger;
|
||||
import org.springframework.core.convert.support.StringToLocale;
|
||||
import org.springframework.core.convert.support.StringToLong;
|
||||
import org.springframework.core.convert.support.StringToShort;
|
||||
|
||||
/**
|
||||
* Tests for the default converters in the converters package.
|
||||
@@ -33,14 +34,12 @@ public class DefaultConverterTests {
|
||||
public void testStringToByte() throws Exception {
|
||||
StringToByte b = new StringToByte();
|
||||
assertEquals(Byte.valueOf("1"), b.convert("1"));
|
||||
assertEquals("1", b.convertBack(Byte.valueOf("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToCharacter() {
|
||||
StringToCharacter c = new StringToCharacter();
|
||||
assertEquals(Character.valueOf('1'), c.convert("1"));
|
||||
assertEquals("1", c.convertBack(Character.valueOf('1')));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -48,84 +47,54 @@ public class DefaultConverterTests {
|
||||
StringToBoolean c = new StringToBoolean();
|
||||
assertEquals(Boolean.valueOf(true), c.convert("true"));
|
||||
assertEquals(Boolean.valueOf(false), c.convert("false"));
|
||||
assertEquals("true", c.convertBack(Boolean.TRUE));
|
||||
assertEquals("false", c.convertBack(Boolean.FALSE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBooleanCustomString() {
|
||||
StringToBoolean c = new StringToBoolean("yes", "no");
|
||||
assertEquals(Boolean.valueOf(true), c.convert("yes"));
|
||||
assertEquals(Boolean.valueOf(false), c.convert("no"));
|
||||
assertEquals("yes", c.convertBack(Boolean.TRUE));
|
||||
assertEquals("no", c.convertBack(Boolean.FALSE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBooleanInvalidValue() {
|
||||
StringToBoolean c = new StringToBoolean("yes", "no");
|
||||
try {
|
||||
c.convert("true");
|
||||
fail("Should have failed");
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToShort() {
|
||||
StringToShort c = new StringToShort();
|
||||
assertEquals(Short.valueOf("1"), c.convert("1"));
|
||||
assertEquals("1", c.convertBack(Short.valueOf("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToInteger() {
|
||||
StringToInteger c = new StringToInteger();
|
||||
assertEquals(Integer.valueOf("1"), c.convert("1"));
|
||||
assertEquals("1", c.convertBack(Integer.valueOf("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToLong() {
|
||||
StringToLong c = new StringToLong();
|
||||
assertEquals(Long.valueOf("1"), c.convert("1"));
|
||||
assertEquals("1", c.convertBack(Long.valueOf("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToFloat() {
|
||||
StringToFloat c = new StringToFloat();
|
||||
assertEquals(Float.valueOf("1.0"), c.convert("1.0"));
|
||||
assertEquals("1.0", c.convertBack(Float.valueOf("1.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToDouble() {
|
||||
StringToDouble c = new StringToDouble();
|
||||
assertEquals(Double.valueOf("1.0"), c.convert("1.0"));
|
||||
assertEquals("1.0", c.convertBack(Double.valueOf("1.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBigInteger() {
|
||||
StringToBigInteger c = new StringToBigInteger();
|
||||
assertEquals(new BigInteger("1"), c.convert("1"));
|
||||
assertEquals("1", c.convertBack(new BigInteger("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBigDouble() {
|
||||
StringToBigDecimal c = new StringToBigDecimal();
|
||||
assertEquals(new BigDecimal("1.0"), c.convert("1.0"));
|
||||
assertEquals("1.0", c.convertBack(new BigDecimal("1.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToEnum() {
|
||||
StringToEnum c = new StringToEnum();
|
||||
assertEquals(Foo.BAR, c.convert("BAR", Foo.class));
|
||||
assertEquals("BAR", c.convertBack(Foo.BAR, String.class));
|
||||
public void testStringToEnum() throws Exception {
|
||||
Converter<String, Foo> c = new StringToEnumFactory().getConverter(Foo.class);
|
||||
assertEquals(Foo.BAR, c.convert("BAR"));
|
||||
}
|
||||
|
||||
public static enum Foo {
|
||||
@@ -136,20 +105,19 @@ public class DefaultConverterTests {
|
||||
public void testStringToLocale() {
|
||||
StringToLocale c = new StringToLocale();
|
||||
assertEquals(Locale.ENGLISH, c.convert("en"));
|
||||
assertEquals("en", c.convertBack(Locale.ENGLISH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberToNumber() {
|
||||
NumberToNumber n = new NumberToNumber();
|
||||
assertEquals(Long.valueOf(1), n.convert(Integer.valueOf(1), Long.class));
|
||||
public void testNumberToNumber() throws Exception {
|
||||
Converter<Number, Long> c = new NumberToNumberFactory().getConverter(Long.class);
|
||||
assertEquals(Long.valueOf(1), c.convert(Integer.valueOf(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberToNumberNotSupportedNumber() {
|
||||
NumberToNumber n = new NumberToNumber();
|
||||
public void testNumberToNumberNotSupportedNumber() throws Exception {
|
||||
Converter<Number, CustomNumber> c = new NumberToNumberFactory().getConverter(CustomNumber.class);
|
||||
try {
|
||||
n.convert(Integer.valueOf(1), CustomNumber.class);
|
||||
c.convert(Integer.valueOf(1));
|
||||
fail("Should have failed");
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
||||
@@ -159,14 +127,13 @@ public class DefaultConverterTests {
|
||||
@Test
|
||||
public void testNumberToCharacter() {
|
||||
NumberToCharacter n = new NumberToCharacter();
|
||||
assertEquals(Character.valueOf('A'), n.convert(Integer.valueOf(65), Character.class));
|
||||
assertEquals(Integer.valueOf(65), n.convertBack(Character.valueOf('A'), Integer.class));
|
||||
assertEquals(Character.valueOf('A'), n.convert(Integer.valueOf(65)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectToString() {
|
||||
ObjectToString o = new ObjectToString();
|
||||
assertEquals("3", o.convert(3, String.class));
|
||||
assertEquals("3", o.convert(3));
|
||||
}
|
||||
|
||||
public static class CustomNumber extends Number {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package org.springframework.core.convert.service;
|
||||
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.support.ArrayToArray;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
|
||||
public class ArrayToArrayTests {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.core.convert.service;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -10,6 +10,8 @@ import java.util.SortedSet;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.ArrayToCollection;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
|
||||
public class ArrayToCollectionTests {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.core.convert.service;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.CollectionToArray;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
|
||||
public class CollectionToArrayTests {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.core.convert.service;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -9,6 +9,8 @@ import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.CollectionToCollection;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
|
||||
public class CollectionToCollectionTests {
|
||||
|
||||
@@ -13,11 +13,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.core.convert.service;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertSame;
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import java.util.AbstractList;
|
||||
@@ -30,13 +29,10 @@ import java.util.Map;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.ConversionExecutionException;
|
||||
import org.springframework.core.convert.ConversionException;
|
||||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.NumberToNumber;
|
||||
import org.springframework.core.convert.converter.StringToEnum;
|
||||
import org.springframework.core.convert.converter.StringToInteger;
|
||||
|
||||
public class GenericConversionServiceTests {
|
||||
|
||||
@@ -57,29 +53,15 @@ public class GenericConversionServiceTests {
|
||||
public void executeCompatibleSource() {
|
||||
assertEquals(false, service.convert(false, type(boolean.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeCompatibleSource2() {
|
||||
assertEquals(3, service.getConversionExecutor(Integer.class, TypeDescriptor.valueOf(int.class)).execute(new Integer(3)));
|
||||
assertEquals(3, service.getConversionExecutor(int.class, TypeDescriptor.valueOf(Integer.class)).execute(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void converterConvertForwardIndex() {
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertReverseIndex() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(Integer.class, type(String.class));
|
||||
String threeString = (String) executor.execute(new Integer(3));
|
||||
assertEquals("3", threeString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertExecutorNotFound() {
|
||||
try {
|
||||
@@ -107,12 +89,6 @@ public class GenericConversionServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCompatibleTypes() {
|
||||
String source = "foo";
|
||||
assertSame(source, service.getConversionExecutor(String.class, type(String.class)).execute(source));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertNull() {
|
||||
service.addConverter(new StringToInteger());
|
||||
@@ -123,11 +99,10 @@ public class GenericConversionServiceTests {
|
||||
@Test
|
||||
public void convertWrongTypeArgument() {
|
||||
service.addConverter(new StringToInteger());
|
||||
ConversionExecutor executor = service.getConversionExecutor(Integer.class, type(String.class));
|
||||
try {
|
||||
executor.execute("BOGUS");
|
||||
service.convert("BOGUS", type(Integer.class));
|
||||
fail("Should have failed");
|
||||
} catch (ConversionExecutionException e) {
|
||||
} catch (ConversionException e) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -265,12 +240,13 @@ public class GenericConversionServiceTests {
|
||||
public Map<Integer, FooEnum> genericMap = new HashMap<Integer, FooEnum>();
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void convertMapToMap() throws Exception {
|
||||
Map<String, String> foo = new HashMap<String, String>();
|
||||
foo.put("1", "BAR");
|
||||
foo.put("2", "BAZ");
|
||||
service.addConverter(new StringToInteger());
|
||||
service.addConverter(new StringToEnum());
|
||||
service.addConverter(new StringToEnumFactory().getConverter(FooEnum.class));
|
||||
service.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
|
||||
}
|
||||
|
||||
@@ -297,71 +273,6 @@ public class GenericConversionServiceTests {
|
||||
BAR, BAZ
|
||||
}
|
||||
|
||||
@Test
|
||||
public void superConverterConvertForwardIndex() {
|
||||
service.addConverter(new StringToEnum());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(FooEnum.class));
|
||||
assertEquals(FooEnum.BAR, executor.execute("BAR"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void superTwoWayConverterConvertReverseIndex() {
|
||||
service.addConverter(new StringToEnum());
|
||||
ConversionExecutor executor = service.getConversionExecutor(FooEnum.class, type(String.class));
|
||||
assertEquals("BAR", executor.execute(FooEnum.BAR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void superConverterConvertNotConvertibleAbstractType() {
|
||||
service.addConverter(new StringToEnum());
|
||||
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Enum.class));
|
||||
try {
|
||||
executor.execute("WHATEV");
|
||||
fail("Should have failed");
|
||||
} catch (ConversionExecutionException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void superConverterConvertNotConvertibleAbstractType2() {
|
||||
service.addConverter(new NumberToNumber());
|
||||
Number customNumber = new Number() {
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float floatValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long longValue() {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
ConversionExecutor executor = service.getConversionExecutor(Integer.class, type(customNumber.getClass()));
|
||||
try {
|
||||
executor.execute(3);
|
||||
fail("Should have failed");
|
||||
} catch (ConversionExecutionException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuperTwoWayConverterConverterAdaption() {
|
||||
service.addConverter(GenericConversionService.converterFor(String.class, FooEnum.class, new StringToEnum()));
|
||||
assertEquals(FooEnum.BAR, service.convert("BAR", type(FooEnum.class)));
|
||||
}
|
||||
|
||||
private TypeDescriptor type(Class<?> clazz) {
|
||||
return TypeDescriptor.valueOf(clazz);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.core.convert.service;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -7,6 +7,8 @@ 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.support.MapToMap;
|
||||
|
||||
public class MapToMapTests {
|
||||
|
||||
Reference in New Issue
Block a user