Skip to content

Commit 18db705

Browse files
committed
Merge branch '3.3.x' into 3.4.x
Closes gh-44215
2 parents 30d7af4 + 3e5929a commit 18db705

File tree

2 files changed

+23
-147
lines changed

2 files changed

+23
-147
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfigurationTests.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -65,10 +65,18 @@ void registrationNonServletBean() {
6565
});
6666
}
6767

68-
// If a DispatcherServlet instance is registered with a name different
69-
// from the default one, we're registering one anyway
7068
@Test
71-
void registrationOverrideWithDispatcherServletWrongName() {
69+
void registrationOverrideWithDefaultDispatcherServletNameResultsInSingleDispatcherServlet() {
70+
this.contextRunner.withUserConfiguration(CustomDispatcherServletSameName.class).run((context) -> {
71+
ServletRegistrationBean<?> registration = context.getBean(ServletRegistrationBean.class);
72+
assertThat(registration.getUrlMappings()).containsExactly("/");
73+
assertThat(registration.getServletName()).isEqualTo("dispatcherServlet");
74+
assertThat(context).getBeanNames(DispatcherServlet.class).hasSize(1);
75+
});
76+
}
77+
78+
@Test
79+
void registrationOverrideWithNonDefaultDispatcherServletNameResultsInTwoDispatcherServlets() {
7280
this.contextRunner
7381
.withUserConfiguration(CustomDispatcherServletDifferentName.class, CustomDispatcherServletPath.class)
7482
.run((context) -> {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java

+11-143
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -19,10 +19,7 @@
1919
import io.undertow.Undertow.Builder;
2020
import io.undertow.servlet.api.DeploymentInfo;
2121
import jakarta.servlet.Filter;
22-
import jakarta.servlet.Servlet;
2322
import jakarta.servlet.ServletContext;
24-
import jakarta.servlet.http.HttpServletRequest;
25-
import jakarta.servlet.http.HttpServletResponse;
2623
import org.apache.catalina.Context;
2724
import org.apache.catalina.connector.Connector;
2825
import org.apache.catalina.startup.Tomcat;
@@ -34,10 +31,7 @@
3431
import org.springframework.boot.autoconfigure.AutoConfigurations;
3532
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
3633
import org.springframework.boot.test.context.FilteredClassLoader;
37-
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
38-
import org.springframework.boot.test.context.runner.ContextConsumer;
3934
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
40-
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
4135
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
4236
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
4337
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
@@ -49,19 +43,15 @@
4943
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
5044
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
5145
import org.springframework.boot.web.servlet.FilterRegistrationBean;
52-
import org.springframework.boot.web.servlet.ServletRegistrationBean;
5346
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
5447
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
5548
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
5649
import org.springframework.boot.web.servlet.server.CookieSameSiteSupplier;
5750
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
58-
import org.springframework.context.ApplicationContext;
5951
import org.springframework.context.annotation.Bean;
6052
import org.springframework.context.annotation.Configuration;
6153
import org.springframework.stereotype.Component;
6254
import org.springframework.web.filter.ForwardedHeaderFilter;
63-
import org.springframework.web.servlet.DispatcherServlet;
64-
import org.springframework.web.servlet.FrameworkServlet;
6555

6656
import static org.assertj.core.api.Assertions.assertThat;
6757
import static org.mockito.ArgumentMatchers.any;
@@ -77,69 +67,31 @@
7767
* @author Raheela Aslam
7868
* @author Madhura Bhave
7969
*/
80-
@DirtiesUrlFactories
8170
class ServletWebServerFactoryAutoConfigurationTests {
8271

8372
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
8473
AnnotationConfigServletWebServerApplicationContext::new)
85-
.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class,
86-
DispatcherServletAutoConfiguration.class))
74+
.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class))
8775
.withUserConfiguration(WebServerConfiguration.class);
8876

8977
@Test
9078
void createFromConfigClass() {
91-
this.contextRunner.run(verifyContext());
92-
}
93-
94-
@Test
95-
void contextAlreadyHasDispatcherServletWithDefaultName() {
96-
this.contextRunner.withUserConfiguration(DispatcherServletConfiguration.class).run(verifyContext());
97-
}
98-
99-
@Test
100-
void contextAlreadyHasDispatcherServlet() {
101-
this.contextRunner.withUserConfiguration(SpringServletConfiguration.class).run((context) -> {
102-
verifyContext(context);
103-
assertThat(context.getBeanNamesForType(DispatcherServlet.class)).hasSize(2);
104-
});
105-
}
106-
107-
@Test
108-
void contextAlreadyHasNonDispatcherServlet() {
109-
this.contextRunner.withUserConfiguration(NonSpringServletConfiguration.class).run((context) -> {
110-
verifyContext(context); // the non default servlet is still registered
111-
assertThat(context).doesNotHaveBean(DispatcherServlet.class);
112-
});
113-
}
114-
115-
@Test
116-
void contextAlreadyHasNonServlet() {
117-
this.contextRunner.withUserConfiguration(NonServletConfiguration.class).run((context) -> {
118-
assertThat(context).doesNotHaveBean(DispatcherServlet.class);
119-
assertThat(context).doesNotHaveBean(Servlet.class);
120-
});
121-
}
122-
123-
@Test
124-
void contextAlreadyHasDispatcherServletAndRegistration() {
125-
this.contextRunner.withUserConfiguration(DispatcherServletWithRegistrationConfiguration.class)
126-
.run((context) -> {
127-
verifyContext(context);
128-
assertThat(context).hasSingleBean(DispatcherServlet.class);
129-
});
79+
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ServletWebServerFactoryCustomizer.class));
13080
}
13181

