fix: 修复更新jmx文件时未更新dubbo节点信息的缺陷

修复更新jmx文件时未更新dubbo节点信息的缺陷
This commit is contained in:
song.tianyang 2021-02-22 11:41:19 +08:00
parent 85cd5077f5
commit c737b01c27
1 changed files with 86 additions and 86 deletions

View File

@ -463,108 +463,43 @@ public class APITestService {
} }
/** /**
* 更新jmx数据处理jmx里的各种参数
*
* 与1.7分支合并时如果该方法产生冲突请以master为准
* @author song tianyang
* @param jmxString 原JMX文件 * @param jmxString 原JMX文件
* @param testNameParam 某些节点要替换的testName * @param testNameParam 某些节点要替换的testName
* @param isFromScenario 是否来源于场景 来源于场景的话testName要进行处理 * @param isFromScenario 是否来源于场景 来源于场景的话testName要进行处理
* @return * @return
*/ */
public String updateJmxString(String jmxString, String testNameParam, boolean isFromScenario) { public String updateJmxString(String jmxString, String testNameParam, boolean isFromScenario) {
// 与1.7分支合并时如果该方法产生冲突请以master为准
String attribute_testName = "testname";
String [] requestElementNameArr = new String[]{"HTTPSamplerProxy","TCPSampler","JDBCSampler","DubboSample"};
try { try {
//将ThreadGroup的testname改为接口名称 //将ThreadGroup的testname改为接口名称
Document doc = DocumentHelper.parseText(jmxString);// 获取可续保保单列表报文模板 Document doc = DocumentHelper.parseText(jmxString);// 获取可续保保单列表报文模板
Element root = doc.getRootElement(); Element root = doc.getRootElement();
Element rootHashTreeElement = root.element("hashTree"); Element rootHashTreeElement = root.element("hashTree");
Element innerHashTreeElement = rootHashTreeElement.elements("hashTree").get(0);
List<Element> thirdHashTreeElementList = innerHashTreeElement.elements("hashTree");
Element theadGroupElement = innerHashTreeElement.elements("ThreadGroup").get(0); List<Element> innerHashTreeElementList = rootHashTreeElement.elements("hashTree");
theadGroupElement.attribute("testname").setText(testNameParam); for (Element innerHashTreeElement : innerHashTreeElementList) {
List<Element> thirdHashTreeElementList = innerHashTreeElement.elements("hashTree");
for (Element element : thirdHashTreeElementList) { List<Element> theadGroupElementList = innerHashTreeElement.elements("ThreadGroup");
String testName = testNameParam; for (Element theadGroupElement : theadGroupElementList) {
List<Element> sampleProxyElementList = element.elements("HTTPSamplerProxy"); if(StringUtils.isNotEmpty(testNameParam)){
for (Element itemElement : sampleProxyElementList) { theadGroupElement.attribute(attribute_testName).setText(testNameParam);
if(isFromScenario){
testName = itemElement.attributeValue("testname");
String[] testNameArr = testName.split("<->");
if (testNameArr.length > 0) {
testName = testNameArr[0];
}
} }
itemElement.attribute("testname").setText(testName);
}
List<Element> tcpSamplerList = element.elements("TCPSampler");
for (Element itemElement : tcpSamplerList) {
if(isFromScenario){
testName = itemElement.attributeValue("testname");
String[] testNameArr = testName.split("<->");
if (testNameArr.length > 0) {
testName = testNameArr[0];
}
}
itemElement.attribute("testname").setText(testName);
}
List<Element> jdbcSamplerList = element.elements("JDBCSampler");
for (Element itemElement : jdbcSamplerList) {
if(isFromScenario){
testName = itemElement.attributeValue("testname");
String[] testNameArr = testName.split("<->");
if (testNameArr.length > 0) {
testName = testNameArr[0];
}
}
itemElement.attribute("testname").setText(testName);
}
List<Element> dubboSampleList = element.elements("DubboSample");
for (Element itemElement : dubboSampleList) {
if(isFromScenario){
testName = itemElement.attributeValue("testname");
String[] testNameArr = testName.split("<->");
if (testNameArr.length > 0) {
testName = testNameArr[0];
}
}
itemElement.attribute("testname").setText(testName);
//dubbo节点要更新 标签guiClass testClass
itemElement.setName("io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample");
itemElement.attribute("testclass").setText("io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample");
itemElement.attribute("guiclass").setText("io.github.ningyu.jmeter.plugin.dubbo.gui.DubboSampleGui");
} }
//检查有没有自定义参数 for (Element element : thirdHashTreeElementList) {
List<Element> scriptHashTreeElementList = element.elements("hashTree"); String testName = testNameParam;
for (Element scriptHashTreeElement : scriptHashTreeElementList) {
boolean isRemove = false;
List<Element> removeElement = new ArrayList<>();
List<Element> scriptElementItemList = scriptHashTreeElement.elements();
for (Element hashTreeItemElement : scriptElementItemList) {
String className = hashTreeItemElement.attributeValue("testclass");
String qname = hashTreeItemElement.getQName().getName();
if (isRemove) { //更新请求类节点的部份属性
if (org.apache.commons.lang3.StringUtils.equals("hashTree", qname)) { this.updateRequestElementInfo(element,testNameParam,requestElementNameArr,isFromScenario);
removeElement.add(hashTreeItemElement); //检查有无jmeter不是别的自定义参数
} this.checkPrivateFunctionNode(element);
}
isRemove = false;
if (org.apache.commons.lang3.StringUtils.equals(className, "JSR223PostProcessor")) {
List<Element> scriptElements = hashTreeItemElement.elements("stringProp");
for (Element scriptElement : scriptElements) {
String scriptName = scriptElement.attributeValue("name");
String contentValue = scriptElement.getStringValue();
if ("script".equals(scriptName) && contentValue.startsWith("io.metersphere.api.jmeter.JMeterVars.addVars")) {
isRemove = true;
removeElement.add(hashTreeItemElement);
}
}
}
}
for (Element itemElement : removeElement) {
scriptHashTreeElement.remove(itemElement);
}
} }
} }
jmxString = root.asXML(); jmxString = root.asXML();
@ -573,4 +508,69 @@ public class APITestService {
} }
return jmxString; return jmxString;
} }
private void checkPrivateFunctionNode(Element element) {
List<Element> scriptHashTreeElementList = element.elements("hashTree");
for (Element scriptHashTreeElement : scriptHashTreeElementList) {
boolean isRemove = false;
List<Element> removeElement = new ArrayList<>();
List<Element> scriptElementItemList = scriptHashTreeElement.elements();
for (Element hashTreeItemElement : scriptElementItemList) {
String className = hashTreeItemElement.attributeValue("testclass");
String qname = hashTreeItemElement.getQName().getName();
if (isRemove) {
if (org.apache.commons.lang3.StringUtils.equals("hashTree", qname)) {
removeElement.add(hashTreeItemElement);
}
}
isRemove = false;
if (org.apache.commons.lang3.StringUtils.equals(className, "JSR223PostProcessor")) {
List<Element> scriptElements = hashTreeItemElement.elements("stringProp");
for (Element scriptElement : scriptElements) {
String scriptName = scriptElement.attributeValue("name");
String contentValue = scriptElement.getStringValue();
if ("script".equals(scriptName) && contentValue.startsWith("io.metersphere.api.jmeter.JMeterVars.addVars")) {
isRemove = true;
removeElement.add(hashTreeItemElement);
}
}
}
}
for (Element itemElement : removeElement) {
scriptHashTreeElement.remove(itemElement);
}
}
}
private void updateRequestElementInfo(Element element,String testNameParam,String [] requestElementNameArr,boolean isFromScenario){
String attribute_testName = "testname";
String scenarioCaseNameSplit = "<->";
String testName = testNameParam;
for(String requestElementName : requestElementNameArr){
List<Element> sampleProxyElementList = element.elements(requestElementName);
for (Element itemElement : sampleProxyElementList) {
if(isFromScenario){
testName = itemElement.attributeValue(attribute_testName);
String[] testNameArr = testName.split(scenarioCaseNameSplit);
if (testNameArr.length > 0) {
testName = testNameArr[0];
}
}
itemElement.attribute(attribute_testName).setText(testName);
//double的话有额外处理方式
if(StringUtils.equals(requestElementName,"DubboSample")){
//dubbo节点要更新 标签guiClass testClass
itemElement.setName("io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample");
itemElement.attribute("testclass").setText("io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample");
itemElement.attribute("guiclass").setText("io.github.ningyu.jmeter.plugin.dubbo.gui.DubboSampleGui");
}
}
}
}
} }