[bug-67579] test OPCPackage

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913368 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2023-10-26 20:03:33 +00:00
parent 6e680589de
commit ef743728ab

View File

@ -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;
}
}
}