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.

HPSFFileHandler.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.assertDoesNotThrow;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertFalse;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import static org.junit.jupiter.api.Assertions.fail;
  22. import static org.junit.jupiter.api.Assumptions.assumeFalse;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.util.Set;
  28. import org.apache.poi.examples.hpsf.CopyCompare;
  29. import org.apache.poi.hpsf.DocumentSummaryInformation;
  30. import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
  31. import org.apache.poi.hpsf.PropertySet;
  32. import org.apache.poi.hpsf.SummaryInformation;
  33. import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
  34. import org.apache.poi.poifs.filesystem.DirectoryNode;
  35. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  36. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  37. import org.apache.poi.util.TempFile;
  38. import org.junit.jupiter.api.Test;
  39. public class HPSFFileHandler extends POIFSFileHandler {
  40. private static final String NL = System.getProperty("line.separator");
  41. private static final ThreadLocal<File> copyOutput = ThreadLocal.withInitial(HPSFFileHandler::getTempFile);
  42. static final Set<String> EXCLUDES_HANDLE_ADD = StressTestUtils.unmodifiableHashSet(
  43. "spreadsheet/45290.xls",
  44. "spreadsheet/46904.xls",
  45. "spreadsheet/55982.xls",
  46. "spreadsheet/testEXCEL_3.xls",
  47. "spreadsheet/testEXCEL_4.xls",
  48. "hpsf/Test_Humor-Generation.ppt",
  49. "document/word2.doc"
  50. );
  51. @Override
  52. public void handleExtracting(File file) throws Exception {
  53. if (!Boolean.getBoolean("scratchpad.ignore")) {
  54. super.handleExtracting(file);
  55. return;
  56. }
  57. long length = file.length();
  58. long modified = file.lastModified();
  59. try (POIFSFileSystem poifs = new POIFSFileSystem(file);
  60. HPSFPropertiesExtractor extractor = new HPSFPropertiesExtractor(poifs)) {
  61. String fileAndParentName = file.getParentFile().getName() + "/" + file.getName();
  62. String relPath = file.getPath().replaceAll(".*test-data", "test-data").replace('\\', '/');
  63. assertFalse(EXPECTED_EXTRACTOR_FAILURES.contains(fileAndParentName),
  64. "Expected Extraction to fail for file " + relPath + " and handler " + this + ", but did not fail!");
  65. assertNotNull(extractor.getDocumentSummaryInformationText());
  66. assertNotNull(extractor.getSummaryInformationText());
  67. String text = extractor.getText();
  68. //System.out.println(text);
  69. assertNotNull(text);
  70. }
  71. assertEquals(length, file.length(), "File should not be modified by extractor");
  72. assertEquals(modified, file.lastModified(), "File should not be modified by extractor");
  73. }
  74. @Override
  75. public void handleFile(InputStream stream, String path) throws Exception {
  76. POIFSFileSystem poifs = new POIFSFileSystem(stream);
  77. HPSFPropertiesOnlyDocument hpsf = new HPSFPropertiesOnlyDocument(poifs);
  78. DocumentSummaryInformation dsi = hpsf.getDocumentSummaryInformation();
  79. SummaryInformation si = hpsf.getSummaryInformation();
  80. boolean hasDSI = hasPropertyStream(poifs, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
  81. boolean hasSI = hasPropertyStream(poifs, SummaryInformation.DEFAULT_STREAM_NAME);
  82. assertEquals(hasDSI, dsi != null);
  83. assertEquals(hasSI, si != null);
  84. handlePOIDocument(hpsf);
  85. }
  86. private static boolean hasPropertyStream(POIFSFileSystem poifs, String streamName) throws IOException {
  87. DirectoryNode root = poifs.getRoot();
  88. if (!root.hasEntry(streamName)) {
  89. return false;
  90. }
  91. try (DocumentInputStream dis = root.createDocumentInputStream(streamName)) {
  92. return PropertySet.isPropertySetStream(dis);
  93. }
  94. }
  95. private static File getTempFile() {
  96. File f = null;
  97. try {
  98. f = TempFile.createTempFile("hpsfCopy", "out");
  99. } catch (IOException e) {
  100. fail(e);
  101. }
  102. f.deleteOnExit();
  103. return f;
  104. }
  105. @Override
  106. public void handleAdditional(File file) throws Exception {
  107. assumeFalse(EXCLUDES_HANDLE_ADD.contains(file.getParentFile().getName()+"/"+file.getName()));
  108. boolean result = CopyCompare.compare(file.getAbsolutePath(), copyOutput.get().getAbsolutePath());
  109. assertTrue(result);
  110. }
  111. // a test-case to test this locally without executing the full TestAllFiles
  112. @Override
  113. @Test
  114. @SuppressWarnings("java:S2699")
  115. void test() throws Exception {
  116. String path = "test-data/diagram/44501.vsd";
  117. try (InputStream stream = new FileInputStream(path)) {
  118. handleFile(stream, path);
  119. }
  120. }
  121. // a test-case to test this locally without executing the full TestAllFiles
  122. @Test
  123. void testExtractor() {
  124. File file = new File("test-data/hpsf/TestBug44375.xls");
  125. assertDoesNotThrow(() -> handleExtracting(file));
  126. }
  127. }