13282
@Test
13383
void webServerHasNoServletContext() {
134-
this.contextRunner.withUserConfiguration(EnsureWebServerHasNoServletContext.class).run(verifyContext());
84+
this.contextRunner.withUserConfiguration(EnsureWebServerHasNoServletContext.class)
85+
.run((context) -> assertThat(context).hasNotFailed());
13586
}
13687

13788
@Test
138-
void customizeWebServerFactoryThroughCallback() {
139-
this.contextRunner.withUserConfiguration(CallbackEmbeddedServerFactoryCustomizer.class).run((context) -> {
140-
verifyContext(context);
141-
assertThat(context.getBean(MockServletWebServerFactory.class).getPort()).isEqualTo(9000);
142-
});
89+
void webServerFactoryCustomizerBeansAreCalledToCustomizeWebServerFactory() {
90+
this.contextRunner
91+
.withBean(WebServerFactoryCustomizer.class,
92+
() -> (WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>) (factory) -> factory
93+
.setPort(9000))
94+
.run((context) -> assertThat(context.getBean(MockServletWebServerFactory.class).getPort()).isEqualTo(9000));
14395
}
14496

14597
@Test
@@ -424,17 +376,6 @@ void relativeRedirectsShouldNotBeEnabledWhenNotUsingTomcatContainer() {
424376
});
425377
}
426378

427-
private ContextConsumer<AssertableWebApplicationContext> verifyContext() {
428-
return this::verifyContext;
429-
}
430-
431-
private void verifyContext(ApplicationContext context) {
432-
MockServletWebServerFactory factory = context.getBean(MockServletWebServerFactory.class);
433-
Servlet servlet = context.getBean(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME,
434-
Servlet.class);
435-
then(factory.getServletContext()).should().addServlet("dispatcherServlet", servlet);
436-
}
437-
438379
@Configuration(proxyBeanMethods = false)
439380
@ConditionalOnExpression("true")
440381
static class WebServerConfiguration {
@@ -446,68 +387,6 @@ ServletWebServerFactory webServerFactory() {
446387

447388
}
448389

449-
@Configuration(proxyBeanMethods = false)
450-
static class DispatcherServletConfiguration {
451-
452-
@Bean
453-
DispatcherServlet dispatcherServlet() {
454-
return new DispatcherServlet();
455-
}
456-
457-
}
458-
459-
@Configuration(proxyBeanMethods = false)
460-
static class SpringServletConfiguration {
461-
462-
@Bean
463-
DispatcherServlet springServlet() {
464-
return new DispatcherServlet();
465-
}
466-
467-
}
468-
469-
@Configuration(proxyBeanMethods = false)
470-
static class NonSpringServletConfiguration {
471-
472-
@Bean
473-
FrameworkServlet dispatcherServlet() {
474-
return new FrameworkServlet() {
475-
@Override
476-
protected void doService(HttpServletRequest request, HttpServletResponse response) {
477-
}
478-
};
479-
}
480-
481-
}
482-
483-
@Configuration(proxyBeanMethods = false)
484-
static class NonServletConfiguration {
485-
486-
@Bean
487-
String dispatcherServlet() {
488-
return "foo";
489-
}
490-
491-
}
492-
493-
@Configuration(proxyBeanMethods = false)
494-
static class DispatcherServletWithRegistrationConfiguration {
495-
496-
@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
497-
DispatcherServlet dispatcherServlet() {
498-
return new DispatcherServlet();
499-
}
500-
501-
@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
502-
ServletRegistrationBean<DispatcherServlet> dispatcherRegistration(DispatcherServlet dispatcherServlet) {
503-
ServletRegistrationBean<DispatcherServlet> registration = new ServletRegistrationBean<>(dispatcherServlet,
504-
"/app/*");
505-
registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
506-
return registration;
507-
}
508-
509-
}
510-
511390
@Component
512391
static class EnsureWebServerHasNoServletContext implements BeanPostProcessor {
513392

@@ -527,17 +406,6 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
527406

528407
}
529408

530-
@Component
531-
static class CallbackEmbeddedServerFactoryCustomizer
532-
implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
533-
534-
@Override
535-
public void customize(ConfigurableServletWebServerFactory serverFactory) {
536-
serverFactory.setPort(9000);
537-
}
538-
539-
}
540-
541409
@Configuration(proxyBeanMethods = false)
542410
static class TomcatConnectorCustomizerConfiguration {
543411

0 commit comments

Comments
 (0)