refined exception messages; added unit tests for custom array types
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3577 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
122d313f9c
commit
2c62aedd12
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.springframework.core.convert;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Exception to be thrown when an actual type conversion attempt fails.
|
||||
*
|
||||
|
|
@ -40,8 +42,8 @@ public final class ConversionFailedException extends ConversionException {
|
|||
* @param cause the cause of the conversion failure
|
||||
*/
|
||||
public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
|
||||
super("Unable to convert value \"" + value + "\" from type '" + sourceType.getName() +
|
||||
"' to type '" + targetType.getName() + "'", cause);
|
||||
super("Unable to convert value \"" + ObjectUtils.nullSafeToString(value) + "\" from type '" +
|
||||
sourceType.getName() + "' to type '" + targetType.getName() + "'", cause);
|
||||
this.sourceType = sourceType;
|
||||
this.targetType = targetType;
|
||||
this.value = value;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -36,7 +36,8 @@ public final class ConverterNotFoundException extends ConversionException {
|
|||
* @param message a descriptive message
|
||||
*/
|
||||
public ConverterNotFoundException(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
super("No converter found capable of converting from [" + sourceType.getName() + "] to [" + targetType.getName() + "]");
|
||||
super("No converter found capable of converting from '" + sourceType.getName() +
|
||||
"' to '" + targetType.getName() + "'");
|
||||
this.sourceType = sourceType;
|
||||
this.targetType = targetType;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue