diff --git a/backend/src/main/java/io/metersphere/commons/utils/ListUtils.java b/backend/src/main/java/io/metersphere/commons/utils/ListUtils.java new file mode 100644 index 0000000000..ed93d6ebdd --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/utils/ListUtils.java @@ -0,0 +1,21 @@ +package io.metersphere.commons.utils; + + +import java.util.List; + +/** + * @author song.tianyang + * @Date 2021/6/15 10:48 上午 + */ +public class ListUtils { + public void set(List list, int index, T t, T emptyObj){ + if(list.size()<=index){ + for(int i = 0;i <= index; i++){ + if(i >= list.size()){ + list.add(emptyObj); + } + } + } + list.set(index,t); + } +} diff --git a/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataIgnoreErrorListener.java b/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataIgnoreErrorListener.java index 923386d2c4..d8cb49a105 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataIgnoreErrorListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataIgnoreErrorListener.java @@ -8,6 +8,7 @@ import io.metersphere.base.domain.TestCaseWithBLOBs; import io.metersphere.commons.constants.TestCaseConstants; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.commons.utils.ListUtils; import io.metersphere.commons.utils.LogUtil; import io.metersphere.excel.domain.ExcelErrData; import io.metersphere.excel.domain.TestCaseExcelData; @@ -167,35 +168,8 @@ public class TestCaseDataIgnoreErrorListener extends EasyExcelListener stepDescList = new ArrayList<>(); List stepResList = new ArrayList<>(); -// String[] stepDesc = new String[1]; -// String[] stepRes = new String[1]; - + ListUtils listUtils = new ListUtils(); if (data.getStepDesc() != null) { String[] stepDesc = data.getStepDesc().split("\r\n|\n"); StringBuffer stepBuffer = new StringBuffer(); - int stepIndex = 1; + int lastStepIndex = 1; for (String row : stepDesc) { - if(StringUtils.startsWithAny(row, - stepIndex+")","("+stepIndex+")","(\"+stepIndex+\")", - stepIndex+".",stepIndex+",",stepIndex+",")){ - stepDescList.add(stepBuffer.toString()); + int rowIndex = this.parseIndexInRow(row); + if(rowIndex > -1){ + listUtils.set(stepDescList,lastStepIndex-1,stepBuffer.toString(),""); stepBuffer = new StringBuffer(); - stepIndex++; + lastStepIndex = rowIndex; stepBuffer.append(row); }else { stepBuffer.append(row); } } if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepDescList.add(stepBuffer.toString()); + listUtils.set(stepDescList,lastStepIndex-1,stepBuffer.toString(),""); } } else { stepDescList.add(""); @@ -362,27 +333,25 @@ public class TestCaseDataIgnoreErrorListener extends EasyExcelListener -1){ + listUtils.set(stepResList,lastStepIndex-1,stepBuffer.toString(),""); stepBuffer = new StringBuffer(); - stepIndex++; + lastStepIndex = rowIndex; stepBuffer.append(row); }else { stepBuffer.append(row); } } if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepResList.add(stepBuffer.toString()); + listUtils.set(stepResList,lastStepIndex-1,stepBuffer.toString(),""); } } else { stepResList.add(""); } - String pattern = "(^\\d+)(\\.)?"; int index = stepDescList.size() > stepResList.size() ? stepDescList.size() : stepResList.size(); for (int i = 0; i < index; i++) { @@ -390,26 +359,12 @@ public class TestCaseDataIgnoreErrorListener extends EasyExcelListener 0){ + String indexString = rowSplit[0]; + if(StringUtils.isNumeric(indexString)){ + try { + index = Integer.parseInt(indexString); + }catch (Exception e){} + if(index > -1){ + break; + } + } + } + } + } + return index; + } + @Override public void invoke(TestCaseExcelData testCaseExcelData, AnalysisContext analysisContext) { String errMsg; @@ -446,7 +427,7 @@ public class TestCaseDataIgnoreErrorListener extends EasyExcelListener BATCH_COUNT) { + if (errList.isEmpty() && list.size() > BATCH_COUNT) { saveData(); list.clear(); } diff --git a/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataListener.java b/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataListener.java index fe2560e99b..95c66f437d 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataListener.java @@ -8,6 +8,7 @@ import io.metersphere.base.domain.TestCaseWithBLOBs; import io.metersphere.commons.constants.TestCaseConstants; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.commons.utils.ListUtils; import io.metersphere.commons.utils.LogUtil; import io.metersphere.excel.domain.ExcelErrData; import io.metersphere.excel.domain.TestCaseExcelData; @@ -351,30 +352,41 @@ public class TestCaseDataListener extends EasyExcelListener { List stepResList = new ArrayList<>(); // String[] stepDesc = new String[1]; // String[] stepRes = new String[1]; - + ListUtils listUtils = new ListUtils(); if (data.getStepDesc() != null) { String[] stepDesc = data.getStepDesc().split("\r\n|\n"); StringBuffer stepBuffer = new StringBuffer(); - int stepIndex = 1; + int lastStepIndex = 1; for (String row : stepDesc) { - if(StringUtils.startsWithAny(row, - stepIndex+")","("+stepIndex+")","(\"+stepIndex+\")", - stepIndex+".",stepIndex+",",stepIndex+",")){ - if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepDescList.add(stepBuffer.toString()); - } + int rowIndex = this.parseIndexInRow(row); + if(rowIndex > -1){ + + listUtils.set(stepDescList,lastStepIndex-1,stepBuffer.toString(),""); +// stepResList.set(stepIndex-1,stepBuffer.toString()); stepBuffer = new StringBuffer(); - stepIndex++; + lastStepIndex = rowIndex; stepBuffer.append(row); }else { - if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepBuffer.append("\r\n"); - } stepBuffer.append(row); } +// if(StringUtils.startsWithAny(row, +// stepIndex+")","("+stepIndex+")","(\"+stepIndex+\")", +// stepIndex+".",stepIndex+",",stepIndex+",")){ +// if(StringUtils.isNotEmpty(stepBuffer.toString())){ +// stepDescList.add(stepBuffer.toString()); +// } +// stepBuffer = new StringBuffer(); +// stepIndex++; +// stepBuffer.append(row); +// }else { +// if(StringUtils.isNotEmpty(stepBuffer.toString())){ +// stepBuffer.append("\r\n"); +// } +// stepBuffer.append(row); +// } } if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepDescList.add(stepBuffer.toString()); + listUtils.set(stepDescList,lastStepIndex-1,stepBuffer.toString(),""); } } else { stepDescList.add(""); @@ -383,32 +395,43 @@ public class TestCaseDataListener extends EasyExcelListener { if (data.getStepResult() != null) { String [] stepRes = data.getStepResult().split("\r\n|\n"); StringBuffer stepBuffer = new StringBuffer(); - int stepIndex = 1; + int lastStepIndex = 1; for (String row : stepRes) { - if(StringUtils.startsWithAny(row, - stepIndex+")","("+stepIndex+")","(\"+stepIndex+\")", - stepIndex+".",stepIndex+",",stepIndex+",")){ - if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepResList.add(stepBuffer.toString()); - } + int rowIndex = this.parseIndexInRow(row); + if(rowIndex > -1){ + + listUtils.set(stepResList,lastStepIndex-1,stepBuffer.toString(),""); +// stepResList.set(stepIndex-1,stepBuffer.toString()); stepBuffer = new StringBuffer(); - stepIndex++; + lastStepIndex = rowIndex; stepBuffer.append(row); }else { - if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepBuffer.append("\r\n"); - } stepBuffer.append(row); } +// if(StringUtils.startsWithAny(row, +// stepIndex+")","("+stepIndex+")","(\"+stepIndex+\")", +// stepIndex+".",stepIndex+",",stepIndex+",")){ +// if(StringUtils.isNotEmpty(stepBuffer.toString())){ +// stepResList.add(stepBuffer.toString()); +// } +// stepBuffer = new StringBuffer(); +// stepIndex++; +// stepBuffer.append(row); +// }else { +// if(StringUtils.isNotEmpty(stepBuffer.toString())){ +// stepBuffer.append("\r\n"); +// } +// stepBuffer.append(row); +// } } if(StringUtils.isNotEmpty(stepBuffer.toString())){ - stepResList.add(stepBuffer.toString()); + listUtils.set(stepResList,lastStepIndex-1,stepBuffer.toString(),""); } } else { stepResList.add(""); } - String pattern = "(^\\d+)(\\.)?"; +// String pattern = "(^\\d+)(\\.)?"; int index = stepDescList.size() > stepResList.size() ? stepDescList.size() : stepResList.size(); for (int i = 0; i < index; i++) { @@ -417,25 +440,27 @@ public class TestCaseDataListener extends EasyExcelListener { JSONObject step = new JSONObject(true); step.put("num", i + 1); - Pattern descPattern = Pattern.compile(pattern); - Pattern resPattern = Pattern.compile(pattern); +// Pattern descPattern = Pattern.compile(pattern); +// Pattern resPattern = Pattern.compile(pattern); if (i < stepDescList.size()) { - Matcher descMatcher = descPattern.matcher(stepDescList.get(i)); - if (descMatcher.find()) { - step.put("desc", descMatcher.replaceAll("")); - } else { - step.put("desc", stepDescList.get(i)); - } +// Matcher descMatcher = descPattern.matcher(stepDescList.get(i)); +// if (descMatcher.find()) { +// step.put("desc", descMatcher.replaceAll("")); +// } else { +// step.put("desc", stepDescList.get(i)); +// } + step.put("desc", stepDescList.get(i)); } if (i < stepResList.size()) { - Matcher resMatcher = resPattern.matcher(stepResList.get(i)); - if (resMatcher.find()) { - step.put("result", resMatcher.replaceAll("")); - } else { - step.put("result", stepResList.get(i)); - } +// Matcher resMatcher = resPattern.matcher(stepResList.get(i)); +// if (resMatcher.find()) { +// step.put("result", resMatcher.replaceAll("")); +// } else { +// step.put("result", stepResList.get(i)); +// } + step.put("result", stepResList.get(i)); } jsonArray.add(step); @@ -443,6 +468,32 @@ public class TestCaseDataListener extends EasyExcelListener { return jsonArray.toJSONString(); } + private int parseIndexInRow(String row) { + String parseString = row; + int index = -1; + String [] indexSplitCharArr = new String[]{")",")","]","】",".",",",",","。"}; + if(StringUtils.startsWithAny(row,"(","(","[","【")){ + parseString = parseString.substring(1); + } + for (String splitChar : indexSplitCharArr) { + if(StringUtils.contains(parseString,splitChar)){ + String[] rowSplit = StringUtils.split(parseString,splitChar); + if(rowSplit.length > 0){ + String indexString = rowSplit[0]; + if(StringUtils.isNumeric(indexString)){ + try { + index = Integer.parseInt(indexString); + }catch (Exception e){} + if(index > -1){ + break; + } + } + } + } + } + return index; + } + @Override public void invoke(TestCaseExcelData testCaseExcelData, AnalysisContext analysisContext) { String errMsg;