From 0d1644282fbc43167b626b84e7978de53a336b2c Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 27 Oct 2023 16:48:34 +0000 Subject: [PATCH] some tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913394 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ooxml/TrackingInputStream.java | 40 +++++++++++++++++++ .../apache/poi/openxml4j/TestOPCPackage.java | 33 ++------------- .../poi/xslf/usermodel/TestXMLSlideShow.java | 27 +++++++++++++ .../poi/xssf/usermodel/TestXSSFWorkbook.java | 23 ++--------- 4 files changed, 73 insertions(+), 50 deletions(-) create mode 100644 poi-ooxml/src/test/java/org/apache/poi/ooxml/TrackingInputStream.java diff --git a/poi-ooxml/src/test/java/org/apache/poi/ooxml/TrackingInputStream.java b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TrackingInputStream.java new file mode 100644 index 0000000000..3d73e160a9 --- /dev/null +++ b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TrackingInputStream.java @@ -0,0 +1,40 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ooxml; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +public final class TrackingInputStream extends FilterInputStream { + private boolean closed; + + public TrackingInputStream(InputStream stream) { + super(stream); + } + + @Override + public void close() throws IOException { + super.close(); + closed = true; + } + + public boolean isClosed() { + return closed; + } +} diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/TestOPCPackage.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/TestOPCPackage.java index cbea4f0297..3a2d9bdcd3 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/TestOPCPackage.java +++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/TestOPCPackage.java @@ -18,26 +18,17 @@ package org.apache.poi.openxml4j; import org.apache.poi.hssf.HSSFTestDataSamples; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.ooxml.TrackingInputStream; import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.openxml4j.util.ZipSecureFile; -import org.apache.poi.xssf.usermodel.TestXSSFWorkbook; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.jupiter.api.Test; -import java.io.File; -import java.io.FilterInputStream; -import java.io.IOException; -import java.io.InputStream; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; class TestOPCPackage { @Test void testPackageCloseClosesInputStream() throws Exception { - try (WrappedStream stream = new WrappedStream( + try (TrackingInputStream stream = new TrackingInputStream( HSSFTestDataSamples.openSampleFileStream("HeaderFooterComplexFormats.xlsx"))) { try (OPCPackage opcPackage = OPCPackage.open(stream)) { assertFalse(opcPackage.isClosed()); @@ -48,7 +39,7 @@ class TestOPCPackage { @Test void testPackageCloseDoesNptCloseInputStream() throws Exception { - try (WrappedStream stream = new WrappedStream( + try (TrackingInputStream stream = new TrackingInputStream( HSSFTestDataSamples.openSampleFileStream("HeaderFooterComplexFormats.xlsx"))) { try (OPCPackage opcPackage = OPCPackage.open(stream, false)) { assertFalse(opcPackage.isClosed()); @@ -57,22 +48,4 @@ class TestOPCPackage { } } - 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; - } - } - } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java index 295767a75c..7d0dbd567f 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java @@ -25,7 +25,10 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.io.InputStream; +import org.apache.poi.POIDataSamples; +import org.apache.poi.ooxml.TrackingInputStream; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.sl.usermodel.BaseTestSlideShow; @@ -180,8 +183,32 @@ class TestXMLSlideShow extends BaseTestSlideShow { } } + @Test + void testInputStreamClosed() throws Exception { + try (TrackingInputStream stream = new TrackingInputStream( + POIDataSamples.getSlideShowInstance().openResourceAsStream("45545_Comment.pptx"))) { + try (XMLSlideShow xmlComments = new XMLSlideShow(stream)) { + assertFalse(xmlComments.getSlides().isEmpty()); + } + assertTrue(stream.isClosed(), "stream was closed?"); + } + } + + @Test + void testInputStreamNotClosedWhenOptionUsed() throws Exception { + try (TrackingInputStream stream = new TrackingInputStream( + POIDataSamples.getSlideShowInstance().openResourceAsStream("45545_Comment.pptx"))) { + try (XMLSlideShow xmlComments = new XMLSlideShow(stream, false)) { + assertFalse(xmlComments.getSlides().isEmpty()); + } + assertFalse(stream.isClosed(), "stream was not closed?"); + } + } + @Override public XMLSlideShow reopen(SlideShow show) throws IOException { return writeOutAndReadBack((XMLSlideShow) show); } + + } 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 7d6d7b668b..475cc73c95 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 @@ -26,6 +26,7 @@ import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.POIDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.ooxml.POIXMLProperties; +import org.apache.poi.ooxml.TrackingInputStream; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.ContentTypes; import org.apache.poi.openxml4j.opc.OPCPackage; @@ -80,7 +81,6 @@ 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; @@ -1450,7 +1450,7 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { @Test void testWorkbookCloseClosesInputStream() throws Exception { - try (WrappedStream stream = new WrappedStream( + try (TrackingInputStream stream = new TrackingInputStream( HSSFTestDataSamples.openSampleFileStream("github-321.xlsx"))) { try (XSSFWorkbook wb = new XSSFWorkbook(stream)) { XSSFSheet xssfSheet = wb.getSheetAt(0); @@ -1462,7 +1462,7 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { @Test void testWorkbookCloseCanBeStoppedFromClosingInputStream() throws Exception { - try (WrappedStream stream = new WrappedStream( + try (TrackingInputStream stream = new TrackingInputStream( HSSFTestDataSamples.openSampleFileStream("github-321.xlsx"))) { // uses new constructor, available since POI 5.2.5 try (XSSFWorkbook wb = new XSSFWorkbook(stream, false)) { @@ -1519,21 +1519,4 @@ 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