From bcfd81f489670ace3304ee68934f24143c9f517d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 26 Oct 2023 15:19:42 +0000 Subject: [PATCH] [bug-67579] test that we close input stream git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913352 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/TestHSSFWorkbook.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 94f3c4fa78..9cba23ff82 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -32,6 +32,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -1231,4 +1232,34 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { } } } + + @Test + void testWorkbookCloseClosesInputStream() throws Exception { + try (WrappedStream stream = new WrappedStream( + HSSFTestDataSamples.openSampleFileStream("49423.xls"))) { + try (HSSFWorkbook wb = new HSSFWorkbook(stream)) { + HSSFSheet hssfSheet = wb.getSheetAt(0); + assertNotNull(hssfSheet); + } + assertTrue(stream.isClosed(), "stream should be closed by HSSFWorkbook"); + } + } + + private static class WrappedStream extends FilterInputStream { + private boolean closed; + + WrappedStream(InputStream stream) { + super(stream); + } + + @Override + public void close() throws IOException { + super.close(); + closed = true; + } + + boolean isClosed() { + return closed; + } + } } -- 2.39.5