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.conn;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import org.junit.Assert;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.mockito.Mockito;
36  
37  @SuppressWarnings({"boxing","static-access"}) // test code
38  public class TestEofSensorInputStream {
39  
40      private InputStream instream;
41      private EofSensorWatcher eofwatcher;
42      private EofSensorInputStream eofstream;
43  
44      @Before
45      public void setup() throws Exception {
46          instream = Mockito.mock(InputStream.class);
47          eofwatcher = Mockito.mock(EofSensorWatcher.class);
48          eofstream = new EofSensorInputStream(instream, eofwatcher);
49      }
50  
51      @Test
52      public void testClose() throws Exception {
53          Mockito.when(eofwatcher.streamClosed(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
54  
55          eofstream.close();
56  
57          Assert.assertTrue(eofstream.isSelfClosed());
58          Assert.assertNull(eofstream.getWrappedStream());
59  
60          Mockito.verify(instream, Mockito.times(1)).close();
61          Mockito.verify(eofwatcher).streamClosed(instream);
62  
63          eofstream.close();
64      }
65  
66      @Test
67      public void testCloseIOError() throws Exception {
68          Mockito.when(eofwatcher.streamClosed(Mockito.<InputStream>any())).thenThrow(new IOException());
69  
70          try {
71              eofstream.close();
72              Assert.fail("IOException expected");
73          } catch (final IOException ex) {
74          }
75          Assert.assertTrue(eofstream.isSelfClosed());
76          Assert.assertNull(eofstream.getWrappedStream());
77  
78          Mockito.verify(eofwatcher).streamClosed(instream);
79      }
80  
81      @Test
82      public void testReleaseConnection() throws Exception {
83          Mockito.when(eofwatcher.streamClosed(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
84  
85          eofstream.releaseConnection();
86  
87          Assert.assertTrue(eofstream.isSelfClosed());
88          Assert.assertNull(eofstream.getWrappedStream());
89  
90          Mockito.verify(instream, Mockito.times(1)).close();
91          Mockito.verify(eofwatcher).streamClosed(instream);
92  
93          eofstream.releaseConnection();
94      }
95  
96      @Test
97      public void testAbortConnection() throws Exception {
98          Mockito.when(eofwatcher.streamAbort(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
99  
100         eofstream.abortConnection();
101 
102         Assert.assertTrue(eofstream.isSelfClosed());
103         Assert.assertNull(eofstream.getWrappedStream());
104 
105         Mockito.verify(instream, Mockito.times(1)).close();
106         Mockito.verify(eofwatcher).streamAbort(instream);
107 
108         eofstream.abortConnection();
109     }
110 
111     @Test
112     public void testAbortConnectionIOError() throws Exception {
113         Mockito.when(eofwatcher.streamAbort(Mockito.<InputStream>any())).thenThrow(new IOException());
114 
115         try {
116             eofstream.abortConnection();
117             Assert.fail("IOException expected");
118         } catch (final IOException ex) {
119         }
120         Assert.assertTrue(eofstream.isSelfClosed());
121         Assert.assertNull(eofstream.getWrappedStream());
122 
123         Mockito.verify(eofwatcher).streamAbort(instream);
124     }
125 
126     @Test
127     public void testRead() throws Exception {
128         Mockito.when(eofwatcher.eofDetected(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
129         Mockito.when(instream.read()).thenReturn(0, -1);
130 
131         Assert.assertEquals(0, eofstream.read());
132 
133         Assert.assertFalse(eofstream.isSelfClosed());
134         Assert.assertNotNull(eofstream.getWrappedStream());
135 
136         Mockito.verify(eofwatcher, Mockito.never()).eofDetected(instream);
137 
138         Assert.assertEquals(-1, eofstream.read());
139 
140         Assert.assertFalse(eofstream.isSelfClosed());
141         Assert.assertNull(eofstream.getWrappedStream());
142 
143         Mockito.verify(instream, Mockito.times(1)).close();
144         Mockito.verify(eofwatcher).eofDetected(instream);
145 
146         Assert.assertEquals(-1, eofstream.read());
147     }
148 
149     @Test
150     public void testReadIOError() throws Exception {
151         Mockito.when(eofwatcher.eofDetected(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
152         Mockito.when(instream.read()).thenThrow(new IOException());
153 
154         try {
155             eofstream.read();
156             Assert.fail("IOException expected");
157         } catch (final IOException ex) {
158         }
159         Assert.assertFalse(eofstream.isSelfClosed());
160         Assert.assertNull(eofstream.getWrappedStream());
161 
162         Mockito.verify(eofwatcher).streamAbort(instream);
163     }
164 
165     @Test
166     public void testReadByteArray() throws Exception {
167         Mockito.when(eofwatcher.eofDetected(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
168         Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
169             .thenReturn(1, -1);
170 
171         final byte[] tmp = new byte[1];
172 
173         Assert.assertEquals(1, eofstream.read(tmp));
174 
175         Assert.assertFalse(eofstream.isSelfClosed());
176         Assert.assertNotNull(eofstream.getWrappedStream());
177 
178         Mockito.verify(eofwatcher, Mockito.never()).eofDetected(instream);
179 
180         Assert.assertEquals(-1, eofstream.read(tmp));
181 
182         Assert.assertFalse(eofstream.isSelfClosed());
183         Assert.assertNull(eofstream.getWrappedStream());
184 
185         Mockito.verify(instream, Mockito.times(1)).close();
186         Mockito.verify(eofwatcher).eofDetected(instream);
187 
188         Assert.assertEquals(-1, eofstream.read(tmp));
189     }
190 
191     @Test
192     public void testReadByteArrayIOError() throws Exception {
193         Mockito.when(eofwatcher.eofDetected(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
194         Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
195             .thenThrow(new IOException());
196 
197         final byte[] tmp = new byte[1];
198         try {
199             eofstream.read(tmp);
200             Assert.fail("IOException expected");
201         } catch (final IOException ex) {
202         }
203         Assert.assertFalse(eofstream.isSelfClosed());
204         Assert.assertNull(eofstream.getWrappedStream());
205 
206         Mockito.verify(eofwatcher).streamAbort(instream);
207     }
208 
209     @Test
210     public void testReadAfterAbort() throws Exception {
211         Mockito.when(eofwatcher.streamAbort(Mockito.<InputStream>any())).thenReturn(Boolean.TRUE);
212 
213         eofstream.abortConnection();
214 
215         try {
216             eofstream.read();
217             Assert.fail("IOException expected");
218         } catch (final IOException ex) {
219         }
220         final byte[] tmp = new byte[1];
221         try {
222             eofstream.read(tmp);
223             Assert.fail("IOException expected");
224         } catch (final IOException ex) {
225         }
226     }
227 
228 }