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.

AbstractFileHandler.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.stress;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assumptions.assumeFalse;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.HashSet;
  25. import java.util.Set;
  26. import org.apache.poi.EncryptedDocumentException;
  27. import org.apache.poi.extractor.ExtractorFactory;
  28. import org.apache.poi.extractor.POIOLE2TextExtractor;
  29. import org.apache.poi.extractor.POITextExtractor;
  30. import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
  31. import org.apache.poi.hssf.extractor.EventBasedExcelExtractor;
  32. import org.apache.poi.ooxml.POIXMLException;
  33. import org.apache.poi.ss.extractor.ExcelExtractor;
  34. import org.apache.poi.util.IOUtils;
  35. /**
  36. * Base class with things that can be run for any supported file handler
  37. * in the integration tests, mostly text-extraction related at the moment.
  38. */
  39. public abstract class AbstractFileHandler implements FileHandler {
  40. public static final Set<String> EXPECTED_EXTRACTOR_FAILURES = new HashSet<>();
  41. static {
  42. // password protected files without password
  43. // ... currently none ...
  44. // unsupported file-types, no supported OLE2 parts
  45. EXPECTED_EXTRACTOR_FAILURES.add("hmef/quick-winmail.dat");
  46. EXPECTED_EXTRACTOR_FAILURES.add("hmef/winmail-sample1.dat");
  47. EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-simple.dat");
  48. EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-with-attachments.dat");
  49. EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug63955-winmail.dat");
  50. EXPECTED_EXTRACTOR_FAILURES.add("hpsf/Test0313rur.adm");
  51. EXPECTED_EXTRACTOR_FAILURES.add("poifs/Notes.ole2");
  52. EXPECTED_EXTRACTOR_FAILURES.add("poifs/64322.ole2");
  53. //commons-compress 1.21 no longer supports truncated files
  54. EXPECTED_EXTRACTOR_FAILURES.add("document/truncated62886.docx");
  55. }
  56. @Override
  57. public void handleExtracting(File file) throws Exception {
  58. boolean before = ExtractorFactory.getThreadPrefersEventExtractors();
  59. try {
  60. ExtractorFactory.setThreadPrefersEventExtractors(true);
  61. handleExtractingInternal(file);
  62. ExtractorFactory.setThreadPrefersEventExtractors(false);
  63. handleExtractingInternal(file);
  64. } finally {
  65. ExtractorFactory.setThreadPrefersEventExtractors(before);
  66. }
  67. /* Did fail for some documents with special XML contents...
  68. try {
  69. OOXMLPrettyPrint.main(new String[] { file.getAbsolutePath(),
  70. "/tmp/pretty-" + file.getName() });
  71. } catch (ZipException e) {
  72. // ignore, not a Zip/OOXML file
  73. }*/
  74. }
  75. private void handleExtractingInternal(File file) throws Exception {
  76. long length = file.length();
  77. long modified = file.lastModified();
  78. POITextExtractor extractor = null;
  79. String fileAndParentName = file.getParentFile().getName() + "/" + file.getName();
  80. try {
  81. // fix windows absolute paths for exception message tracking
  82. String relPath = file.getPath().replaceAll(".*test-data", "test-data").replace('\\', '/');
  83. extractor = ExtractorFactory.createExtractor(file);
  84. assertNotNull(extractor, "Should get a POITextExtractor but had none for file " + relPath);
  85. assertNotNull(extractor.getText(), "Should get some text but had none for file " + relPath);
  86. // also try metadata
  87. @SuppressWarnings("resource")
  88. POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor();
  89. assertNotNull(metadataExtractor.getText());
  90. assertFalse(EXPECTED_EXTRACTOR_FAILURES.contains(fileAndParentName),
  91. "Expected Extraction to fail for file " + relPath + " and handler " + this + ", but did not fail!");
  92. assertEquals(length, file.length(), "File should not be modified by extractor");
  93. assertEquals(modified, file.lastModified(), "File should not be modified by extractor");
  94. handleExtractingAsStream(file);
  95. if (extractor instanceof POIOLE2TextExtractor) {
  96. try (HPSFPropertiesExtractor hpsfExtractor = new HPSFPropertiesExtractor((POIOLE2TextExtractor) extractor)) {
  97. assertNotNull(hpsfExtractor.getDocumentSummaryInformationText());
  98. assertNotNull(hpsfExtractor.getSummaryInformationText());
  99. String text = hpsfExtractor.getText();
  100. //System.out.println(text);
  101. assertNotNull(text);
  102. }
  103. }
  104. // test again with including formulas and cell-comments as this caused some bugs
  105. if (extractor instanceof ExcelExtractor &&
  106. // comment-extraction and formula extraction are not well supported in event based extraction
  107. !(extractor instanceof EventBasedExcelExtractor)) {
  108. ((ExcelExtractor) extractor).setFormulasNotResults(true);
  109. String text = extractor.getText();
  110. assertNotNull(text);
  111. // */
  112. ((ExcelExtractor) extractor).setIncludeCellComments(true);
  113. text = extractor.getText();
  114. assertNotNull(text);
  115. }
  116. } catch (IOException | POIXMLException e) {
  117. Exception prevE = e;
  118. Throwable cause;
  119. while ((cause = prevE.getCause()) instanceof Exception) {
  120. if (cause instanceof IOException || cause instanceof POIXMLException) {
  121. prevE = (Exception)cause;
  122. } else {
  123. throw (Exception)cause;
  124. }
  125. }
  126. throw e;
  127. } catch (IllegalArgumentException e) {
  128. if(!EXPECTED_EXTRACTOR_FAILURES.contains(fileAndParentName)) {
  129. throw e;
  130. }
  131. } catch (EncryptedDocumentException e) {
  132. String msg = "org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files";
  133. assumeFalse(msg.equals(e.getMessage()));
  134. throw e;
  135. } catch (IllegalStateException e) {
  136. if (!e.getMessage().contains("POI Scratchpad jar missing") || !Boolean.getBoolean("scratchpad.ignore")) {
  137. throw e;
  138. }
  139. } finally {
  140. IOUtils.closeQuietly(extractor);
  141. }
  142. }
  143. private void handleExtractingAsStream(File file) throws IOException {
  144. try (InputStream stream = new FileInputStream(file)) {
  145. try (POITextExtractor streamExtractor = ExtractorFactory.createExtractor(stream)) {
  146. assertNotNull(streamExtractor);
  147. assertNotNull(streamExtractor.getText());
  148. }
  149. }
  150. }
  151. @Override
  152. public void handleAdditional(File file) throws Exception {
  153. // by default we do nothing here
  154. }
  155. }