Skip to content

Commit 83de8d4

Browse files
committed
Move some tests out of actuator-autoconfigure-all
1 parent 1307c6b commit 83de8d4

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828

2929
testImplementation(project(":spring-boot-project:spring-boot-test"))
3030
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
31+
testImplementation(testFixtures(project(":spring-boot-project:spring-boot-web-server")))
3132
testImplementation("org.springframework:spring-webflux")
3233

3334
testRuntimeOnly("ch.qos.logback:logback-classic")

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextFactory.java

Lines changed: 4 additions & 2 deletions
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.
@@ -72,7 +72,9 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
7272

7373
public void registerWebServerFactoryBeans(ApplicationContext parentContext,
7474
ConfigurableApplicationContext managementContext, AnnotationConfigRegistry registry) {
75-
registry.register(this.autoConfigurationClasses);
75+
if (this.autoConfigurationClasses != null && this.autoConfigurationClasses.length > 0) {
76+
registry.register(this.autoConfigurationClasses);
77+
}
7678
registerWebServerFactoryFromParent(parentContext, managementContext);
7779
}
7880

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,31 @@
1818

1919
import java.util.function.Consumer;
2020

21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
2123
import org.junit.jupiter.api.Test;
2224
import org.junit.jupiter.api.extension.ExtendWith;
2325

2426
import org.springframework.aot.AotDetector;
2527
import org.springframework.aot.test.generate.TestGenerationContext;
28+
import org.springframework.boot.WebApplicationType;
2629
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
2730
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
31+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
2832
import org.springframework.boot.autoconfigure.AutoConfigurations;
29-
import org.springframework.boot.servlet.actuate.autoconfigure.ServletManagementContextAutoConfiguration;
3033
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3134
import org.springframework.boot.test.system.CapturedOutput;
3235
import org.springframework.boot.test.system.OutputCaptureExtension;
3336
import org.springframework.boot.test.util.TestPropertyValues;
34-
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
35-
import org.springframework.boot.tomcat.actuate.autoconfigure.web.TomcatServletManagementContextAutoConfiguration;
36-
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration;
37+
import org.springframework.boot.web.server.WebServer;
38+
import org.springframework.boot.web.server.servlet.MockServletWebServer;
39+
import org.springframework.boot.web.server.servlet.MockServletWebServerFactory;
3740
import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext;
3841
import org.springframework.boot.web.server.servlet.context.ServletWebServerApplicationContext;
42+
import org.springframework.boot.web.servlet.ServletContextInitializer;
3943
import org.springframework.context.ApplicationContextInitializer;
44+
import org.springframework.context.annotation.Bean;
45+
import org.springframework.context.annotation.Configuration;
4046
import org.springframework.context.aot.ApplicationContextAotGenerator;
4147
import org.springframework.context.support.GenericApplicationContext;
4248
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
@@ -45,13 +51,13 @@
4551
import org.springframework.util.StringUtils;
4652

4753
import static org.assertj.core.api.Assertions.assertThat;
54+
import static org.mockito.BDDMockito.willAnswer;
4855

4956
/**
5057
* AOT tests for {@link ChildManagementContextInitializer}.
5158
*
5259
* @author Phillip Webb
5360
*/
54-
@DirtiesUrlFactories
5561
@ExtendWith(OutputCaptureExtension.class)
5662
class ChildManagementContextInitializerAotTests {
5763

@@ -62,10 +68,8 @@ void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
6268
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
6369
AnnotationConfigServletWebServerApplicationContext::new)
6470
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class,
65-
TomcatServletWebServerAutoConfiguration.class,
66-
TomcatServletManagementContextAutoConfiguration.class,
67-
ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
68-
EndpointAutoConfiguration.class));
71+
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class))
72+
.withUserConfiguration(WebServerConfiguration.class, TestServletManagementContextConfiguration.class);
6973
contextRunner.withPropertyValues("server.port=0", "management.server.port=0").prepare((context) -> {
7074
TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class);
7175
ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(
@@ -78,10 +82,10 @@ void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
7882
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
7983
.getInstance(ApplicationContextInitializer.class, className.toString());
8084
initializer.initialize(freshApplicationContext);
81-
assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 0));
85+
assertThat(output).satisfies(numberOfOccurrences("WebServer started", 0));
8286
TestPropertyValues.of(AotDetector.AOT_ENABLED + "=true")
8387
.applyToSystemProperties(freshApplicationContext::refresh);
84-
assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2));
88+
assertThat(output).satisfies(numberOfOccurrences("WebServer started", 2));
8589
});
8690
});
8791
}
@@ -97,4 +101,40 @@ static class TestTarget {
97101

98102
}
99103

