Register runtime hint for @WebAppConfiguration's resource path

This commit introduces automatic registration of a runtime hint for
classpath resources configured via the `value` attribute in
@WebAppConfiguration.

Closes gh-29026
This commit is contained in:
Sam Brannen
2022-09-04 16:45:02 +02:00
parent f6db2cc35f
commit dc7c7ac22a
5 changed files with 51 additions and 11 deletions

View File

@@ -42,6 +42,7 @@ import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.SmartContextLoader;
import org.springframework.test.context.TestContextBootstrapper;
import org.springframework.test.context.web.WebMergedContextConfiguration;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -60,6 +61,8 @@ import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;
*/
public class TestContextAotGenerator {
private static final String SLASH = "/";
private static final Log logger = LogFactory.getLog(TestContextAotGenerator.class);
private final ApplicationContextAotGenerator aotGenerator = new ApplicationContextAotGenerator();
@@ -251,6 +254,21 @@ public class TestContextAotGenerator {
// @TestPropertySource(locations = ... )
registerHintsForClasspathResources(mergedConfig.getPropertySourceLocations());
if (mergedConfig instanceof WebMergedContextConfiguration webMergedConfig) {
String resourceBasePath = webMergedConfig.getResourceBasePath();
if (resourceBasePath.startsWith(CLASSPATH_URL_PREFIX)) {
String pattern = resourceBasePath.substring(CLASSPATH_URL_PREFIX.length());
if (!pattern.startsWith(SLASH)) {
pattern = SLASH + pattern;
}
if (!pattern.endsWith(SLASH)) {
pattern += SLASH;
}
pattern += "*";
this.runtimeHints.resources().registerPattern(pattern);
}
}
}
private void registerHintsForClasspathResources(String... locations) {
@@ -258,8 +276,8 @@ public class TestContextAotGenerator {
.filter(location -> location.startsWith(CLASSPATH_URL_PREFIX))
.map(location -> {
location = location.substring(CLASSPATH_URL_PREFIX.length());
if (!location.startsWith("/")) {
location = "/" + location;
if (!location.startsWith(SLASH)) {
location = SLASH + location;
}
return location;
})