Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/MethodBasedCondition.java

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.jupiter.api.condition;
12
13 import static java.lang.String.format;
14 import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled;
15 import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;
16 import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
17
18 import java.lang.annotation.Annotation;
19 import java.lang.reflect.Method;
20 import java.util.Optional;
21 import java.util.function.Function;
22 import java.util.function.Supplier;
23
24 import org.junit.jupiter.api.extension.ConditionEvaluationResult;
25 import org.junit.jupiter.api.extension.ExecutionCondition;
26 import org.junit.jupiter.api.extension.ExtensionContext;
27 import org.junit.platform.commons.JUnitException;
28 import org.junit.platform.commons.util.ClassLoaderUtils;
29 import org.junit.platform.commons.util.Preconditions;
30 import org.junit.platform.commons.util.ReflectionUtils;
31 import org.junit.platform.commons.util.StringUtils;
32
33 /**
34 * @since 5.7
35 */
36 abstract class MethodBasedCondition<A extends Annotation> implements ExecutionCondition {
37
38 private final Class<A> annotationType;
39 private final Function<A, String> methodName;
40 private final Function<A, String> customDisabledReason;
41
42 MethodBasedCondition(Class<A> annotationType, Function<A, String> methodName,
43 Function<A, String> customDisabledReason) {
44 this.annotationType = annotationType;
45 this.methodName = methodName;
46 this.customDisabledReason = customDisabledReason;
47 }
48
49 @Override
50 public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
51 Optional<A> annotation = findAnnotation(context.getElement(), this.annotationType);
52 return annotation //
53 .map(this.methodName) //
54 .map(methodName -> getConditionMethod(methodName, context)) //
55 .map(method -> invokeConditionMethod(method, context)) //
56 .map(methodResult -> buildConditionEvaluationResult(methodResult, annotation.get())) //
57 .orElseGet(this::enabledByDefault);
58 }
59
60 // package-private for testing
61 Method getConditionMethod(String fullyQualifiedMethodName, ExtensionContext context) {
62 Class<?> testClass = context.getRequiredTestClass();
63 if (!fullyQualifiedMethodName.contains("#")) {
64 return findMethod(testClass, fullyQualifiedMethodName);
65 }
66 String[] methodParts = ReflectionUtils.parseFullyQualifiedMethodName(fullyQualifiedMethodName);
67 String className = methodParts[0];
68 String methodName = methodParts[1];
69 ClassLoader classLoader = ClassLoaderUtils.getClassLoader(testClass);
70 Class<?> clazz = ReflectionUtils.tryToLoadClass(className, classLoader).getOrThrow(
71 cause -> new JUnitException(format("Could not load class [%s]", className), cause));
72 return findMethod(clazz, methodName);
73 }
74
75 private Method findMethod(Class<?> clazz, String methodName) {
76 return ReflectionUtils.findMethod(clazz, methodName) //
77 .orElseGet(() -> ReflectionUtils.getRequiredMethod(clazz, methodName, ExtensionContext.class));
78 }
79
80 private boolean invokeConditionMethod(Method method, ExtensionContext context) {
81 Preconditions.condition(method.getReturnType() == boolean.class,
82 () -> format("Method [%s] must return a boolean", method));
83 Preconditions.condition(acceptsExtensionContextOrNoArguments(method),
84 () -> format("Method [%s] must accept either an ExtensionContext or no arguments", method));
85
86 Object testInstance = context.getTestInstance().orElse(null);
87 if (method.getParameterCount() == 0) {
88 return (boolean) ReflectionUtils.invokeMethod(method, testInstance);
89 }
90 return (boolean) ReflectionUtils.invokeMethod(method, testInstance, context);
91 }
92
93 private boolean acceptsExtensionContextOrNoArguments(Method method) {
94 int parameterCount = method.getParameterCount();
95 return parameterCount == 0 || (parameterCount == 1 && method.getParameterTypes()[0] == ExtensionContext.class);
96 }
97
98 private ConditionEvaluationResult buildConditionEvaluationResult(boolean methodResult, A annotation) {
99 Supplier<String> defaultReason = () -> format("@%s(\"%s\") evaluated to %s",
100 this.annotationType.getSimpleName(), this.methodName.apply(annotation), methodResult);
101 if (isEnabled(methodResult)) {
102 return enabled(defaultReason.get());
103 }
104 String customReason = this.customDisabledReason.apply(annotation);
105 return StringUtils.isNotBlank(customReason) ? disabled(customReason) : disabled(defaultReason.get());
106 }
107
108 protected abstract boolean isEnabled(boolean methodResult);
109
110 private ConditionEvaluationResult enabledByDefault() {
111 return enabled(format("@%s is not present", this.annotationType.getSimpleName()));
112 }
113
114 }