Use random port in spring-boot tests

Update several tests to use random ports instead of hard coding '8080'
or '8081'.

fixes gh-607
This commit is contained in:
Stephane Nicoll 2014-03-31 14:00:13 +02:00 committed by Phillip Webb
parent fca7a8dc96
commit af33cc2b97
6 changed files with 73 additions and 41 deletions

View File

@ -541,7 +541,7 @@ public class SpringApplicationTests {
@Bean @Bean
public JettyEmbeddedServletContainerFactory container() { public JettyEmbeddedServletContainerFactory container() {
return new JettyEmbeddedServletContainerFactory(); return new JettyEmbeddedServletContainerFactory(0);
} }
} }

View File

@ -44,10 +44,12 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.SocketUtils;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFuture;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -88,18 +90,17 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container = factory this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration()); .getEmbeddedServletContainer(exampleServletRegistration());
this.container.start(); this.container.start();
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World")); assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World"));
} }
@Test @Test
public void emptyServerWhenPortIsZero() throws Exception { public void emptyServerWhenPortIsMinusOne() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setPort(0); factory.setPort(-1);
this.container = factory this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration()); .getEmbeddedServletContainer(exampleServletRegistration());
this.container.start(); this.container.start();
this.thrown.expect(IOException.class); assertThat(this.container.getPort(), lessThan(0)); // Jetty is -2
getResponse("http://localhost:8080/hello");
} }
@Test @Test
@ -110,7 +111,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container.start(); this.container.start();
this.container.stop(); this.container.stop();
this.thrown.expect(IOException.class); this.thrown.expect(IOException.class);
getResponse("http://localhost:8080/hello"); getResponse(getLocalUrl("/hello"));
} }
@Test @Test
@ -121,8 +122,8 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container.start(); this.container.start();
HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory(); HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory();
ListenableFuture<ClientHttpResponse> response1 = clientHttpRequestFactory ListenableFuture<ClientHttpResponse> response1 = clientHttpRequestFactory
.createAsyncRequest(new URI("http://localhost:8080/hello"), .createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET)
HttpMethod.GET).executeAsync(); .executeAsync();
assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200)); assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200));
this.container.stop(); this.container.stop();
@ -131,8 +132,8 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container.start(); this.container.start();
ListenableFuture<ClientHttpResponse> response2 = clientHttpRequestFactory ListenableFuture<ClientHttpResponse> response2 = clientHttpRequestFactory
.createAsyncRequest(new URI("http://localhost:8080/hello"), .createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET)
HttpMethod.GET).executeAsync(); .executeAsync();
assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200)); assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200));
} }
@ -143,7 +144,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
exampleServletRegistration(), new FilterRegistrationBean( exampleServletRegistration(), new FilterRegistrationBean(
new ExampleFilter())); new ExampleFilter()));
this.container.start(); this.container.start();
assertThat(getResponse("http://localhost:8080/hello"), equalTo("[Hello World]")); assertThat(getResponse(getLocalUrl("/hello")), equalTo("[Hello World]"));
} }
@Test @Test
@ -188,12 +189,14 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
@Test @Test
public void specificPort() throws Exception { public void specificPort() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setPort(8081); int specificPort = SocketUtils.findAvailableTcpPort(40000);
factory.setPort(specificPort);
this.container = factory this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration()); .getEmbeddedServletContainer(exampleServletRegistration());
this.container.start(); this.container.start();
assertThat(getResponse("http://localhost:8081/hello"), equalTo("Hello World")); assertThat(getResponse("http://localhost:" + specificPort + "/hello"),
assertEquals(8081, this.container.getPort()); equalTo("Hello World"));
assertEquals(specificPort, this.container.getPort());
} }
@Test @Test
@ -203,7 +206,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container = factory this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration()); .getEmbeddedServletContainer(exampleServletRegistration());
this.container.start(); this.container.start();
assertThat(getResponse("http://localhost:8080/say/hello"), equalTo("Hello World")); assertThat(getResponse(getLocalUrl("/say/hello")), equalTo("Hello World"));
} }
@Test @Test
@ -264,7 +267,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
factory.setDocumentRoot(this.temporaryFolder.getRoot()); factory.setDocumentRoot(this.temporaryFolder.getRoot());
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
assertThat(getResponse("http://localhost:8080/test.txt"), equalTo("test")); assertThat(getResponse(getLocalUrl("/test.txt")), equalTo("test"));
} }
@Test @Test
@ -278,7 +281,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
factory.setMimeMappings(mimeMappings); factory.setMimeMappings(mimeMappings);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
ClientHttpResponse response = getClientResponse("http://localhost:8080/test.xxcss"); ClientHttpResponse response = getClientResponse(getLocalUrl("/test.xxcss"));
assertThat(response.getHeaders().getContentType().toString(), equalTo("text/css")); assertThat(response.getHeaders().getContentType().toString(), equalTo("text/css"));
response.close(); response.close();
} }
@ -290,8 +293,12 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container = factory.getEmbeddedServletContainer( this.container = factory.getEmbeddedServletContainer(
exampleServletRegistration(), errorServletRegistration()); exampleServletRegistration(), errorServletRegistration());
this.container.start(); this.container.start();
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World")); assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World"));
assertThat(getResponse("http://localhost:8080/bang"), equalTo("Hello World")); assertThat(getResponse(getLocalUrl("/bang")), equalTo("Hello World"));
}
protected String getLocalUrl(String resourcePath) {
return "http://localhost:" + this.container.getPort() + resourcePath;
} }
protected String getResponse(String url) throws IOException, URISyntaxException { protected String getResponse(String url) throws IOException, URISyntaxException {

View File

@ -26,6 +26,7 @@ import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletConta
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -49,6 +50,7 @@ import static org.junit.Assert.assertThat;
* @author Phillip Webb * @author Phillip Webb
*/ */
public class EmbeddedServletContainerMvcIntegrationTests { public class EmbeddedServletContainerMvcIntegrationTests {
private AnnotationConfigEmbeddedWebApplicationContext context; private AnnotationConfigEmbeddedWebApplicationContext context;
@After @After
@ -63,29 +65,30 @@ public class EmbeddedServletContainerMvcIntegrationTests {
@Test @Test
public void tomcat() throws Exception { public void tomcat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext( this.context = new AnnotationConfigEmbeddedWebApplicationContext(
TomcatEmbeddedServletContainerFactory.class, Config.class); TomcatConfig.class);
doTest(this.context, "http://localhost:8080/hello"); doTest(this.context, "/hello");
} }
@Test @Test
public void jetty() throws Exception { public void jetty() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext( this.context = new AnnotationConfigEmbeddedWebApplicationContext(
JettyEmbeddedServletContainerFactory.class, Config.class); JettyConfig.class);
doTest(this.context, "http://localhost:8080/hello"); doTest(this.context, "/hello");
} }
@Test @Test
public void advancedConfig() throws Exception { public void advancedConfig() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext( this.context = new AnnotationConfigEmbeddedWebApplicationContext(
AdvancedConfig.class); AdvancedConfig.class);
doTest(this.context, "http://localhost:8081/example/spring/hello"); doTest(this.context, "/example/spring/hello");
} }
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context, String url) private void doTest(AnnotationConfigEmbeddedWebApplicationContext context,
throws Exception { String resourcePath) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(url), ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(
HttpMethod.GET); "http://localhost:" + context.getEmbeddedServletContainer().getPort()
+ resourcePath), HttpMethod.GET);
ClientHttpResponse response = request.execute(); ClientHttpResponse response = request.execute();
try { try {
String actual = StreamUtils.copyToString(response.getBody(), String actual = StreamUtils.copyToString(response.getBody(),
@ -97,6 +100,24 @@ public class EmbeddedServletContainerMvcIntegrationTests {
} }
} }
@Configuration
@Import(Config.class)
public static class TomcatConfig {
@Bean
public EmbeddedServletContainerFactory containerFactory() {
return new TomcatEmbeddedServletContainerFactory(0);
}
}
@Configuration
@Import(Config.class)
public static class JettyConfig {
@Bean
public EmbeddedServletContainerFactory containerFactory() {
return new JettyEmbeddedServletContainerFactory(0);
}
}
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
public static class Config { public static class Config {
@ -125,9 +146,9 @@ public class EmbeddedServletContainerMvcIntegrationTests {
@Bean @Bean
public EmbeddedServletContainerFactory containerFactory() { public EmbeddedServletContainerFactory containerFactory() {
JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(); JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(
factory.setPort(this.env.getProperty("port", Integer.class)); 0);
factory.setContextPath("/example"); factory.setContextPath(this.env.getProperty("context"));
return factory; return factory;
} }

View File

@ -45,7 +45,7 @@ public class JettyEmbeddedServletContainerFactoryTests extends
@Override @Override
protected JettyEmbeddedServletContainerFactory getFactory() { protected JettyEmbeddedServletContainerFactory getFactory() {
return new JettyEmbeddedServletContainerFactory(); return new JettyEmbeddedServletContainerFactory(0);
} }
@Test @Test

View File

@ -28,10 +28,10 @@ import org.apache.catalina.startup.Tomcat;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests; import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
import org.springframework.util.SocketUtils;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.inOrder;
@ -51,7 +51,7 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
@Override @Override
protected TomcatEmbeddedServletContainerFactory getFactory() { protected TomcatEmbeddedServletContainerFactory getFactory() {
return new TomcatEmbeddedServletContainerFactory(); return new TomcatEmbeddedServletContainerFactory(0);
} }
// JMX MBean names clash if you get more than one Engine with the same name... // JMX MBean names clash if you get more than one Engine with the same name...
@ -59,12 +59,16 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
public void tomcatEngineNames() throws Exception { public void tomcatEngineNames() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory(); TomcatEmbeddedServletContainerFactory factory = getFactory();
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
factory.setPort(8081); factory.setPort(SocketUtils.findAvailableTcpPort(40000));
TomcatEmbeddedServletContainer container2 = (TomcatEmbeddedServletContainer) factory TomcatEmbeddedServletContainer container2 = (TomcatEmbeddedServletContainer) factory
.getEmbeddedServletContainer(); .getEmbeddedServletContainer();
assertEquals("Tomcat", ((TomcatEmbeddedServletContainer) this.container)
.getTomcat().getEngine().getName()); // Make sure that the names are different
assertEquals("Tomcat-1", container2.getTomcat().getEngine().getName()); String firstContainerName = ((TomcatEmbeddedServletContainer) this.container)
.getTomcat().getEngine().getName();
String secondContainerName = container2.getTomcat().getEngine().getName();
assertFalse("Tomcat engines must have different names",
firstContainerName.equals(secondContainerName));
container2.stop(); container2.stop();
} }