fix: 修复测试用例导入步骤与预期结果对不上号的问题

修复测试用例导入步骤与预期结果对不上号的问题
This commit is contained in:
song-tianyang 2021-06-15 13:55:46 +08:00 committed by 刘瑞斌
parent e967add994
commit 100ad31561
3 changed files with 155 additions and 102 deletions

View File

@ -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<T> {
public void set(List<T> 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);
}
}

View File

@ -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<TestCaseE
}
}
}
/*
校验用例
*/
// if (testCaseNames.contains(data.getName())) {
// TestCaseWithBLOBs testCase = new TestCaseWithBLOBs();
// BeanUtils.copyBean(testCase, data);
// testCase.setProjectId(projectId);
// String steps = getSteps(data);
// testCase.setSteps(steps);
// testCase.setType("functional");
// boolean dbExist = testCaseService.exist(testCase);
// boolean excelExist = false;
// if (dbExist) {
// // db exist
// stringBuilder.append(Translator.get("test_case_already_exists") + "" + data.getName() + "; ");
// } else {
// // @Data 重写了 equals hashCode 方法
// excelExist = excelDataList.contains(data);
// }
// if (excelExist) {
// // excel exist
// stringBuilder.append(Translator.get("test_case_already_exists_excel") + "" + data.getName() + "; ");
// } else {
// excelDataList.add(data);
// }
// } else {
testCaseNames.add(data.getName());
excelDataList.add(data);
// }
return stringBuilder.toString();
}
@ -333,27 +307,24 @@ public class TestCaseDataIgnoreErrorListener extends EasyExcelListener<TestCaseE
List<String> stepDescList = new ArrayList<>();
List<String> stepResList = new ArrayList<>();
// String[] stepDesc = new String[1];
// String[] stepRes = new String[1];
ListUtils<String> listUtils = new ListUtils<String>();
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<TestCaseE
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+"")){
stepResList.add(stepBuffer.toString());
int rowIndex = this.parseIndexInRow(row);
if(rowIndex > -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<TestCaseE
// 保持插入顺序判断用例是否有相同的steps
JSONObject step = new JSONObject(true);
step.put("num", i + 1);
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));
}
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));
}
step.put("result", stepResList.get(i));
}
jsonArray.add(step);
@ -417,6 +372,32 @@ public class TestCaseDataIgnoreErrorListener extends EasyExcelListener<TestCaseE
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;
@ -446,7 +427,7 @@ public class TestCaseDataIgnoreErrorListener extends EasyExcelListener<TestCaseE
list.add(testCaseExcelData);
}
if (list.size() > BATCH_COUNT) {
if (errList.isEmpty() && list.size() > BATCH_COUNT) {
saveData();
list.clear();
}

View File

@ -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<TestCaseExcelData> {
List<String> stepResList = new ArrayList<>();
// String[] stepDesc = new String[1];
// String[] stepRes = new String[1];
ListUtils<String> listUtils = new ListUtils<String>();
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<TestCaseExcelData> {
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<TestCaseExcelData> {
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<TestCaseExcelData> {
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;