From: PJ Fanning Date: Sun, 17 Oct 2021 10:24:03 +0000 (+0000) Subject: extra tests for Zip package parts backed by temp files X-Git-Tag: REL_5_2_0~366 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b20897a996059e6df7d651ff28361a0c22c7ef1d;p=poi.git extra tests for Zip package parts backed by temp files git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894313 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java index a64326c32a..1d6bc25a34 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java @@ -62,7 +62,7 @@ public final class EncryptedTempFilePackagePart extends PackagePart { */ public EncryptedTempFilePackagePart(OPCPackage pack, PackagePartName partName, String contentType) throws InvalidFormatException, IOException { - this(pack, partName, contentType, false); + this(pack, partName, contentType, true); } /** diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java index d7f7a2dcf7..fd86f6ee8c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java @@ -57,7 +57,7 @@ public final class MemoryPackagePart extends PackagePart { */ public MemoryPackagePart(OPCPackage pack, PackagePartName partName, String contentType) throws InvalidFormatException { - super(pack, partName, contentType); + this(pack, partName, contentType, true); } /** diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java index f3e1fc157f..e3ea42c5d3 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java @@ -61,7 +61,7 @@ public final class TempFilePackagePart extends PackagePart { */ public TempFilePackagePart(OPCPackage pack, PackagePartName partName, String contentType) throws InvalidFormatException, IOException { - this(pack, partName, contentType, false); + this(pack, partName, contentType, true); } /** diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java index 064d2d83c1..dc5d2bf09d 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java @@ -25,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertSame; import java.io.IOException; import java.util.List; +import org.apache.poi.openxml4j.opc.ZipPackage; import org.apache.poi.ss.usermodel.BaseTestPicture; import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType; import org.apache.poi.ss.usermodel.Drawing; @@ -105,50 +106,73 @@ public final class TestXSSFPicture extends BaseTestPicture { } /** - * same image refrerred by mulitple sheets + * same image referred to by multiple sheets */ @Test - void multiRelationShips() throws IOException { - try (XSSFWorkbook wb1 = new XSSFWorkbook()) { - byte[] pic1Data = "test jpeg data".getBytes(LocaleUtil.CHARSET_1252); - byte[] pic2Data = "test png data".getBytes(LocaleUtil.CHARSET_1252); + void multiRelationships() throws IOException { + multiRelationships(false, false); + } - List pictures = wb1.getAllPictures(); - assertEquals(0, pictures.size()); + @Test + void multiRelationshipsWithTempFileParts() throws IOException { + multiRelationships(true, false); + } + + @Test + void multiRelationshipsWithEncryptedTempFileParts() throws IOException { + multiRelationships(true, true); + } + + private void multiRelationships(boolean tempFileParts, boolean encrypt) throws IOException { + final boolean originalTempFileSetting = ZipPackage.useTempFilePackageParts(); + final boolean originalEncryptSetting = ZipPackage.encryptTempFilePackageParts(); + try { + ZipPackage.setUseTempFilePackageParts(tempFileParts); + ZipPackage.setEncryptTempFilePackageParts(encrypt); + try (XSSFWorkbook wb1 = new XSSFWorkbook()) { + byte[] pic1Data = "test jpeg data".getBytes(LocaleUtil.CHARSET_1252); + byte[] pic2Data = "test png data".getBytes(LocaleUtil.CHARSET_1252); - int pic1 = wb1.addPicture(pic1Data, XSSFWorkbook.PICTURE_TYPE_JPEG); - int pic2 = wb1.addPicture(pic2Data, XSSFWorkbook.PICTURE_TYPE_PNG); + List pictures = wb1.getAllPictures(); + assertEquals(0, pictures.size()); - XSSFSheet sheet1 = wb1.createSheet(); - XSSFDrawing drawing1 = sheet1.createDrawingPatriarch(); - XSSFPicture shape1 = drawing1.createPicture(new XSSFClientAnchor(), pic1); - XSSFPicture shape2 = drawing1.createPicture(new XSSFClientAnchor(), pic2); + int pic1 = wb1.addPicture(pic1Data, XSSFWorkbook.PICTURE_TYPE_JPEG); + int pic2 = wb1.addPicture(pic2Data, XSSFWorkbook.PICTURE_TYPE_PNG); - XSSFSheet sheet2 = wb1.createSheet(); - XSSFDrawing drawing2 = sheet2.createDrawingPatriarch(); - XSSFPicture shape3 = drawing2.createPicture(new XSSFClientAnchor(), pic2); - XSSFPicture shape4 = drawing2.createPicture(new XSSFClientAnchor(), pic1); + XSSFSheet sheet1 = wb1.createSheet(); + XSSFDrawing drawing1 = sheet1.createDrawingPatriarch(); + XSSFPicture shape1 = drawing1.createPicture(new XSSFClientAnchor(), pic1); + XSSFPicture shape2 = drawing1.createPicture(new XSSFClientAnchor(), pic2); - assertEquals(2, pictures.size()); + XSSFSheet sheet2 = wb1.createSheet(); + XSSFDrawing drawing2 = sheet2.createDrawingPatriarch(); + XSSFPicture shape3 = drawing2.createPicture(new XSSFClientAnchor(), pic2); + XSSFPicture shape4 = drawing2.createPicture(new XSSFClientAnchor(), pic1); - try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) { - pictures = wb2.getAllPictures(); assertEquals(2, pictures.size()); - sheet1 = wb2.getSheetAt(0); - drawing1 = sheet1.createDrawingPatriarch(); - XSSFPicture shape11 = (XSSFPicture) drawing1.getShapes().get(0); - assertArrayEquals(shape1.getPictureData().getData(), shape11.getPictureData().getData()); - XSSFPicture shape22 = (XSSFPicture) drawing1.getShapes().get(1); - assertArrayEquals(shape2.getPictureData().getData(), shape22.getPictureData().getData()); - - sheet2 = wb2.getSheetAt(1); - drawing2 = sheet2.createDrawingPatriarch(); - XSSFPicture shape33 = (XSSFPicture) drawing2.getShapes().get(0); - assertArrayEquals(shape3.getPictureData().getData(), shape33.getPictureData().getData()); - XSSFPicture shape44 = (XSSFPicture) drawing2.getShapes().get(1); - assertArrayEquals(shape4.getPictureData().getData(), shape44.getPictureData().getData()); + try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) { + pictures = wb2.getAllPictures(); + assertEquals(2, pictures.size()); + + sheet1 = wb2.getSheetAt(0); + drawing1 = sheet1.createDrawingPatriarch(); + XSSFPicture shape11 = (XSSFPicture) drawing1.getShapes().get(0); + assertArrayEquals(shape1.getPictureData().getData(), shape11.getPictureData().getData()); + XSSFPicture shape22 = (XSSFPicture) drawing1.getShapes().get(1); + assertArrayEquals(shape2.getPictureData().getData(), shape22.getPictureData().getData()); + + sheet2 = wb2.getSheetAt(1); + drawing2 = sheet2.createDrawingPatriarch(); + XSSFPicture shape33 = (XSSFPicture) drawing2.getShapes().get(0); + assertArrayEquals(shape3.getPictureData().getData(), shape33.getPictureData().getData()); + XSSFPicture shape44 = (XSSFPicture) drawing2.getShapes().get(1); + assertArrayEquals(shape4.getPictureData().getData(), shape44.getPictureData().getData()); + } } + } finally { + ZipPackage.setUseTempFilePackageParts(originalTempFileSetting); + ZipPackage.setEncryptTempFilePackageParts(originalEncryptSetting); } } }