Skip to content

Commit 665a997

Browse files
committed
refined exception messages; added unit tests for custom array types
1 parent 5109501 commit 665a997

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.core.convert;
1818

19+
import org.springframework.util.ObjectUtils;
20+
1921
/**
2022
* Exception to be thrown when an actual type conversion attempt fails.
2123
*
@@ -40,8 +42,8 @@ public final class ConversionFailedException extends ConversionException {
4042
* @param cause the cause of the conversion failure
4143
*/
4244
public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
43-
super("Unable to convert value \"" + value + "\" from type '" + sourceType.getName() +
44-
"' to type '" + targetType.getName() + "'", cause);
45+
super("Unable to convert value \"" + ObjectUtils.nullSafeToString(value) + "\" from type '" +
46+
sourceType.getName() + "' to type '" + targetType.getName() + "'", cause);
4547
this.sourceType = sourceType;
4648
this.targetType = targetType;
4749
this.value = value;

org.springframework.core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,8 @@ public final class ConverterNotFoundException extends ConversionException {
3636
* @param message a descriptive message
3737
*/
3838
public ConverterNotFoundException(TypeDescriptor sourceType, TypeDescriptor targetType) {
39-
super("No converter found capable of converting from [" + sourceType.getName() + "] to [" + targetType.getName() + "]");
39+
super("No converter found capable of converting from '" + sourceType.getName() +
40+
"' to '" + targetType.getName() + "'");
4041
this.sourceType = sourceType;
4142
this.targetType = targetType;
4243
}

org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.core.convert.TypeDescriptor;
3333
import org.springframework.core.convert.converter.Converter;
3434
import org.springframework.util.StopWatch;
35+
import org.springframework.util.StringUtils;
3536

3637
/**
3738
* @author Keith Donald
@@ -216,6 +217,26 @@ public void testObjectArrayToStringArray() {
216217
assertEquals("RESULT", converted[0]);
217218
}
218219

220+
@Test
221+
public void testStringArrayToIntegerArray() {
222+
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
223+
conversionService.addConverter(new MyStringArrayToIntegerArrayConverter());
224+
Integer[] converted = conversionService.convert(new String[] {"x1", "z3"}, Integer[].class);
225+
assertEquals(2, converted.length);
226+
assertEquals(1, converted[0].intValue());
227+
assertEquals(3, converted[1].intValue());
228+
}
229+
230+
@Test
231+
public void testStringToIntegerArray() {
232+
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
233+
conversionService.addConverter(new MyStringToIntegerArrayConverter());
234+
Integer[] converted = conversionService.convert("x1,z3", Integer[].class);
235+
assertEquals(2, converted.length);
236+
assertEquals(1, converted[0].intValue());
237+
assertEquals(3, converted[1].intValue());
238+
}
239+
219240
@Test
220241
public void testWildcardMap() throws Exception {
221242
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
@@ -372,6 +393,31 @@ public String convert(MyBaseInterface source) {
372393
}
373394

374395

396+
private static class MyStringArrayToIntegerArrayConverter implements Converter<String[], Integer[]> {
397+
398+
public Integer[] convert(String[] source) {
399+
Integer[] result = new Integer[source.length];
400+
for (int i = 0; i < source.length; i++) {
401+
result[i] = Integer.parseInt(source[i].substring(1));
402+
}
403+
return result;
404+
}
405+
}
406+
407+
408+
private static class MyStringToIntegerArrayConverter implements Converter<String, Integer[]> {
409+
410+
public Integer[] convert(String source) {
411+
String[] srcArray = StringUtils.commaDelimitedListToStringArray(source);
412+
Integer[] result = new Integer[srcArray.length];
413+
for (int i = 0; i < srcArray.length; i++) {
414+
result[i] = Integer.parseInt(srcArray[i].substring(1));
415+
}
416+
return result;
417+
}
418+
}
419+
420+
375421
public static class WithCopyConstructor {
376422

377423
public WithCopyConstructor() {

0 commit comments

Comments
 (0)