valueOf and Constructor-based conversion of source objects to targetTypes

This commit is contained in:
Keith Donald
2009-11-12 05:25:03 +00:00
parent b5cda8db7b
commit b163f123ce
4 changed files with 118 additions and 3 deletions

View File

@@ -67,6 +67,7 @@ public class GenericConversionServiceTests {
@Test
public void converterNotFound() {
try {
conversionService.removeConvertible(Object.class, Object.class);
conversionService.convert("3", Integer.class);
fail("Should have thrown an exception");
} catch (ConverterNotFoundException e) {
@@ -125,6 +126,7 @@ public class GenericConversionServiceTests {
@Test
public void convertObjectToPrimitive() {
conversionService.removeConvertible(Object.class, Object.class);
assertFalse(conversionService.canConvert(String.class, boolean.class));
conversionService.addConverter(new StringToBooleanConverter());
assertTrue(conversionService.canConvert(String.class, boolean.class));
@@ -660,6 +662,46 @@ public class GenericConversionServiceTests {
assertEquals(new Long(1), result.get(1L));
}
@Test
public void convertObjectToObjectValueOFMethod() {
assertEquals(new Integer(3), conversionService.convert("3", Integer.class));
}
@Test
public void convertObjectToObjectConstructor() {
assertEquals(new SSN("123456789"), conversionService.convert("123456789", SSN.class));
assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class));
}
@Test(expected=ConversionFailedException.class)
public void convertObjectToObjectNoValueOFMethodOrConstructor() {
conversionService.convert(new Long(3), Integer.class);
}
private static class SSN {
private String value;
public SSN(String value) {
this.value = value;
}
public boolean equals(Object o) {
if (!(o instanceof SSN)) {
return false;
}
SSN ssn = (SSN) o;
return this.value.equals(ssn.value);
}
public int hashCode() {
return value.hashCode();
}
public String toString() {
return value;
}
}
@Test
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
conversionService.addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(
@@ -675,6 +717,8 @@ public class GenericConversionServiceTests {
public void parent() {
GenericConversionService parent = new GenericConversionService();
conversionService.setParent(parent);
conversionService.removeConvertible(Object.class, Object.class);
parent.removeConvertible(Object.class, Object.class);
assertFalse(conversionService.canConvert(String.class, Integer.class));
try {
conversionService.convert("3", Integer.class);
@@ -682,7 +726,7 @@ public class GenericConversionServiceTests {
}
}
public static enum FooEnum {
BAR, BAZ
}