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.

TestReSave.java 4.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.hssf.dev;
  16. import static org.junit.Assert.assertTrue;
  17. import java.io.File;
  18. import java.io.PrintStream;
  19. import org.apache.poi.EncryptedDocumentException;
  20. import org.apache.poi.POIDataSamples;
  21. import org.apache.poi.hssf.OldExcelFormatException;
  22. import org.apache.poi.hssf.record.RecordInputStream;
  23. import org.apache.poi.util.LocaleUtil;
  24. import org.apache.poi.util.RecordFormatException;
  25. import org.junit.BeforeClass;
  26. import org.junit.Ignore;
  27. import org.junit.Test;
  28. public class TestReSave extends BaseXLSIteratingTest {
  29. @BeforeClass
  30. public static void setup() {
  31. EXCLUDED.clear();
  32. EXCLUDED.put("35897-type4.xls", EncryptedDocumentException.class); // unsupported crypto api header
  33. EXCLUDED.put("51832.xls", EncryptedDocumentException.class);
  34. EXCLUDED.put("xor-encryption-abc.xls", EncryptedDocumentException.class);
  35. EXCLUDED.put("password.xls", EncryptedDocumentException.class);
  36. EXCLUDED.put("46904.xls", OldExcelFormatException.class);
  37. EXCLUDED.put("59074.xls", OldExcelFormatException.class);
  38. EXCLUDED.put("testEXCEL_2.xls", OldExcelFormatException.class); // Biff 2 / Excel 2, pre-OLE2
  39. EXCLUDED.put("testEXCEL_3.xls", OldExcelFormatException.class); // Biff 3 / Excel 3, pre-OLE2
  40. EXCLUDED.put("testEXCEL_4.xls", OldExcelFormatException.class); // Biff 4 / Excel 4, pre-OLE2
  41. EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
  42. EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
  43. EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
  44. EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
  45. EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well
  46. EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class);
  47. EXCLUDED.put("XRefCalc.xls", RuntimeException.class); // "Buffer overrun"
  48. EXCLUDED.put("61300.xls", RecordFormatException.class);
  49. }
  50. @Override
  51. void runOneFile(File fileIn) throws Exception {
  52. // avoid running on files leftover from previous failed runs
  53. if(fileIn.getName().endsWith("-saved.xls")) {
  54. return;
  55. }
  56. PrintStream save = System.out;
  57. try {
  58. // redirect standard out during the test to avoid spamming the console with output
  59. System.setOut(new PrintStream(NULL_OUTPUT_STREAM,true,LocaleUtil.CHARSET_1252.name()));
  60. File reSavedFile = new File(fileIn.getParentFile(), fileIn.getName().replace(".xls", "-saved.xls"));
  61. try {
  62. ReSave.main(new String[] { fileIn.getAbsolutePath() });
  63. // also try BiffViewer on the saved file
  64. new TestBiffViewer().runOneFile(reSavedFile);
  65. // had one case where the re-saved could not be re-saved!
  66. ReSave.main(new String[] { "-bos", reSavedFile.getAbsolutePath() });
  67. } finally {
  68. // clean up the re-saved file
  69. assertTrue(!reSavedFile.exists() || reSavedFile.delete());
  70. }
  71. } finally {
  72. System.setOut(save);
  73. }
  74. }
  75. @Ignore("Only used for local testing")
  76. @Test
  77. public void testOneFile() throws Exception {
  78. String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY);
  79. if(dataDirName == null) {
  80. dataDirName = "test-data";
  81. }
  82. runOneFile(new File(dataDirName + "/spreadsheet", "49931.xls"));
  83. }
  84. }