fix(测试跟踪): 导入xmind支持 自由主题风格/支持多画布

This commit is contained in:
fit2-zhao 2020-09-30 11:14:01 +08:00
parent 1c7ce5e20b
commit 8ba24261b6
4 changed files with 188 additions and 157 deletions

View File

@ -270,25 +270,34 @@ public class XmindCaseParser {
StringBuffer processBuffer = new StringBuffer(); StringBuffer processBuffer = new StringBuffer();
try { try {
// 获取思维导图内容 // 获取思维导图内容
JsonRootBean root = XmindParser.parseObject(multipartFile); List<JsonRootBean> roots = XmindParser.parseObject(multipartFile);
for (JsonRootBean root : roots) {
if (root != null && root.getRootTopic() != null && root.getRootTopic().getChildren() != null) { if (root != null && root.getRootTopic() != null && root.getRootTopic().getChildren() != null) {
// 判断是模块还是用例 // 判断是模块还是用例
for (Attached item : root.getRootTopic().getChildren().getAttached()) { for (Attached item : root.getRootTopic().getChildren().getAttached()) {
if (isAvailable(item.getTitle(), "(?:tc:|tc|tc)")) { // 用例 if (isAvailable(item.getTitle(), "(?:tc:|tc|tc)")) { // 用例
return replace(item.getTitle(), "(?:tc:|tc|tc)") + "" + Translator.get("test_case_create_module_fail"); return replace(item.getTitle(), "(?:tc:|tc|tc)") + "" + Translator.get("test_case_create_module_fail");
} else { } else {
item.setPath(item.getTitle()); String nodePath = item.getTitle();
item.setPath(nodePath);
if (item.getChildren() != null && !item.getChildren().getAttached().isEmpty()) { if (item.getChildren() != null && !item.getChildren().getAttached().isEmpty()) {
recursion(processBuffer, item, 1, item.getChildren().getAttached()); recursion(processBuffer, item, 1, item.getChildren().getAttached());
} else {
if (!nodePath.startsWith("/")) {
nodePath = "/" + nodePath;
}
if (nodePath.endsWith("/")) {
nodePath = nodePath.substring(0, nodePath.length() - 1);
}
nodePaths.add(nodePath); // 没有用例的路径
}
} }
} }
} }
} }
this.validate(); this.validate();
} catch (Exception ex) { } catch (Exception ex) {
processBuffer.append(Translator.get("incorrect_format")); return ex.getMessage();
LogUtil.error(ex.getMessage());
return "xmind "+Translator.get("incorrect_format");
} }
return process.toString(); return process.toString();
} }

View File

