From c737b01c272ee7db5c90f834e8e98424ea6124b4 Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Mon, 22 Feb 2021 11:41:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=9B=B4=E6=96=B0jmx?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=97=B6=E6=9C=AA=E6=9B=B4=E6=96=B0dubbo?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E4=BF=A1=E6=81=AF=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复更新jmx文件时未更新dubbo节点信息的缺陷 --- .../api/service/APITestService.java | 172 +++++++++--------- 1 file changed, 86 insertions(+), 86 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index fe6cfde712..b0b4387f3d 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -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 thirdHashTreeElementList = innerHashTreeElement.elements("hashTree"); - Element theadGroupElement = innerHashTreeElement.elements("ThreadGroup").get(0); - theadGroupElement.attribute("testname").setText(testNameParam); - - for (Element element : thirdHashTreeElementList) { - String testName = testNameParam; - List 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 innerHashTreeElementList = rootHashTreeElement.elements("hashTree"); + for (Element innerHashTreeElement : innerHashTreeElementList) { + List thirdHashTreeElementList = innerHashTreeElement.elements("hashTree"); + List theadGroupElementList = innerHashTreeElement.elements("ThreadGroup"); + for (Element theadGroupElement : theadGroupElementList) { + if(StringUtils.isNotEmpty(testNameParam)){ + theadGroupElement.attribute(attribute_testName).setText(testNameParam); } - itemElement.attribute("testname").setText(testName); - } - List 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 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 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 scriptHashTreeElementList = element.elements("hashTree"); - for (Element scriptHashTreeElement : scriptHashTreeElementList) { - boolean isRemove = false; - List removeElement = new ArrayList<>(); - List 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 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 scriptHashTreeElementList = element.elements("hashTree"); + for (Element scriptHashTreeElement : scriptHashTreeElementList) { + boolean isRemove = false; + List removeElement = new ArrayList<>(); + List 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 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 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"); + } + + } + } + } }