diff options
author | PJ Fanning <fanningpj@apache.org> | 2023-10-26 20:03:33 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2023-10-26 20:03:33 +0000 |
commit | ef743728ab7fc26e1ee1b6a32fa6898357445e7b (patch) | |
tree | 4ee4cec8025296ee8dd824b1a0ee4a1f9a0083f0 | |
parent | 6e680589def9bb574c5621f704932c49da82e73b (diff) | |
download | poi-ef743728ab7fc26e1ee1b6a32fa6898357445e7b.tar.gz poi-ef743728ab7fc26e1ee1b6a32fa6898357445e7b.zip |
[bug-67579] test OPCPackage
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913368 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/openxml4j/TestOPCPackage.java | 78 |
1 files changed, 78 insertions, 0 deletions
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; + } + } + +} |