Skip to content

Commit

Permalink
Add BOMInputStreamTest.testCloseHandleIOException()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jul 14, 2024
1 parent b89b74c commit 933e3c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -88,17 +86,9 @@ public void testClose() throws IOException {
assertTrue(stream.isClosed(), "closed");
}

@SuppressWarnings("resource")
@Test
public void testCloseHandleIOException() throws IOException {
final IOException exception = new IOException();
@SuppressWarnings({ "deprecation" })
final ProxyInputStream inputStream = AutoCloseInputStream.builder().setInputStream(new BrokenInputStream(exception)).get();
assertFalse(inputStream.isClosed(), "closed");
final ProxyInputStream spy = spy(inputStream);
assertThrows(IOException.class, spy::close);
verify(spy).handleIOException(exception);
assertFalse(spy.isClosed(), "closed");
ProxyInputStreamTest.testCloseHandleIOException(AutoCloseInputStream.builder());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ public void testClose() throws Exception {
}
}

@Test
public void testCloseHandleIOException() throws IOException {
ProxyInputStreamTest.testCloseHandleIOException(BOMInputStream.builder());
}

@Test
public void testEmptyBufferWithBOM() throws Exception {
final byte[] data = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -28,6 +32,7 @@
import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.build.AbstractStreamBuilder;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -48,6 +53,18 @@ void setIn(final InputStream proxy) {
}
}

@SuppressWarnings("resource")
static <T, B extends AbstractStreamBuilder<T, B>> void testCloseHandleIOException(final AbstractStreamBuilder<T, B> builder) throws IOException {
final IOException exception = new IOException();
@SuppressWarnings({ "deprecation" })
final ProxyInputStream inputStream = (ProxyInputStream) builder.setInputStream(new BrokenInputStream(exception)).get();
assertFalse(inputStream.isClosed(), "closed");
final ProxyInputStream spy = spy(inputStream);
assertThrows(IOException.class, spy::close);
verify(spy).handleIOException(exception);
assertFalse(spy.isClosed(), "closed");
}

@SuppressWarnings({ "resource", "unused" }) // For subclasses
protected T createFixture() throws IOException {
return (T) new ProxyInputStreamFixture(createProxySource());
Expand Down

0 comments on commit 933e3c4

Please sign in to comment.