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.

TestHPSFPropertiesExtractor.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.hpsf.extractor;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.apache.poi.POIDataSamples;
  22. import org.apache.poi.hpsf.*;
  23. import org.apache.poi.hssf.HSSFTestDataSamples;
  24. import org.apache.poi.hssf.extractor.ExcelExtractor;
  25. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  27. import org.junit.Test;
  28. public final class TestHPSFPropertiesExtractor {
  29. private static final POIDataSamples _samples = POIDataSamples.getHPSFInstance();
  30. @Test
  31. public void testNormalProperties() throws Exception {
  32. try (InputStream is = _samples.openResourceAsStream("TestMickey.doc");
  33. POIFSFileSystem fs = new POIFSFileSystem(is);
  34. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs)) {
  35. // Check each bit in turn
  36. String summary = ext.getSummaryInformationText();
  37. String docSummary = ext.getDocumentSummaryInformationText();
  38. assertContains(summary, "TEMPLATE = Normal");
  39. assertContains(summary, "SUBJECT = sample subject");
  40. assertContains(docSummary, "MANAGER = sample manager");
  41. assertContains(docSummary, "COMPANY = sample company");
  42. // Now overall
  43. String text = ext.getText();
  44. assertContains(text, "TEMPLATE = Normal");
  45. assertContains(text, "SUBJECT = sample subject");
  46. assertContains(text, "MANAGER = sample manager");
  47. assertContains(text, "COMPANY = sample company");
  48. }
  49. }
  50. @Test
  51. public void testNormalUnicodeProperties() throws Exception {
  52. try (InputStream is = _samples.openResourceAsStream("TestUnicode.xls");
  53. POIFSFileSystem fs = new POIFSFileSystem(is);
  54. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs)) {
  55. // Check each bit in turn
  56. String summary = ext.getSummaryInformationText();
  57. String docSummary = ext.getDocumentSummaryInformationText();
  58. assertContains(summary, "AUTHOR = marshall");
  59. assertContains(summary, "TITLE = Titel: \u00c4h");
  60. assertContains(docSummary, "COMPANY = Schreiner");
  61. assertContains(docSummary, "SCALE = false");
  62. // Now overall
  63. String text = ext.getText();
  64. assertContains(text, "AUTHOR = marshall");
  65. assertContains(text, "TITLE = Titel: \u00c4h");
  66. assertContains(text, "COMPANY = Schreiner");
  67. assertContains(text, "SCALE = false");
  68. }
  69. }
  70. @Test
  71. public void testCustomProperties() throws Exception {
  72. try (InputStream is = _samples.openResourceAsStream("TestMickey.doc");
  73. POIFSFileSystem fs = new POIFSFileSystem(is);
  74. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs)) {
  75. // Custom properties are part of the document info stream
  76. String dinfText = ext.getDocumentSummaryInformationText();
  77. assertContains(dinfText, "Client = sample client");
  78. assertContains(dinfText, "Division = sample division");
  79. String text = ext.getText();
  80. assertContains(text, "Client = sample client");
  81. assertContains(text, "Division = sample division");
  82. }
  83. }
  84. @Test
  85. public void testConstructors() throws IOException {
  86. POIFSFileSystem fs;
  87. HSSFWorkbook wb;
  88. try {
  89. fs = new POIFSFileSystem(_samples.openResourceAsStream("TestUnicode.xls"));
  90. wb = new HSSFWorkbook(fs);
  91. } catch (IOException e) {
  92. throw new RuntimeException(e);
  93. }
  94. ExcelExtractor excelExt = new ExcelExtractor(wb);
  95. final String fsText;
  96. HPSFPropertiesExtractor fsExt = new HPSFPropertiesExtractor(fs);
  97. fsExt.setFilesystem(null); // Don't close re-used test resources!
  98. try {
  99. fsText = fsExt.getText();
  100. } finally {
  101. fsExt.close();
  102. }
  103. final String hwText;
  104. HPSFPropertiesExtractor hwExt = new HPSFPropertiesExtractor(wb);
  105. hwExt.setFilesystem(null); // Don't close re-used test resources!
  106. try {
  107. hwText = hwExt.getText();
  108. } finally {
  109. hwExt.close();
  110. }
  111. final String eeText;
  112. HPSFPropertiesExtractor eeExt = new HPSFPropertiesExtractor(excelExt);
  113. eeExt.setFilesystem(null); // Don't close re-used test resources!
  114. try {
  115. eeText = eeExt.getText();
  116. } finally {
  117. eeExt.close();
  118. wb.close();
  119. }
  120. assertEquals(fsText, hwText);
  121. assertEquals(fsText, eeText);
  122. assertContains(fsText, "AUTHOR = marshall");
  123. assertContains(fsText, "TITLE = Titel: \u00c4h");
  124. }
  125. @Test
  126. public void test42726() throws IOException {
  127. try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("42726.xls");
  128. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(wb)) {
  129. String txt = ext.getText();
  130. assertContains(txt, "PID_AUTHOR");
  131. assertContains(txt, "PID_EDITTIME");
  132. assertContains(txt, "PID_REVNUMBER");
  133. assertContains(txt, "PID_THUMBNAIL");
  134. }
  135. }
  136. @Test
  137. public void testThumbnail() throws Exception {
  138. POIFSFileSystem fs = new POIFSFileSystem(_samples.openResourceAsStream("TestThumbnail.xls"));
  139. HSSFWorkbook wb = new HSSFWorkbook(fs);
  140. Thumbnail thumbnail = new Thumbnail(wb.getSummaryInformation().getThumbnail());
  141. assertEquals(-1, thumbnail.getClipboardFormatTag());
  142. assertEquals(3, thumbnail.getClipboardFormat());
  143. assertNotNull(thumbnail.getThumbnailAsWMF());
  144. wb.close();
  145. }
  146. @Test
  147. public void test52258() throws Exception {
  148. try (InputStream is = _samples.openResourceAsStream("TestVisioWithCodepage.vsd");
  149. POIFSFileSystem fs = new POIFSFileSystem(is);
  150. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs)) {
  151. assertNotNull(ext.getDocSummaryInformation());
  152. assertNotNull(ext.getDocumentSummaryInformationText());
  153. assertNotNull(ext.getSummaryInformation());
  154. assertNotNull(ext.getSummaryInformationText());
  155. assertNotNull(ext.getText());
  156. }
  157. }
  158. @Test
  159. public void test61300Extractor() throws IOException {
  160. try (POIFSFileSystem poifs = new POIFSFileSystem(
  161. POIDataSamples.getPOIFSInstance().getFile("61300.bin"))) {
  162. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(poifs);
  163. assertContains(ext.getText(), "PID_CODEPAGE = 1252");
  164. }
  165. }
  166. }