Introduce support for a custom reason in @⁠DisabledInAotMode

Closes gh-33833
This commit is contained in:
Sam Brannen
2024-11-01 12:24:53 +01:00
parent fdb763eeaf
commit 86784b61cc
51 changed files with 198 additions and 105 deletions

View File

@@ -59,4 +59,19 @@ import org.junit.jupiter.api.extension.ExtendWith;
@Documented
@ExtendWith(DisabledInAotModeCondition.class)
public @interface DisabledInAotMode {
/**
* Custom reason to document why the test class or test method is disabled in
* AOT mode.
* <p>If a custom reason is not supplied, the default reason will be used:
* {@code "Disabled in Spring AOT mode"}.
* <p>If a custom reason is supplied, it will be combined with the default
* reason. For example,
* {@code @DisabledInAotMode("@ContextHierarchy is not supported")} will result
* in a combined reason like the following:
* {@code "Disabled in Spring AOT mode ==> @ContextHierarchy is not supported"}.
* @since 6.2
*/
String value() default "";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,16 +16,22 @@
package org.springframework.test.context.aot;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Optional;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.aot.AotDetector;
import org.springframework.core.annotation.AnnotatedElementUtils;
/**
* {@link ExecutionCondition} implementation for {@link DisabledInAotMode}.
* {@link ExecutionCondition} implementation for {@link DisabledInAotMode @DisabledInAotMode}.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 6.1.2
*/
class DisabledInAotModeCondition implements ExecutionCondition {
@@ -34,9 +40,18 @@ class DisabledInAotModeCondition implements ExecutionCondition {
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
boolean aotEnabled = AotDetector.useGeneratedArtifacts();
if (aotEnabled) {
return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode");
AnnotatedElement element = context.getElement().orElseThrow(() -> new IllegalStateException("No AnnotatedElement"));
String customReason = findMergedAnnotation(element, DisabledInAotMode.class)
.map(DisabledInAotMode::value).orElse(null);
return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode", customReason);
}
return ConditionEvaluationResult.enabled("Spring AOT mode disabled");
return ConditionEvaluationResult.enabled("Spring AOT mode is not enabled");
}
private static <A extends Annotation> Optional<A> findMergedAnnotation(
AnnotatedElement element, Class<A> annotationType) {
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
}
}