104+
@Configuration(proxyBeanMethods = false)
105+
static class TestServletManagementContextConfiguration {
106+
107+
@Bean
108+
ManagementContextFactory managementContextFactory() {
109+
return new ManagementContextFactory(WebApplicationType.SERVLET, LogOnStartServletWebServerFactory.class);
110+
}
111+
112+
}
113+
114+
@Configuration(proxyBeanMethods = false)
115+
static class WebServerConfiguration {
116+
117+
@Bean
118+
LogOnStartServletWebServerFactory servletWebServerFactory() {
119+
return new LogOnStartServletWebServerFactory();
120+
}
121+
122+
}
123+
124+
static class LogOnStartServletWebServerFactory extends MockServletWebServerFactory {
125+
126+
private static final Log log = LogFactory.getLog(LogOnStartServletWebServerFactory.class);
127+
128+
@Override
129+
public MockServletWebServer getWebServer(ServletContextInitializer... initializers) {
130+
WebServer webServer = super.getWebServer(initializers);
131+
willAnswer((invocation) -> {
132+
log.info("WebServer started");
133+
return null;
134+
}).given(webServer).start();
135+
return (MockServletWebServer) webServer;
136+
}
137+
138+
}
139+
100140
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.web.server;
17+
package org.springframework.boot.servlet.actuate.autoconfigure;
1818

1919
import java.util.function.Consumer;
2020

@@ -23,27 +23,26 @@
2323

2424
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
2525
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
26+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
2627
import org.springframework.boot.autoconfigure.AutoConfigurations;
27-
import org.springframework.boot.servlet.actuate.autoconfigure.ServletManagementContextAutoConfiguration;
2828
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2929
import org.springframework.boot.test.system.CapturedOutput;
3030
import org.springframework.boot.test.system.OutputCaptureExtension;
3131
import org.springframework.boot.tomcat.actuate.autoconfigure.web.TomcatServletManagementContextAutoConfiguration;
3232
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration;
3333
import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext;
34-
import org.springframework.boot.webmvc.autoconfigure.DispatcherServletAutoConfiguration;
3534
import org.springframework.util.StringUtils;
3635

3736
import static org.assertj.core.api.Assertions.assertThat;
3837

3938
/**
40-
* Tests for {@link ManagementContextAutoConfiguration}.
39+
* Integration tests for {@link ServletManagementContextAutoConfiguration}.
4140
*
4241
* @author Madhura Bhave
4342
* @author Andy Wilkinson
4443
*/
4544
@ExtendWith(OutputCaptureExtension.class)
46-
class ManagementContextAutoConfigurationTests {
45+
class ServletManagementContextAutoConfigurationIntegrationTests {
4746

4847
@Test
4948
void childManagementContextShouldStartForEmbeddedServer(CapturedOutput output) {
@@ -95,8 +94,7 @@ void givenSamePortManagementServerWhenManagementServerAddressIsConfiguredThenCon
9594
AnnotationConfigServletWebServerApplicationContext::new)
9695
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class,
9796
TomcatServletWebServerAutoConfiguration.class, ServletManagementContextAutoConfiguration.class,
98-
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class,
99-
DispatcherServletAutoConfiguration.class));
97+
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class));
10098
contextRunner.withPropertyValues("server.port=0", "management.server.address=127.0.0.1")
10199
.run((context) -> assertThat(context).getFailure()
102100
.hasMessageStartingWith("Management-specific server address cannot be configured"));

0 commit comments

Comments
 (0)