提交 d966965a 编写于 作者: K Keith Donald

default converter tests

上级 5e46fab3
...@@ -37,7 +37,7 @@ import org.springframework.util.NumberUtils; ...@@ -37,7 +37,7 @@ import org.springframework.util.NumberUtils;
*/ */
public class NumberToNumber implements SuperConverter<Number, Number> { public class NumberToNumber implements SuperConverter<Number, Number> {
public <RT extends Number> RT convert(Number source, Class<RT> targetClass) throws Exception { public <RT extends Number> RT convert(Number source, Class<RT> targetClass) {
return NumberUtils.convertNumberToTargetClass(source, targetClass); return NumberUtils.convertNumberToTargetClass(source, targetClass);
} }
......
...@@ -24,11 +24,11 @@ import java.math.BigDecimal; ...@@ -24,11 +24,11 @@ import java.math.BigDecimal;
*/ */
public class StringToBigDecimal implements Converter<String, BigDecimal> { public class StringToBigDecimal implements Converter<String, BigDecimal> {
public BigDecimal convert(String source) throws Exception { public BigDecimal convert(String source) {
return new BigDecimal(source); return new BigDecimal(source);
} }
public String convertBack(BigDecimal target) throws Exception { public String convertBack(BigDecimal target) {
return target.toString(); return target.toString();
} }
......
...@@ -24,11 +24,11 @@ import java.math.BigInteger; ...@@ -24,11 +24,11 @@ import java.math.BigInteger;
*/ */
public class StringToBigInteger implements Converter<String, BigInteger> { public class StringToBigInteger implements Converter<String, BigInteger> {
public BigInteger convert(String source) throws Exception { public BigInteger convert(String source) {
return new BigInteger(source); return new BigInteger(source);
} }
public String convertBack(BigInteger target) throws Exception { public String convertBack(BigInteger target) {
return target.toString(); return target.toString();
} }
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
*/ */
package org.springframework.core.convert.converter; package org.springframework.core.convert.converter;
import org.springframework.util.Assert;
/** /**
* Converts String to a Boolean. The trueString and falseStrings are configurable. * Converts String to a Boolean. The trueString and falseStrings are configurable.
* *
...@@ -23,10 +25,6 @@ package org.springframework.core.convert.converter; ...@@ -23,10 +25,6 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToBoolean implements Converter<String, Boolean> { public class StringToBoolean implements Converter<String, Boolean> {
private static final String VALUE_TRUE = "true";
private static final String VALUE_FALSE = "false";
private String trueString; private String trueString;
private String falseString; private String falseString;
...@@ -35,47 +33,38 @@ public class StringToBoolean implements Converter<String, Boolean> { ...@@ -35,47 +33,38 @@ public class StringToBoolean implements Converter<String, Boolean> {
* Create a StringToBoolean converter with the default 'true' and 'false' strings. * Create a StringToBoolean converter with the default 'true' and 'false' strings.
*/ */
public StringToBoolean() { public StringToBoolean() {
this("true", "false");
} }
/** /**
* Create a StringToBoolean converter configured with specific values for true and false strings. * Create a StringToBoolean converter configured with specific values for true and false strings.
* @param trueString special true string to use * @param trueString special true string to use (required)
* @param falseString special false string to use * @param falseString special false string to use (required)
*/ */
public StringToBoolean(String trueString, String falseString) { public StringToBoolean(String trueString, String falseString) {
Assert.hasText(trueString, "The true string is required");
Assert.hasText(falseString, "The false string is required");
this.trueString = trueString; this.trueString = trueString;
this.falseString = falseString; this.falseString = falseString;
} }
public Boolean convert(String source) throws Exception { public Boolean convert(String source) {
if (trueString != null && source.equals(trueString)) { if (source.equals(trueString)) {
return Boolean.TRUE;
} else if (falseString != null && source.equals(falseString)) {
return Boolean.FALSE;
} else if (trueString == null && source.equals(VALUE_TRUE)) {
return Boolean.TRUE; return Boolean.TRUE;
} else if (falseString == null && source.equals(VALUE_FALSE)) { } else if (source.equals(falseString)) {
return Boolean.FALSE; return Boolean.FALSE;
} else { } else {
throw new IllegalArgumentException("Invalid boolean value [" + source + "]"); throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected '" + trueString + "' or '" + falseString + "'");
} }
} }
public String convertBack(Boolean target) throws Exception { public String convertBack(Boolean target) {
if (Boolean.TRUE.equals(target)) { if (Boolean.TRUE.equals(target)) {
if (trueString != null) { return trueString;
return trueString;
} else {
return VALUE_TRUE;
}
} else if (Boolean.FALSE.equals(target)) { } else if (Boolean.FALSE.equals(target)) {
if (falseString != null) { return falseString;
return falseString;
} else {
return VALUE_FALSE;
}
} else { } else {
throw new IllegalArgumentException("Invalid boolean value [" + target + "]"); throw new IllegalArgumentException("Invalid boolean value " + target);
} }
} }
......
...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter; ...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToByte implements Converter<String, Byte> { public class StringToByte implements Converter<String, Byte> {
public Byte convert(String source) throws Exception { public Byte convert(String source) {
return new Byte(source); return Byte.valueOf(source);
} }
public String convertBack(Byte target) throws Exception { public String convertBack(Byte target) {
return target.toString(); return target.toString();
} }
......
...@@ -22,14 +22,14 @@ package org.springframework.core.convert.converter; ...@@ -22,14 +22,14 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToCharacter implements Converter<String, Character> { public class StringToCharacter implements Converter<String, Character> {
public Character convert(String source) throws Exception { public Character convert(String source) {
if (source.length() != 1) { if (source.length() != 1) {
throw new IllegalArgumentException("To be a Character the String '" + source + "' must have a length of 1"); throw new IllegalArgumentException("To be a Character the String '" + source + "' must have a length of 1");
} }
return new Character(source.charAt(0)); return Character.valueOf(source.charAt(0));
} }
public String convertBack(Character target) throws Exception { public String convertBack(Character target) {
return target.toString(); return target.toString();
} }
......
...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter; ...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToDouble implements Converter<String, Double> { public class StringToDouble implements Converter<String, Double> {
public Double convert(String source) throws Exception { public Double convert(String source) {
return Double.valueOf(source); return Double.valueOf(source);
} }
public String convertBack(Double target) throws Exception { public String convertBack(Double target) {
return target.toString(); return target.toString();
} }
......
...@@ -24,11 +24,11 @@ package org.springframework.core.convert.converter; ...@@ -24,11 +24,11 @@ package org.springframework.core.convert.converter;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class StringToEnum implements SuperTwoWayConverter<String, Enum> { public class StringToEnum implements SuperTwoWayConverter<String, Enum> {
public <RT extends Enum> RT convert(String source, Class<RT> targetClass) throws Exception { public <RT extends Enum> RT convert(String source, Class<RT> targetClass) {
return (RT) Enum.valueOf(targetClass, source); return (RT) Enum.valueOf(targetClass, source);
} }
public <RS extends String> RS convertBack(Enum target, Class<RS> sourceClass) throws Exception { public <RS extends String> RS convertBack(Enum target, Class<RS> sourceClass) {
return (RS) target.name(); return (RS) target.name();
} }
......
...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter; ...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToFloat implements Converter<String, Float> { public class StringToFloat implements Converter<String, Float> {
public Float convert(String source) throws Exception { public Float convert(String source) {
return Float.valueOf(source); return Float.valueOf(source);
} }
public String convertBack(Float target) throws Exception { public String convertBack(Float target) {
return target.toString(); return target.toString();
} }
......
...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter; ...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToInteger implements Converter<String, Integer> { public class StringToInteger implements Converter<String, Integer> {
public Integer convert(String source) throws Exception { public Integer convert(String source) {
return Integer.valueOf(source); return Integer.valueOf(source);
} }
public String convertBack(Integer target) throws Exception { public String convertBack(Integer target) {
return target.toString(); return target.toString();
} }
......
...@@ -26,11 +26,11 @@ import org.springframework.util.StringUtils; ...@@ -26,11 +26,11 @@ import org.springframework.util.StringUtils;
*/ */
public class StringToLocale implements Converter<String, Locale> { public class StringToLocale implements Converter<String, Locale> {
public Locale convert(String source) throws Exception { public Locale convert(String source) {
return StringUtils.parseLocaleString(source); return StringUtils.parseLocaleString(source);
} }
public String convertBack(Locale target) throws Exception { public String convertBack(Locale target) {
return target.toString(); return target.toString();
} }
......
...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter; ...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToLong implements Converter<String, Long> { public class StringToLong implements Converter<String, Long> {
public Long convert(String source) throws Exception { public Long convert(String source) {
return Long.valueOf(source); return Long.valueOf(source);
} }
public String convertBack(Long target) throws Exception { public String convertBack(Long target) {
return target.toString(); return target.toString();
} }
......
...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter; ...@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/ */
public class StringToShort implements Converter<String, Short> { public class StringToShort implements Converter<String, Short> {
public Short convert(String source) throws Exception { public Short convert(String source) {
return Short.valueOf(source); return Short.valueOf(source);
} }
public String convertBack(Short target) throws Exception { public String convertBack(Short target) {
return target.toString(); return target.toString();
} }
......
package org.springframework.core.convert.converters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Locale;
import org.junit.Test;
import org.springframework.core.convert.converter.NumberToNumber;
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;
/**
* Tests for the default converters in the converters package.
s */
public class DefaultConverterTests {
@Test
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
public void testStringToBoolean() {
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 static enum Foo {
BAR, BAZ;
}
@Test
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));
}
@Test
public void testNumberToNumberNotSupportedNumber() {
NumberToNumber n = new NumberToNumber();
try {
n.convert(Integer.valueOf(1), CustomNumber.class);
fail("Should have failed");
} catch (IllegalArgumentException e) {
}
}
public static class CustomNumber extends 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;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册