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.

BaseXLSIteratingTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.assertNotNull;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FilenameFilter;
  20. import java.io.OutputStream;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Locale;
  25. import java.util.Map;
  26. import org.apache.poi.POIDataSamples;
  27. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.util.NullOutputStream;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.ExpectedException;
  33. import org.junit.runner.RunWith;
  34. import org.junit.runners.Parameterized;
  35. import org.junit.runners.Parameterized.Parameter;
  36. import org.junit.runners.Parameterized.Parameters;
  37. /**
  38. * Base class for integration-style tests which iterate over all test-files
  39. * and execute the same action to find out if any change breaks these applications.
  40. *
  41. * This test uses {@link Parameterized} to run the test for each file separatedely.
  42. */
  43. @RunWith(Parameterized.class)
  44. public abstract class BaseXLSIteratingTest {
  45. protected static final OutputStream NULL_OUTPUT_STREAM = new NullOutputStream();
  46. @Rule
  47. public ExpectedException thrown = ExpectedException.none();
  48. protected static final Map<String,Class<? extends Throwable>> EXCLUDED =
  49. new HashMap<String,Class<? extends Throwable>>();
  50. @Parameters(name="{index}: {0}")
  51. public static Iterable<Object[]> files() {
  52. String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY);
  53. if(dataDirName == null) {
  54. dataDirName = "test-data";
  55. }
  56. List<Object[]> files = new ArrayList<Object[]>();
  57. findFile(files, dataDirName + "/spreadsheet");
  58. findFile(files, dataDirName + "/hpsf");
  59. return files;
  60. }
  61. private static void findFile(List<Object[]> list, String dir) {
  62. String[] files = new File(dir).list(new FilenameFilter() {
  63. @Override
  64. public boolean accept(File arg0, String arg1) {
  65. return arg1.toLowerCase(Locale.ROOT).endsWith(".xls");
  66. }
  67. });
  68. assertNotNull("Did not find any xls files in directory " + dir, files);
  69. for(String file : files) {
  70. list.add(new Object[] { new File(dir, file) });
  71. }
  72. }
  73. @Parameter
  74. public File file;
  75. @Test
  76. public void testMain() throws Exception {
  77. // we had intermittent problems when this was set differently somehow, let's try to set it here so it always is set correctly for these tests
  78. Biff8EncryptionKey.setCurrentUserPassword(null);
  79. String fileName = file.getName();
  80. if (EXCLUDED.containsKey(fileName)) {
  81. thrown.expect(EXCLUDED.get(fileName));
  82. }
  83. try {
  84. runOneFile(file);
  85. } catch (Exception e) {
  86. // try to read it in HSSFWorkbook to quickly fail if we cannot read the file there at all and thus probably should use EXCLUDED instead
  87. FileInputStream stream = new FileInputStream(file);
  88. HSSFWorkbook wb = null;
  89. try {
  90. wb = new HSSFWorkbook(stream);
  91. assertNotNull(wb);
  92. } finally {
  93. if (wb != null) {
  94. wb.close();
  95. }
  96. stream.close();
  97. }
  98. throw e;
  99. }
  100. }
  101. abstract void runOneFile(File pFile) throws Exception;
  102. }