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.

TestXSSFPictureData.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.xssf.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.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertSame;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import org.apache.poi.util.LocaleUtil;
  23. import org.apache.poi.xssf.XSSFTestDataSamples;
  24. import org.junit.jupiter.api.Test;
  25. public final class TestXSSFPictureData {
  26. @Test
  27. void testRead() throws IOException {
  28. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
  29. List<XSSFPictureData> pictures = wb.getAllPictures();
  30. //wb.getAllPictures() should return the same instance across multiple calls
  31. assertSame(pictures, wb.getAllPictures());
  32. assertEquals(5, pictures.size());
  33. String[] ext = {"jpeg", "emf", "png", "emf", "wmf"};
  34. String[] mimetype = {"image/jpeg", "image/x-emf", "image/png", "image/x-emf", "image/x-wmf"};
  35. for (int i = 0; i < pictures.size(); i++) {
  36. assertEquals(ext[i], pictures.get(i).suggestFileExtension());
  37. assertEquals(mimetype[i], pictures.get(i).getMimeType());
  38. }
  39. int num = pictures.size();
  40. byte[] pictureData = {0xA, 0xB, 0XC, 0xD, 0xE, 0xF};
  41. int idx = wb.addPicture(pictureData, XSSFWorkbook.PICTURE_TYPE_JPEG);
  42. assertEquals(num + 1, pictures.size());
  43. //idx is 0-based index in the #pictures array
  44. assertEquals(pictures.size() - 1, idx);
  45. XSSFPictureData pict = pictures.get(idx);
  46. assertEquals("jpeg", pict.suggestFileExtension());
  47. assertArrayEquals(pictureData, pict.getData());
  48. wb.close();
  49. }
  50. @Test
  51. void testNew() throws IOException {
  52. XSSFWorkbook wb = new XSSFWorkbook();
  53. XSSFSheet sheet = wb.createSheet();
  54. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  55. byte[] jpegData = "test jpeg data".getBytes(LocaleUtil.CHARSET_1252);
  56. byte[] wmfData = "test wmf data".getBytes(LocaleUtil.CHARSET_1252);
  57. byte[] pngData = "test png data".getBytes(LocaleUtil.CHARSET_1252);
  58. List<XSSFPictureData> pictures = wb.getAllPictures();
  59. assertEquals(0, pictures.size());
  60. int jpegIdx = wb.addPicture(jpegData, XSSFWorkbook.PICTURE_TYPE_JPEG);
  61. assertEquals(1, pictures.size());
  62. assertEquals("jpeg", pictures.get(jpegIdx).suggestFileExtension());
  63. assertArrayEquals(jpegData, pictures.get(jpegIdx).getData());
  64. int wmfIdx = wb.addPicture(wmfData, XSSFWorkbook.PICTURE_TYPE_WMF);
  65. assertEquals(2, pictures.size());
  66. assertEquals("wmf", pictures.get(wmfIdx).suggestFileExtension());
  67. assertArrayEquals(wmfData, pictures.get(wmfIdx).getData());
  68. int pngIdx = wb.addPicture(pngData, XSSFWorkbook.PICTURE_TYPE_PNG);
  69. assertEquals(3, pictures.size());
  70. assertEquals("png", pictures.get(pngIdx).suggestFileExtension());
  71. assertArrayEquals(pngData, pictures.get(pngIdx).getData());
  72. //TODO finish usermodel API for XSSFPicture
  73. XSSFPicture p1 = drawing.createPicture(new XSSFClientAnchor(), jpegIdx);
  74. assertNotNull(p1);
  75. XSSFPicture p2 = drawing.createPicture(new XSSFClientAnchor(), wmfIdx);
  76. assertNotNull(p2);
  77. XSSFPicture p3 = drawing.createPicture(new XSSFClientAnchor(), pngIdx);
  78. assertNotNull(p3);
  79. //check that the added pictures are accessible after write
  80. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  81. List<XSSFPictureData> pictures2 = wbBack.getAllPictures();
  82. assertEquals(3, pictures2.size());
  83. assertEquals("jpeg", pictures2.get(jpegIdx).suggestFileExtension());
  84. assertArrayEquals(jpegData, pictures2.get(jpegIdx).getData());
  85. assertEquals("wmf", pictures2.get(wmfIdx).suggestFileExtension());
  86. assertArrayEquals(wmfData, pictures2.get(wmfIdx).getData());
  87. assertEquals("png", pictures2.get(pngIdx).suggestFileExtension());
  88. assertArrayEquals(pngData, pictures2.get(pngIdx).getData());
  89. wbBack.close();
  90. wb.close();
  91. }
  92. @Test
  93. void testNewPictFormat() throws IOException {
  94. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  95. XSSFSheet sheet = wb.createSheet();
  96. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  97. byte[] pictData = "test pict data".getBytes(LocaleUtil.CHARSET_1252);
  98. List<XSSFPictureData> pictures = wb.getAllPictures();
  99. assertEquals(0, pictures.size());
  100. int pictIdx = wb.addPicture(pictData, XSSFWorkbook.PICTURE_TYPE_PICT);
  101. assertEquals(1, pictures.size());
  102. assertEquals("pict", pictures.get(pictIdx).suggestFileExtension());
  103. assertArrayEquals(pictData, pictures.get(pictIdx).getData());
  104. //TODO finish usermodel API for XSSFPicture
  105. XSSFPicture p1 = drawing.createPicture(new XSSFClientAnchor(), pictIdx);
  106. assertNotNull(p1);
  107. //check that the added pictures are accessible after write
  108. try (XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
  109. List<XSSFPictureData> pictures2 = wbBack.getAllPictures();
  110. assertEquals(1, pictures2.size());
  111. assertEquals("pict", pictures2.get(pictIdx).suggestFileExtension());
  112. assertArrayEquals(pictData, pictures2.get(pictIdx).getData());
  113. }
  114. }
  115. }
  116. /**
  117. * Bug 53568: XSSFPicture.getPictureData() can return null.
  118. */
  119. @Test
  120. void test53568() throws IOException {
  121. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53568.xlsx");
  122. List<XSSFPictureData> pictures = wb.getAllPictures();
  123. assertNotNull(pictures);
  124. assertEquals(4, pictures.size());
  125. XSSFSheet sheet1 = wb.getSheetAt(0);
  126. List<XSSFShape> shapes1 = sheet1.createDrawingPatriarch().getShapes();
  127. assertNotNull(shapes1);
  128. assertEquals(5, shapes1.size());
  129. for(int i = 0; i < wb.getNumberOfSheets(); i++){
  130. XSSFSheet sheet = wb.getSheetAt(i);
  131. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  132. for(XSSFShape shape : drawing.getShapes()){
  133. if(shape instanceof XSSFPicture){
  134. XSSFPicture pic = (XSSFPicture)shape;
  135. XSSFPictureData picData = pic.getPictureData();
  136. assertNotNull(picData);
  137. }
  138. }
  139. }
  140. wb.close();
  141. }
  142. }