diff --git a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java index 6aad24c7cc..400a0079e4 100644 --- a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java +++ b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java @@ -2,6 +2,7 @@ package io.metersphere.plugin.api.spi; import io.metersphere.plugin.api.dto.ParameterConfig; +import lombok.Setter; import org.apache.jorphan.collections.HashTree; import java.lang.reflect.ParameterizedType; @@ -21,12 +22,9 @@ public abstract class AbstractJmeterElementConverter { * 获取转换器的函数 * 主应用在实例化转换器的时候会设置 */ + @Setter private Function, AbstractJmeterElementConverter> getConverterFunc; - public void setGetConverterFunc(Function, AbstractJmeterElementConverter> getConverterFunc) { - this.getConverterFunc = getConverterFunc; - } - public AbstractJmeterElementConverter() { Type genericSuperclass = getClass().getGenericSuperclass(); if (genericSuperclass instanceof ParameterizedType parameterizedType) { @@ -46,8 +44,7 @@ public abstract class AbstractJmeterElementConverter { public void parseChild(HashTree tree, AbstractMsTestElement element, ParameterConfig config) { if (element != null && element.getChildren() != null) { element.getChildren().forEach(child -> - getConverterFunc.apply(child.getClass()) - .toHashTree(tree, child, config)); + getConverterFunc.apply(child.getClass()).toHashTree(tree, child, config)); } } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java index 757db53ad9..057a479223 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/JmeterTestElementParser.java @@ -29,17 +29,13 @@ public class JmeterTestElementParser implements TestElementParser { private Boolean onSampleError; private String name; private ParameterConfig config; - private boolean displayJMeterProperties = false; - - private boolean displayJMeterVariables = true; - - private boolean displaySystemProperties = false; /** * 解析生成 jmx 脚本 - * @param msTestElement - * @param config - * @return + * + * @param msTestElement 组件 + * @param config 参数配置 + * @return jmx 脚本 */ @Override public String parse(AbstractMsTestElement msTestElement, ParameterConfig config) { @@ -53,8 +49,7 @@ public class JmeterTestElementParser implements TestElementParser { groupTree.add(getDebugSampler()); // 解析 msTestElement - JmeterElementConverterRegister.getConverter(msTestElement.getClass()) - .toHashTree(groupTree, msTestElement, config); + JmeterElementConverterRegister.getConverter(msTestElement.getClass()).toHashTree(groupTree, msTestElement, config); return getJmx(hashTree); } @@ -114,14 +109,17 @@ public class JmeterTestElementParser implements TestElementParser { debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName()); debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); - debugSampler.setDisplaySystemProperties(this.displaySystemProperties); - debugSampler.setDisplayJMeterVariables(this.displayJMeterVariables); - debugSampler.setDisplayJMeterProperties(this.displayJMeterProperties); + boolean displaySystemProperties = false; + debugSampler.setDisplaySystemProperties(displaySystemProperties); + boolean displayJMeterVariables = true; + debugSampler.setDisplayJMeterVariables(displayJMeterVariables); + boolean displayJMeterProperties = false; + debugSampler.setDisplayJMeterProperties(displayJMeterProperties); // 上面三行直接Set属性会导致DebugSampler构建时取不到值,可能是JMeter的Bug,需要SetProperty - debugSampler.setProperty("displayJMeterProperties", this.displayJMeterProperties); - debugSampler.setProperty("displayJMeterVariables", this.displayJMeterVariables); - debugSampler.setProperty("displaySystemProperties", this.displaySystemProperties); + debugSampler.setProperty("displayJMeterProperties", displayJMeterProperties); + debugSampler.setProperty("displayJMeterVariables", displayJMeterVariables); + debugSampler.setProperty("displaySystemProperties", displaySystemProperties); return debugSampler; } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java index a95fec7085..8dd611331e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/JmeterElementConverterRegister.java @@ -5,10 +5,11 @@ import io.metersphere.api.parser.jmeter.MsHTTPElementConverter; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.MsTestElement; import io.metersphere.plugin.sdk.util.PluginLogUtils; +import io.metersphere.sdk.exception.MSException; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; -import java.util.function.Function; /** * @Author: jianxing @@ -21,36 +22,52 @@ public class JmeterElementConverterRegister { * key 为 MsTestElement 实现类的 Class * value 为对应的转换器 */ - private static final Map, AbstractJmeterElementConverter> parserMap = new HashMap<>(); + private static final Map, AbstractJmeterElementConverter> parserMap = new HashMap<>(); static { // 注册默认的转换器 todo 注册插件的转换器 - JmeterElementConverterRegister.register(MsHTTPElementConverter.class); - JmeterElementConverterRegister.register(MsCommonElementConverter.class); + register(MsHTTPElementConverter.class); + register(MsCommonElementConverter.class); } /** * 注册 MsTestElement 对应的转换器 - * @param elementConverterClass + * + * @param elementConverterClass 转换器的类 */ - public static void register(Class elementConverterClass) { + public static void register(Class> elementConverterClass) { try { - AbstractJmeterElementConverter elementConverter = elementConverterClass.getConstructor().newInstance(); + AbstractJmeterElementConverter elementConverter = elementConverterClass.getDeclaredConstructor().newInstance(); // 设置获取转换器的方法 - Function, AbstractJmeterElementConverter> getConverterFunc = JmeterElementConverterRegister::getConverter; - elementConverter.setGetConverterFunc(getConverterFunc); + elementConverter.setGetConverterFunc(JmeterElementConverterRegister::getConverter); // 注册到解析器集合中 parserMap.put(elementConverter.testElementClass, elementConverter); - } catch (Exception e) { - PluginLogUtils.error("注册转换器失败: " + elementConverterClass); - PluginLogUtils.error(e); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | + NoSuchMethodException e) { + handleRegistrationException(elementConverterClass, e); } } /** * 获取对应组件的转换器 + * + * @param msTestElementClass 组件的类 + * @return 转换器 */ public static AbstractJmeterElementConverter getConverter(Class msTestElementClass) { - return parserMap.get(msTestElementClass); + return parserMap.computeIfAbsent(msTestElementClass, cls -> { + throw new MSException("No corresponding converter found: " + cls); + }); + } + + /** + * 处理注册转换器时的异常 + * + * @param elementConverterClass 转换器的类 + * @param e 异常 + */ + private static void handleRegistrationException(Class elementConverterClass, Exception e) { + PluginLogUtils.error("注册转换器失败: " + elementConverterClass, e); } } +