diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java index d3f0839d9b..7a16daa39f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java @@ -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; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java index 3c166afccf..20cc6381b7 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java @@ -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; } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 8d652f4764..0f4e66bf60 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -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 { + + 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 { + + 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() {