You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXWPFPictureData.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertArrayEquals;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertNull;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.util.List;
  24. import org.apache.poi.common.usermodel.PictureType;
  25. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  26. import org.apache.poi.openxml4j.opc.PackageRelationship;
  27. import org.apache.poi.xssf.usermodel.XSSFRelation;
  28. import org.apache.poi.xwpf.XWPFTestDataSamples;
  29. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  30. import org.junit.jupiter.api.Assertions;
  31. import org.junit.jupiter.api.Test;
  32. class TestXWPFPictureData {
  33. @Test
  34. void testRead() throws InvalidFormatException, IOException {
  35. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx")) {
  36. List<XWPFPictureData> pictures = sampleDoc.getAllPictures();
  37. assertEquals(5, pictures.size());
  38. String[] ext = {"wmf", "png", "emf", "emf", "jpeg"};
  39. for (int i = 0; i < pictures.size(); i++) {
  40. assertEquals(ext[i], pictures.get(i).suggestFileExtension());
  41. }
  42. int num = pictures.size();
  43. byte[] pictureData = XWPFTestDataSamples.getImage("nature1.jpg");
  44. String relationId = sampleDoc.addPictureData(pictureData, XWPFDocument.PICTURE_TYPE_JPEG);
  45. // picture list was updated
  46. assertEquals(num + 1, pictures.size());
  47. XWPFPictureData pict = (XWPFPictureData) sampleDoc.getRelationById(relationId);
  48. assertNotNull(pict);
  49. assertEquals("jpeg", pict.suggestFileExtension());
  50. assertArrayEquals(pictureData, pict.getData());
  51. byte[] pictureData2 = XWPFTestDataSamples.getImage("nature1.png");
  52. String relationId2 = sampleDoc.addPictureData(pictureData2, PictureType.PNG);
  53. assertNotEquals(relationId, relationId2);
  54. // picture list was updated
  55. assertEquals(num + 2, pictures.size());
  56. XWPFPictureData pict2 = (XWPFPictureData) sampleDoc.getRelationById(relationId2);
  57. assertNotNull(pict2);
  58. assertEquals("png", pict2.suggestFileExtension());
  59. assertArrayEquals(pictureData2, pict2.getData());
  60. }
  61. }
  62. @Test
  63. void testReadMaxSize() throws InvalidFormatException, IOException {
  64. int prev = XWPFPictureData.getMaxImageSize();
  65. try {
  66. // check for a regression in 5.2.1:
  67. // even if we set the maximum to a very high value it should not
  68. // simply allocate that much here
  69. XWPFPictureData.setMaxImageSize(Integer.MAX_VALUE-1);
  70. testRead();
  71. } finally {
  72. XWPFPictureData.setMaxImageSize(prev);
  73. }
  74. }
  75. @Test
  76. void testPictureInHeader() throws IOException {
  77. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx")) {
  78. verifyOneHeaderPicture(sampleDoc);
  79. XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
  80. verifyOneHeaderPicture(readBack);
  81. }
  82. }
  83. @Test
  84. void testCreateHeaderPicture() throws Exception {
  85. try (XWPFDocument doc = new XWPFDocument()) {
  86. // Starts with no header
  87. XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
  88. assertNull(policy);
  89. // Add a default header
  90. policy = doc.createHeaderFooterPolicy();
  91. XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
  92. header.createParagraph().createRun().setText("Hello, Header World!");
  93. header.createParagraph().createRun().setText("Paragraph 2");
  94. assertEquals(0, header.getAllPictures().size());
  95. assertEquals(2, header.getParagraphs().size());
  96. // Add a picture to the first paragraph
  97. header.getParagraphs().get(0).getRuns().get(0).addPicture(
  98. new ByteArrayInputStream(new byte[]{1, 2, 3, 4}),
  99. Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
  100. // Check
  101. verifyOneHeaderPicture(doc);
  102. // Save, re-load, re-check
  103. XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
  104. verifyOneHeaderPicture(readBack);
  105. }
  106. }
  107. private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
  108. XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  109. XWPFHeader header = policy.getDefaultHeader();
  110. List<XWPFPictureData> pictures = header.getAllPictures();
  111. assertEquals(1, pictures.size());
  112. }
  113. @Test
  114. void testNew() throws InvalidFormatException, IOException {
  115. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx")) {
  116. byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg");
  117. assertNotNull(jpegData);
  118. byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif");
  119. assertNotNull(gifData);
  120. byte[] pngData = XWPFTestDataSamples.getImage("nature1.png");
  121. assertNotNull(pngData);
  122. List<XWPFPictureData> pictures = doc.getAllPictures();
  123. assertEquals(0, pictures.size());
  124. // Document shouldn't have any image relationships
  125. assertEquals(13, doc.getPackagePart().getRelationships().size());
  126. for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
  127. assertNotEquals(XSSFRelation.IMAGE_JPEG.getRelation(), rel.getRelationshipType(), "Shouldn't have JPEG yet");
  128. }
  129. // Add the image
  130. String relationId = doc.addPictureData(jpegData, XWPFDocument.PICTURE_TYPE_JPEG);
  131. assertEquals(1, pictures.size());
  132. XWPFPictureData jpgPicData = (XWPFPictureData) doc.getRelationById(relationId);
  133. assertNotNull(jpgPicData);
  134. assertEquals("jpeg", jpgPicData.suggestFileExtension());
  135. assertArrayEquals(jpegData, jpgPicData.getData());
  136. // Ensure it now has one
  137. assertEquals(14, doc.getPackagePart().getRelationships().size());
  138. PackageRelationship jpegRel = null;
  139. for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
  140. if (rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) {
  141. assertNull(jpegRel, "Found 2 jpegs!");
  142. jpegRel = rel;
  143. }
  144. }
  145. assertNotNull(jpegRel, "JPEG Relationship not found");
  146. // Check the details
  147. assertNotNull(jpegRel);
  148. assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType());
  149. assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString());
  150. assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath());
  151. XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
  152. assertArrayEquals(jpegData, pictureDataByID.getData());
  153. // Save an re-load, check it appears
  154. try (XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
  155. assertEquals(1, docBack.getAllPictures().size());
  156. assertEquals(1, docBack.getAllPackagePictures().size());
  157. // verify the picture that we read back in
  158. pictureDataByID = docBack.getPictureDataByID(jpegRel.getId());
  159. assertArrayEquals(jpegData, pictureDataByID.getData());
  160. }
  161. }
  162. }
  163. @Test
  164. void testBug51770() throws IOException {
  165. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx")) {
  166. XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
  167. XWPFHeader header = policy.getDefaultHeader();
  168. header.getParagraphs().stream()
  169. .map(XWPFParagraph::getRuns)
  170. .flatMap(List::stream)
  171. .map(XWPFRun::getEmbeddedPictures)
  172. .flatMap(List::stream)
  173. .map(XWPFPicture::getPictureData)
  174. .forEach(Assertions::assertNull);
  175. }
  176. }
  177. }