1818
1919import java .util .function .Consumer ;
2020
21+ import org .apache .commons .logging .Log ;
22+ import org .apache .commons .logging .LogFactory ;
2123import org .junit .jupiter .api .Test ;
2224import org .junit .jupiter .api .extension .ExtendWith ;
2325
2426import org .springframework .aot .AotDetector ;
2527import org .springframework .aot .test .generate .TestGenerationContext ;
28+ import org .springframework .boot .WebApplicationType ;
2629import org .springframework .boot .actuate .autoconfigure .endpoint .EndpointAutoConfiguration ;
2730import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointAutoConfiguration ;
31+ import org .springframework .boot .actuate .autoconfigure .web .ManagementContextFactory ;
2832import org .springframework .boot .autoconfigure .AutoConfigurations ;
29- import org .springframework .boot .servlet .actuate .autoconfigure .ServletManagementContextAutoConfiguration ;
3033import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
3134import org .springframework .boot .test .system .CapturedOutput ;
3235import org .springframework .boot .test .system .OutputCaptureExtension ;
3336import 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 ;
3740import org .springframework .boot .web .server .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
3841import org .springframework .boot .web .server .servlet .context .ServletWebServerApplicationContext ;
42+ import org .springframework .boot .web .servlet .ServletContextInitializer ;
3943import org .springframework .context .ApplicationContextInitializer ;
44+ import org .springframework .context .annotation .Bean ;
45+ import org .springframework .context .annotation .Configuration ;
4046import org .springframework .context .aot .ApplicationContextAotGenerator ;
4147import org .springframework .context .support .GenericApplicationContext ;
4248import org .springframework .core .test .tools .CompileWithForkedClassLoader ;
4551import org .springframework .util .StringUtils ;
4652
4753import 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 )
5662class 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}
0 commit comments