From 5123e143157c31595b968215e91b30079c63b556 Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Tue, 22 Aug 2023 10:11:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20=E5=8E=BB=E6=8E=89=E7=94=A8=E4=BE=8B=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E6=AD=A5=E9=AA=A4=E5=8D=95=E5=85=83=E6=A0=BC=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1029160 --user=宋昌昌 【测试跟踪】功能用例-excel导入未换行显示 https://www.tapd.cn/55049933/s/1406796 --- .../listener/TestCaseNoModelDataListener.java | 161 +++++++----------- 1 file changed, 64 insertions(+), 97 deletions(-) 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 9c58749d75..e30a6720b3 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 @@ -35,6 +35,7 @@ import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.Serial; import java.lang.reflect.Field; import java.util.*; import java.util.stream.Collectors; @@ -117,14 +118,14 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener mergeInfoSet) { this.mergeInfoSet = mergeInfoSet; - this.excelDataClass = c; - this.testCaseService = CommonBeanFactory.getBean(TestCaseService.class); - this.testCaseNodeService = CommonBeanFactory.getBean(TestCaseNodeService.class); + excelDataClass = c; + testCaseService = CommonBeanFactory.getBean(TestCaseService.class); + testCaseNodeService = CommonBeanFactory.getBean(TestCaseNodeService.class); customIds = new HashSet<>(); this.request = request; - this.customFieldValidatorMap = CustomFieldValidatorFactory.getValidatorMap(); + customFieldValidatorMap = CustomFieldValidatorFactory.getValidatorMap(); List customFields = request.getCustomFields(); if (CollectionUtils.isNotEmpty(customFields)) { @@ -138,14 +139,15 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener headMap, AnalysisContext context) { this.headMap = headMap; try { - this.genExcelHeadToFieldNameDicAndGetNotRequiredFields(); + genExcelHeadToFieldNameDicAndGetNotRequiredFields(); } catch (NoSuchFieldException e) { LogUtil.error(e); } - this.formatHeadMap(); + formatHeadMap(); super.invokeHeadMap(headMap, context); } + @Override public void invoke(Map data, AnalysisContext analysisContext) { if (headMap == null) { @@ -158,41 +160,51 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener() {{ - add(testCaseExcelData.getStepDesc()); - }}); - testCaseExcelData.setMergeStepResult(new ArrayList<>() {{ - add(testCaseExcelData.getStepResult()); - }}); + testCaseExcelData = parseDataToModel(data); + testCaseExcelData.setMergeStepDesc(new ArrayList<>() { + @Serial + private static final long serialVersionUID = -2563948462432733672L; + + { + add(testCaseExcelData.getStepDesc()); + } + }); + testCaseExcelData.setMergeStepResult(new ArrayList<>() { + @Serial + private static final long serialVersionUID = 8985001651375529701L; + + { + add(testCaseExcelData.getStepResult()); + } + }); // 记录下数据并返回 - this.currentMergeData = testCaseExcelData; - if (!this.isMergeLastRow) { + currentMergeData = testCaseExcelData; + if (!isMergeLastRow) { return; } else { - this.currentMergeData = null; + currentMergeData = null; } } else { // 获取存储的数据,并添加多个步骤 - this.currentMergeData.getMergeStepDesc() + currentMergeData.getMergeStepDesc() .add(data.get(getStepDescColIndex())); - this.currentMergeData.getMergeStepResult() + currentMergeData.getMergeStepResult() .add(data.get(getStepResultColIndex())); // 是最后一行的合并单元格,保存并清空 currentMergeData,走之后的逻辑 - if (this.isMergeLastRow) { - testCaseExcelData = this.currentMergeData; - this.currentMergeData = null; + if (isMergeLastRow) { + testCaseExcelData = currentMergeData; + currentMergeData = null; } else { return; } } } else { - this.firstMergeRowIndex = null; - testCaseExcelData = this.parseDataToModel(data); + firstMergeRowIndex = null; + testCaseExcelData = parseDataToModel(data); } buildUpdateOrErrorList(rowIndex, testCaseExcelData); @@ -246,8 +258,8 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener data, Integer rowIndex) { - this.isMergeRow = false; - this.isMergeLastRow = false; + isMergeRow = false; + isMergeLastRow = false; if (getNameColIndex() == null) { MSException.throwException("缺少名称表头"); } @@ -260,7 +272,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener getNames() { - return this.names; + return names; } public List getIds() { - return this.ids; + return ids; } public void setNames(List names) { @@ -533,27 +546,27 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener result = list.stream() - .map(item -> this.convert2TestCase(item)) + .map(item -> convert2TestCase(item)) .collect(Collectors.toList()); testCaseService.saveImportData(result, request, testCaseCustomFieldMap, pathMap); - this.names = result.stream().map(TestCase::getName).collect(Collectors.toList()); - this.ids = result.stream().map(TestCase::getId).collect(Collectors.toList()); - this.isUpdated = true; + names = result.stream().map(TestCase::getName).collect(Collectors.toList()); + ids = result.stream().map(TestCase::getId).collect(Collectors.toList()); + isUpdated = true; } if (CollectionUtils.isNotEmpty(updateList)) { List result2 = updateList.stream() - .map(item -> this.convert2TestCaseForUpdate(item)) + .map(item -> convert2TestCaseForUpdate(item)) .collect(Collectors.toList()); testCaseService.updateImportData(result2, request, testCaseCustomFieldMap); - this.isUpdated = true; - this.names = result2.stream().map(TestCase::getName).collect(Collectors.toList()); - this.ids = result2.stream().map(TestCase::getId).collect(Collectors.toList()); + isUpdated = true; + names = result2.stream().map(TestCase::getName).collect(Collectors.toList()); + ids = result2.stream().map(TestCase::getId).collect(Collectors.toList()); updateList.clear(); } } @@ -573,6 +586,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener textFieldSet = data.getTextFieldSet(); List testCaseCustomFields = new ArrayList<>(); customData.forEach((k, v) -> { - if ((v instanceof List && CollectionUtils.isNotEmpty((List)v)) + if ((v instanceof List && CollectionUtils.isNotEmpty((List) v)) || StringUtils.isNotBlank(v.toString())) { CustomFieldDao customFieldDao = customFieldsMap.get(k); if (customFieldDao != null) { @@ -678,6 +692,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener 0) { - String indexString = rowSplit[0]; - if (StringUtils.isNumeric(indexString) && indexString.equals(String.valueOf(rowIndex))) { - try { - index = Integer.parseInt(indexString); - rowMessage = StringUtils.substring(parseString, indexString.length() + splitChar.length()); - } catch (Exception e) { - } - - if (index > -1) { - break; - } else { - rowMessage = row; - } - } - } - } - } - rowInfo.index = index; - if (rowMessage == null) { - rowMessage = StringUtils.EMPTY; - } - rowInfo.rowInfo = rowMessage; - return rowInfo; - } - private Integer getNameColIndex() { return findColIndex("name"); } @@ -872,13 +839,13 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener> getErrList() { - return this.errList; + return errList; } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { // 如果文件最后一行是没有内容的步骤,这里处理最后一条合并单元格的数据 - if (this.currentMergeData != null) { + if (currentMergeData != null) { buildUpdateOrErrorList(firstMergeRowIndex, currentMergeData); } saveData();