From aa7eee178ca62d1006b6ccc771bc1814b9c90acd Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 21 Jul 2022 21:40:32 +0000 Subject: [PATCH] add tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902927 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/TestXWPFDocument.java | 20 ++++++++++++++++--- .../xwpf/usermodel/TestXWPFPictureData.java | 12 +++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java index c23e53b063..8d3fe70ae3 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -32,6 +33,7 @@ import java.util.List; import java.util.Optional; import org.apache.poi.POIDataSamples; +import org.apache.poi.common.usermodel.PictureType; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.POIXMLProperties; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -153,9 +155,21 @@ public final class TestXWPFDocument { assertNotNull(relationById); byte[] newJpeg = relationById.getData(); assertEquals(newJpeg.length, jpeg.length); - for (int i = 0; i < jpeg.length; i++) { - assertEquals(newJpeg[i], jpeg[i]); - } + assertArrayEquals(jpeg, newJpeg); + } + } + + @Test + void testAddPicture2() throws IOException, InvalidFormatException { + try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) { + byte[] data = XWPFTestDataSamples.getImage("nature1.png"); + String relationId = doc.addPictureData(data, PictureType.PNG); + + XWPFPictureData relationById = (XWPFPictureData) doc.getRelationById(relationId); + assertNotNull(relationById); + byte[] newData = relationById.getData(); + assertEquals(newData.length, data.length); + assertArrayEquals(data, newData); } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java index bd9ee097ef..926ea82868 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java @@ -27,6 +27,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.List; +import org.apache.poi.common.usermodel.PictureType; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.xssf.usermodel.XSSFRelation; @@ -59,6 +60,17 @@ class TestXWPFPictureData { assertNotNull(pict); assertEquals("jpeg", pict.suggestFileExtension()); assertArrayEquals(pictureData, pict.getData()); + + byte[] pictureData2 = XWPFTestDataSamples.getImage("nature1.png"); + + String relationId2 = sampleDoc.addPictureData(pictureData2, PictureType.PNG); + assertNotEquals(relationId, relationId2); + // picture list was updated + assertEquals(num + 2, pictures.size()); + XWPFPictureData pict2 = (XWPFPictureData) sampleDoc.getRelationById(relationId2); + assertNotNull(pict2); + assertEquals("png", pict2.suggestFileExtension()); + assertArrayEquals(pictureData2, pict2.getData()); } } -- 2.39.5