2222import java .nio .charset .Charset ;
2323
2424import org .junit .After ;
25+ import org .junit .Before ;
2526import org .junit .Test ;
2627import org .springframework .boot .actuate .endpoint .Endpoint ;
2728import org .springframework .boot .actuate .endpoint .mvc .MvcEndpoint ;
2829import org .springframework .boot .autoconfigure .PropertyPlaceholderAutoConfiguration ;
2930import org .springframework .boot .autoconfigure .web .DispatcherServletAutoConfiguration ;
3031import org .springframework .boot .autoconfigure .web .EmbeddedServletContainerAutoConfiguration ;
3132import org .springframework .boot .autoconfigure .web .HttpMessageConvertersAutoConfiguration ;
33+ import org .springframework .boot .autoconfigure .web .ServerProperties ;
3234import org .springframework .boot .autoconfigure .web .ServerPropertiesAutoConfiguration ;
3335import org .springframework .boot .autoconfigure .web .WebMvcAutoConfiguration ;
3436import org .springframework .boot .context .embedded .AnnotationConfigEmbeddedWebApplicationContext ;
4547import org .springframework .http .client .ClientHttpResponse ;
4648import org .springframework .http .client .SimpleClientHttpRequestFactory ;
4749import org .springframework .stereotype .Controller ;
50+ import org .springframework .util .SocketUtils ;
4851import org .springframework .util .StreamUtils ;
4952import org .springframework .web .bind .annotation .RequestMapping ;
5053import org .springframework .web .bind .annotation .ResponseBody ;
5558
5659/**
5760 * Tests for {@link EndpointWebMvcAutoConfiguration}.
58- *
61+ *
5962 * @author Phillip Webb
6063 * @author Greg Turnquist
6164 */
6265public class EndpointWebMvcAutoConfigurationTests {
6366
6467 private final AnnotationConfigEmbeddedWebApplicationContext applicationContext = new AnnotationConfigEmbeddedWebApplicationContext ();
6568
69+ private static ThreadLocal <Ports > ports = new ThreadLocal <Ports >();
70+
71+ @ Before
72+ public void grabPorts () {
73+ ports .set (new Ports ());
74+ }
75+
6676 @ After
6777 public void close () {
6878 if (this .applicationContext != null ) {
@@ -73,12 +83,13 @@ public void close() {
7383 @ Test
7484 public void onSamePort () throws Exception {
7585 this .applicationContext .register (RootConfig .class , BaseConfiguration .class ,
86+ ServerPortConfig .class ,
7687 EndpointWebMvcAutoConfiguration .class );
7788 this .applicationContext .refresh ();
78- assertContent ("/controller" , 8080 , "controlleroutput" );
79- assertContent ("/endpoint" , 8080 , "endpointoutput" );
80- assertContent ("/controller" , 8081 , null );
81- assertContent ("/endpoint" , 8081 , null );
89+ assertContent ("/controller" , ports . get (). server , "controlleroutput" );
90+ assertContent ("/endpoint" , ports . get (). server , "endpointoutput" );
91+ assertContent ("/controller" , ports . get (). management , null );
92+ assertContent ("/endpoint" , ports . get (). management , null );
8293 this .applicationContext .close ();
8394 assertAllClosed ();
8495 }
@@ -89,10 +100,10 @@ public void onDifferentPort() throws Exception {
89100 BaseConfiguration .class , EndpointWebMvcAutoConfiguration .class ,
90101 ErrorMvcAutoConfiguration .class );
91102 this .applicationContext .refresh ();
92- assertContent ("/controller" , 8080 , "controlleroutput" );
93- assertContent ("/endpoint" , 8080 , null );
94- assertContent ("/controller" , 8081 , null );
95- assertContent ("/endpoint" , 8081 , "endpointoutput" );
103+ assertContent ("/controller" , ports . get (). server , "controlleroutput" );
104+ assertContent ("/endpoint" , ports . get (). server , null );
105+ assertContent ("/controller" , ports . get (). management , null );
106+ assertContent ("/endpoint" , ports . get (). management , "endpointoutput" );
96107 this .applicationContext .close ();
97108 assertAllClosed ();
98109 }
@@ -107,9 +118,9 @@ public void onRandomPort() throws Exception {
107118 this .applicationContext .addApplicationListener (grabManagementPort );
108119 this .applicationContext .refresh ();
109120 int managementPort = grabManagementPort .getServletContainer ().getPort ();
110- assertThat (managementPort , not (equalTo (8080 )));
111- assertContent ("/controller" , 8080 , "controlleroutput" );
112- assertContent ("/endpoint" , 8080 , null );
121+ assertThat (managementPort , not (equalTo (ports . get (). server )));
122+ assertContent ("/controller" , ports . get (). server , "controlleroutput" );
123+ assertContent ("/endpoint" , ports . get (). server , null );
113124 assertContent ("/controller" , managementPort , null );
114125 assertContent ("/endpoint" , managementPort , "endpointoutput" );
115126 }
@@ -119,25 +130,25 @@ public void disabled() throws Exception {
119130 this .applicationContext .register (RootConfig .class , DisableConfig .class ,
120131 BaseConfiguration .class , EndpointWebMvcAutoConfiguration .class );
121132 this .applicationContext .refresh ();
122- assertContent ("/controller" , 8080 , "controlleroutput" );
123- assertContent ("/endpoint" , 8080 , null );
124- assertContent ("/controller" , 8081 , null );
125- assertContent ("/endpoint" , 8081 , null );
133+ assertContent ("/controller" , ports . get (). server , "controlleroutput" );
134+ assertContent ("/endpoint" , ports . get (). server , null );
135+ assertContent ("/controller" , ports . get (). management , null );
136+ assertContent ("/endpoint" , ports . get (). management , null );
126137 this .applicationContext .close ();
127138 assertAllClosed ();
128139 }
129140
130141 @ Test
131142 public void specificPortsViaProperties () throws Exception {
132- EnvironmentTestUtils .addEnvironment (this .applicationContext , "server.port:7070" ,
133- "management.port:7071" );
143+ EnvironmentTestUtils .addEnvironment (this .applicationContext , "server.port:"
144+ + ports . get (). server , "management.port:" + ports . get (). management );
134145 this .applicationContext .register (RootConfig .class , BaseConfiguration .class ,
135146 EndpointWebMvcAutoConfiguration .class , ErrorMvcAutoConfiguration .class );
136147 this .applicationContext .refresh ();
137- assertContent ("/controller" , 7070 , "controlleroutput" );
138- assertContent ("/endpoint" , 7070 , null );
139- assertContent ("/controller" , 7071 , null );
140- assertContent ("/endpoint" , 7071 , "endpointoutput" );
148+ assertContent ("/controller" , ports . get (). server , "controlleroutput" );
149+ assertContent ("/endpoint" , ports . get (). server , null );
150+ assertContent ("/controller" , ports . get (). management , null );
151+ assertContent ("/endpoint" , ports . get (). management , "endpointoutput" );
141152 this .applicationContext .close ();
142153 assertAllClosed ();
143154 }
@@ -146,7 +157,7 @@ public void specificPortsViaProperties() throws Exception {
146157 public void contextPath () throws Exception {
147158 EnvironmentTestUtils .addEnvironment (this .applicationContext ,
148159 "management.contextPath:/test" );
149- this .applicationContext .register (RootConfig .class ,
160+ this .applicationContext .register (RootConfig .class , ServerPortConfig . class ,
150161 PropertyPlaceholderAutoConfiguration .class ,
151162 ManagementServerPropertiesAutoConfiguration .class ,
152163 ServerPropertiesAutoConfiguration .class ,
@@ -155,17 +166,17 @@ public void contextPath() throws Exception {
155166 DispatcherServletAutoConfiguration .class , WebMvcAutoConfiguration .class ,
156167 EndpointWebMvcAutoConfiguration .class );
157168 this .applicationContext .refresh ();
158- assertContent ("/controller" , 8080 , "controlleroutput" );
159- assertContent ("/test/endpoint" , 8080 , "endpointoutput" );
169+ assertContent ("/controller" , ports . get (). server , "controlleroutput" );
170+ assertContent ("/test/endpoint" , ports . get (). server , "endpointoutput" );
160171 this .applicationContext .close ();
161172 assertAllClosed ();
162173 }
163174
164175 private void assertAllClosed () throws Exception {
165- assertContent ("/controller" , 8080 , null );
166- assertContent ("/endpoint" , 8080 , null );
167- assertContent ("/controller" , 8081 , null );
168- assertContent ("/endpoint" , 8081 , null );
176+ assertContent ("/controller" , ports . get (). server , null );
177+ assertContent ("/endpoint" , ports . get (). server , null );
178+ assertContent ("/controller" , ports . get (). management , null );
179+ assertContent ("/endpoint" , ports . get (). management , null );
169180 }
170181
171182 public void assertContent (String url , int port , Object expected ) throws Exception {
@@ -194,6 +205,14 @@ public void assertContent(String url, int port, Object expected) throws Exceptio
194205 }
195206 }
196207
208+ private static class Ports {
209+
210+ int server = SocketUtils .findAvailableTcpPort ();
211+
212+ int management = SocketUtils .findAvailableTcpPort ();
213+
214+ }
215+
197216 @ Configuration
198217 @ Import ({ PropertyPlaceholderAutoConfiguration .class ,
199218 EmbeddedServletContainerAutoConfiguration .class ,
@@ -217,6 +236,19 @@ public TestController testController() {
217236 public TestEndpoint testEndpoint () {
218237 return new TestEndpoint ();
219238 }
239+
240+ }
241+
242+ @ Configuration
243+ public static class ServerPortConfig {
244+
245+ @ Bean
246+ public ServerProperties serverProperties () {
247+ ServerProperties properties = new ServerProperties ();
248+ properties .setPort (ports .get ().server );
249+ return properties ;
250+ }
251+
220252 }
221253
222254 @ Controller
@@ -231,18 +263,20 @@ public String requestMappedMethod() {
231263 }
232264
233265 @ Configuration
266+ @ Import (ServerPortConfig .class )
234267 public static class DifferentPortConfig {
235268
236269 @ Bean
237270 public ManagementServerProperties managementServerProperties () {
238271 ManagementServerProperties properties = new ManagementServerProperties ();
239- properties .setPort (8081 );
272+ properties .setPort (ports . get (). management );
240273 return properties ;
241274 }
242275
243276 }
244277
245278 @ Configuration
279+ @ Import (ServerPortConfig .class )
246280 public static class RandomPortConfig {
247281
248282 @ Bean
@@ -255,6 +289,7 @@ public ManagementServerProperties managementServerProperties() {
255289 }
256290
257291 @ Configuration
292+ @ Import (ServerPortConfig .class )
258293 public static class DisableConfig {
259294
260295 @ Bean
0 commit comments