From ff9965b2e7e55663b9845b22ccdcca724a34d14e Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Mon, 27 Jul 2020 16:42:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=BC=E5=85=A5=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E7=94=A8=E4=BE=8B=E6=89=80=E6=9C=89=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=80=BC=E9=83=BD=E7=9B=B8=E5=90=8C=E6=89=8D=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E7=94=A8=E4=BE=8B=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/listener/EasyExcelListener.java | 3 ++ .../excel/listener/TestCaseDataListener.java | 47 +++++++++++++++---- .../track/service/TestCaseService.java | 26 ++++++++-- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java b/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java index 30d6186fd7..04c6f00074 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/EasyExcelListener.java @@ -7,6 +7,7 @@ import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.util.StringUtils; import io.metersphere.commons.utils.LogUtil; import io.metersphere.excel.domain.ExcelErrData; +import io.metersphere.excel.domain.TestCaseExcelData; import io.metersphere.excel.utils.EasyExcelI18nTranslator; import io.metersphere.excel.utils.ExcelValidateHelper; import io.metersphere.i18n.Translator; @@ -24,6 +25,8 @@ public abstract class EasyExcelListener extends AnalysisEventListener { protected EasyExcelI18nTranslator easyExcelI18nTranslator; + protected List excelDataList = new ArrayList<>(); + /** * 每隔2000条存储数据库,然后清理list ,方便内存回收 */ 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 8a200615d9..c5efa2da88 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/TestCaseDataListener.java @@ -10,10 +10,7 @@ import io.metersphere.i18n.Translator; import io.metersphere.track.service.TestCaseService; import org.apache.commons.lang3.StringUtils; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.UUID; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -57,10 +54,35 @@ public class TestCaseDataListener extends EasyExcelListener { if (!userIds.contains(data.getMaintainer())) { stringBuilder.append(Translator.get("user_not_exists") + ":" + data.getMaintainer() + "; "); } + if (testCaseNames.contains(data.getName())) { - stringBuilder.append(Translator.get("test_case_already_exists_excel") + ":" + data.getName() + "; "); + TestCaseWithBLOBs testCase = new TestCaseWithBLOBs(); + BeanUtils.copyBean(testCase, data); + testCase.setProjectId(projectId); + String steps = getSteps(data); + testCase.setSteps(steps); + + boolean dbExist = testCaseService.exist(testCase); + boolean excelExist = false; + + if (dbExist) { + // db exist + stringBuilder.append(Translator.get("test_case_already_exists_excel") + ":" + 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(); } @@ -103,6 +125,13 @@ public class TestCaseDataListener extends EasyExcelListener { testCase.setNodePath(nodePath); + String steps = getSteps(data); + testCase.setSteps(steps); + + return testCase; + } + + public String getSteps(TestCaseExcelData data) { JSONArray jsonArray = new JSONArray(); String[] stepDesc = new String[1]; @@ -124,7 +153,8 @@ public class TestCaseDataListener extends EasyExcelListener { for (int i = 0; i < index; i++) { - JSONObject step = new JSONObject(); + // 保持插入顺序,判断用例是否有相同的steps + JSONObject step = new JSONObject(true); step.put("num", i + 1); Pattern descPattern = Pattern.compile(pattern); @@ -150,10 +180,7 @@ public class TestCaseDataListener extends EasyExcelListener { jsonArray.add(step); } - - testCase.setSteps(jsonArray.toJSONString()); - - return testCase; + return jsonArray.toJSONString(); } } diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index 7307ca7d52..27ab4b3248 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -106,13 +106,16 @@ public class TestCaseService { TestCaseExample.Criteria criteria = example.createCriteria(); criteria.andNameEqualTo(testCase.getName()) .andProjectIdEqualTo(testCase.getProjectId()) - .andNodeIdEqualTo(testCase.getNodeId()) .andNodePathEqualTo(testCase.getNodePath()) .andTypeEqualTo(testCase.getType()) .andMaintainerEqualTo(testCase.getMaintainer()) .andPriorityEqualTo(testCase.getPriority()) .andMethodEqualTo(testCase.getMethod()); +// if (StringUtils.isNotBlank(testCase.getNodeId())) { +// criteria.andNodeIdEqualTo(testCase.getTestId()); +// } + if (StringUtils.isNotBlank(testCase.getTestId())) { criteria.andTestIdEqualTo(testCase.getTestId()); } @@ -371,8 +374,8 @@ public class TestCaseService { JSONArray jsonArray = JSON.parseArray(steps); for (int j = 0; j < jsonArray.size(); j++) { int num = j + 1; - step.append(num + ":" + jsonArray.getJSONObject(j).getString("desc") + "\n"); - result.append(num + ":" + jsonArray.getJSONObject(j).getString("result") + "\n"); + step.append(num + "." + jsonArray.getJSONObject(j).getString("desc") + "\n"); + result.append(num + "." + jsonArray.getJSONObject(j).getString("result") + "\n"); } data.setStepDesc(step.toString()); @@ -471,4 +474,21 @@ public class TestCaseService { return Optional.ofNullable(testCase.getNum() + 1).orElse(100001); } } + + + /** + * 导入用例前,检查数据库是否存在此用例 + * @param testCaseWithBLOBs + * @return + */ + public boolean exist(TestCaseWithBLOBs testCaseWithBLOBs) { + + try { + checkTestCaseExist(testCaseWithBLOBs); + } catch (MSException e) { + return true; + } + + return false; + } }