polish
This commit is contained in:
parent
e153a1a968
commit
d848d21f09
|
|
@ -27,14 +27,16 @@ import org.springframework.core.convert.converter.Converter;
|
||||||
class StringToBooleanConverter implements Converter<String, Boolean> {
|
class StringToBooleanConverter implements Converter<String, Boolean> {
|
||||||
|
|
||||||
public Boolean convert(String source) {
|
public Boolean convert(String source) {
|
||||||
if (source.equals("true")) {
|
if (source.equals("")) {
|
||||||
|
return null;
|
||||||
|
} else if (source.equals("true")) {
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
}
|
}
|
||||||
else if (source.equals("false")) {
|
else if (source.equals("false")) {
|
||||||
return Boolean.FALSE;
|
return Boolean.FALSE;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected 'true' or 'false'");
|
throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected \"\", 'true', or 'false'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,9 @@ class StringToNumberConverterFactory implements ConverterFactory<String, Number>
|
||||||
}
|
}
|
||||||
|
|
||||||
public T convert(String source) {
|
public T convert(String source) {
|
||||||
|
if ("".equals(source)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return NumberUtils.parseNumber(source, targetType);
|
return NumberUtils.parseNumber(source, targetType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,14 +40,41 @@ public class DefaultConversionServiceTests {
|
||||||
StringToCharacterConverter c = new StringToCharacterConverter();
|
StringToCharacterConverter c = new StringToCharacterConverter();
|
||||||
assertEquals(Character.valueOf('1'), c.convert("1"));
|
assertEquals(Character.valueOf('1'), c.convert("1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringToCharacterEmptyString() {
|
||||||
|
StringToCharacterConverter c = new StringToCharacterConverter();
|
||||||
|
assertEquals(null, c.convert(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testStringToCharacterInvalidString() {
|
||||||
|
new StringToCharacterConverter().convert("invalid");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStringToBoolean() {
|
public void testStringToBooleanTrue() {
|
||||||
StringToBooleanConverter c = new StringToBooleanConverter();
|
StringToBooleanConverter c = new StringToBooleanConverter();
|
||||||
assertEquals(Boolean.valueOf(true), c.convert("true"));
|
assertEquals(Boolean.valueOf(true), c.convert("true"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringToBooleanFalse() {
|
||||||
|
StringToBooleanConverter c = new StringToBooleanConverter();
|
||||||
assertEquals(Boolean.valueOf(false), c.convert("false"));
|
assertEquals(Boolean.valueOf(false), c.convert("false"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringToBooleanEmptyString() {
|
||||||
|
StringToBooleanConverter c = new StringToBooleanConverter();
|
||||||
|
assertEquals(null, c.convert(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testStringToBooleanInvalidString() {
|
||||||
|
new StringToBooleanConverter().convert("invalid");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStringToByte() throws Exception {
|
public void testStringToByte() throws Exception {
|
||||||
assertEquals(Byte.valueOf("1"), c.getConverter(Byte.class).convert("1"));
|
assertEquals(Byte.valueOf("1"), c.getConverter(Byte.class).convert("1"));
|
||||||
|
|
@ -88,12 +115,28 @@ public class DefaultConversionServiceTests {
|
||||||
assertEquals(new BigDecimal("1.0"), c.getConverter(BigDecimal.class).convert("1.0"));
|
assertEquals(new BigDecimal("1.0"), c.getConverter(BigDecimal.class).convert("1.0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringToNumber() {
|
||||||
|
assertEquals(new BigDecimal("1.0"), c.getConverter(Number.class).convert("1.0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringToNumberEmptyString() {
|
||||||
|
assertEquals(null, c.getConverter(Number.class).convert(""));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStringToEnum() throws Exception {
|
public void testStringToEnum() throws Exception {
|
||||||
Converter<String, Foo> c = new StringToEnumConverterFactory().getConverter(Foo.class);
|
Converter<String, Foo> c = new StringToEnumConverterFactory().getConverter(Foo.class);
|
||||||
assertEquals(Foo.BAR, c.convert("BAR"));
|
assertEquals(Foo.BAR, c.convert("BAR"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringToEnumEmptyString() throws Exception {
|
||||||
|
Converter<String, Foo> c = new StringToEnumConverterFactory().getConverter(Foo.class);
|
||||||
|
assertEquals(null, c.convert(""));
|
||||||
|
}
|
||||||
|
|
||||||
public static enum Foo {
|
public static enum Foo {
|
||||||
BAR, BAZ;
|
BAR, BAZ;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue