refined exception messages; added unit tests for custom array types

This commit is contained in:
Juergen Hoeller
2010-08-14 19:42:29 +00:00
parent 5109501d16
commit 665a997f66
3 changed files with 53 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
/**
* @author Keith Donald
@@ -216,6 +217,26 @@ public class GenericConversionServiceTests {
assertEquals("RESULT", converted[0]);
}
@Test
public void testStringArrayToIntegerArray() {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
conversionService.addConverter(new MyStringArrayToIntegerArrayConverter());
Integer[] converted = conversionService.convert(new String[] {"x1", "z3"}, Integer[].class);
assertEquals(2, converted.length);
assertEquals(1, converted[0].intValue());
assertEquals(3, converted[1].intValue());
}
@Test
public void testStringToIntegerArray() {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
conversionService.addConverter(new MyStringToIntegerArrayConverter());
Integer[] converted = conversionService.convert("x1,z3", Integer[].class);
assertEquals(2, converted.length);
assertEquals(1, converted[0].intValue());
assertEquals(3, converted[1].intValue());
}
@Test
public void testWildcardMap() throws Exception {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
@@ -372,6 +393,31 @@ public class GenericConversionServiceTests {
}
private static class MyStringArrayToIntegerArrayConverter implements Converter<String[], Integer[]> {
public Integer[] convert(String[] source) {
Integer[] result = new Integer[source.length];
for (int i = 0; i < source.length; i++) {
result[i] = Integer.parseInt(source[i].substring(1));
}
return result;
}
}
private static class MyStringToIntegerArrayConverter implements Converter<String, Integer[]> {
public Integer[] convert(String source) {
String[] srcArray = StringUtils.commaDelimitedListToStringArray(source);
Integer[] result = new Integer[srcArray.length];
for (int i = 0; i < srcArray.length; i++) {
result[i] = Integer.parseInt(srcArray[i].substring(1));
}
return result;
}
}
public static class WithCopyConstructor {
public WithCopyConstructor() {