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.hc.core5.http.io;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.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      @BeforeEach
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.any())).thenReturn(Boolean.TRUE);
54  
55          eofstream.close();
56  
57          Assertions.assertTrue(eofstream.isSelfClosed());
58          Assertions.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.any())).thenThrow(new IOException());
69  
70          Assertions.assertThrows(IOException.class, () -> eofstream.close());
71          Assertions.assertTrue(eofstream.isSelfClosed());
72          Assertions.assertNull(eofstream.getWrappedStream());
73  
74          Mockito.verify(eofwatcher).streamClosed(inStream);
75      }
76  
77      @Test
78      public void testReleaseConnection() throws Exception {
79          Mockito.when(eofwatcher.streamClosed(Mockito.any())).thenReturn(Boolean.TRUE);
80  
81          eofstream.close();
82  
83          Assertions.assertTrue(eofstream.isSelfClosed());
84          Assertions.assertNull(eofstream.getWrappedStream());
85  
86          Mockito.verify(inStream, Mockito.times(1)).close();
87          Mockito.verify(eofwatcher).streamClosed(inStream);
88  
89          eofstream.close();
90      }
91  
92      @Test
93      public void testAbortConnection() throws Exception {
94          Mockito.when(eofwatcher.streamAbort(Mockito.any())).thenReturn(Boolean.TRUE);
95  
96          eofstream.abort();
97  
98          Assertions.assertTrue(eofstream.isSelfClosed());
99          Assertions.assertNull(eofstream.getWrappedStream());
100 
101         Mockito.verify(inStream, Mockito.times(1)).close();
102         Mockito.verify(eofwatcher).streamAbort(inStream);
103 
104         eofstream.abort();
105     }
106 
107     @Test
108     public void testAbortConnectionIOError() throws Exception {
109         Mockito.when(eofwatcher.streamAbort(Mockito.any())).thenThrow(new IOException());
110 
111         Assertions.assertThrows(IOException.class, () -> eofstream.abort());
112         Assertions.assertTrue(eofstream.isSelfClosed());
113         Assertions.assertNull(eofstream.getWrappedStream());
114 
115         Mockito.verify(eofwatcher).streamAbort(inStream);
116     }
117 
118     @Test
119     public void testRead() throws Exception {
120         Mockito.when(eofwatcher.eofDetected(Mockito.any())).thenReturn(Boolean.TRUE);
121         Mockito.when(inStream.read()).thenReturn(0, -1);
122 
123         Assertions.assertEquals(0, eofstream.read());
124 
125         Assertions.assertFalse(eofstream.isSelfClosed());
126         Assertions.assertNotNull(eofstream.getWrappedStream());
127 
128         Mockito.verify(eofwatcher, Mockito.never()).eofDetected(inStream);
129 
130         Assertions.assertEquals(-1, eofstream.read());
131 
132         Assertions.assertFalse(eofstream.isSelfClosed());
133         Assertions.assertNull(eofstream.getWrappedStream());
134 
135         Mockito.verify(inStream, Mockito.times(1)).close();
136         Mockito.verify(eofwatcher).eofDetected(inStream);
137 
138         Assertions.assertEquals(-1, eofstream.read());
139     }
140 
141     @Test
142     public void testReadIOError() throws Exception {
143         Mockito.when(eofwatcher.eofDetected(Mockito.any())).thenReturn(Boolean.TRUE);
144         Mockito.when(inStream.read()).thenThrow(new IOException());
145 
146         Assertions.assertThrows(IOException.class, () -> eofstream.read());
147         Assertions.assertFalse(eofstream.isSelfClosed());
148         Assertions.assertNull(eofstream.getWrappedStream());
149 
150         Mockito.verify(eofwatcher).streamAbort(inStream);
151     }
152 
153     @Test
154     public void testReadByteArray() throws Exception {
155         Mockito.when(eofwatcher.eofDetected(Mockito.any())).thenReturn(Boolean.TRUE);
156         Mockito.when(inStream.read(Mockito.any(), Mockito.anyInt(), Mockito.anyInt()))
157             .thenReturn(1, -1);
158 
159         final byte[] tmp = new byte[1];
160 
161         Assertions.assertEquals(1, eofstream.read(tmp));
162 
163         Assertions.assertFalse(eofstream.isSelfClosed());
164         Assertions.assertNotNull(eofstream.getWrappedStream());
165 
166         Mockito.verify(eofwatcher, Mockito.never()).eofDetected(inStream);
167 
168         Assertions.assertEquals(-1, eofstream.read(tmp));
169 
170         Assertions.assertFalse(eofstream.isSelfClosed());
171         Assertions.assertNull(eofstream.getWrappedStream());
172 
173         Mockito.verify(inStream, Mockito.times(1)).close();
174         Mockito.verify(eofwatcher).eofDetected(inStream);
175 
176         Assertions.assertEquals(-1, eofstream.read(tmp));
177     }
178 
179     @Test
180     public void testReadByteArrayIOError() throws Exception {
181         Mockito.when(eofwatcher.eofDetected(Mockito.any())).thenReturn(Boolean.TRUE);
182         Mockito.when(inStream.read(Mockito.any(), Mockito.anyInt(), Mockito.anyInt()))
183             .thenThrow(new IOException());
184 
185         final byte[] tmp = new byte[1];
186         Assertions.assertThrows(IOException.class, () -> eofstream.read(tmp));
187         Assertions.assertFalse(eofstream.isSelfClosed());
188         Assertions.assertNull(eofstream.getWrappedStream());
189 
190         Mockito.verify(eofwatcher).streamAbort(inStream);
191     }
192 
193     @Test
194     public void testReadAfterAbort() throws Exception {
195         Mockito.when(eofwatcher.streamAbort(Mockito.any())).thenReturn(Boolean.TRUE);
196 
197         eofstream.abort();
198 
199         Assertions.assertThrows(IOException.class, () -> eofstream.read());
200         final byte[] tmp = new byte[1];
201         Assertions.assertThrows(IOException.class, () -> eofstream.read(tmp));
202     }
203 
204 }