Skip to content

Commit 0bc5c2b

Browse files
committed
Ensure that containers' static resource handling not MVC's is used
Closes gh-25949
1 parent 709db55 commit 0bc5c2b

File tree

8 files changed

+47
-30
lines changed

8 files changed

+47
-30
lines changed

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ configurations {
1111

1212
dependencies {
1313
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
14-
testImplementation(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
1514
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
1615
testImplementation("com.samskivert:jmustache")
1716
testImplementation("jakarta.servlet:jakarta.servlet-api")
@@ -26,7 +25,6 @@ dependencies {
2625
testRepository(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository"))
2726
testRepository(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-maven-plugin", configuration: "mavenRepository"))
2827
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter", configuration: "mavenRepository"))
29-
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator", configuration: "mavenRepository"))
3028
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-jetty", configuration: "mavenRepository"))
3129
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-json", configuration: "mavenRepository"))
3230
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-parent", configuration: "mavenRepository"))

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/test/java/com/autoconfig/ExampleAutoConfiguration.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,27 +16,44 @@
1616

1717
package com.autoconfig;
1818

19-
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
20-
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
19+
import java.io.IOException;
20+
21+
import javax.servlet.ServletException;
22+
import javax.servlet.http.HttpServlet;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
2126
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
27+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
2228
import org.springframework.context.annotation.Bean;
2329
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.http.MediaType;
2431

25-
@ConditionalOnWarDeployment
2632
@Configuration
2733
public class ExampleAutoConfiguration {
2834

2935
@Bean
30-
public TestEndpoint testEndpoint() {
31-
return new TestEndpoint();
36+
@ConditionalOnWarDeployment
37+
public ServletRegistrationBean<TestServlet> onWarTestServlet() {
38+
ServletRegistrationBean<TestServlet> registration = new ServletRegistrationBean<>(new TestServlet());
39+
registration.addUrlMappings("/conditionalOnWar");
40+
return registration;
41+
}
42+
43+
@Bean
44+
public ServletRegistrationBean<TestServlet> testServlet() {
45+
ServletRegistrationBean<TestServlet> registration = new ServletRegistrationBean<>(new TestServlet());
46+
registration.addUrlMappings("/always");
47+
return registration;
3248
}
3349

34-
@Endpoint(id = "war")
35-
static class TestEndpoint {
50+
static class TestServlet extends HttpServlet {
3651

37-
@ReadOperation
38-
String hello() {
39-
return "{\"hello\":\"world\"}";
52+
@Override
53+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
54+
resp.setContentType(MediaType.APPLICATION_JSON_VALUE);
55+
resp.getWriter().println("{\"hello\":\"world\"}");
56+
resp.flushBuffer();
4057
}
4158

4259
}

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/test/java/com/example/ResourceHandlingApplication.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,16 @@ public ServletRegistrationBean<?> resourcePathsServletRegistration() {
5656
}
5757

5858
public static void main(String[] args) {
59-
new SpringApplicationBuilder(ResourceHandlingApplication.class).properties("server.port:0")
60-
.listeners(new WebServerPortFileWriter(args[0])).run(args);
59+
try {
60+
Class.forName("org.springframework.web.servlet.DispatcherServlet");
61+
System.err.println("Spring MVC must not be present, otherwise its static resource handling "
62+
+ "will be used rather than the embedded containers'");
63+
System.exit(1);
64+
}
65+
catch (Throwable ex) {
66+
new SpringApplicationBuilder(ResourceHandlingApplication.class).properties("server.port:0")
67+
.listeners(new WebServerPortFileWriter(args[0])).run(args);
68+
}
6169
}
6270

6371
private static final class GetResourcePathsServlet extends HttpServlet {

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/test/java/org/springframework/boot/context/embedded/ApplicationBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -161,8 +161,6 @@ private void copyAutoConfigurationFiles(File appDirectory) throws IOException {
161161
metaInf.mkdirs();
162162
FileCopyUtils.copy(new File("src/test/resources/META-INF/spring.factories"),
163163
new File(metaInf, "spring.factories"));
164-
FileCopyUtils.copy(new File("src/test/resources/application.yml"),
165-
new File(srcMainResources, "application.yml"));
166164
}
167165

168166
private void packageApplication(File appDirectory, File settingsXml) throws MavenInvocationException {

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/test/java/org/springframework/boot/context/embedded/EmbeddedServletContainerJarPackagingIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,7 +81,7 @@ void launcherIsNotAvailableViaHttp(RestTemplate rest) {
8181

8282
@TestTemplate
8383
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
84-
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
84+
ResponseEntity<String> entity = rest.getForEntity("/war", String.class);
8585
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
8686
}
8787

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/test/java/org/springframework/boot/context/embedded/EmbeddedServletContainerWarPackagingIntegrationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,8 +104,9 @@ void loaderClassesAreNotAvailableViaResourcePaths(RestTemplate rest) {
104104

105105
@TestTemplate
106106
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
107-
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
108-
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
107+
assertThat(rest.getForEntity("/always", String.class).getStatusCode()).isEqualTo(HttpStatus.OK);
108+
assertThat(rest.getForEntity("/conditionalOnWar", String.class).getStatusCode())
109+
.isEqualTo(HttpStatus.NOT_FOUND);
109110
}
110111

111112
private List<String> readLines(String input) {

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/test/resources/application.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-boot-tests/spring-boot-integration-tests/spring-boot-server-tests/src/test/resources/pom-template.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@
1818
<dependencies>
1919
<dependency>
2020
<groupId>org.springframework.boot</groupId>
21-
<artifactId>spring-boot-starter-json</artifactId>
22-
</dependency>
23-
<dependency>
24-
<groupId>org.springframework.boot</groupId>
25-
<artifactId>spring-boot-starter-actuator</artifactId>
21+
<artifactId>spring-boot-starter</artifactId>
2622
</dependency>
2723
<dependency>
2824
<groupId>org.springframework.boot</groupId>
2925
<artifactId>spring-boot-starter-{{container}}</artifactId>
3026
</dependency>
3127
<dependency>
3228
<groupId>org.springframework</groupId>
33-
<artifactId>spring-webmvc</artifactId>
29+
<artifactId>spring-web</artifactId>
3430
</dependency>
3531
<dependency>
3632
<groupId>javax.servlet</groupId>

0 commit comments

Comments
 (0)