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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.fail;
  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.io.PrintStream;
  25. import java.nio.charset.StandardCharsets;
  26. import java.util.Set;
  27. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  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.poifs.filesystem.DirectoryNode;
  34. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  35. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  36. import org.apache.poi.util.TempFile;
  37. import org.junit.jupiter.api.Test;
  38. class HPSFFileHandler extends POIFSFileHandler {
  39. private static final String NL = System.getProperty("line.separator");
  40. private static final ThreadLocal<File> copyOutput = ThreadLocal.withInitial(HPSFFileHandler::getTempFile);
  41. static final Set<String> EXCLUDES_HANDLE_ADD = StressTestUtils.unmodifiableHashSet(
  42. "spreadsheet/45290.xls",
  43. "spreadsheet/46904.xls",
  44. "spreadsheet/55982.xls",
  45. "spreadsheet/testEXCEL_3.xls",
  46. "spreadsheet/testEXCEL_4.xls",
  47. "hpsf/Test_Humor-Generation.ppt",
  48. "document/word2.doc"
  49. );
  50. @Override
  51. public void handleFile(InputStream stream, String path) throws Exception {
  52. POIFSFileSystem poifs = new POIFSFileSystem(stream);
  53. HPSFPropertiesOnlyDocument hpsf = new HPSFPropertiesOnlyDocument(poifs);
  54. DocumentSummaryInformation dsi = hpsf.getDocumentSummaryInformation();
  55. SummaryInformation si = hpsf.getSummaryInformation();
  56. boolean hasDSI = hasPropertyStream(poifs, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
  57. boolean hasSI = hasPropertyStream(poifs, SummaryInformation.DEFAULT_STREAM_NAME);
  58. assertEquals(hasDSI, dsi != null);
  59. assertEquals(hasSI, si != null);
  60. handlePOIDocument(hpsf);
  61. }
  62. private static boolean hasPropertyStream(POIFSFileSystem poifs, String streamName) throws IOException {
  63. DirectoryNode root = poifs.getRoot();
  64. if (!root.hasEntry(streamName)) {
  65. return false;
  66. }
  67. try (DocumentInputStream dis = root.createDocumentInputStream(streamName)) {
  68. return PropertySet.isPropertySetStream(dis);
  69. }
  70. }
  71. private static File getTempFile() {
  72. File f = null;
  73. try {
  74. f = TempFile.createTempFile("hpsfCopy", "out");
  75. } catch (IOException e) {
  76. fail(e);
  77. }
  78. f.deleteOnExit();
  79. return f;
  80. }
  81. @Override
  82. public void handleAdditional(File file) throws Exception {
  83. assumeFalse(EXCLUDES_HANDLE_ADD.contains(file.getParentFile().getName()+"/"+file.getName()));
  84. UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
  85. PrintStream psNew = new PrintStream(bos, true, "ISO-8859-1");
  86. CopyCompare.setOut(psNew);
  87. CopyCompare.main(new String[]{file.getAbsolutePath(), copyOutput.get().getAbsolutePath()});
  88. assertEquals("Equal" + NL, bos.toString(StandardCharsets.UTF_8));
  89. }
  90. // a test-case to test this locally without executing the full TestAllFiles
  91. @Override
  92. @Test
  93. @SuppressWarnings("java:S2699")
  94. void test() throws Exception {
  95. String path = "test-data/diagram/44501.vsd";
  96. try (InputStream stream = new FileInputStream(path)) {
  97. handleFile(stream, path);
  98. }
  99. }
  100. // a test-case to test this locally without executing the full TestAllFiles
  101. @Test
  102. void testExtractor() {
  103. File file = new File("test-data/hpsf/TestBug44375.xls");
  104. assertDoesNotThrow(() -> handleExtracting(file));
  105. }
  106. }