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.impl.execchain;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.net.SocketException;
33  
34  import org.apache.http.HttpEntity;
35  import org.apache.http.util.EntityUtils;
36  import org.junit.Assert;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.mockito.Mockito;
40  
41  @SuppressWarnings("boxing") // test code
42  public class TestResponseEntityWrapper {
43  
44      private InputStream instream;
45      private HttpEntity entity;
46      private ConnectionHolder connHolder;
47      private ResponseEntityProxy wrapper;
48  
49      @Before
50      public void setup() throws Exception {
51          instream = Mockito.mock(InputStream.class);
52          entity = Mockito.mock(HttpEntity.class);
53          Mockito.when(entity.getContent()).thenReturn(instream);
54          connHolder = Mockito.mock(ConnectionHolder.class);
55          wrapper = new ResponseEntityProxy(entity, connHolder);
56      }
57  
58      @Test
59      public void testReusableEntityStreamClosed() throws Exception {
60          Mockito.when(entity.isStreaming()).thenReturn(true);
61          Mockito.when(connHolder.isReusable()).thenReturn(true);
62          EntityUtils.consume(wrapper);
63  
64          Mockito.verify(instream, Mockito.times(1)).close();
65          Mockito.verify(connHolder).releaseConnection();
66      }
67  
68      @Test
69      public void testReusableEntityStreamClosedIOError() throws Exception {
70          Mockito.when(entity.isStreaming()).thenReturn(true);
71          Mockito.when(connHolder.isReusable()).thenReturn(true);
72          Mockito.doThrow(new IOException()).when(instream).close();
73          try {
74              EntityUtils.consume(wrapper);
75              Assert.fail("IOException expected");
76          } catch (final IOException ex) {
77          }
78          Mockito.verify(connHolder).abortConnection();
79      }
80  
81      @Test
82      public void testEntityStreamClosedIOErrorAlreadyReleased() throws Exception {
83          Mockito.when(entity.isStreaming()).thenReturn(true);
84          Mockito.when(connHolder.isReusable()).thenReturn(true);
85          Mockito.when(connHolder.isReleased()).thenReturn(true);
86          Mockito.doThrow(new SocketException()).when(instream).close();
87          EntityUtils.consume(wrapper);
88          Mockito.verify(connHolder).abortConnection();
89      }
90  
91      @Test
92      public void testReusableEntityWriteTo() throws Exception {
93          final OutputStream outstream = Mockito.mock(OutputStream.class);
94          Mockito.when(entity.isStreaming()).thenReturn(true);
95          Mockito.when(connHolder.isReusable()).thenReturn(true);
96          wrapper.writeTo(outstream);
97          Mockito.verify(connHolder).releaseConnection();
98      }
99  
100     @Test
101     public void testReusableEntityWriteToIOError() throws Exception {
102         final OutputStream outstream = Mockito.mock(OutputStream.class);
103         Mockito.when(entity.isStreaming()).thenReturn(true);
104         Mockito.when(connHolder.isReusable()).thenReturn(true);
105         Mockito.doThrow(new IOException()).when(entity).writeTo(outstream);
106         try {
107             wrapper.writeTo(outstream);
108             Assert.fail("IOException expected");
109         } catch (final IOException ex) {
110         }
111         Mockito.verify(connHolder, Mockito.never()).releaseConnection();
112         Mockito.verify(connHolder).abortConnection();
113     }
114 
115     @Test
116     public void testReusableEntityEndOfStream() throws Exception {
117         Mockito.when(instream.read()).thenReturn(-1);
118         Mockito.when(entity.isStreaming()).thenReturn(true);
119         Mockito.when(connHolder.isReusable()).thenReturn(true);
120         final InputStream content = wrapper.getContent();
121         Assert.assertEquals(-1, content.read());
122         Mockito.verify(instream).close();
123         Mockito.verify(connHolder).releaseConnection();
124     }
125 
126     @Test
127     public void testReusableEntityEndOfStreamIOError() throws Exception {
128         Mockito.when(instream.read()).thenReturn(-1);
129         Mockito.when(entity.isStreaming()).thenReturn(true);
130         Mockito.when(connHolder.isReusable()).thenReturn(true);
131         Mockito.doThrow(new IOException()).when(instream).close();
132         final InputStream content = wrapper.getContent();
133         try {
134             content.read();
135             Assert.fail("IOException expected");
136         } catch (final IOException ex) {
137         }
138         Mockito.verify(connHolder).abortConnection();
139     }
140 
141 }