diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java index a6fa950614ce..8c483443170a 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java @@ -114,7 +114,7 @@ public static T convert(String source, Class targetType, ClassLoader clas Class targetTypeToUse = toWrapperType(targetType); Optional converter = stringToObjectConverters.stream().filter( - candidate -> candidate.canConvert(targetTypeToUse)).findFirst(); + candidate -> candidate.canConvertTo(targetTypeToUse)).findFirst(); if (converter.isPresent()) { try { ClassLoader classLoaderToUse = classLoader != null ? classLoader diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverter.java index 678c004f1229..099c32ebdade 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverter.java @@ -70,7 +70,7 @@ class FallbackStringToObjectConverter implements StringToObjectConverter { = new ConcurrentHashMap<>(64); @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return findFactoryExecutable(targetType) != NULL_EXECUTABLE; } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToBooleanConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToBooleanConverter.java index 2bde9ac323c2..c6b9427f8adb 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToBooleanConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToBooleanConverter.java @@ -15,7 +15,7 @@ class StringToBooleanConverter implements StringToObjectConverter { @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return targetType == Boolean.class; } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCharacterConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCharacterConverter.java index 925acdfe62d5..b20afe3a9497 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCharacterConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCharacterConverter.java @@ -15,7 +15,7 @@ class StringToCharacterConverter implements StringToObjectConverter { @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return targetType == Character.class; } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToClassConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToClassConverter.java index df2b0164ad5c..7747a02b24be 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToClassConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToClassConverter.java @@ -15,7 +15,7 @@ class StringToClassConverter implements StringToObjectConverter { @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return targetType == Class.class; } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCommonJavaTypesConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCommonJavaTypesConverter.java index 2988714318e3..f4cede3e6fd0 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCommonJavaTypesConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToCommonJavaTypesConverter.java @@ -49,7 +49,7 @@ class StringToCommonJavaTypesConverter implements StringToObjectConverter { } @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return CONVERTERS.containsKey(targetType); } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToEnumConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToEnumConverter.java index 48c07fa59eb7..50544cb29471 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToEnumConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToEnumConverter.java @@ -13,7 +13,7 @@ class StringToEnumConverter implements StringToObjectConverter { @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return targetType.isEnum(); } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToJavaTimeConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToJavaTimeConverter.java index 6ecbf84b25e5..819a0b71b038 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToJavaTimeConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToJavaTimeConverter.java @@ -53,7 +53,7 @@ class StringToJavaTimeConverter implements StringToObjectConverter { } @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return CONVERTERS.containsKey(targetType); } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToNumberConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToNumberConverter.java index b8cd6e7d3e4f..39a67333d654 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToNumberConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToNumberConverter.java @@ -38,7 +38,7 @@ class StringToNumberConverter implements StringToObjectConverter { } @Override - public boolean canConvert(Class targetType) { + public boolean canConvertTo(Class targetType) { return CONVERTERS.containsKey(targetType); } diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToObjectConverter.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToObjectConverter.java index 243bfeec4afc..bc11f5317523 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToObjectConverter.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToObjectConverter.java @@ -21,12 +21,15 @@ interface StringToObjectConverter { * supplied target type (which is guaranteed to be a wrapper type for * primitives — for example, {@link Integer} instead of {@code int}). */ - boolean canConvert(Class targetType); + boolean canConvertTo(Class targetType); /** * Convert the supplied {@link String} to the supplied target type (which is * guaranteed to be a wrapper type for primitives — for example, * {@link Integer} instead of {@code int}). + * + *

This method will only be invoked in {@link #canConvertTo(Class)} + * returned {@code true} for the same target type. */ Object convert(String source, Class targetType) throws Exception; @@ -35,6 +38,9 @@ interface StringToObjectConverter { * guaranteed to be a wrapper type for primitives — for example, * {@link Integer} instead of {@code int}). * + *

This method will only be invoked in {@link #canConvertTo(Class)} + * returned {@code true} for the same target type. + * *

The default implementation simply delegates to {@link #convert(String, Class)}. * Can be overridden by concrete implementations of this interface that need * access to the supplied {@link ClassLoader}. diff --git a/platform-tests/src/test/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverterTests.java b/platform-tests/src/test/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverterTests.java index efc98701df7d..175e9dd7bbc3 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverterTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverterTests.java @@ -87,13 +87,13 @@ void convertsStringToNewspaperViaConstructorIgnoringMultipleFactoryMethods() thr @Test @DisplayName("Cannot convert String to Diary because Diary has neither a static factory method nor a factory constructor") void cannotConvertStringToDiary() { - assertThat(converter.canConvert(Diary.class)).isFalse(); + assertThat(converter.canConvertTo(Diary.class)).isFalse(); } @Test @DisplayName("Cannot convert String to Magazine because Magazine has multiple static factory methods") void cannotConvertStringToMagazine() { - assertThat(converter.canConvert(Magazine.class)).isFalse(); + assertThat(converter.canConvertTo(Magazine.class)).isFalse(); } // ------------------------------------------------------------------------- @@ -120,7 +120,7 @@ private static Method magazineMethod(String methodName) { } private static void assertConverts(String input, Class targetType, Object expectedOutput) throws Exception { - assertThat(converter.canConvert(targetType)).isTrue(); + assertThat(converter.canConvertTo(targetType)).isTrue(); var result = converter.convert(input, targetType);