some tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913394 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2023-10-27 16:48:34 +00:00
parent 4cff905375
commit 0d1644282f
4 changed files with 73 additions and 50 deletions

View File

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

View File

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

View File

@ -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<XSLFShape, XSLFTextParagraph> {
}
}
@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<XSLFShape, XSLFTextParagraph> show) throws IOException {
return writeOutAndReadBack((XMLSlideShow) show);
}
}

View File

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