Skip to content

Commit

Permalink
Register runtime hint for @WebAppConfiguration's resource path
Browse files Browse the repository at this point in the history
This commit introduces automatic registration of a runtime hint for
classpath resources configured via the `value` attribute in
@WebAppConfiguration.

Closes gh-29026
  • Loading branch information
sbrannen committed Sep 4, 2022
1 parent f6db2cc commit dc7c7ac
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand All @@ -60,6 +61,8 @@
*/
public class TestContextAotGenerator {

private static final String SLASH = "/";

private static final Log logger = LogFactory.getLog(TestContextAotGenerator.class);

private final ApplicationContextAotGenerator aotGenerator = new ApplicationContextAotGenerator();
Expand Down Expand Up @@ -251,15 +254,30 @@ private void registerHintsForMergedConfig(MergedContextConfiguration mergedConfi

// @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) {
Arrays.stream(locations)
.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;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,13 @@ private static void assertRuntimeHints(RuntimeHints runtimeHints) {
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/xml/test-config.xml"))
.accepts(runtimeHints);

// @TestPropertySource(locations = ... )
// @TestPropertySource(locations = ...)
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
.accepts(runtimeHints);

// @WebAppConfiguration(value = ...)
assertThat(resource().forResource("/META-INF/web-resources/resources/Spring.js")).accepts(runtimeHints);
assertThat(resource().forResource("/META-INF/web-resources/WEB-INF/views/home.jsp")).accepts(runtimeHints);
}

private static void assertReflectionRegistered(RuntimeHints runtimeHints, String type, MemberCategory memberCategory) {
Expand Down Expand Up @@ -328,9 +332,9 @@ record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext005_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext005_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext005_BeanDefinitions.java",
"org/springframework/web/reactive/config/DelegatingWebFluxConfiguration__TestContext005_Autowiring.java",
"org/springframework/web/reactive/config/DelegatingWebFluxConfiguration__TestContext005_BeanDefinitions.java",
"org/springframework/web/reactive/config/WebFluxConfigurationSupport__TestContext005_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext005_Autowiring.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext005_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport__TestContext005_BeanDefinitions.java",
// XmlSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext006_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext006_BeanDefinitions.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -33,7 +34,7 @@
* @author Sam Brannen
* @since 6.0
*/
@SpringJUnitWebConfig(WebTestConfiguration.class)
@SpringJUnitWebConfig(classes = WebTestConfiguration.class, resourcePath = "classpath:META-INF/web-resources")
@TestPropertySource(properties = "test.engine = jupiter")
public class WebSpringJupiterTests {

Expand All @@ -49,7 +50,7 @@ void setUpMockMvc() {
}

@org.junit.jupiter.api.Test
void test(@Value("${test.engine}") String testEngine) throws Exception {
void controller(@Value("${test.engine}") String testEngine) throws Exception {
assertThat(testEngine)
.as("@Value").isEqualTo("jupiter");
assertThat(wac.getEnvironment().getProperty("test.engine"))
Expand All @@ -59,4 +60,13 @@ void test(@Value("${test.engine}") String testEngine) throws Exception {
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
}

@org.junit.jupiter.api.Test
void resources() throws Exception {
this.mockMvc.perform(get("/resources/Spring.js"))
.andExpectAll(
content().contentType("application/javascript"),
content().string(containsString("Spring={};"))
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* @author Sam Brannen
* @since 6.0
*/
@Configuration(proxyBeanMethods = false)
@EnableWebFlux
class WebTestConfiguration {
@EnableWebMvc
class WebTestConfiguration implements WebMvcConfigurer {

@Bean
MessageController messageController() {
return new MessageController();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

}
1 change: 1 addition & 0 deletions src/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<suppress files="src[\\/]main[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]web[\\/]servlet[\\/]result[\\/].+Matchers" checks="IllegalImport" id="bannedHamcrestImports"/>
<!-- spring-test - test -->
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/].+TestNGTests" checks="IllegalImport" id="bannedTestNGImports"/>
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]context[\\/]aot[\\/]samples[\\/]web[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]context[\\/]junit[\\/]jupiter[\\/]web[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]util[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]web[\\/](client|reactive|servlet)[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
Expand Down

0 comments on commit dc7c7ac

Please sign in to comment.