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.2KB

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