From f64524916d449d37350e0763e4f86edc239bbd34 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 26 Oct 2023 14:59:54 +0000 Subject: [PATCH] [bug-67579] test that we do not close input stream git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913350 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/XSSFTestDataSamples.java | 3 +- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java index 362b7039c8..22a3e9e15d 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java @@ -52,8 +52,7 @@ public class XSSFTestDataSamples { } } public static XSSFWorkbook openSampleWorkbook(String sampleName) { - InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName); - try { + try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName)) { return new XSSFWorkbook(is); } catch (IOException e) { throw new RuntimeException(e); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 9e1a7cc0f3..2641862e74 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -80,6 +80,7 @@ import java.io.ByteArrayInputStream; 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.nio.charset.StandardCharsets; @@ -1447,6 +1448,18 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { } } + @Test + void testWorkbookCloseDoesNotCloseInputStream() throws Exception { + try (WrappedStream stream = new WrappedStream( + HSSFTestDataSamples.openSampleFileStream("github-321.xlsx"))) { + try (XSSFWorkbook wb = new XSSFWorkbook(stream)) { + XSSFSheet xssfSheet = wb.getSheetAt(0); + assertNotNull(xssfSheet); + } + assertFalse(stream.isClosed(), "stream should noy be closed by XSSFWorkbook"); + } + } + @Test void readFromZipStream() throws IOException { File tempFile = TempFile.createTempFile("poitest", ".zip"); @@ -1492,4 +1505,21 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { return new CellReference(cell).formatAsString(); } + 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