summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2023-10-26 14:59:54 +0000
committerPJ Fanning <fanningpj@apache.org>2023-10-26 14:59:54 +0000
commitf64524916d449d37350e0763e4f86edc239bbd34 (patch)
tree184e654f9bc7432a0be50c0170666b787ca89f02
parentfee050b391eb9c9eec493aab62f3be9bc078f3aa (diff)
downloadpoi-f64524916d449d37350e0763e4f86edc239bbd34.tar.gz
poi-f64524916d449d37350e0763e4f86edc239bbd34.zip
[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
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java3
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java30
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;
@@ -1448,6 +1449,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");
try {
@@ -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;
+ }
+ }
}