|
| 1 | +/* |
| 2 | + * Copyright © 2025-present Stefano Cordio |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.scordio.junit.converters; |
| 17 | + |
| 18 | +import org.jspecify.annotations.Nullable; |
| 19 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 20 | +import org.junit.jupiter.params.converter.ArgumentConversionException; |
| 21 | +import org.junit.jupiter.params.converter.ArgumentConverter; |
| 22 | +import org.junit.jupiter.params.support.AnnotationConsumer; |
| 23 | +import org.junit.jupiter.params.support.FieldContext; |
| 24 | +import org.junit.platform.commons.support.HierarchyTraversalMode; |
| 25 | +import org.junit.platform.commons.support.ModifierSupport; |
| 26 | +import org.junit.platform.commons.support.ReflectionSupport; |
| 27 | + |
| 28 | +import java.lang.reflect.Method; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.function.Predicate; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +class MethodArgumentConverter implements ArgumentConverter, AnnotationConsumer<MethodConversion> { |
| 35 | + |
| 36 | + private static final Predicate<Method> IS_STATIC = ModifierSupport::isStatic; |
| 37 | + |
| 38 | + private @Nullable MethodConversion annotation; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void accept(MethodConversion annotation) { |
| 42 | + this.annotation = annotation; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public @Nullable Object convert(@Nullable Object source, ParameterContext context) { |
| 47 | + return convert(source, context.getParameter().getType(), context.getDeclaringExecutable().getDeclaringClass()); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public @Nullable Object convert(@Nullable Object source, FieldContext context) { |
| 52 | + return convert(source, context.getField().getType(), context.getField().getDeclaringClass()); |
| 53 | + } |
| 54 | + |
| 55 | + private @Nullable Object convert(@Nullable Object source, Class<?> targetType, Class<?> declaringClass) { |
| 56 | + Objects.requireNonNull(source, "'null' is not supported"); |
| 57 | + Objects.requireNonNull(annotation, "'annotation' must not be null"); |
| 58 | + Method conversionMethod = annotation.value().isEmpty() |
| 59 | + ? findCandidateMethod(source.getClass(), targetType, declaringClass) |
| 60 | + : findMethodByName(annotation.value(), source.getClass(), targetType, declaringClass); |
| 61 | + return ReflectionSupport.invokeMethod(conversionMethod, null, source); |
| 62 | + } |
| 63 | + |
| 64 | + private static Method findCandidateMethod(Class<?> sourceType, Class<?> targetType, Class<?> declaringClass) { |
| 65 | + Predicate<Method> filter = IS_STATIC.and(accepts(sourceType)).and(produces(targetType)); |
| 66 | + List<Method> methods = ReflectionSupport.findMethods(declaringClass, filter, HierarchyTraversalMode.BOTTOM_UP); |
| 67 | + |
| 68 | + if (methods.isEmpty()) { |
| 69 | + throw new ArgumentConversionException( |
| 70 | + String.format("No conversion method found compatible with source type %s and target type %s", |
| 71 | + sourceType.getName(), targetType.getName())); |
| 72 | + } |
| 73 | + |
| 74 | + if (methods.size() > 1) { |
| 75 | + List<String> signatures = methods.stream() |
| 76 | + .map(method -> String.format("%s %s(%s)", method.getReturnType().getName(), method.getName(), |
| 77 | + method.getParameterTypes()[0].getName())) |
| 78 | + .collect(Collectors.toList()); |
| 79 | + |
| 80 | + throw new ArgumentConversionException( |
| 81 | + String.format("Too many conversion methods compatible with source type %s and target type %s: %s", |
| 82 | + sourceType.getName(), targetType.getName(), signatures)); |
| 83 | + } |
| 84 | + |
| 85 | + return methods.get(0); |
| 86 | + } |
| 87 | + |
| 88 | + private static Method findMethodByName(String name, Class<?> sourceType, Class<?> targetType, |
| 89 | + Class<?> declaringClass) { |
| 90 | + Predicate<Method> filter = IS_STATIC.and(hasName(name)).and(accepts(sourceType)).and(produces(targetType)); |
| 91 | + List<Method> methods = ReflectionSupport.findMethods(declaringClass, filter, HierarchyTraversalMode.BOTTOM_UP); |
| 92 | + |
| 93 | + if (methods.isEmpty()) { |
| 94 | + throw new ArgumentConversionException( |
| 95 | + String.format("No conversion method found with the following signature: static %s %s(%s)", |
| 96 | + targetType.getName(), name, sourceType.getName())); |
| 97 | + } |
| 98 | + |
| 99 | + return methods.get(0); |
| 100 | + } |
| 101 | + |
| 102 | + private static Predicate<Method> accepts(Class<?> sourceType) { |
| 103 | + return method -> { |
| 104 | + Class<?>[] parameterTypes = method.getParameterTypes(); |
| 105 | + return parameterTypes.length == 1 && ReflectionUtils.isAssignableTo(sourceType, parameterTypes[0]); |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + private static Predicate<Method> produces(Class<?> targetType) { |
| 110 | + return method -> targetType.isAssignableFrom(method.getReturnType()); |
| 111 | + } |
| 112 | + |
| 113 | + private static Predicate<Method> hasName(String name) { |
| 114 | + return method -> method.getName().equals(name); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments