22
22
import java .nio .charset .Charset ;
23
23
24
24
import org .junit .After ;
25
+ import org .junit .Before ;
25
26
import org .junit .Test ;
26
27
import org .springframework .boot .actuate .endpoint .Endpoint ;
27
28
import org .springframework .boot .actuate .endpoint .mvc .MvcEndpoint ;
28
29
import org .springframework .boot .autoconfigure .PropertyPlaceholderAutoConfiguration ;
29
30
import org .springframework .boot .autoconfigure .web .DispatcherServletAutoConfiguration ;
30
31
import org .springframework .boot .autoconfigure .web .EmbeddedServletContainerAutoConfiguration ;
31
32
import org .springframework .boot .autoconfigure .web .HttpMessageConvertersAutoConfiguration ;
33
+ import org .springframework .boot .autoconfigure .web .ServerProperties ;
32
34
import org .springframework .boot .autoconfigure .web .ServerPropertiesAutoConfiguration ;
33
35
import org .springframework .boot .autoconfigure .web .WebMvcAutoConfiguration ;
34
36
import org .springframework .boot .context .embedded .AnnotationConfigEmbeddedWebApplicationContext ;
45
47
import org .springframework .http .client .ClientHttpResponse ;
46
48
import org .springframework .http .client .SimpleClientHttpRequestFactory ;
47
49
import org .springframework .stereotype .Controller ;
50
+ import org .springframework .util .SocketUtils ;
48
51
import org .springframework .util .StreamUtils ;
49
52
import org .springframework .web .bind .annotation .RequestMapping ;
50
53
import org .springframework .web .bind .annotation .ResponseBody ;
55
58
56
59
/**
57
60
* Tests for {@link EndpointWebMvcAutoConfiguration}.
58
- *
61
+ *
59
62
* @author Phillip Webb
60
63
* @author Greg Turnquist
61
64
*/
62
65
public class EndpointWebMvcAutoConfigurationTests {
63
66
64
67
private final AnnotationConfigEmbeddedWebApplicationContext applicationContext = new AnnotationConfigEmbeddedWebApplicationContext ();
65
68
69
+ private static ThreadLocal <Ports > ports = new ThreadLocal <Ports >();
70
+
71
+ @ Before
72
+ public void grabPorts () {
73
+ ports .set (new Ports ());
74
+ }
75
+
66
76
@ After
67
77
public void close () {
68
78
if (this .applicationContext != null ) {
@@ -73,12 +83,13 @@ public void close() {
73
83
@ Test
74
84
public void onSamePort () throws Exception {
75
85
this .applicationContext .register (RootConfig .class , BaseConfiguration .class ,
86
+ ServerPortConfig .class ,
76
87
EndpointWebMvcAutoConfiguration .class );
77
88
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 );
82
93
this .applicationContext .close ();
83
94
assertAllClosed ();
84
95
}
@@ -89,10 +100,10 @@ public void onDifferentPort() throws Exception {
89
100
BaseConfiguration .class , EndpointWebMvcAutoConfiguration .class ,
90
101
ErrorMvcAutoConfiguration .class );
91
102
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" );
96
107
this .applicationContext .close ();
97
108
assertAllClosed ();
98
109
}
@@ -107,9 +118,9 @@ public void onRandomPort() throws Exception {
107
118
this .applicationContext .addApplicationListener (grabManagementPort );
108
119
this .applicationContext .refresh ();
109
120
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 );
113
124
assertContent ("/controller" , managementPort , null );
114
125
assertContent ("/endpoint" , managementPort , "endpointoutput" );
115
126
}
@@ -119,25 +130,25 @@ public void disabled() throws Exception {
119
130
this .applicationContext .register (RootConfig .class , DisableConfig .class ,
120
131
BaseConfiguration .class , EndpointWebMvcAutoConfiguration .class );
121
132
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 );
126
137
this .applicationContext .close ();
127
138
assertAllClosed ();
128
139
}
129
140
130
141
@ Test
131
142
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 );
134
145
this .applicationContext .register (RootConfig .class , BaseConfiguration .class ,
135
146
EndpointWebMvcAutoConfiguration .class , ErrorMvcAutoConfiguration .class );
136
147
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" );
141
152
this .applicationContext .close ();
142
153
assertAllClosed ();
143
154
}
@@ -146,7 +157,7 @@ public void specificPortsViaProperties() throws Exception {
146
157
public void contextPath () throws Exception {
147
158
EnvironmentTestUtils .addEnvironment (this .applicationContext ,
148
159
"management.contextPath:/test" );
149
- this .applicationContext .register (RootConfig .class ,
160
+ this .applicationContext .register (RootConfig .class , ServerPortConfig . class ,
150
161
PropertyPlaceholderAutoConfiguration .class ,
151
162
ManagementServerPropertiesAutoConfiguration .class ,
152
163
ServerPropertiesAutoConfiguration .class ,
@@ -155,17 +166,17 @@ public void contextPath() throws Exception {
155
166
DispatcherServletAutoConfiguration .class , WebMvcAutoConfiguration .class ,
156
167
EndpointWebMvcAutoConfiguration .class );
157
168
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" );
160
171
this .applicationContext .close ();
161
172
assertAllClosed ();
162
173
}
163
174
164
175
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 );
169
180
}
170
181
171
182
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
194
205
}
195
206
}
196
207
208
+ private static class Ports {
209
+
210
+ int server = SocketUtils .findAvailableTcpPort ();
211
+
212
+ int management = SocketUtils .findAvailableTcpPort ();
213
+
214
+ }
215
+
197
216
@ Configuration
198
217
@ Import ({ PropertyPlaceholderAutoConfiguration .class ,
199
218
EmbeddedServletContainerAutoConfiguration .class ,
@@ -217,6 +236,19 @@ public TestController testController() {
217
236
public TestEndpoint testEndpoint () {
218
237
return new TestEndpoint ();
219
238
}
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
+
220
252
}
221
253
222
254
@ Controller
@@ -231,18 +263,20 @@ public String requestMappedMethod() {
231
263
}
232
264
233
265
@ Configuration
266
+ @ Import (ServerPortConfig .class )
234
267
public static class DifferentPortConfig {
235
268
236
269
@ Bean
237
270
public ManagementServerProperties managementServerProperties () {
238
271
ManagementServerProperties properties = new ManagementServerProperties ();
239
- properties .setPort (8081 );
272
+ properties .setPort (ports . get (). management );
240
273
return properties ;
241
274
}
242
275
243
276
}
244
277
245
278
@ Configuration
279
+ @ Import (ServerPortConfig .class )
246
280
public static class RandomPortConfig {
247
281
248
282
@ Bean
@@ -255,6 +289,7 @@ public ManagementServerProperties managementServerProperties() {
255
289
}
256
290
257
291
@ Configuration
292
+ @ Import (ServerPortConfig .class )
258
293
public static class DisableConfig {
259
294
260
295
@ Bean
0 commit comments