1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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")
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).close();
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 }