4444import org .springframework .http .client .HttpComponentsAsyncClientHttpRequestFactory ;
4545import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
4646import org .springframework .util .FileCopyUtils ;
47+ import org .springframework .util .SocketUtils ;
4748import org .springframework .util .StreamUtils ;
4849import org .springframework .util .concurrent .ListenableFuture ;
4950
5051import static org .hamcrest .Matchers .equalTo ;
52+ import static org .hamcrest .Matchers .lessThan ;
5153import static org .hamcrest .Matchers .notNullValue ;
5254import static org .junit .Assert .assertEquals ;
5355import static org .junit .Assert .assertThat ;
@@ -88,18 +90,17 @@ public void startServlet() throws Exception {
8890 this .container = factory
8991 .getEmbeddedServletContainer (exampleServletRegistration ());
9092 this .container .start ();
91- assertThat (getResponse ("http://localhost:8080/ hello" ), equalTo ("Hello World" ));
93+ assertThat (getResponse (getLocalUrl ( "/ hello") ), equalTo ("Hello World" ));
9294 }
9395
9496 @ Test
95- public void emptyServerWhenPortIsZero () throws Exception {
97+ public void emptyServerWhenPortIsMinusOne () throws Exception {
9698 AbstractEmbeddedServletContainerFactory factory = getFactory ();
97- factory .setPort (0 );
99+ factory .setPort (- 1 );
98100 this .container = factory
99101 .getEmbeddedServletContainer (exampleServletRegistration ());
100102 this .container .start ();
101- this .thrown .expect (IOException .class );
102- getResponse ("http://localhost:8080/hello" );
103+ assertThat (this .container .getPort (), lessThan (0 )); // Jetty is -2
103104 }
104105
105106 @ Test
@@ -110,7 +111,7 @@ public void stopServlet() throws Exception {
110111 this .container .start ();
111112 this .container .stop ();
112113 this .thrown .expect (IOException .class );
113- getResponse ("http://localhost:8080/ hello" );
114+ getResponse (getLocalUrl ( "/ hello") );
114115 }
115116
116117 @ Test
@@ -121,8 +122,8 @@ public void restartWithKeepAlive() throws Exception {
121122 this .container .start ();
122123 HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory ();
123124 ListenableFuture <ClientHttpResponse > response1 = clientHttpRequestFactory
124- .createAsyncRequest (new URI ("http://localhost:8080/ hello" ),
125- HttpMethod . GET ) .executeAsync ();
125+ .createAsyncRequest (new URI (getLocalUrl ( "/ hello" )), HttpMethod . GET )
126+ .executeAsync ();
126127 assertThat (response1 .get (10 , TimeUnit .SECONDS ).getRawStatusCode (), equalTo (200 ));
127128
128129 this .container .stop ();
@@ -131,8 +132,8 @@ public void restartWithKeepAlive() throws Exception {
131132 this .container .start ();
132133
133134 ListenableFuture <ClientHttpResponse > response2 = clientHttpRequestFactory
134- .createAsyncRequest (new URI ("http://localhost:8080/ hello" ),
135- HttpMethod . GET ) .executeAsync ();
135+ .createAsyncRequest (new URI (getLocalUrl ( "/ hello" )), HttpMethod . GET )
136+ .executeAsync ();
136137 assertThat (response2 .get (10 , TimeUnit .SECONDS ).getRawStatusCode (), equalTo (200 ));
137138 }
138139
@@ -143,7 +144,7 @@ public void startServletAndFilter() throws Exception {
143144 exampleServletRegistration (), new FilterRegistrationBean (
144145 new ExampleFilter ()));
145146 this .container .start ();
146- assertThat (getResponse ("http://localhost:8080/ hello" ), equalTo ("[Hello World]" ));
147+ assertThat (getResponse (getLocalUrl ( "/ hello") ), equalTo ("[Hello World]" ));
147148 }
148149
149150 @ Test
@@ -188,12 +189,14 @@ public void onStartup(ServletContext servletContext)
188189 @ Test
189190 public void specificPort () throws Exception {
190191 AbstractEmbeddedServletContainerFactory factory = getFactory ();
191- factory .setPort (8081 );
192+ int specificPort = SocketUtils .findAvailableTcpPort (40000 );
193+ factory .setPort (specificPort );
192194 this .container = factory
193195 .getEmbeddedServletContainer (exampleServletRegistration ());
194196 this .container .start ();
195- assertThat (getResponse ("http://localhost:8081/hello" ), equalTo ("Hello World" ));
196- assertEquals (8081 , this .container .getPort ());
197+ assertThat (getResponse ("http://localhost:" + specificPort + "/hello" ),
198+ equalTo ("Hello World" ));
199+ assertEquals (specificPort , this .container .getPort ());
197200 }
198201
199202 @ Test
@@ -203,7 +206,7 @@ public void specificContextRoot() throws Exception {
203206 this .container = factory
204207 .getEmbeddedServletContainer (exampleServletRegistration ());
205208 this .container .start ();
206- assertThat (getResponse ("http://localhost:8080/ say/hello" ), equalTo ("Hello World" ));
209+ assertThat (getResponse (getLocalUrl ( "/ say/hello") ), equalTo ("Hello World" ));
207210 }
208211
209212 @ Test
@@ -264,7 +267,7 @@ public void documentRoot() throws Exception {
264267 factory .setDocumentRoot (this .temporaryFolder .getRoot ());
265268 this .container = factory .getEmbeddedServletContainer ();
266269 this .container .start ();
267- assertThat (getResponse ("http://localhost:8080/ test.txt" ), equalTo ("test" ));
270+ assertThat (getResponse (getLocalUrl ( "/ test.txt") ), equalTo ("test" ));
268271 }
269272
270273 @ Test
@@ -278,7 +281,7 @@ public void mimeType() throws Exception {
278281 factory .setMimeMappings (mimeMappings );
279282 this .container = factory .getEmbeddedServletContainer ();
280283 this .container .start ();
281- ClientHttpResponse response = getClientResponse ("http://localhost:8080/ test.xxcss" );
284+ ClientHttpResponse response = getClientResponse (getLocalUrl ( "/ test.xxcss") );
282285 assertThat (response .getHeaders ().getContentType ().toString (), equalTo ("text/css" ));
283286 response .close ();
284287 }
@@ -290,8 +293,12 @@ public void errorPage() throws Exception {
290293 this .container = factory .getEmbeddedServletContainer (
291294 exampleServletRegistration (), errorServletRegistration ());
292295 this .container .start ();
293- assertThat (getResponse ("http://localhost:8080/hello" ), equalTo ("Hello World" ));
294- assertThat (getResponse ("http://localhost:8080/bang" ), equalTo ("Hello World" ));
296+ assertThat (getResponse (getLocalUrl ("/hello" )), equalTo ("Hello World" ));
297+ assertThat (getResponse (getLocalUrl ("/bang" )), equalTo ("Hello World" ));
298+ }
299+
300+ protected String getLocalUrl (String resourcePath ) {
301+ return "http://localhost:" + this .container .getPort () + resourcePath ;
295302 }
296303
297304 protected String getResponse (String url ) throws IOException , URISyntaxException {
0 commit comments