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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.assertNotNull;
  17. import static org.junit.jupiter.api.Assertions.assertThrows;
  18. import java.io.File;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import java.util.Map;
  24. import java.util.stream.Stream;
  25. import org.apache.poi.POIDataSamples;
  26. import org.junit.jupiter.params.ParameterizedTest;
  27. import org.junit.jupiter.params.provider.Arguments;
  28. import org.junit.jupiter.params.provider.MethodSource;
  29. /**
  30. * Base class for integration-style tests which iterate over all test-files
  31. * and execute the same action to find out if any change breaks these applications.
  32. */
  33. public abstract class BaseTestIteratingXLS {
  34. protected static final Map<String,Class<? extends Throwable>> EXCLUDED = new HashMap<>();
  35. private static Stream<Arguments> files() {
  36. String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY);
  37. if(dataDirName == null) {
  38. dataDirName = "test-data";
  39. }
  40. List<Arguments> files = new ArrayList<>();
  41. findFile(files, dataDirName + "/spreadsheet");
  42. findFile(files, dataDirName + "/hpsf");
  43. return files.stream();
  44. }
  45. private static void findFile(List<Arguments> list, String dir) {
  46. String[] files = new File(dir).list((arg0, arg1) -> arg1.toLowerCase(Locale.ROOT).endsWith(".xls"));
  47. assertNotNull(files, "Did not find any xls files in directory " + dir);
  48. for(String file : files) {
  49. list.add(Arguments.of(new File(dir, file)));
  50. }
  51. }
  52. @ParameterizedTest
  53. @MethodSource("files")
  54. void testMain(File file) throws Exception {
  55. String fileName = file.getName();
  56. Class<? extends Throwable> t = EXCLUDED.get(fileName);
  57. if (t == null) {
  58. runOneFile(file);
  59. } else {
  60. assertThrows(t, () -> runOneFile(file));
  61. }
  62. }
  63. abstract void runOneFile(File pFile) throws Exception;
  64. }