18
18
19
19
import java .util .function .Consumer ;
20
20
21
+ import org .apache .commons .logging .Log ;
22
+ import org .apache .commons .logging .LogFactory ;
21
23
import org .junit .jupiter .api .Test ;
22
24
import org .junit .jupiter .api .extension .ExtendWith ;
23
25
24
26
import org .springframework .aot .AotDetector ;
25
27
import org .springframework .aot .test .generate .TestGenerationContext ;
28
+ import org .springframework .boot .WebApplicationType ;
26
29
import org .springframework .boot .actuate .autoconfigure .endpoint .EndpointAutoConfiguration ;
27
30
import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointAutoConfiguration ;
31
+ import org .springframework .boot .actuate .autoconfigure .web .ManagementContextFactory ;
28
32
import org .springframework .boot .autoconfigure .AutoConfigurations ;
29
- import org .springframework .boot .servlet .actuate .autoconfigure .ServletManagementContextAutoConfiguration ;
30
33
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
31
34
import org .springframework .boot .test .system .CapturedOutput ;
32
35
import org .springframework .boot .test .system .OutputCaptureExtension ;
33
36
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 ;
37
40
import org .springframework .boot .web .server .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
38
41
import org .springframework .boot .web .server .servlet .context .ServletWebServerApplicationContext ;
42
+ import org .springframework .boot .web .servlet .ServletContextInitializer ;
39
43
import org .springframework .context .ApplicationContextInitializer ;
44
+ import org .springframework .context .annotation .Bean ;
45
+ import org .springframework .context .annotation .Configuration ;
40
46
import org .springframework .context .aot .ApplicationContextAotGenerator ;
41
47
import org .springframework .context .support .GenericApplicationContext ;
42
48
import org .springframework .core .test .tools .CompileWithForkedClassLoader ;
45
51
import org .springframework .util .StringUtils ;
46
52
47
53
import static org .assertj .core .api .Assertions .assertThat ;
54
+ import static org .mockito .BDDMockito .willAnswer ;
48
55
49
56
/**
50
57
* AOT tests for {@link ChildManagementContextInitializer}.
51
58
*
52
59
* @author Phillip Webb
53
60
*/
54
- @ DirtiesUrlFactories
55
61
@ ExtendWith (OutputCaptureExtension .class )
56
62
class ChildManagementContextInitializerAotTests {
57
63
@@ -62,10 +68,8 @@ void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
62
68
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner (
63
69
AnnotationConfigServletWebServerApplicationContext ::new )
64
70
.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 );
69
73
contextRunner .withPropertyValues ("server.port=0" , "management.server.port=0" ).prepare ((context ) -> {
70
74
TestGenerationContext generationContext = new TestGenerationContext (TestTarget .class );
71
75
ClassName className = new ApplicationContextAotGenerator ().processAheadOfTime (
@@ -78,10 +82,10 @@ void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
78
82
ApplicationContextInitializer <GenericApplicationContext > initializer = compiled
79
83
.getInstance (ApplicationContextInitializer .class , className .toString ());
80
84
initializer .initialize (freshApplicationContext );
81
- assertThat (output ).satisfies (numberOfOccurrences ("Tomcat started on port " , 0 ));
85
+ assertThat (output ).satisfies (numberOfOccurrences ("WebServer started" , 0 ));
82
86
TestPropertyValues .of (AotDetector .AOT_ENABLED + "=true" )
83
87
.applyToSystemProperties (freshApplicationContext ::refresh );
84
- assertThat (output ).satisfies (numberOfOccurrences ("Tomcat started on port " , 2 ));
88
+ assertThat (output ).satisfies (numberOfOccurrences ("WebServer started" , 2 ));
85
89
});
86
90
});
87
91
}
@@ -97,4 +101,40 @@ static class TestTarget {
97
101
98
102
}
99
103
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
+
100
140
}
0 commit comments