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.

HSSFFileHandler.java 4.7KB

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.Assert.assertFalse;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.io.PrintStream;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25. import org.apache.poi.EncryptedDocumentException;
  26. import org.apache.poi.hssf.OldExcelFormatException;
  27. import org.apache.poi.hssf.dev.BiffViewer;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.util.RecordFormatException;
  30. import org.junit.Test;
  31. public class HSSFFileHandler extends SpreadsheetHandler {
  32. private final POIFSFileHandler delegate = new POIFSFileHandler();
  33. @Override
  34. public void handleFile(InputStream stream, String path) throws Exception {
  35. HSSFWorkbook wb = new HSSFWorkbook(stream);
  36. handleWorkbook(wb);
  37. // TODO: some documents fail currently...
  38. // Note - as of Bugzilla 48036 (svn r828244, r828247) POI is capable of evaluating
  39. // IntersectionPtg. However it is still not capable of parsing it.
  40. // So FormulaEvalTestData.xls now contains a few formulas that produce errors here.
  41. //HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
  42. //evaluator.evaluateAll();
  43. delegate.handlePOIDocument(wb);
  44. // also try to see if some of the Records behave incorrectly
  45. // TODO: still fails on some records... RecordsStresser.handleWorkbook(wb);
  46. }
  47. private static final Set<String> EXPECTED_ADDITIONAL_FAILURES = new HashSet<String>();
  48. static {
  49. // encrypted
  50. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/35897-type4.xls");
  51. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/xor-encryption-abc.xls");
  52. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/password.xls");
  53. // broken files
  54. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/43493.xls");
  55. // TODO: ok to ignore?
  56. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/50833.xls");
  57. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/51832.xls");
  58. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/XRefCalc.xls");
  59. EXPECTED_ADDITIONAL_FAILURES.add("spreadsheet/61300.xls");
  60. }
  61. @Override
  62. public void handleAdditional(File file) throws Exception {
  63. // redirect stdout as the examples often write lots of text
  64. PrintStream oldOut = System.out;
  65. try {
  66. System.setOut(new PrintStream(new OutputStream() {
  67. @Override
  68. public void write(int b) throws IOException {
  69. }
  70. }));
  71. BiffViewer.main(new String[]{file.getAbsolutePath()});
  72. assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!",
  73. EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName()));
  74. } catch (OldExcelFormatException e) {
  75. // old excel formats are not supported here
  76. } catch (EncryptedDocumentException e) {
  77. if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
  78. throw e;
  79. }
  80. } catch (RecordFormatException e) {
  81. if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
  82. throw e;
  83. }
  84. } catch (RuntimeException e) {
  85. if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
  86. throw e;
  87. }
  88. } finally {
  89. System.setOut(oldOut);
  90. }
  91. }
  92. // a test-case to test this locally without executing the full TestAllFiles
  93. @Test
  94. public void test() throws Exception {
  95. File file = new File("test-data/spreadsheet/49219.xls");
  96. InputStream stream = new FileInputStream(file);
  97. try {
  98. handleFile(stream, file.getPath());
  99. } finally {
  100. stream.close();
  101. }
  102. }
  103. // a test-case to test this locally without executing the full TestAllFiles
  104. @Test
  105. public void testExtractor() throws Exception {
  106. handleExtracting(new File("test-data/spreadsheet/BOOK_in_capitals.xls"));
  107. }
  108. }