Skip to content

Commit 933e3c4

Browse files
committed
Add BOMInputStreamTest.testCloseHandleIOException()
1 parent b89b74c commit 933e3c4

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/test/java/org/apache/commons/io/input/AutoCloseInputStreamTest.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
23-
import static org.mockito.Mockito.spy;
24-
import static org.mockito.Mockito.verify;
2523

2624
import java.io.ByteArrayInputStream;
2725
import java.io.IOException;
@@ -88,17 +86,9 @@ public void testClose() throws IOException {
8886
assertTrue(stream.isClosed(), "closed");
8987
}
9088

91-
@SuppressWarnings("resource")
9289
@Test
9390
public void testCloseHandleIOException() throws IOException {
94-
final IOException exception = new IOException();
95-
@SuppressWarnings({ "deprecation" })
96-
final ProxyInputStream inputStream = AutoCloseInputStream.builder().setInputStream(new BrokenInputStream(exception)).get();
97-
assertFalse(inputStream.isClosed(), "closed");
98-
final ProxyInputStream spy = spy(inputStream);
99-
assertThrows(IOException.class, spy::close);
100-
verify(spy).handleIOException(exception);
101-
assertFalse(spy.isClosed(), "closed");
91+
ProxyInputStreamTest.testCloseHandleIOException(AutoCloseInputStream.builder());
10292
}
10393

10494
@Test

src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ public void testClose() throws Exception {
262262
}
263263
}
264264

265+
@Test
266+
public void testCloseHandleIOException() throws IOException {
267+
ProxyInputStreamTest.testCloseHandleIOException(BOMInputStream.builder());
268+
}
269+
265270
@Test
266271
public void testEmptyBufferWithBOM() throws Exception {
267272
final byte[] data = {};

src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertFalse;
2223
import static org.junit.jupiter.api.Assertions.assertSame;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
import static org.mockito.Mockito.spy;
26+
import static org.mockito.Mockito.verify;
2327

2428
import java.io.ByteArrayInputStream;
2529
import java.io.IOException;
@@ -28,6 +32,7 @@
2832
import java.util.Arrays;
2933

3034
import org.apache.commons.io.IOUtils;
35+
import org.apache.commons.io.build.AbstractStreamBuilder;
3136
import org.junit.jupiter.api.Test;
3237

3338
/**
@@ -48,6 +53,18 @@ void setIn(final InputStream proxy) {
4853
}
4954
}
5055

56+
@SuppressWarnings("resource")
57+
static <T, B extends AbstractStreamBuilder<T, B>> void testCloseHandleIOException(final AbstractStreamBuilder<T, B> builder) throws IOException {
58+
final IOException exception = new IOException();
59+
@SuppressWarnings({ "deprecation" })
60+
final ProxyInputStream inputStream = (ProxyInputStream) builder.setInputStream(new BrokenInputStream(exception)).get();
61+
assertFalse(inputStream.isClosed(), "closed");
62+
final ProxyInputStream spy = spy(inputStream);
63+
assertThrows(IOException.class, spy::close);
64+
verify(spy).handleIOException(exception);
65+
assertFalse(spy.isClosed(), "closed");
66+
}
67+
5168
@SuppressWarnings({ "resource", "unused" }) // For subclasses
5269
protected T createFixture() throws IOException {
5370
return (T) new ProxyInputStreamFixture(createProxySource());

0 commit comments

Comments
 (0)