Register runtime hints for @ContextConfiguration#locations

This commit introduces automatic registration of runtime hints for
classpath resources configured via the `locations` attribute in
@ContextConfiguration.

Closes gh-29021
This commit is contained in:
Sam Brannen
2022-09-03 17:25:46 +02:00
parent c0040b9024
commit 920fbb34c8
2 changed files with 26 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.test.context.aot;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
@@ -47,6 +48,8 @@ import org.springframework.util.MultiValueMap;
import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_CONSTRUCTORS;
import static org.springframework.aot.hint.MemberCategory.INVOKE_PUBLIC_METHODS;
import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;
/**
* {@code TestContextAotGenerator} generates AOT artifacts for integration tests
* that depend on support from the <em>Spring TestContext Framework</em>.
@@ -242,6 +245,16 @@ public class TestContextAotGenerator {
// @ContextConfiguration(initializers = ...)
mergedConfig.getContextInitializerClasses().forEach(this::registerDeclaredConstructors);
// @ContextConfiguration(locations = ...)
registerHintsForClasspathResources(mergedConfig.getLocations());
}
private void registerHintsForClasspathResources(String... locations) {
Arrays.stream(locations)
.filter(location -> location.startsWith(CLASSPATH_URL_PREFIX))
.map(location -> location.substring(CLASSPATH_URL_PREFIX.length()))
.forEach(this.runtimeHints.resources()::registerPattern);
}
private void registerDeclaredConstructors(Class<?> type) {