Skip to content

Commit 8ed6eb6

Browse files
committed
Enhance canConvert with inner converters evaluation
1 parent f96c9ee commit 8ed6eb6

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/DefaultConversionService.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,20 @@ private DefaultConversionService() {
7878
*/
7979
@Override
8080
public boolean canConvert(Object source, Class<?> targetType, ClassLoader classLoader) {
81-
return source instanceof String;
81+
if (source == null && targetType.isPrimitive()) {
82+
return false;
83+
}
84+
85+
if (!(source instanceof String)) {
86+
return false;
87+
}
88+
89+
if (String.class.equals(targetType)) {
90+
return true;
91+
}
92+
93+
return stringToObjectConverters.stream().anyMatch(
94+
candidate -> candidate.canConvertTo(toWrapperType(targetType)));
8295
}
8396

8497
/**
@@ -145,7 +158,6 @@ public Object convert(Object source, Class<?> targetType, ClassLoader classLoade
145158
return source;
146159
}
147160

148-
// FIXME move/copy next three lines to canConvert?
149161
Class<?> targetTypeToUse = toWrapperType(targetType);
150162
Optional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(
151163
candidate -> candidate.canConvertTo(targetTypeToUse)).findFirst();

jupiter-tests/src/test/java/org/junit/jupiter/params/converter/DefaultArgumentConverterTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void throwsExceptionOnInvalidStringForPrimitiveTypes() {
186186
void throwsExceptionWhenImplicitConverstionIsUnsupported() {
187187
assertThatExceptionOfType(ArgumentConversionException.class) //
188188
.isThrownBy(() -> convert("foo", Enigma.class)) //
189-
.withMessage("No built-in converter for source type java.lang.String and target type %s", // FIXME enhance DefaultConversionService::canConvert
189+
.withMessage("No registered or built-in converter for source type java.lang.String and target type %s",
190190
Enigma.class.getName());
191191

192192
assertThatExceptionOfType(ArgumentConversionException.class) //

0 commit comments

Comments
 (0)