From c92a0bd493ee24d4add3de2d50e61e83937ebb03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Tue, 28 Nov 2023 15:46:31 +0100 Subject: [PATCH] Prevent AotDetector to be initialized at build-time This commit moves the condition used by `@DisabledInAotMode` to a concrete implementation rather than using `@DisabledIf` as it causes build initialization in a native image. Closes gh-31705 --- .../test/context/aot/DisabledInAotMode.java | 6 +-- .../aot/DisabledInAotModeCondition.java | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotModeCondition.java diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotMode.java b/spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotMode.java index ea94e60e6d..2a322d2e45 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotMode.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotMode.java @@ -22,7 +22,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.junit.jupiter.api.condition.DisabledIf; +import org.junit.jupiter.api.extension.ExtendWith; /** * {@code @DisabledInAotMode} signals that an annotated test class is disabled @@ -48,6 +48,7 @@ import org.junit.jupiter.api.condition.DisabledIf; * annotation. * * @author Sam Brannen + * @author Stephane Nicoll * @since 6.1 * @see org.springframework.aot.AotDetector#useGeneratedArtifacts() AotDetector.useGeneratedArtifacts() * @see org.junit.jupiter.api.condition.EnabledInNativeImage @EnabledInNativeImage @@ -56,7 +57,6 @@ import org.junit.jupiter.api.condition.DisabledIf; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented -@DisabledIf(value = "org.springframework.aot.AotDetector#useGeneratedArtifacts", - disabledReason = "Disabled in Spring AOT mode") +@ExtendWith(DisabledInAotModeCondition.class) public @interface DisabledInAotMode { } diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotModeCondition.java b/spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotModeCondition.java new file mode 100644 index 0000000000..e1e2d0d48d --- /dev/null +++ b/spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotModeCondition.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.aot; + +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; + +/** + * {@link ExecutionCondition} implementation for {@link DisabledInAotMode}. + * + * @author Stephane Nicoll + * @since 6.1.2 + */ +class DisabledInAotModeCondition implements ExecutionCondition { + + @Override + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { + boolean aotEnabled = AotDetector.useGeneratedArtifacts(); + if (aotEnabled) { + return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode"); + } + return ConditionEvaluationResult.enabled("Spring AOT mode disabled"); + } + +}