|
24 | 24 |
|
25 | 25 | import java.io.ByteArrayInputStream; |
26 | 26 | import java.io.IOException; |
| 27 | +import java.io.InputStream; |
27 | 28 |
|
28 | 29 | import org.apache.commons.io.IOUtils; |
29 | 30 | import org.apache.commons.io.input.ObservableInputStream.Observer; |
@@ -127,23 +128,52 @@ public long getFinishedCount() { |
127 | 128 |
|
128 | 129 | } |
129 | 130 |
|
| 131 | + private ObservableInputStream brokenObservableInputStream() { |
| 132 | + return new ObservableInputStream(BrokenInputStream.INSTANCE); |
| 133 | + } |
| 134 | + |
| 135 | + private InputStream createInputStream() { |
| 136 | + final byte[] buffer = MessageDigestInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE); |
| 137 | + return new ObservableInputStream(new ByteArrayInputStream(buffer)); |
| 138 | + } |
| 139 | + |
| 140 | + @SuppressWarnings("resource") |
| 141 | + @Test |
| 142 | + public void testAvailableAfterClose() throws Exception { |
| 143 | + final InputStream shadow; |
| 144 | + try (InputStream in = createInputStream()) { |
| 145 | + assertTrue(in.available() > 0); |
| 146 | + shadow = in; |
| 147 | + } |
| 148 | + assertEquals(0, shadow.available()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testAvailableAfterOpen() throws Exception { |
| 153 | + try (InputStream in = createInputStream()) { |
| 154 | + assertTrue(in.available() > 0); |
| 155 | + assertNotEquals(IOUtils.EOF, in.read()); |
| 156 | + assertTrue(in.available() > 0); |
| 157 | + } |
| 158 | + } |
| 159 | + |
130 | 160 | @Test |
131 | 161 | public void testBrokenInputStreamRead() throws IOException { |
132 | | - try (ObservableInputStream ois = new ObservableInputStream(BrokenInputStream.INSTANCE)) { |
| 162 | + try (ObservableInputStream ois = brokenObservableInputStream()) { |
133 | 163 | assertThrows(IOException.class, ois::read); |
134 | 164 | } |
135 | 165 | } |
136 | 166 |
|
137 | 167 | @Test |
138 | 168 | public void testBrokenInputStreamReadBuffer() throws IOException { |
139 | | - try (ObservableInputStream ois = new ObservableInputStream(BrokenInputStream.INSTANCE)) { |
| 169 | + try (ObservableInputStream ois = brokenObservableInputStream()) { |
140 | 170 | assertThrows(IOException.class, () -> ois.read(new byte[1])); |
141 | 171 | } |
142 | 172 | } |
143 | 173 |
|
144 | 174 | @Test |
145 | 175 | public void testBrokenInputStreamReadSubBuffer() throws IOException { |
146 | | - try (ObservableInputStream ois = new ObservableInputStream(BrokenInputStream.INSTANCE)) { |
| 176 | + try (ObservableInputStream ois = brokenObservableInputStream()) { |
147 | 177 | assertThrows(IOException.class, () -> ois.read(new byte[2], 0, 1)); |
148 | 178 | } |
149 | 179 | } |
@@ -277,10 +307,8 @@ private void testNotificationCallbacks(final int bufferSize) throws IOException |
277 | 307 | final byte[] buffer = IOUtils.byteArray(); |
278 | 308 | final LengthObserver lengthObserver = new LengthObserver(); |
279 | 309 | final MethodCountObserver methodCountObserver = new MethodCountObserver(); |
280 | | - try (ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer), |
281 | | - lengthObserver, methodCountObserver)) { |
282 | | - assertEquals(IOUtils.DEFAULT_BUFFER_SIZE, |
283 | | - IOUtils.copy(ois, NullOutputStream.INSTANCE, bufferSize)); |
| 310 | + try (ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer), lengthObserver, methodCountObserver)) { |
| 311 | + assertEquals(IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.copy(ois, NullOutputStream.INSTANCE, bufferSize)); |
284 | 312 | } |
285 | 313 | assertEquals(IOUtils.DEFAULT_BUFFER_SIZE, lengthObserver.getTotal()); |
286 | 314 | assertEquals(1, methodCountObserver.getClosedCount()); |
|
0 commit comments