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 testNameParam 某些节点要替换的testName
* @param isFromScenario 是否来源于场景 来源于场景的话testName要进行处理
* @return
*/
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 {
//将ThreadGroup的testname改为接口名称
Document doc = DocumentHelper.parseText(jmxString);// 获取可续保保单列表报文模板
Element root = doc.getRootElement();
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);
theadGroupElement.attribute("testname").setText(testNameParam);
for (Element element : thirdHashTreeElementList) {
String testName = testNameParam;
List<Element> sampleProxyElementList = element.elements("HTTPSamplerProxy");
for (Element itemElement : sampleProxyElementList) {
if(isFromScenario){
testName = itemElement.attributeValue("testname");
String[] testNameArr = testName.split("<->");
if (testNameArr.length > 0) {
testName = testNameArr[0];
}
List<Element> innerHashTreeElementList = rootHashTreeElement.elements("hashTree");
for (Element innerHashTreeElement : innerHashTreeElementList) {
List<Element> thirdHashTreeElementList = innerHashTreeElement.elements("hashTree");
List<Element> theadGroupElementList = innerHashTreeElement.elements("ThreadGroup");
for (Element theadGroupElement : theadGroupElementList) {
if(StringUtils.isNotEmpty(testNameParam)){
theadGroupElement.attribute(attribute_testName).setText(testNameParam);
}
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");
}
//检查有没有自定义参数
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();
for (Element element : thirdHashTreeElementList) {
String testName = testNameParam;
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);
}
//更新请求类节点的部份属性
this.updateRequestElementInfo(element,testNameParam,requestElementNameArr,isFromScenario);
//检查有无jmeter不是别的自定义参数
this.checkPrivateFunctionNode(element);
}
}
jmxString = root.asXML();
@ -573,4 +508,69 @@ public class APITestService {
}
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");
}
}
}
}
}