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.

StressMap.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 java.io.File;
  17. import java.io.IOException;
  18. import java.util.AbstractMap.SimpleEntry;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.LinkedHashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.function.BiConsumer;
  25. import java.util.regex.Pattern;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.Stream;
  28. import java.util.stream.StreamSupport;
  29. import org.apache.commons.collections4.MultiValuedMap;
  30. import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
  31. import org.apache.poi.ss.usermodel.Cell;
  32. import org.apache.poi.ss.usermodel.CellType;
  33. import org.apache.poi.ss.usermodel.Row;
  34. import org.apache.poi.ss.usermodel.Sheet;
  35. import org.apache.poi.ss.usermodel.Workbook;
  36. import org.apache.poi.ss.usermodel.WorkbookFactory;
  37. public class StressMap {
  38. private final MultiValuedMap<String, ExcInfo> exMap = new ArrayListValuedHashMap<>();
  39. private final Map<String,String> handlerMap = new LinkedHashMap<>();
  40. private final boolean SCRATCH_IGNORE = Boolean.getBoolean("scratchpad.ignore");
  41. private final Pattern SCRATCH_HANDLER = Pattern.compile("(HSLF|HWPF|HSMF|HMEF)");
  42. public void load(File mapFile) throws IOException {
  43. try (Workbook wb = WorkbookFactory.create(mapFile)) {
  44. readExMap(wb.getSheet("Exceptions"));
  45. readHandlerMap(wb.getSheet("Handlers"));
  46. }
  47. }
  48. public List<FileHandlerKnown> getHandler(String file) {
  49. // ... failures/handlers lookup doesn't work on windows otherwise
  50. final String uniFile = file.replace('\\', '/');
  51. String firstHandler = handlerMap.entrySet().stream()
  52. .filter(me -> uniFile.endsWith(me.getKey()))
  53. .map(Map.Entry::getValue).findFirst().orElse("NULL");
  54. return Stream.of(firstHandler, secondHandler(firstHandler))
  55. .filter(h -> !"NULL".equals(h))
  56. .map(FileHandlerKnown::valueOf)
  57. .collect(Collectors.toList());
  58. }
  59. public ExcInfo getExcInfo(String file, String testName, FileHandlerKnown handler) {
  60. // ... failures/handlers lookup doesn't work on windows otherwise
  61. final String uniFile = file.replace('\\', '/');
  62. return exMap.get(uniFile).stream()
  63. .filter(e -> e.isMatch(testName, handler.name()))
  64. .findFirst().orElse(null);
  65. }
  66. public void readHandlerMap(Sheet sh) {
  67. if (sh == null) {
  68. return;
  69. }
  70. handlerMap.clear();
  71. boolean isFirst = true;
  72. for (Row row : sh) {
  73. if (isFirst) {
  74. isFirst = false;
  75. continue;
  76. }
  77. Cell cell = row.getCell(2);
  78. if (SCRATCH_IGNORE || cell == null || cell.getCellType() != CellType.STRING) {
  79. cell = row.getCell(1);
  80. }
  81. handlerMap.put(row.getCell(0).getStringCellValue(), cell.getStringCellValue());
  82. }
  83. }
  84. public void readExMap(Sheet sh) {
  85. if (sh == null) {
  86. return;
  87. }
  88. exMap.clear();
  89. Iterator<Row> iter = sh.iterator();
  90. List<Map.Entry<String, BiConsumer<ExcInfo,String>>> cols = initCols(iter.next());
  91. int idx = 0, handlerIdx = -1;
  92. for (Map.Entry<String, BiConsumer<ExcInfo, String>> e : cols) {
  93. if ("Handler".equals(e.getKey())) {
  94. handlerIdx = idx;
  95. }
  96. idx++;
  97. }
  98. while (iter.hasNext()) {
  99. Row row = iter.next();
  100. if (SCRATCH_IGNORE && handlerIdx > -1) {
  101. String handler = row.getCell(handlerIdx).getStringCellValue();
  102. if (SCRATCH_HANDLER.matcher(handler).find()) {
  103. // ignore exception of ignored files
  104. continue;
  105. }
  106. }
  107. ExcInfo info = new ExcInfo();
  108. for (Cell cell : row) {
  109. if (cell.getCellType() == CellType.STRING) {
  110. cols.get(cell.getColumnIndex()).getValue().accept(info, cell.getStringCellValue());
  111. }
  112. }
  113. exMap.put(info.getFile(), info);
  114. }
  115. }
  116. private static List<Map.Entry<String, BiConsumer<ExcInfo,String>>> initCols(Row row) {
  117. Map<String,BiConsumer<ExcInfo,String>> m = new HashMap<>();
  118. m.put("File", ExcInfo::setFile);
  119. m.put("Tests", ExcInfo::setTests);
  120. m.put("Handler", ExcInfo::setHandler);
  121. m.put("Password", ExcInfo::setPassword);
  122. m.put("Exception Class", ExcInfo::setExClazz);
  123. m.put("Exception Message", ExcInfo::setExMessage);
  124. return StreamSupport
  125. .stream(row.spliterator(), false)
  126. .map(Cell::getStringCellValue)
  127. .map(v -> new SimpleEntry<>(v, m.getOrDefault(v, (e,s) -> {})))
  128. .collect(Collectors.toList());
  129. }
  130. private static String secondHandler(String handlerStr) {
  131. switch (handlerStr) {
  132. case "XSSF":
  133. case "XWPF":
  134. case "XSLF":
  135. case "XDGF":
  136. return "OPC";
  137. case "HSSF":
  138. case "HWPF":
  139. case "HSLF":
  140. case "HDGF":
  141. case "HSMF":
  142. case "HBPF":
  143. return "HPSF";
  144. default:
  145. return "NULL";
  146. }
  147. }
  148. }