View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.client.utils;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import org.apache.http.HttpEntity;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.client.methods.CloseableHttpResponse;
35  import org.apache.http.impl.client.CloseableHttpClient;
36  import org.junit.Test;
37  import org.mockito.Mockito;
38  
39  @SuppressWarnings("boxing") // test code
40  public class TestHttpClientUtils {
41  
42      @Test
43      public void testCloseQuietlyResponseNull() throws Exception {
44          final HttpResponse response = null;
45          HttpClientUtils.closeQuietly(response);
46      }
47  
48      @Test
49      public void testCloseQuietlyResponseEntityNull() throws Exception {
50          final HttpResponse response = Mockito.mock(HttpResponse.class);
51          HttpClientUtils.closeQuietly(response);
52          Mockito.verify(response).getEntity();
53      }
54  
55      @Test
56      public void testCloseQuietlyResponseEntityNonStreaming() throws Exception {
57          final HttpResponse response = Mockito.mock(HttpResponse.class);
58          final HttpEntity entity = Mockito.mock(HttpEntity.class);
59          Mockito.when(response.getEntity()).thenReturn(entity);
60          Mockito.when(entity.isStreaming()).thenReturn(Boolean.FALSE);
61          HttpClientUtils.closeQuietly(response);
62          Mockito.verify(entity, Mockito.never()).getContent();
63      }
64  
65      @Test
66      public void testCloseQuietlyResponseEntity() throws Exception {
67          final HttpResponse response = Mockito.mock(HttpResponse.class);
68          final HttpEntity entity = Mockito.mock(HttpEntity.class);
69          final InputStream inStream = Mockito.mock(InputStream.class);
70          Mockito.when(response.getEntity()).thenReturn(entity);
71          Mockito.when(entity.isStreaming()).thenReturn(Boolean.TRUE);
72          Mockito.when(entity.getContent()).thenReturn(inStream);
73          HttpClientUtils.closeQuietly(response);
74          Mockito.verify(inStream).close();
75      }
76  
77      @Test
78      public void testCloseQuietlyResponseIgnoreIOError() throws Exception {
79          final HttpResponse response = Mockito.mock(HttpResponse.class);
80          final HttpEntity entity = Mockito.mock(HttpEntity.class);
81          final InputStream inStream = Mockito.mock(InputStream.class);
82          Mockito.when(response.getEntity()).thenReturn(entity);
83          Mockito.when(entity.getContent()).thenReturn(inStream);
84          Mockito.doThrow(new IOException()).when(inStream).close();
85          HttpClientUtils.closeQuietly(response);
86      }
87  
88      @Test
89      public void testCloseQuietlyCloseableResponseNull() throws Exception {
90          final CloseableHttpResponse response = null;
91          HttpClientUtils.closeQuietly(response);
92      }
93  
94      @Test
95      public void testCloseQuietlyCloseableResponseEntityNull() throws Exception {
96          final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
97          HttpClientUtils.closeQuietly(response);
98          Mockito.verify(response).getEntity();
99          Mockito.verify(response).close();
100     }
101 
102     @Test
103     public void testCloseQuietlyCloseableResponseEntityNonStreaming() throws Exception {
104         final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
105         final HttpEntity entity = Mockito.mock(HttpEntity.class);
106         Mockito.when(response.getEntity()).thenReturn(entity);
107         Mockito.when(entity.isStreaming()).thenReturn(Boolean.FALSE);
108         HttpClientUtils.closeQuietly(response);
109         Mockito.verify(entity, Mockito.never()).getContent();
110         Mockito.verify(response).close();
111     }
112 
113     @Test
114     public void testCloseQuietlyCloseableResponseEntity() throws Exception {
115         final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
116         final HttpEntity entity = Mockito.mock(HttpEntity.class);
117         final InputStream inStream = Mockito.mock(InputStream.class);
118         Mockito.when(response.getEntity()).thenReturn(entity);
119         Mockito.when(entity.isStreaming()).thenReturn(Boolean.TRUE);
120         Mockito.when(entity.getContent()).thenReturn(inStream);
121         HttpClientUtils.closeQuietly(response);
122         Mockito.verify(inStream).close();
123         Mockito.verify(response).close();
124     }
125 
126     @Test
127     public void testCloseQuietlyCloseableResponseIgnoreIOError() throws Exception {
128         final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
129         final HttpEntity entity = Mockito.mock(HttpEntity.class);
130         final InputStream inStream = Mockito.mock(InputStream.class);
131         Mockito.when(response.getEntity()).thenReturn(entity);
132         Mockito.when(entity.getContent()).thenReturn(inStream);
133         Mockito.doThrow(new IOException()).when(inStream).close();
134         HttpClientUtils.closeQuietly(response);
135     }
136 
137     @Test
138     public void testCloseQuietlyHttpClientNull() throws Exception {
139         final CloseableHttpClient httpclient = null;
140         HttpClientUtils.closeQuietly(httpclient);
141     }
142 
143     @Test
144     public void testCloseQuietlyHttpClient() throws Exception {
145         final CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class);
146         HttpClientUtils.closeQuietly(httpclient);
147         Mockito.verify(httpclient).close();
148     }
149 
150     @Test
151     public void testCloseQuietlyCloseableHttpClientIgnoreIOError() throws Exception {
152         final CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class);
153         Mockito.doThrow(new IOException()).when(httpclient).close();
154         HttpClientUtils.closeQuietly(httpclient);
155     }
156 
157 }