add tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902927 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-07-21 21:40:32 +00:00
parent 47a86037b6
commit aa7eee178c
2 changed files with 29 additions and 3 deletions

View File

@ -17,6 +17,7 @@
package org.apache.poi.xwpf.usermodel; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -32,6 +33,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.common.usermodel.PictureType;
import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLProperties; import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -153,9 +155,21 @@ public final class TestXWPFDocument {
assertNotNull(relationById); assertNotNull(relationById);
byte[] newJpeg = relationById.getData(); byte[] newJpeg = relationById.getData();
assertEquals(newJpeg.length, jpeg.length); assertEquals(newJpeg.length, jpeg.length);
for (int i = 0; i < jpeg.length; i++) { assertArrayEquals(jpeg, newJpeg);
assertEquals(newJpeg[i], jpeg[i]); }
} }
@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);
} }
} }

View File

@ -27,6 +27,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.apache.poi.common.usermodel.PictureType;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xssf.usermodel.XSSFRelation;
@ -59,6 +60,17 @@ class TestXWPFPictureData {
assertNotNull(pict); assertNotNull(pict);
assertEquals("jpeg", pict.suggestFileExtension()); assertEquals("jpeg", pict.suggestFileExtension());
assertArrayEquals(pictureData, pict.getData()); 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());
} }
} }