Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestHWPFPictures.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.hwpf;
  16. import javax.imageio.ImageIO;
  17. import java.awt.image.BufferedImage;
  18. import java.io.ByteArrayInputStream;
  19. import java.util.List;
  20. import junit.framework.TestCase;
  21. import org.apache.poi.POIDataSamples;
  22. import org.apache.poi.hwpf.model.PicturesTable;
  23. import org.apache.poi.hwpf.usermodel.Picture;
  24. import org.apache.poi.POIDataSamples;
  25. /**
  26. * Test picture support in HWPF
  27. * @author nick
  28. */
  29. public final class TestHWPFPictures extends TestCase {
  30. private String docAFile;
  31. private String docBFile;
  32. private String docCFile;
  33. private String docDFile;
  34. private String imgAFile;
  35. private String imgBFile;
  36. private String imgCFile;
  37. private String imgDFile;
  38. protected void setUp() {
  39. docAFile = "testPictures.doc";
  40. docBFile = "two_images.doc";
  41. docCFile = "vector_image.doc";
  42. docDFile = "GaiaTest.doc";
  43. imgAFile = "simple_image.jpg";
  44. imgBFile = "simple_image.png";
  45. imgCFile = "vector_image.emf";
  46. imgDFile = "GaiaTestImg.png";
  47. }
  48. /**
  49. * Test just opening the files
  50. */
  51. public void testOpen() {
  52. HWPFTestDataSamples.openSampleFile(docAFile);
  53. HWPFTestDataSamples.openSampleFile(docBFile);
  54. }
  55. /**
  56. * Test that we have the right numbers of images in each file
  57. */
  58. public void testImageCount() {
  59. HWPFDocument docA = HWPFTestDataSamples.openSampleFile(docAFile);
  60. HWPFDocument docB = HWPFTestDataSamples.openSampleFile(docBFile);
  61. assertNotNull(docA.getPicturesTable());
  62. assertNotNull(docB.getPicturesTable());
  63. PicturesTable picA = docA.getPicturesTable();
  64. PicturesTable picB = docB.getPicturesTable();
  65. List<Picture> picturesA = picA.getAllPictures();
  66. List<Picture> picturesB = picB.getAllPictures();
  67. assertEquals(7, picturesA.size());
  68. assertEquals(2, picturesB.size());
  69. }
  70. /**
  71. * Test that we have the right images in at least one file
  72. */
  73. public void testImageData() {
  74. HWPFDocument docB = HWPFTestDataSamples.openSampleFile(docBFile);
  75. PicturesTable picB = docB.getPicturesTable();
  76. List<Picture> picturesB = picB.getAllPictures();
  77. assertEquals(2, picturesB.size());
  78. Picture pic1 = picturesB.get(0);
  79. Picture pic2 = picturesB.get(1);
  80. assertNotNull(pic1);
  81. assertNotNull(pic2);
  82. // Check the same
  83. byte[] pic1B = readFile(imgAFile);
  84. byte[] pic2B = readFile(imgBFile);
  85. assertEquals(pic1B.length, pic1.getContent().length);
  86. assertEquals(pic2B.length, pic2.getContent().length);
  87. assertBytesSame(pic1B, pic1.getContent());
  88. assertBytesSame(pic2B, pic2.getContent());
  89. }
  90. /**
  91. * Test that compressed image data is correctly returned.
  92. */
  93. public void testCompressedImageData() {
  94. HWPFDocument docC = HWPFTestDataSamples.openSampleFile(docCFile);
  95. PicturesTable picC = docC.getPicturesTable();
  96. List<Picture> picturesC = picC.getAllPictures();
  97. assertEquals(1, picturesC.size());
  98. Picture pic = picturesC.get(0);
  99. assertNotNull(pic);
  100. // Check the same
  101. byte[] picBytes = readFile(imgCFile);
  102. assertEquals(picBytes.length, pic.getContent().length);
  103. assertBytesSame(picBytes, pic.getContent());
  104. }
  105. public void testMacImages() throws Exception {
  106. HWPFDocument docC = HWPFTestDataSamples.openSampleFile("53446.doc");
  107. PicturesTable picturesTable = docC.getPicturesTable();
  108. List<Picture> pictures = picturesTable.getAllPictures();
  109. assertEquals(4, pictures.size());
  110. int[][] expectedSizes = {
  111. { 185, 42 }, // PNG
  112. { 260, 114 }, // PNG
  113. { 185, 42 }, // PNG
  114. { 260, 114 }, // PNG
  115. };
  116. for (int i = 0; i < pictures.size(); i++) {
  117. BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures.get(i).getContent()));
  118. assertNotNull(image);
  119. int[] dimensions = expectedSizes[i];
  120. assertEquals(dimensions[0], image.getWidth());
  121. assertEquals(dimensions[1], image.getHeight());
  122. }
  123. }
  124. /**
  125. * Pending the missing files being uploaded to
  126. * bug #44937
  127. */
  128. public void BROKENtestEscherDrawing() {
  129. HWPFDocument docD = HWPFTestDataSamples.openSampleFile(docDFile);
  130. List<Picture> allPictures = docD.getPicturesTable().getAllPictures();
  131. assertEquals(1, allPictures.size());
  132. Picture pic = allPictures.get(0);
  133. assertNotNull(pic);
  134. byte[] picD = readFile(imgDFile);
  135. assertEquals(picD.length, pic.getContent().length);
  136. assertBytesSame(picD, pic.getContent());
  137. }
  138. private void assertBytesSame(byte[] a, byte[] b) {
  139. assertEquals(a.length, b.length);
  140. for(int i=0; i<a.length; i++) {
  141. assertEquals(a[i],b[i]);
  142. }
  143. }
  144. private static byte[] readFile(String file) {
  145. return POIDataSamples.getDocumentInstance().readFile(file);
  146. }
  147. }