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> {
|
||||
|
||||
public Boolean convert(String source) {
|
||||
if (source.equals("true")) {
|
||||
if (source.equals("")) {
|
||||
return null;
|
||||
} else if (source.equals("true")) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
else if (source.equals("false")) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
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) {
|
||||
if ("".equals(source)) {
|
||||
return null;
|
||||
}
|
||||
return NumberUtils.parseNumber(source, targetType);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,12 +42,39 @@ public class DefaultConversionServiceTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBoolean() {
|
||||
public void testStringToCharacterEmptyString() {
|
||||
StringToCharacterConverter c = new StringToCharacterConverter();
|
||||
assertEquals(null, c.convert(""));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testStringToCharacterInvalidString() {
|
||||
new StringToCharacterConverter().convert("invalid");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBooleanTrue() {
|
||||
StringToBooleanConverter c = new StringToBooleanConverter();
|
||||
assertEquals(Boolean.valueOf(true), c.convert("true"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToBooleanFalse() {
|
||||
StringToBooleanConverter c = new StringToBooleanConverter();
|
||||
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
|
||||
public void testStringToByte() throws Exception {
|
||||
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"));
|
||||
}
|
||||
|
||||
@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
|
||||
public void testStringToEnum() throws Exception {
|
||||
Converter<String, Foo> c = new StringToEnumConverterFactory().getConverter(Foo.class);
|
||||
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 {
|
||||
BAR, BAZ;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue