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.

BaseIntegrationTest.java 7.3KB

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.jupiter.api.Assertions.assertNotNull;
  17. import static org.junit.jupiter.api.Assumptions.assumeFalse;
  18. import java.io.BufferedInputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.zip.ZipException;
  24. import org.apache.poi.EncryptedDocumentException;
  25. import org.apache.poi.OldFileFormatException;
  26. import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
  27. /**
  28. * This class is used for mass-regression testing via a
  29. * separate project, this class provides functionality to
  30. * run integration tests on one file and handle some
  31. * types of files/exceptions, e.g. old file formats.
  32. *
  33. */
  34. @SuppressWarnings({"java:S2187", "unused"})
  35. public class BaseIntegrationTest {
  36. private final File rootDir;
  37. private final String file;
  38. private FileHandler handler;
  39. public BaseIntegrationTest(File rootDir, String file, FileHandler handler) {
  40. this.rootDir = rootDir;
  41. this.file = file;
  42. this.handler = handler;
  43. }
  44. /**
  45. * Keep this public so it can be used by the regression-tests
  46. */
  47. public void test() throws Exception {
  48. assertNotNull( handler, "Unknown file extension for file: " + file );
  49. testOneFile(new File(rootDir, file));
  50. }
  51. protected void testOneFile(File inputFile) throws Exception {
  52. try {
  53. handleFile(inputFile);
  54. } catch (OfficeXmlFileException e) {
  55. // switch XWPF and HWPF and so forth depending on the error message
  56. handleWrongOLE2XMLExtension(inputFile, e);
  57. } catch (OldFileFormatException e) {
  58. // Not even text extraction is supported for these: handler.handleExtracting(inputFile);
  59. assumeFalse( true, "File " + file + " excluded because it is an unsupported old format" );
  60. } catch (EncryptedDocumentException e) {
  61. // Do not try to read encrypted files
  62. assumeFalse( true, "File " + file + " excluded because it is password-encrypted" );
  63. } catch (ZipException e) {
  64. // some files are corrupted
  65. if (e.getMessage().equals("unexpected EOF") || e.getMessage().equals("Truncated ZIP file")) {
  66. assumeFalse( true, "File " + file + " excluded because the Zip file is incomplete" );
  67. }
  68. throw e;
  69. } catch (IOException e) {
  70. // ignore some other ways of corrupted files
  71. String message = e.getMessage();
  72. if(message != null && message.contains("Truncated ZIP file")) {
  73. assumeFalse( true, "File " + file + " excluded because the Zip file is incomplete" );
  74. }
  75. // sometimes binary format has XML-format-extension...
  76. if(message != null && message.contains("rong file format or file extension for OO XML file")) {
  77. handleWrongOLE2XMLExtension(inputFile, e);
  78. return;
  79. }
  80. throw e;
  81. } catch (IllegalArgumentException e) {
  82. // ignore errors for documents with incorrect extension
  83. String message = e.getMessage();
  84. if(message != null && (message.equals("The document is really a RTF file") ||
  85. message.equals("The document is really a PDF file") ||
  86. message.equals("The document is really a HTML file"))) {
  87. assumeFalse( true, "File " + file + " excluded because it is actually a PDF/RTF/HTML file" );
  88. }
  89. if(message != null && message.equals("The document is really a OOXML file")) {
  90. handleWrongOLE2XMLExtension(inputFile, e);
  91. return;
  92. }
  93. throw e;
  94. }
  95. try {
  96. handler.handleExtracting(inputFile);
  97. } catch (EncryptedDocumentException e) {
  98. // Do not try to read encrypted files
  99. assumeFalse( true, "File " + file + " excluded because it is password-encrypted" );
  100. }
  101. }
  102. void handleWrongOLE2XMLExtension(File inputFile, Exception e) throws Exception {
  103. // we sometimes have wrong extensions, so for some exceptions we try to handle it
  104. // with the correct FileHandler instead
  105. String message = e.getMessage();
  106. // ignore some file-types that we do not want to handle here
  107. assumeFalse( message != null && (message.equals("The document is really a RTF file") ||
  108. message.equals("The document is really a PDF file") ||
  109. message.equals("The document is really a HTML file")), "File " + file + " excluded because it is actually a PDF/RTF/HTML file" );
  110. if(message != null && (message.equals("The document is really a XLS file"))) {
  111. handler = new HSSFFileHandler();
  112. } else if(message != null && (message.equals("The document is really a PPT file"))) {
  113. handler = new HSLFFileHandler();
  114. } else if(message != null && (message.equals("The document is really a DOC file"))) {
  115. handler = new HWPFFileHandler();
  116. } else if(message != null && (message.equals("The document is really a VSD file"))) {
  117. handler = new HDGFFileHandler();
  118. // use XWPF instead of HWPF and XSSF instead of HSSF as the file seems to have the wrong extension
  119. } else if (handler instanceof HWPFFileHandler) {
  120. handler = new XWPFFileHandler();
  121. } else if (handler instanceof HSSFFileHandler) {
  122. handler = new XSSFFileHandler();
  123. } else if (handler instanceof HSLFFileHandler) {
  124. handler = new XSLFFileHandler();
  125. // and the other way around, use HWPF instead of XWPF and so forth
  126. } else if(handler instanceof XWPFFileHandler) {
  127. handler = new HWPFFileHandler();
  128. } else if(handler instanceof XSSFFileHandler) {
  129. handler = new HSSFFileHandler();
  130. } else if(handler instanceof XSLFFileHandler) {
  131. handler = new HSLFFileHandler();
  132. } else {
  133. // nothing matched => throw the exception to the outside
  134. throw e;
  135. }
  136. // we found a different handler to try processing again
  137. handleFile(inputFile);
  138. }
  139. private void handleFile(File inputFile) throws Exception {
  140. try (InputStream newStream = new BufferedInputStream(new FileInputStream(inputFile), 64*1024)) {
  141. handler.handleFile(newStream, inputFile.getAbsolutePath());
  142. }
  143. }
  144. }