From 87a46db5c27cf65359fbeef24994c2e80edd8008 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 4 Nov 2012 07:41:57 +0000 Subject: [PATCH] Added test cases for HttpClientUtils git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1405511 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/client/utils/HttpClientUtils.java | 79 +++++++++++- .../client/utils/TestHttpClientUtils.java | 116 ++++++++++++++++++ 2 files changed, 189 insertions(+), 6 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/client/utils/HttpClientUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/HttpClientUtils.java index 0e744d38d..58bf87030 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/HttpClientUtils.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/HttpClientUtils.java @@ -31,11 +31,13 @@ import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; /** * Static helpers for dealing with {@link HttpResponse}s. - * + * * @since 4.2 */ public class HttpClientUtils { @@ -47,7 +49,7 @@ public class HttpClientUtils { * Unconditionally close a response. *

* Example Code: - * + * *

      * HttpResponse httpResponse = null;
      * try {
@@ -58,11 +60,11 @@ public class HttpClientUtils {
      *     HttpClientUtils.closeQuietly(httpResponse);
      * }
      * 
- * + * * @param response * the HttpResponse to release resources, may be null or already * closed. - * + * * @since 4.2 */ public static void closeQuietly(final HttpResponse response) { @@ -77,12 +79,47 @@ public class HttpClientUtils { } } + /** + * Unconditionally close a response. + *

+ * Example Code: + * + *

+     * HttpResponse httpResponse = null;
+     * try {
+     *     httpResponse = httpClient.execute(httpGet);
+     * } catch (Exception e) {
+     *     // error handling
+     * } finally {
+     *     HttpClientUtils.closeQuietly(httpResponse);
+     * }
+     * 
+ * + * @param response + * the HttpResponse to release resources, may be null or already + * closed. + * + * @since 4.3 + */ + public static void closeQuietly(final CloseableHttpResponse response) { + if (response != null) { + try { + try { + EntityUtils.consume(response.getEntity()); + } finally { + response.close(); + } + } catch (final IOException ex) { + } + } + } + /** * Unconditionally close a httpClient. Shuts down the underlying connection * manager and releases the resources. *

* Example Code: - * + * *

      * HttpClient httpClient = null;
      * try {
@@ -93,7 +130,7 @@ public class HttpClientUtils {
      *   HttpClientUtils.closeQuietly(httpClient);
      * }
      * 
- * + * * @param httpClient * the HttpClient to close, may be null or already closed. * @since 4.2 @@ -107,4 +144,34 @@ public class HttpClientUtils { } } + /** + * Unconditionally close a httpClient. Shuts down the underlying connection + * manager and releases the resources. + *

+ * Example Code: + * + *

+     * CloseableHttpClient httpClient = HttpClients.createDefault();
+     * try {
+     *   ...
+     * } catch (Exception e) {
+     *   // error handling
+     * } finally {
+     *   HttpClientUtils.closeQuietly(httpClient);
+     * }
+     * 
+ * + * @param httpClient + * the HttpClient to close, may be null or already closed. + * @since 4.3 + */ + public static void closeQuietly(final CloseableHttpClient httpClient) { + if (httpClient != null) { + try { + httpClient.close(); + } catch (final IOException ex) { + } + } + } + } diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestHttpClientUtils.java b/httpclient/src/test/java/org/apache/http/client/utils/TestHttpClientUtils.java index 36c076990..e3a6fe91c 100644 --- a/httpclient/src/test/java/org/apache/http/client/utils/TestHttpClientUtils.java +++ b/httpclient/src/test/java/org/apache/http/client/utils/TestHttpClientUtils.java @@ -26,8 +26,15 @@ */ package org.apache.http.client.utils; +import java.io.InputStream; +import java.io.IOException; + +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; import org.junit.Test; +import org.mockito.Mockito; public class TestHttpClientUtils { @@ -37,4 +44,113 @@ public class TestHttpClientUtils { HttpClientUtils.closeQuietly(response); } + @Test + public void testCloseQuietlyResponseEntityNull() throws Exception { + HttpResponse response = Mockito.mock(HttpResponse.class); + HttpClientUtils.closeQuietly(response); + Mockito.verify(response).getEntity(); + } + + @Test + public void testCloseQuietlyResponseEntityNonStreaming() throws Exception { + HttpResponse response = Mockito.mock(HttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + Mockito.when(response.getEntity()).thenReturn(entity); + Mockito.when(entity.isStreaming()).thenReturn(false); + HttpClientUtils.closeQuietly(response); + Mockito.verify(entity, Mockito.never()).getContent(); + } + + @Test + public void testCloseQuietlyResponseEntity() throws Exception { + HttpResponse response = Mockito.mock(HttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + InputStream instream = Mockito.mock(InputStream.class); + Mockito.when(response.getEntity()).thenReturn(entity); + Mockito.when(entity.isStreaming()).thenReturn(true); + Mockito.when(entity.getContent()).thenReturn(instream); + HttpClientUtils.closeQuietly(response); + Mockito.verify(instream).close(); + } + + @Test + public void testCloseQuietlyResponseIgnoreIOError() throws Exception { + HttpResponse response = Mockito.mock(HttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + InputStream instream = Mockito.mock(InputStream.class); + Mockito.when(response.getEntity()).thenReturn(entity); + Mockito.when(entity.getContent()).thenReturn(instream); + Mockito.doThrow(new IOException()).when(instream).close(); + HttpClientUtils.closeQuietly(response); + } + + @Test + public void testCloseQuietlyCloseableResponseNull() throws Exception { + CloseableHttpResponse response = null; + HttpClientUtils.closeQuietly(response); + } + + @Test + public void testCloseQuietlyCloseableResponseEntityNull() throws Exception { + CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); + HttpClientUtils.closeQuietly(response); + Mockito.verify(response).getEntity(); + Mockito.verify(response).close(); + } + + @Test + public void testCloseQuietlyCloseableResponseEntityNonStreaming() throws Exception { + CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + Mockito.when(response.getEntity()).thenReturn(entity); + Mockito.when(entity.isStreaming()).thenReturn(false); + HttpClientUtils.closeQuietly(response); + Mockito.verify(entity, Mockito.never()).getContent(); + Mockito.verify(response).close(); + } + + @Test + public void testCloseQuietlyCloseableResponseEntity() throws Exception { + CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + InputStream instream = Mockito.mock(InputStream.class); + Mockito.when(response.getEntity()).thenReturn(entity); + Mockito.when(entity.isStreaming()).thenReturn(true); + Mockito.when(entity.getContent()).thenReturn(instream); + HttpClientUtils.closeQuietly(response); + Mockito.verify(instream).close(); + Mockito.verify(response).close(); + } + + @Test + public void testCloseQuietlyCloseableResponseIgnoreIOError() throws Exception { + CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + InputStream instream = Mockito.mock(InputStream.class); + Mockito.when(response.getEntity()).thenReturn(entity); + Mockito.when(entity.getContent()).thenReturn(instream); + Mockito.doThrow(new IOException()).when(instream).close(); + HttpClientUtils.closeQuietly(response); + } + + @Test + public void testCloseQuietlyHttpClientNull() throws Exception { + CloseableHttpClient httpclient = null; + HttpClientUtils.closeQuietly(httpclient); + } + + @Test + public void testCloseQuietlyHttpClient() throws Exception { + CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class); + HttpClientUtils.closeQuietly(httpclient); + Mockito.verify(httpclient).close(); + } + + @Test + public void testCloseQuietlyCloseableHttpClientIgnoreIOError() throws Exception { + CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class); + Mockito.doThrow(new IOException()).when(httpclient).close(); + HttpClientUtils.closeQuietly(httpclient); + } + }