@ -5,6 +5,7 @@ import org.json.JSONObject;
import org.json.XML; import org.json.XML;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class XmindLegacy { public class XmindLegacy {
@ -18,7 +19,7 @@ public class XmindLegacy {
* @throws IOException * @throws IOException
* @throws DocumentException * @throws DocumentException
*/ */
public static String getContent(String xmlContent, String xmlComments) throws IOException, DocumentException { public static List<String> getContent(String xmlContent, String xmlComments) throws IOException, DocumentException {
// 删除content.xml里面不能识别的字符串 // 删除content.xml里面不能识别的字符串
xmlContent = xmlContent.replace("xmlns=\"urn:xmind:xmap:xmlns:content:2.0\"", ""); xmlContent = xmlContent.replace("xmlns=\"urn:xmind:xmap:xmlns:content:2.0\"", "");
xmlContent = xmlContent.replace("xmlns:fo=\"http://www.w3.org/1999/XSL/Format\"", ""); xmlContent = xmlContent.replace("xmlns:fo=\"http://www.w3.org/1999/XSL/Format\"", "");
@ -28,6 +29,8 @@ public class XmindLegacy {
// 去除title中svg:width属性 // 去除title中svg:width属性
xmlContent = xmlContent.replaceAll("<title svg:width=\"[0-9]*\">", "<title>"); xmlContent = xmlContent.replaceAll("<title svg:width=\"[0-9]*\">", "<title>");
//去除自由风格主题
xmlContent = xmlContent.replaceAll("<topics type=\"detached\">", "");
Document document = DocumentHelper.parseText(xmlContent);// 读取XML文件,获得document对象 Document document = DocumentHelper.parseText(xmlContent);// 读取XML文件,获得document对象
Element root = document.getRootElement(); Element root = document.getRootElement();
@ -58,22 +61,27 @@ public class XmindLegacy {
} }
// 第一个topic转换为json中的rootTopic // 第一个topic转换为json中的rootTopic
Node rootTopic = root.selectSingleNode("/xmap-content/sheet/topic"); List<Node> rootTopics = root.selectNodes("/xmap-content/sheet/topic");
for (Node rootTopic : rootTopics) {
rootTopic.setName("rootTopic"); rootTopic.setName("rootTopic");
// 将xml中topic节点转换为attached节点 // 将xml中topic节点转换为attached节点
List<Node> topicList = rootTopic.selectNodes("//topic"); List<Node> topicList = rootTopic.selectNodes("//topic");
for (Node node : topicList) { for (Node node : topicList) {
node.setName("attached"); node.setName("attached");
} }
// 选取第一个sheet
Element sheet = root.elements("sheet").get(0); }
List<String> sheets = new ArrayList<>();
for (Element sheet : root.elements("sheet")) {
String res = sheet.asXML(); String res = sheet.asXML();
// 将xml转为json // 将xml转为json
JSONObject xmlJSONObj = XML.toJSONObject(res); JSONObject xmlJSONObj = XML.toJSONObject(res);
JSONObject jsonObject = xmlJSONObj.getJSONObject("sheet"); JSONObject jsonObject = xmlJSONObj.getJSONObject("sheet");
sheets.add(jsonObject.toString(4));
}
// 设置缩进 // 设置缩进
return jsonObject.toString(4); return sheets;
} }
} }

View File

@ -34,61 +34,68 @@ public class XmindParser {
* @throws ArchiveException * @throws ArchiveException
* @throws DocumentException * @throws DocumentException
*/ */
public static String parseJson(MultipartFile multipartFile) throws IOException, ArchiveException, DocumentException { public static List<String> parseJson(MultipartFile multipartFile) throws IOException, ArchiveException, DocumentException {
File file = FileUtil.multipartFileToFile(multipartFile); File file = FileUtil.multipartFileToFile(multipartFile);
List<String> contents = null;
String res = null;
if (file == null || !file.exists()) if (file == null || !file.exists())
MSException.throwException(Translator.get("incorrect_format")); MSException.throwException(Translator.get("incorrect_format"));
try {
String res = ZipUtils.extract(file); res = ZipUtils.extract(file);
String content = null;
if (isXmindZen(res, file)) { if (isXmindZen(res, file)) {
content = getXmindZenContent(file, res); contents = (getXmindZenContent(file, res));
} else { } else {
content = getXmindLegacyContent(file, res); contents = getXmindLegacyContent(file, res);
} }
} catch (Exception e) {
MSException.throwException(e.getMessage());
} finally {
// 删除生成的文件夹 // 删除生成的文件夹
if (res != null) {
File dir = new File(res); File dir = new File(res);
FileUtil.deleteDir(dir); FileUtil.deleteDir(dir);
JsonRootBean jsonRootBean = JSON.parseObject(content, JsonRootBean.class); }
// 删除零时文件 // 删除零时文件
if (file != null) if (file != null)
file.delete(); file.delete();
String json = (JSON.toJSONString(jsonRootBean, false));
if (StringUtils.isEmpty(content) || content.split("(?:tc:|tc|TC:|TC|tc|TC)").length == 1) {
MSException.throwException(Translator.get("import_xmind_not_found"));
} }
if (!StringUtils.isEmpty(content) && content.split("(?:tc:|tc|TC:|TC|tc|TC)").length > 500) { return contents;
}
public static List<JsonRootBean> parseObject(MultipartFile multipartFile) throws DocumentException, ArchiveException, IOException {
List<String> contents = parseJson(multipartFile);
int caseCount = 0;
List<JsonRootBean> jsonRootBeans = new ArrayList<>();
if (contents != null) {
for (String content : contents) {
caseCount += content.split("(?:tc:|tc|TC:|TC|tc|TC)").length;
JsonRootBean jsonRootBean = JSON.parseObject(content, JsonRootBean.class);
jsonRootBeans.add(jsonRootBean);
}
if (caseCount > 500) {
MSException.throwException(Translator.get("import_xmind_count_error")); MSException.throwException(Translator.get("import_xmind_count_error"));
} }
return json;
} }
return jsonRootBeans;
public static JsonRootBean parseObject(MultipartFile multipartFile) throws DocumentException, ArchiveException, IOException {
String content = parseJson(multipartFile);
JsonRootBean jsonRootBean = JSON.parseObject(content, JsonRootBean.class);
return jsonRootBean;
} }
/** /**
* @return * @return
*/ */
public static String getXmindZenContent(File file, String extractFileDir) public static List<String> getXmindZenContent(File file, String extractFileDir)
throws IOException, ArchiveException { throws IOException, ArchiveException {
List<String> keys = new ArrayList<>(); List<String> keys = new ArrayList<>();
keys.add(xmindZenJson); keys.add(xmindZenJson);
Map<String, String> map = ZipUtils.getContents(keys, file, extractFileDir); Map<String, String> map = ZipUtils.getContents(keys, file, extractFileDir);
String content = map.get(xmindZenJson); String content = map.get(xmindZenJson);
content = XmindZen.getContent(content); return XmindZen.getContent(content);
return content;
} }
/** /**
* @return * @return
*/ */
public static String getXmindLegacyContent(File file, String extractFileDir) public static List<String> getXmindLegacyContent(File file, String extractFileDir)
throws IOException, ArchiveException, DocumentException { throws IOException, ArchiveException, DocumentException {
List<String> keys = new ArrayList<>(); List<String> keys = new ArrayList<>();
keys.add(xmindLegacyContent); keys.add(xmindLegacyContent);
@ -97,7 +104,7 @@ public class XmindParser {
String contentXml = map.get(xmindLegacyContent); String contentXml = map.get(xmindLegacyContent);
String commentsXml = map.get(xmindLegacyComments); String commentsXml = map.get(xmindLegacyComments);
String xmlContent = XmindLegacy.getContent(contentXml, commentsXml); List<String> xmlContent = XmindLegacy.getContent(contentXml, commentsXml);
return xmlContent; return xmlContent;
} }

View File

@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONObject;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class XmindZen { public class XmindZen {
@ -14,13 +16,18 @@ public class XmindZen {
* @throws IOException * @throws IOException
* @throws DocumentException * @throws DocumentException
*/ */
public static String getContent(String jsonContent) { public static List<String> getContent(String jsonContent) {
JSONObject jsonObject = JSONArray.parseArray(jsonContent).getJSONObject(0); JSONArray jsonArray = JSONArray.parseArray(jsonContent);//.getJSONObject(0);
List<String> contents = new ArrayList<>();
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
JSONObject rootTopic = jsonObject.getJSONObject("rootTopic"); JSONObject rootTopic = jsonObject.getJSONObject("rootTopic");
transferNotes(rootTopic); transferNotes(rootTopic);
JSONObject children = rootTopic.getJSONObject("children"); JSONObject children = rootTopic.getJSONObject("children");
recursionChildren(children); recursionChildren(children);
return jsonObject.toString(); contents.add(jsonObject.toString());
}
return contents;
} }
/** /**