Browse Source
Prior to this commit, test AOT processing failed when using the GraalVM tracing agent and GraalVM Native Build Tools (NBT) plugins for Maven and Gradle. The reason is that the AOT support in the TestContext framework (TCF) relied on AotDetector.useGeneratedArtifacts() which delegates internally to NativeDetector.inNativeImage() which does not differentiate between values stored in the "org.graalvm.nativeimage.imagecode" JVM system property. This commit addresses this issue by introducing a TestAotDetector utility that is specific to the TCF. This detector considers the current runtime to be in "AOT runtime mode" if the "spring.aot.enabled" Spring property is set to "true" or the GraalVM "org.graalvm.nativeimage.imagecode" JVM system property is set to any non-empty value other than "agent". Closes gh-30281pull/30619/head
11 changed files with 165 additions and 37 deletions
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
/* |
||||
* 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.springframework.aot.AotDetector; |
||||
import org.springframework.core.SpringProperties; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* TestContext framework specific utility for determining if AOT-processed |
||||
* optimizations must be used rather than the regular runtime. |
||||
* |
||||
* <p>Strictly for internal use within the framework. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0.9 |
||||
*/ |
||||
public abstract class TestAotDetector { |
||||
|
||||
/** |
||||
* Determine whether AOT optimizations must be considered at runtime. |
||||
* <p>This can be triggered using the {@value AotDetector#AOT_ENABLED} |
||||
* Spring property or via GraalVM's {@code "org.graalvm.nativeimage.imagecode"} |
||||
* JVM system property (if set to any non-empty value other than {@code agent}). |
||||
* @return {@code true} if AOT optimizations must be considered |
||||
* @see <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">GraalVM's ImageInfo.java</a> |
||||
* @see AotDetector#useGeneratedArtifacts() |
||||
*/ |
||||
public static boolean useGeneratedArtifacts() { |
||||
return (SpringProperties.getFlag(AotDetector.AOT_ENABLED) || inNativeImage()); |
||||
} |
||||
|
||||
/** |
||||
* Determine if we are currently running within a GraalVM native image from |
||||
* the perspective of the TestContext framework. |
||||
* @return {@code true} if the {@code org.graalvm.nativeimage.imagecode} JVM |
||||
* system property has been set to any value other than {@code agent}. |
||||
*/ |
||||
private static boolean inNativeImage() { |
||||
String imageCode = System.getProperty("org.graalvm.nativeimage.imagecode"); |
||||
return (StringUtils.hasText(imageCode) && !"agent".equalsIgnoreCase(imageCode.trim())); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* 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 java.util.stream.Stream; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.params.ParameterizedTest; |
||||
import org.junit.jupiter.params.provider.CsvSource; |
||||
|
||||
import org.springframework.aot.generate.InMemoryGeneratedFiles; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
||||
import static org.assertj.core.api.Assertions.assertThatNoException; |
||||
|
||||
/** |
||||
* Tests for error cases in {@link TestContextAotGenerator}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.0.9 |
||||
*/ |
||||
class TestContextAotGeneratorErrorCaseTests { |
||||
|
||||
@ParameterizedTest |
||||
@CsvSource(delimiter = '=', textBlock = """ |
||||
'spring.aot.enabled' = 'true' |
||||
'org.graalvm.nativeimage.imagecode' = 'buildtime' |
||||
'org.graalvm.nativeimage.imagecode' = 'runtime' |
||||
'org.graalvm.nativeimage.imagecode' = 'bogus' |
||||
""") |
||||
void attemptToProcessWhileRunningInAotMode(String property, String value) { |
||||
try { |
||||
System.setProperty(property, value); |
||||
|
||||
assertThatIllegalStateException() |
||||
.isThrownBy(() -> generator().processAheadOfTime(Stream.empty())) |
||||
.withMessage("Cannot perform AOT processing during AOT run-time execution"); |
||||
} |
||||
finally { |
||||
System.clearProperty(property); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
void attemptToProcessWhileRunningInGraalVmNativeBuildToolsAgentMode() { |
||||
final String IMAGECODE = "org.graalvm.nativeimage.imagecode"; |
||||
try { |
||||
System.setProperty(IMAGECODE, "AgenT"); |
||||
|
||||
assertThatNoException().isThrownBy(() -> generator().processAheadOfTime(Stream.empty())); |
||||
} |
||||
finally { |
||||
System.clearProperty(IMAGECODE); |
||||
} |
||||
} |
||||
|
||||
private static TestContextAotGenerator generator() { |
||||
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); |
||||
return new TestContextAotGenerator(generatedFiles); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue