Skip to content

Commit e380b8f

Browse files
committed
Fixes after rebase
1 parent 314453f commit e380b8f

File tree

8 files changed

+32
-52
lines changed

8 files changed

+32
-52
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/DefaultArgumentConverter.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.junit.jupiter.params.support.FieldContext;
2828
import org.junit.platform.commons.support.conversion.ConversionException;
2929
import org.junit.platform.commons.support.conversion.ConversionSupport;
30-
import org.junit.platform.commons.util.ClassLoaderUtils;
3130
import org.junit.platform.commons.util.ReflectionUtils;
3231

3332
/**
@@ -84,17 +83,15 @@ public final Object convert(Object source, Class<?> targetType, ClassLoader clas
8483
return source;
8584
}
8685

87-
Class<?> declaringClass = context.getDeclaringExecutable().getDeclaringClass();
88-
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(declaringClass);
8986
try {
90-
return convert(source, targetType, classLoader);
87+
return delegateConversion(source, targetType, classLoader);
9188
}
9289
catch (ConversionException ex) {
9390
throw new ArgumentConversionException(ex.getMessage(), ex);
9491
}
9592
}
9693

97-
Object convert(Object source, Class<?> targetType, ClassLoader classLoader) {
94+
Object delegateConversion(Object source, Class<?> targetType, ClassLoader classLoader) {
9895
return ConversionSupport.convert(source, targetType, classLoader);
9996
}
10097

junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/TypedArgumentConverter.java

-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ protected TypedArgumentConverter(Class<S> sourceType, Class<T> targetType) {
4747
this.targetType = Preconditions.notNull(targetType, "targetType must not be null");
4848
}
4949

50-
/**
51-
* {@inheritDoc}
52-
*/
5350
@Override
5451
public final Object convert(Object source, ParameterContext context) throws ArgumentConversionException {
5552
return convert(source, context.getParameter().getType());

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
* from a given source type into a given target type and does not need access to
2929
* the {@link ClassLoader} to perform the conversion.
3030
*
31-
* @since 1.12
31+
* @since 1.13
3232
* @see ConversionSupport
3333
* @see TypedConversionService
3434
*/
35-
@API(status = EXPERIMENTAL, since = "1.12")
35+
@API(status = EXPERIMENTAL, since = "1.13")
3636
public interface ConversionService {
3737

3838
/**

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private ConversionSupport() {
5353
*/
5454
@SuppressWarnings("unchecked")
5555
@Deprecated
56-
@API(status = DEPRECATED, since = "5.12")
56+
@API(status = DEPRECATED, since = "5.13")
5757
public static <T> T convert(String source, Class<T> targetType, ClassLoader classLoader) {
5858
return (T) DefaultConversionService.INSTANCE.convert(source, targetType, getClassLoader(classLoader));
5959
}
@@ -72,22 +72,23 @@ public static <T> T convert(String source, Class<T> targetType, ClassLoader clas
7272
* @return the converted object; may be {@code null} but only if the target
7373
* type is a reference type
7474
*
75-
* @since 1.12
75+
* @since 1.13
7676
*/
77-
@API(status = EXPERIMENTAL, since = "1.12")
77+
@API(status = EXPERIMENTAL, since = "1.13")
7878
@SuppressWarnings("unchecked")
7979
public static <T> T convert(Object source, Class<T> targetType, ClassLoader classLoader) {
8080
ClassLoader classLoaderToUse = getClassLoader(classLoader);
8181
ServiceLoader<ConversionService> serviceLoader = ServiceLoader.load(ConversionService.class, classLoaderToUse);
8282

83-
return (T) Stream.concat( //
83+
ConversionService conversionService = Stream.concat( //
8484
StreamSupport.stream(serviceLoader.spliterator(), false), //
8585
Stream.of(DefaultConversionService.INSTANCE)) //
8686
.filter(candidate -> candidate.canConvert(source, targetType, classLoader)) //
8787
.findFirst() //
88-
.map(candidate -> candidate.convert(source, targetType, classLoaderToUse)) //
89-
.orElseThrow(() -> new ConversionException("No registered or built-in converter for source type "
90-
+ source.getClass().getTypeName() + " and target type " + targetType.getTypeName()));
88+
.orElseThrow(() -> new ConversionException("No registered or built-in converter for source '" + source
89+
+ "' and target type " + targetType.getTypeName()));
90+
91+
return (T) conversionService.convert(source, targetType, classLoaderToUse);
9192
}
9293

9394
private static ClassLoader getClassLoader(ClassLoader classLoader) {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
* <p>If the source and target types are identical, the source object will not
4444
* be modified.
4545
*
46-
* @since 1.12
46+
* @since 1.13
4747
*/
48-
@API(status = INTERNAL, since = "5.12")
48+
@API(status = INTERNAL, since = "1.13")
4949
public class DefaultConversionService implements ConversionService {
5050

5151
static final DefaultConversionService INSTANCE = new DefaultConversionService();
@@ -78,8 +78,8 @@ private DefaultConversionService() {
7878
*/
7979
@Override
8080
public boolean canConvert(Object source, Class<?> targetType, ClassLoader classLoader) {
81-
if (source == null && targetType.isPrimitive()) {
82-
return false;
81+
if (source == null) {
82+
return !targetType.isPrimitive();
8383
}
8484

8585
if (!(source instanceof String)) {

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*
2424
* @param <S> the type of the source argument to convert
2525
* @param <T> the type of the target object to create from the source
26-
* @since 1.12
26+
* @since 1.13
2727
*/
28-
@API(status = EXPERIMENTAL, since = "1.12")
28+
@API(status = EXPERIMENTAL, since = "1.13")
2929
public abstract class TypedConversionService<S, T> implements ConversionService {
3030

3131
private final Class<S> sourceType;
@@ -43,17 +43,11 @@ protected TypedConversionService(Class<S> sourceType, Class<T> targetType) {
4343
this.targetType = Preconditions.notNull(targetType, "targetType must not be null");
4444
}
4545

46-
/**
47-
* {@inheritDoc}
48-
*/
4946
@Override
5047
public final boolean canConvert(Object source, Class<?> targetType, ClassLoader classLoader) {
5148
return sourceType.isInstance(source) && ReflectionUtils.isAssignableTo(this.targetType, targetType);
5249
}
5350

54-
/**
55-
* {@inheritDoc}
56-
*/
5751
@Override
5852
public final Object convert(Object source, Class<?> targetType, ClassLoader classLoader) {
5953
return source == null ? convert(null) : convert(this.sourceType.cast(source));

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

+8-18
Original file line numberDiff line numberDiff line change
@@ -78,39 +78,29 @@ void throwsExceptionForNullToPrimitiveTypeConversion(Class<?> type) {
7878
.isThrownBy(() -> convert(null, type)) //
7979
.withMessage("Cannot convert null to primitive value of type " + type.getCanonicalName());
8080

81-
verify(underTest, never()).convert(any(), any(), any(ClassLoader.class));
82-
}
83-
84-
@Test
85-
void throwsExceptionForNonStringsConversion() {
86-
assertThatExceptionOfType(ArgumentConversionException.class) //
87-
.isThrownBy(() -> convert(new Enigma(), String.class)) //
88-
.withMessage("No built-in converter for source type %s and target type java.lang.String",
89-
Enigma.class.getName());
90-
91-
verify(underTest, never()).convert(any(), any(), any(ClassLoader.class));
81+
verify(underTest, never()).delegateConversion(any(), any(), any(ClassLoader.class));
9282
}
9383

9484
@Test
9585
void delegatesStringsConversion() {
96-
doReturn(null).when(underTest).convert(any(), any(), any(ClassLoader.class));
86+
doReturn(null).when(underTest).delegateConversion(any(), any(), any(ClassLoader.class));
9787

9888
convert("value", int.class);
9989

100-
verify(underTest).convert("value", int.class, getClassLoader(DefaultArgumentConverterTests.class));
90+
verify(underTest).delegateConversion("value", int.class, getClassLoader(DefaultArgumentConverterTests.class));
10191
}
10292

10393
@Test
10494
void throwsExceptionForDelegatedConversionFailure() {
10595
ConversionException exception = new ConversionException("fail");
106-
doThrow(exception).when(underTest).convert(any(), any(), any(ClassLoader.class));
96+
doThrow(exception).when(underTest).delegateConversion(any(), any(), any(ClassLoader.class));
10797

10898
assertThatExceptionOfType(ArgumentConversionException.class) //
10999
.isThrownBy(() -> convert("value", int.class)) //
110100
.withCause(exception) //
111101
.withMessage(exception.getMessage());
112102

113-
verify(underTest).convert("value", int.class, getClassLoader(DefaultArgumentConverterTests.class));
103+
verify(underTest).delegateConversion("value", int.class, getClassLoader(DefaultArgumentConverterTests.class));
114104
}
115105

116106
@Test
@@ -123,14 +113,14 @@ void delegatesStringToClassWithCustomTypeFromDifferentClassLoaderConversion() th
123113
var declaringExecutable = ReflectionSupport.findMethod(customType, "foo").orElseThrow();
124114
assertThat(declaringExecutable.getDeclaringClass().getClassLoader()).isSameAs(testClassLoader);
125115

126-
doReturn(customType).when(underTest).convert(any(), any(), any(ClassLoader.class));
116+
doReturn(customType).when(underTest).delegateConversion(any(), any(), any(ClassLoader.class));
127117

128118
var clazz = (Class<?>) convert(customTypeName, Class.class, testClassLoader);
129119
assertThat(clazz).isNotEqualTo(Enigma.class);
130120
assertThat(clazz).isEqualTo(customType);
131121
assertThat(clazz.getClassLoader()).isSameAs(testClassLoader);
132122

133-
verify(underTest).convert(customTypeName, Class.class, testClassLoader);
123+
verify(underTest).delegateConversion(customTypeName, Class.class, testClassLoader);
134124
}
135125
}
136126

@@ -143,7 +133,7 @@ private void assertConverts(Object input, Class<?> targetClass, Object expectedO
143133
.describedAs(input + " --(" + targetClass.getName() + ")--> " + expectedOutput) //
144134
.isEqualTo(expectedOutput);
145135

146-
verify(underTest, never()).convert(any(), any(), any(ClassLoader.class));
136+
verify(underTest, never()).delegateConversion(any(), any(), any(ClassLoader.class));
147137
}
148138

149139
private Object convert(Object input, Class<?> targetClass) {

platform-tests/src/test/java/org/junit/platform/commons/support/conversion/ConversionSupportTests.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ void convertsStringsToPrimitiveWrapperTypes() {
107107
void throwsExceptionForNullToPrimitiveTypeConversion(Class<?> type) {
108108
assertThatExceptionOfType(ConversionException.class) //
109109
.isThrownBy(() -> convert(null, type)) //
110-
.withMessage("Cannot convert null to primitive value of type " + type.getCanonicalName());
110+
.withMessage("No registered or built-in converter for source 'null' and target type %s",
111+
type.getCanonicalName());
111112
}
112113

113114
@ParameterizedTest(name = "[{index}] {0}")
@@ -153,7 +154,7 @@ void throwsExceptionOnInvalidStringForPrimitiveTypes() {
153154
void throwsExceptionWhenImplicitConversionIsUnsupported() {
154155
assertThatExceptionOfType(ConversionException.class) //
155156
.isThrownBy(() -> convert("foo", Enigma.class)) //
156-
.withMessage("No built-in converter for source type java.lang.String and target type %s",
157+
.withMessage("No registered or built-in converter for source 'foo' and target type %s",
157158
Enigma.class.getName());
158159
}
159160

@@ -309,19 +310,19 @@ void convertsStringToUUID() {
309310

310311
// -------------------------------------------------------------------------
311312

312-
private void assertConverts(String input, Class<?> targetClass, Object expectedOutput) {
313+
private void assertConverts(Object input, Class<?> targetClass, Object expectedOutput) {
313314
var result = convert(input, targetClass);
314315

315316
assertThat(result) //
316317
.describedAs(input + " --(" + targetClass.getName() + ")--> " + expectedOutput) //
317318
.isEqualTo(expectedOutput);
318319
}
319320

320-
private Object convert(String input, Class<?> targetClass) {
321+
private Object convert(Object input, Class<?> targetClass) {
321322
return convert(input, targetClass, classLoader());
322323
}
323324

324-
private Object convert(String input, Class<?> targetClass, ClassLoader classLoader) {
325+
private Object convert(Object input, Class<?> targetClass, ClassLoader classLoader) {
325326
return ConversionSupport.convert(input, targetClass, classLoader);
326327
}
327328

0 commit comments

Comments
 (0)