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.

BaseTestIteratingXLS.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.jupiter.api.Assertions.assertDoesNotThrow;
  17. import static org.junit.jupiter.api.Assertions.assertThrows;
  18. import java.io.File;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.function.Function;
  23. import java.util.stream.Stream;
  24. import org.apache.poi.POIDataSamples;
  25. import org.apache.poi.hssf.OldExcelFormatException;
  26. import org.apache.poi.util.RecordFormatException;
  27. import org.apache.tools.ant.DirectoryScanner;
  28. import org.junit.jupiter.api.TestInstance;
  29. import org.junit.jupiter.api.function.Executable;
  30. import org.junit.jupiter.api.parallel.Execution;
  31. import org.junit.jupiter.api.parallel.ExecutionMode;
  32. import org.junit.jupiter.params.ParameterizedTest;
  33. import org.junit.jupiter.params.provider.Arguments;
  34. import org.junit.jupiter.params.provider.MethodSource;
  35. /**
  36. * Base class for integration-style tests which iterate over all test-files
  37. * and execute the same action to find out if any change breaks these applications.
  38. */
  39. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
  40. @Execution(ExecutionMode.CONCURRENT)
  41. public abstract class BaseTestIteratingXLS {
  42. private static final String[] XLS_INCLUDES = {
  43. "spreadsheet/*.xls", "hpsf/*.xls"
  44. };
  45. public Stream<Arguments> files() {
  46. String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY,
  47. new File("test-data").exists() ? "test-data" : "../test-data");
  48. DirectoryScanner scanner = new DirectoryScanner();
  49. scanner.setBasedir(dataDirName);
  50. scanner.setIncludes(XLS_INCLUDES);
  51. scanner.scan();
  52. final Map<String, Class<? extends Throwable>> exc = getExcludes();
  53. Function<String,Arguments> mapArg = (s) -> {
  54. File f = new File(dataDirName, s);
  55. return Arguments.of(f, exc.get(f.getName()));
  56. };
  57. return Arrays.stream(scanner.getIncludedFiles()).map(mapArg);
  58. }
  59. protected Map<String,Class<? extends Throwable>> getExcludes() {
  60. Map<String, Class<? extends Throwable>> excludes = new HashMap<>();
  61. // Biff 2 / Excel 2, pre-OLE2
  62. excludes.put("testEXCEL_2.xls", OldExcelFormatException.class);
  63. // Biff 3 / Excel 3, pre-OLE2
  64. excludes.put("testEXCEL_3.xls", OldExcelFormatException.class);
  65. // Biff 4 / Excel 4, pre-OLE2
  66. excludes.put("testEXCEL_4.xls", OldExcelFormatException.class);
  67. // Biff 5 / Excel 5
  68. excludes.put("testEXCEL_5.xls", OldExcelFormatException.class);
  69. // Biff 5 / Excel 5
  70. excludes.put("60284.xls", OldExcelFormatException.class);
  71. // Biff 5 / Excel 95
  72. excludes.put("testEXCEL_95.xls", OldExcelFormatException.class);
  73. excludes.put("46904.xls", OldExcelFormatException.class);
  74. excludes.put("59074.xls", OldExcelFormatException.class);
  75. excludes.put("61300.xls", RecordFormatException.class);
  76. // BIFF 5
  77. excludes.put("64130.xls", OldExcelFormatException.class);
  78. return excludes;
  79. }
  80. @ParameterizedTest
  81. @MethodSource("files")
  82. void testMain(File file, Class<? extends Throwable> t) throws Exception {
  83. // avoid running files leftover from previous failed runs
  84. // or created by tests running in parallel
  85. // otherwise this would cause sporadic failures with
  86. // parallel test execution
  87. if(file.getName().endsWith("-saved.xls")) {
  88. return;
  89. }
  90. Executable ex = () -> runOneFile(file);
  91. if (t == null) {
  92. assertDoesNotThrow(ex, "Failing file: " + file);
  93. } else {
  94. assertThrows(t, ex, "Failing file: " + file);
  95. }
  96. }
  97. abstract void runOneFile(File pFile) throws Exception;
  98. }