From ef743728ab7fc26e1ee1b6a32fa6898357445e7b Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 26 Oct 2023 20:03:33 +0000 Subject: [PATCH] [bug-67579] test OPCPackage git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913368 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/openxml4j/TestOPCPackage.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 poi-ooxml/src/test/java/org/apache/poi/openxml4j/TestOPCPackage.java 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 new file mode 100644 index 0000000000..cbea4f0297 --- /dev/null +++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/TestOPCPackage.java @@ -0,0 +1,78 @@ +/* ==================================================================== + 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.openxml4j; + +import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +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( + HSSFTestDataSamples.openSampleFileStream("HeaderFooterComplexFormats.xlsx"))) { + try (OPCPackage opcPackage = OPCPackage.open(stream)) { + assertFalse(opcPackage.isClosed()); + } + assertTrue(stream.isClosed(), "stream should be closed by OPCPackage"); + } + } + + @Test + void testPackageCloseDoesNptCloseInputStream() throws Exception { + try (WrappedStream stream = new WrappedStream( + HSSFTestDataSamples.openSampleFileStream("HeaderFooterComplexFormats.xlsx"))) { + try (OPCPackage opcPackage = OPCPackage.open(stream, false)) { + assertFalse(opcPackage.isClosed()); + } + assertFalse(stream.isClosed(), "stream should not be closed by OPCPackage"); + } + } + + 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