From 6cfbcafc10187688b805e9f0751c7c35072812ff Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 29 Nov 2013 16:56:09 +0000 Subject: [PATCH] Switch to httpcomponents for better error handling in tests --- spring-boot-dependencies/pom.xml | 18 ++++++---- spring-boot/pom.xml | 9 +++-- ...tEmbeddedServletContainerFactoryTests.java | 33 +++++++++---------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 9c581d0237..1a66fa1c4c 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -10,7 +10,6 @@ 5.4.0 1.7.3 1.4 - 3.1 1.6 2.1.6 1.3.172 @@ -18,6 +17,8 @@ 4.2.1.Final 1.0.1.Final 4.3.1.Final + 4.3.1 + 4.0 2.2.9 2.2.2 8.1.9.v20130131 @@ -79,11 +80,6 @@ commons-dbcp ${commons-dbcp.version} - - commons-httpclient - commons-httpclient - ${commons-httpclient.version} - com.lambdaworks lettuce @@ -134,6 +130,16 @@ activemq-pool ${activemq.version} + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + org.apache.httpcomponents + httpasyncclient + ${httpasyncclient.version} + org.apache.tomcat.embed tomcat-embed-core diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 2eefd2e0d5..22022b9765 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -91,8 +91,13 @@ - commons-httpclient - commons-httpclient + org.apache.httpcomponents + httpclient + test + + + org.apache.httpcomponents + httpasyncclient test diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 871259b4a9..e572cbcbd9 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -24,17 +24,14 @@ import java.net.URISyntaxException; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Date; +import java.util.concurrent.TimeUnit; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; -import org.apache.commons.httpclient.methods.GetMethod; import org.junit.After; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -44,9 +41,11 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpResponse; -import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.util.FileCopyUtils; import org.springframework.util.StreamUtils; +import org.springframework.util.concurrent.ListenableFuture; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; @@ -115,25 +114,26 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { } @Test - @Ignore public void restartWithKeepAlive() throws Exception { ConfigurableEmbeddedServletContainerFactory factory = getFactory(); this.container = factory .getEmbeddedServletContainer(exampleServletRegistration()); this.container.start(); - MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - HttpClient client = new HttpClient(connectionManager); - GetMethod get1 = new GetMethod("http://localhost:8080/hello"); - assertThat(client.executeMethod(get1), equalTo(200)); - get1.releaseConnection(); + HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory(); + ListenableFuture response1 = clientHttpRequestFactory + .createAsyncRequest(new URI("http://localhost:8080/hello"), + HttpMethod.GET).executeAsync(); + assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200)); this.container.stop(); this.container = factory .getEmbeddedServletContainer(exampleServletRegistration()); + this.container.start(); - GetMethod get2 = new GetMethod("http://localhost:8080/hello"); - assertThat(client.executeMethod(get2), equalTo(200)); - get2.releaseConnection(); + ListenableFuture response2 = clientHttpRequestFactory + .createAsyncRequest(new URI("http://localhost:8080/hello"), + HttpMethod.GET).executeAsync(); + assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200)); } @Test @@ -267,9 +267,6 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { } @Test - @Ignore - // FIXME: how to test an error response (maybe java.net.HttpUrlConnection isn't going - // to cut it) public void errorPage() throws Exception { ConfigurableEmbeddedServletContainerFactory factory = getFactory(); factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/hello")); @@ -292,7 +289,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { protected ClientHttpResponse getClientResponse(String url) throws IOException, URISyntaxException { - SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); + HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(url), HttpMethod.GET); ClientHttpResponse response = request.execute();