From 06418c23cb45b6021474eb959e23ff8dc8f53204 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sat, 10 Oct 2020 13:57:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20=E9=9D=99=E6=80=81=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/api/jmeter/JMeterVars.java | 18 +++++- .../io/metersphere/xmind/XmindCaseParser.java | 57 +++++++++++++------ .../metersphere/xmind/parser/XmindParser.java | 27 ++++----- .../io/metersphere/xmind/parser/ZipUtils.java | 10 ++-- .../io/metersphere/xmind/utils/FileUtil.java | 8 ++- 5 files changed, 82 insertions(+), 38 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java b/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java index 7d7569520e..84b1c089fd 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java @@ -18,10 +18,18 @@ public class JMeterVars { private JMeterVars() { } - // 数据和线程变量保持一致 + /** + * 数据和线程变量保持一致 + */ private static Map variables = new HashMap<>(); - // 线程执行过程调用提取变量值 + /** + * 线程执行过程调用提取变量值 + * + * @param testId + * @param vars + * @param extract + */ public static void addVars(Integer testId, JMeterVariables vars, String extract) { JMeterVariables vs = new JMeterVariables(); @@ -36,7 +44,11 @@ public class JMeterVars { variables.put(testId, vs); } - // 处理所有请求,有提取变量的请求增加后置脚本提取变量值 + /** + * 处理所有请求,有提取变量的请求增加后置脚本提取变量值 + * + * @param tree + */ public static void addJSR223PostProcessor(HashTree tree) { for (Object key : tree.keySet()) { HashTree node = tree.get(key); diff --git a/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java b/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java index b0954d66d2..0ee85a5814 100644 --- a/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java +++ b/backend/src/main/java/io/metersphere/xmind/XmindCaseParser.java @@ -30,14 +30,25 @@ public class XmindCaseParser { private TestCaseService testCaseService; private String maintainer; private String projectId; - private StringBuffer process; // 过程校验记录 - // 已存在用例名称 + /** + * 过程校验记录 + */ + private StringBuffer process; + /** + * 已存在用例名称 + */ private Set testCaseNames; - // 转换后的案例信息 + /** + * 转换后的案例信息 + */ private List testCases; - // 案例详情重写了hashCode方法去重用 + /** + * 案例详情重写了hashCode方法去重用 + */ private List compartDatas; - // 记录没有用例的目录 + /** + * 记录没有用例的目录 + */ private List nodePaths; public XmindCaseParser(TestCaseService testCaseService, String userId, String projectId, Set testCaseNames) { @@ -89,7 +100,9 @@ public class XmindCaseParser { }); } - // 递归处理案例数据 + /** + * 递归处理案例数据 + */ private void recursion(Attached parent, int level, List attacheds) { for (Attached item : attacheds) { if (isAvailable(item.getTitle(), TC_REGEX)) { // 用例 @@ -115,23 +128,27 @@ public class XmindCaseParser { } private boolean isAvailable(String str, String regex) { - if (StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)) + if (StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)) { return false; + } Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher result = pattern.matcher(str); return result.find(); } private String replace(String str, String regex) { - if (StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)) + if (StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)) { return str; + } Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher result = pattern.matcher(str); str = result.replaceAll(""); return str; } - // 获取步骤数据 + /** + * 获取步骤数据 + */ private String getSteps(List attacheds) { JSONArray jsonArray = new JSONArray(); for (int i = 0; i < attacheds.size(); i++) { @@ -147,7 +164,9 @@ public class XmindCaseParser { return jsonArray.toJSONString(); } - // 初始化一个用例 + /** + * 初始化一个用例 + */ private void newTestCase(String title, String nodePath, List attacheds) { TestCaseWithBLOBs testCase = new TestCaseWithBLOBs(); testCase.setProjectId(projectId); @@ -157,7 +176,7 @@ public class XmindCaseParser { testCase.setType("functional"); String tc = title.replace(":", ":"); - String tcArr[] = tc.split(":"); + String[] tcArr = tc.split(":"); if (tcArr.length != 2) { process.append(Translator.get("test_case_name") + "【 " + title + " 】" + Translator.get("incorrect_format")); return; @@ -175,7 +194,7 @@ public class XmindCaseParser { // 用例等级和用例性质处理 if (tcArr[0].indexOf("-") != -1) { - String otArr[] = tcArr[0].split("-"); + String[] otArr = tcArr[0].split("-"); for (String item : otArr) { if (item.toUpperCase().startsWith("P")) { testCase.setPriority(item.toUpperCase()); @@ -222,7 +241,9 @@ public class XmindCaseParser { compartDatas.add(compartData); } - // 验证合法性 + /** + * 验证合法性 + */ private boolean validate(TestCaseWithBLOBs data) { String nodePath = data.getNodePath(); StringBuilder stringBuilder = new StringBuilder(); @@ -265,7 +286,9 @@ public class XmindCaseParser { return true; } - // 导入思维导图处理 + /** + * 导入思维导图处理 + */ public String parse(MultipartFile multipartFile) { try { // 获取思维导图内容 @@ -274,7 +297,8 @@ public class XmindCaseParser { if (root != null && root.getRootTopic() != null && root.getRootTopic().getChildren() != null) { // 判断是模块还是用例 for (Attached item : root.getRootTopic().getChildren().getAttached()) { - if (isAvailable(item.getTitle(), TC_REGEX)) { // 用例 + // 用例 + if (isAvailable(item.getTitle(), TC_REGEX)) { return replace(item.getTitle(), TC_REGEX) + ":" + Translator.get("test_case_create_module_fail"); } else { String nodePath = item.getTitle(); @@ -288,7 +312,8 @@ public class XmindCaseParser { if (nodePath.endsWith("/")) { nodePath = nodePath.substring(0, nodePath.length() - 1); } - nodePaths.add(nodePath); // 没有用例的路径 + // 没有用例的路径 + nodePaths.add(nodePath); } } } diff --git a/backend/src/main/java/io/metersphere/xmind/parser/XmindParser.java b/backend/src/main/java/io/metersphere/xmind/parser/XmindParser.java index 2d7ef83919..f93356a645 100644 --- a/backend/src/main/java/io/metersphere/xmind/parser/XmindParser.java +++ b/backend/src/main/java/io/metersphere/xmind/parser/XmindParser.java @@ -7,7 +7,6 @@ import io.metersphere.xmind.parser.pojo.JsonRootBean; import io.metersphere.xmind.utils.FileUtil; import org.apache.commons.compress.archivers.ArchiveException; import org.dom4j.DocumentException; -import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import java.io.File; @@ -21,9 +20,9 @@ import java.util.Objects; * @Description 解析主体 */ public class XmindParser { - public static final String xmindZenJson = "content.json"; - public static final String xmindLegacyContent = "content.xml"; - public static final String xmindLegacyComments = "comments.xml"; + public static final String CONTENT_JSON = "content.json"; + public static final String CONTENT_XML = "content.xml"; + public static final String COMMENTS_XML = "comments.xml"; /** * 解析脑图文件,返回content整合后的内容 @@ -38,8 +37,9 @@ public class XmindParser { File file = FileUtil.multipartFileToFile(multipartFile); List contents = null; String res = null; - if (file == null || !file.exists()) + if (file == null || !file.exists()) { MSException.throwException(Translator.get("incorrect_format")); + } try { res = ZipUtils.extract(file); if (isXmindZen(res, file)) { @@ -56,8 +56,9 @@ public class XmindParser { FileUtil.deleteDir(dir); } // 删除零时文件 - if (file != null) + if (file != null) { file.delete(); + } } return contents; } @@ -86,9 +87,9 @@ public class XmindParser { public static List getXmindZenContent(File file, String extractFileDir) throws IOException, ArchiveException { List keys = new ArrayList<>(); - keys.add(xmindZenJson); + keys.add(CONTENT_JSON); Map map = ZipUtils.getContents(keys, file, extractFileDir); - String content = map.get(xmindZenJson); + String content = map.get(CONTENT_JSON); return XmindZen.getContent(content); } @@ -98,12 +99,12 @@ public class XmindParser { public static List getXmindLegacyContent(File file, String extractFileDir) throws IOException, ArchiveException, DocumentException { List keys = new ArrayList<>(); - keys.add(xmindLegacyContent); - keys.add(xmindLegacyComments); + keys.add(CONTENT_XML); + keys.add(COMMENTS_XML); Map map = ZipUtils.getContents(keys, file, extractFileDir); - String contentXml = map.get(xmindLegacyContent); - String commentsXml = map.get(xmindLegacyComments); + String contentXml = map.get(CONTENT_XML); + String commentsXml = map.get(COMMENTS_XML); List xmlContent = XmindLegacy.getContent(contentXml, commentsXml); return xmlContent; @@ -115,7 +116,7 @@ public class XmindParser { if (parent.isDirectory()) { String[] files = parent.list(new ZipUtils.FileFilter()); for (int i = 0; i < Objects.requireNonNull(files).length; i++) { - if (files[i].equals(xmindZenJson)) { + if (files[i].equals(CONTENT_JSON)) { return true; } } diff --git a/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java b/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java index 440575096b..c82f4bbe36 100644 --- a/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java +++ b/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java @@ -14,7 +14,7 @@ import java.util.Objects; */ public class ZipUtils { - private static final String currentPath = System.getProperty("user.dir"); + private static final String CURRENT_PATH = System.getProperty("user.dir"); /** * 找到压缩文件中匹配的子文件,返回的为 getContents("comments.xml, unzip @@ -25,7 +25,7 @@ public class ZipUtils { public static Map getContents(List subFileNames, File file, String extractFileDir) throws IOException, ArchiveException { String destFilePath = extractFileDir; - Map map = new HashMap<>(); + Map map = new HashMap<>(16); File destFile = new File(destFilePath); if (destFile.isDirectory()) { String[] res = destFile.list(new FileFilter()); @@ -49,12 +49,14 @@ public class ZipUtils { */ public static String extract(File file) throws IOException, ArchiveException { Expander expander = new Expander(); - String destFileName = currentPath + File.separator + "XMind" + System.currentTimeMillis(); // 目标文件夹名字 + String destFileName = CURRENT_PATH + File.separator + "XMind" + System.currentTimeMillis(); expander.expand(file, new File(destFileName)); return destFileName; } - // 这是一个内部类过滤器,策略模式 + /** + * 这是一个内部类过滤器,策略模式 + */ static class FileFilter implements FilenameFilter { @Override public boolean accept(File dir, String name) { diff --git a/backend/src/main/java/io/metersphere/xmind/utils/FileUtil.java b/backend/src/main/java/io/metersphere/xmind/utils/FileUtil.java index 60ad9cf53f..ecb7878c75 100644 --- a/backend/src/main/java/io/metersphere/xmind/utils/FileUtil.java +++ b/backend/src/main/java/io/metersphere/xmind/utils/FileUtil.java @@ -8,9 +8,14 @@ import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; +/** + * 工具类 + */ public class FileUtil { - //获取流文件 + /** + * 获取流文件 + */ private static void inputStreamToFile(InputStream ins, File file) { try (OutputStream os = new FileOutputStream(file);) { int bytesRead = 0; @@ -57,5 +62,4 @@ public class FileUtil { return dir.delete(); } - }