From 3c64217203393543022a826f90c7732508f6b01a Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Wed, 9 Aug 2023 16:30:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20=E5=8A=9F=E8=83=BD=E7=94=A8=E4=BE=8B=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E7=AC=A6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1012551 --user=宋昌昌 #25705【功能用例】导入用例换行符优化 https://www.tapd.cn/55049933/s/1401943 --- .../excel/constants/TestCaseImportFiled.java | 2 +- .../FunctionCaseTemplateWriteHandler.java | 8 +++ .../listener/TestCaseNoModelDataListener.java | 64 ++++++++++++------- .../resources/i18n/messages_en_US.properties | 2 + .../resources/i18n/messages_zh_CN.properties | 2 + .../resources/i18n/messages_zh_TW.properties | 2 + 6 files changed, 56 insertions(+), 24 deletions(-) diff --git a/test-track/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java b/test-track/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java index 65bcea77ff..ca18f7c150 100644 --- a/test-track/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java +++ b/test-track/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java @@ -20,10 +20,10 @@ public enum TestCaseImportFiled { MODULE("module","所属模块", "所屬模塊", "Module", TestCaseExcelData::getNodePath), TAGS("tags","标签", "標簽", "Tag", TestCaseImportFiled::parseTags), PREREQUISITE("prerequisite","前置条件", "前置條件", "Prerequisite", TestCaseExcelData::getPrerequisite), - REMARK("remark","备注", "備註", "Remark", TestCaseExcelData::getRemark), STEP_DESC("stepDesc","步骤描述", "步驟描述", "Step description", TestCaseExcelData::getStepDesc), STEP_RESULT("stepResult","预期结果", "預期結果", "Step result", TestCaseExcelData::getStepResult), STEP_MODEL("stepModel","编辑模式", "編輯模式", "Edit Model", TestCaseExcelData::getStepModel), + REMARK("remark","备注", "備註", "Remark", TestCaseExcelData::getRemark), STATUS("status","用例状态", "用例狀態", "Case status", TestCaseExcelData::getStatus), MAINTAINER("maintainer","责任人", "責任人", "Maintainer", TestCaseExcelData::getMaintainer), PRIORITY("priority","用例等级", "用例等級", "Priority", TestCaseExcelData::getPriority); diff --git a/test-track/backend/src/main/java/io/metersphere/excel/handler/FunctionCaseTemplateWriteHandler.java b/test-track/backend/src/main/java/io/metersphere/excel/handler/FunctionCaseTemplateWriteHandler.java index 41733a31e3..d731492288 100644 --- a/test-track/backend/src/main/java/io/metersphere/excel/handler/FunctionCaseTemplateWriteHandler.java +++ b/test-track/backend/src/main/java/io/metersphere/excel/handler/FunctionCaseTemplateWriteHandler.java @@ -34,6 +34,8 @@ public class FunctionCaseTemplateWriteHandler implements RowWriteHandler { private Integer priorityIndex; private Integer tagIndex; private Integer statusIndex; + private Integer stepDescIndex; + private Integer stepResultIndex; private Integer stepModelIndex; private Sheet sheet; @@ -61,6 +63,10 @@ public class FunctionCaseTemplateWriteHandler implements RowWriteHandler { tagIndex = index; } else if (TestCaseImportFiled.STATUS.containsHead(head)) { statusIndex = index; + } else if (TestCaseImportFiled.STEP_DESC.containsHead(head)) { + stepDescIndex = index; + } else if (TestCaseImportFiled.STEP_RESULT.containsHead(head)) { + stepResultIndex = index; } else if (TestCaseImportFiled.STEP_MODEL.containsHead(head)) { stepModelIndex = index; } @@ -83,6 +89,8 @@ public class FunctionCaseTemplateWriteHandler implements RowWriteHandler { setComment(moduleIndex, Translator.get("module_created_automatically")); setComment(maintainerIndex, Translator.get("please_input_project_member")); setComment(tagIndex, Translator.get("tag_tip_pattern")); + setComment(stepDescIndex, Translator.get("step_desc_tip")); + setComment(stepResultIndex, Translator.get("step_result_tip")); setComment(stepModelIndex, Translator.get("step_model_tip")); List list = new ArrayList<>(); diff --git a/test-track/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java b/test-track/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java index 2ef67a45ce..bfbe497edb 100644 --- a/test-track/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java +++ b/test-track/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java @@ -685,41 +685,61 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener(); + List> steps = new ArrayList<>(); - // 如果是合并单元格,则组合多条单元格的数据 - if (CollectionUtils.isNotEmpty(data.getMergeStepDesc()) - || CollectionUtils.isNotEmpty(data.getMergeStepResult())) { + if (CollectionUtils.isNotEmpty(data.getMergeStepDesc()) || CollectionUtils.isNotEmpty(data.getMergeStepResult())) { + // 如果是合并单元格,则组合多条单元格的数据 for (int i = 0; i < data.getMergeStepDesc().size(); i++) { - Map step = new LinkedHashMap<>(); - step.put("num", i + 1); - step.put("desc", Optional.ofNullable(data.getMergeStepDesc().get(i)).orElse(StringUtils.EMPTY)); - step.put("result", Optional.ofNullable(data.getMergeStepResult().get(i)).orElse(StringUtils.EMPTY)); - jsonArray.add(step); + List> rowSteps = getSingleRowSteps(data.getMergeStepDesc().get(i), data.getMergeStepResult().get(i), steps.size()); + steps.addAll(rowSteps); } - return JSON.toJSONString(jsonArray); + } else { + // 如果不是合并单元格,则直接解析单元格数据 + steps.addAll(getSingleRowSteps(data.getStepDesc(), data.getStepResult(), steps.size())); } + return JSON.toJSONString(steps); + } + + /** + * 解析单行步骤描述, 预期结果数据 + * @param cellDesc 步骤描述 + * @param cellResult 预期结果 + * @param startStepIndex 步骤开始序号 + * @return 步骤JSON-String + */ + private List> getSingleRowSteps(String cellDesc, String cellResult, Integer startStepIndex) { + List> steps = new ArrayList<>(); List stepDescList = new ArrayList<>(); List stepResList = new ArrayList<>(); - - Set rowNums = new HashSet<>(); - if (data.getStepDesc() != null) { - String[] stepDesc = data.getStepDesc().split("\r|\n|\r\n"); + if (StringUtils.isNotEmpty(cellDesc)) { + // 根据[1], [2]...分割步骤描述, 开头空字符去掉, 末尾保留 + String[] stepDesc = cellDesc.split("\\[\\d+]", -1); + if (StringUtils.isEmpty(stepDesc[0])) { + stepDesc = Arrays.copyOfRange(stepDesc, 1, stepDesc.length); + } int rowIndex = 1; for (String row : stepDesc) { RowInfo rowInfo = this.parseIndexInRow(row, rowIndex); stepDescList.add(rowInfo.rowInfo); - rowNums.add(rowIndex++); } } else { stepDescList.add(StringUtils.EMPTY); } - if (data.getStepResult() != null) { - String[] stepRes = data.getStepResult().split("\r|\n|\r\n"); + if (StringUtils.isNotEmpty(cellResult)) { + // 根据[1], [2]...分割步骤描述, 开头空字符去掉, 末尾保留 + String[] stepRes = cellResult.split("\\[\\d+]", -1); + if (StringUtils.isEmpty(stepRes[0])) { + stepRes = Arrays.copyOfRange(stepRes, 1, stepRes.length); + } int lastStepIndex = 1; for (String row : stepRes) { RowInfo rowInfo = this.parseIndexInRow(row, lastStepIndex); @@ -731,13 +751,11 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener stepResList.size() ? stepDescList.size() : stepResList.size(); - + int index = Math.max(stepDescList.size(), stepResList.size()); for (int i = 0; i < index; i++) { - // 保持插入顺序,判断用例是否有相同的steps Map step = new LinkedHashMap<>(); - step.put("num", i + 1); + step.put("num", startStepIndex + i + 1); if (i < stepDescList.size()) { step.put("desc", stepDescList.get(i)); } else { @@ -750,9 +768,9 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener