refactor(接口测试): 微调脚本解析器

This commit is contained in:
fit2-zhao 2023-12-25 16:13:23 +08:00 committed by Craftsman
parent c0ade8e3aa
commit b85aa15910
3 changed files with 47 additions and 35 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.plugin.api.spi;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import lombok.Setter;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
@ -21,12 +22,9 @@ public abstract class AbstractJmeterElementConverter<T extends MsTestElement> {
* 获取转换器的函数 * 获取转换器的函数
* 主应用在实例化转换器的时候会设置 * 主应用在实例化转换器的时候会设置
*/ */
@Setter
private Function<Class<? extends MsTestElement>, AbstractJmeterElementConverter> getConverterFunc; private Function<Class<? extends MsTestElement>, AbstractJmeterElementConverter> getConverterFunc;
public void setGetConverterFunc(Function<Class<? extends MsTestElement>, AbstractJmeterElementConverter> getConverterFunc) {
this.getConverterFunc = getConverterFunc;
}
public AbstractJmeterElementConverter() { public AbstractJmeterElementConverter() {
Type genericSuperclass = getClass().getGenericSuperclass(); Type genericSuperclass = getClass().getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType parameterizedType) { if (genericSuperclass instanceof ParameterizedType parameterizedType) {
@ -46,8 +44,7 @@ public abstract class AbstractJmeterElementConverter<T extends MsTestElement> {
public void parseChild(HashTree tree, AbstractMsTestElement element, ParameterConfig config) { public void parseChild(HashTree tree, AbstractMsTestElement element, ParameterConfig config) {
if (element != null && element.getChildren() != null) { if (element != null && element.getChildren() != null) {
element.getChildren().forEach(child -> element.getChildren().forEach(child ->
getConverterFunc.apply(child.getClass()) getConverterFunc.apply(child.getClass()).toHashTree(tree, child, config));
.toHashTree(tree, child, config));
} }
} }
} }

View File

@ -29,17 +29,13 @@ public class JmeterTestElementParser implements TestElementParser {
private Boolean onSampleError; private Boolean onSampleError;
private String name; private String name;
private ParameterConfig config; private ParameterConfig config;
private boolean displayJMeterProperties = false;
private boolean displayJMeterVariables = true;
private boolean displaySystemProperties = false;
/** /**
* 解析生成 jmx 脚本 * 解析生成 jmx 脚本
* @param msTestElement *
* @param config * @param msTestElement 组件
* @return * @param config 参数配置
* @return jmx 脚本
*/ */
@Override @Override
public String parse(AbstractMsTestElement msTestElement, ParameterConfig config) { public String parse(AbstractMsTestElement msTestElement, ParameterConfig config) {
@ -53,8 +49,7 @@ public class JmeterTestElementParser implements TestElementParser {
groupTree.add(getDebugSampler()); groupTree.add(getDebugSampler());
// 解析 msTestElement // 解析 msTestElement
JmeterElementConverterRegister.getConverter(msTestElement.getClass()) JmeterElementConverterRegister.getConverter(msTestElement.getClass()).toHashTree(groupTree, msTestElement, config);
.toHashTree(groupTree, msTestElement, config);
return getJmx(hashTree); return getJmx(hashTree);
} }
@ -114,14 +109,17 @@ public class JmeterTestElementParser implements TestElementParser {
debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName()); debugSampler.setProperty(TestElement.TEST_CLASS, DebugSampler.class.getName());
debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); debugSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
debugSampler.setDisplaySystemProperties(this.displaySystemProperties); boolean displaySystemProperties = false;
debugSampler.setDisplayJMeterVariables(this.displayJMeterVariables); debugSampler.setDisplaySystemProperties(displaySystemProperties);
debugSampler.setDisplayJMeterProperties(this.displayJMeterProperties); boolean displayJMeterVariables = true;
debugSampler.setDisplayJMeterVariables(displayJMeterVariables);
boolean displayJMeterProperties = false;
debugSampler.setDisplayJMeterProperties(displayJMeterProperties);
// 上面三行直接Set属性会导致DebugSampler构建时取不到值可能是JMeter的Bug,需要SetProperty // 上面三行直接Set属性会导致DebugSampler构建时取不到值可能是JMeter的Bug,需要SetProperty
debugSampler.setProperty("displayJMeterProperties", this.displayJMeterProperties); debugSampler.setProperty("displayJMeterProperties", displayJMeterProperties);
debugSampler.setProperty("displayJMeterVariables", this.displayJMeterVariables); debugSampler.setProperty("displayJMeterVariables", displayJMeterVariables);
debugSampler.setProperty("displaySystemProperties", this.displaySystemProperties); debugSampler.setProperty("displaySystemProperties", displaySystemProperties);
return debugSampler; return debugSampler;
} }
} }

View File

@ -5,10 +5,11 @@ import io.metersphere.api.parser.jmeter.MsHTTPElementConverter;
import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter;
import io.metersphere.plugin.api.spi.MsTestElement; import io.metersphere.plugin.api.spi.MsTestElement;
import io.metersphere.plugin.sdk.util.PluginLogUtils; 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.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
/** /**
* @Author: jianxing * @Author: jianxing
@ -21,36 +22,52 @@ public class JmeterElementConverterRegister {
* key MsTestElement 实现类的 Class * key MsTestElement 实现类的 Class
* value 为对应的转换器 * value 为对应的转换器
*/ */
private static final Map<Class<? extends MsTestElement>, AbstractJmeterElementConverter<?>> parserMap = new HashMap<>(); private static final Map<Class<? extends MsTestElement>, AbstractJmeterElementConverter<? extends MsTestElement>> parserMap = new HashMap<>();
static { static {
// 注册默认的转换器 todo 注册插件的转换器 // 注册默认的转换器 todo 注册插件的转换器
JmeterElementConverterRegister.register(MsHTTPElementConverter.class); register(MsHTTPElementConverter.class);
JmeterElementConverterRegister.register(MsCommonElementConverter.class); register(MsCommonElementConverter.class);
} }
/** /**
* 注册 MsTestElement 对应的转换器 * 注册 MsTestElement 对应的转换器
* @param elementConverterClass *
* @param elementConverterClass 转换器的类
*/ */
public static void register(Class<? extends AbstractJmeterElementConverter> elementConverterClass) { public static void register(Class<? extends AbstractJmeterElementConverter<? extends MsTestElement>> elementConverterClass) {
try { try {
AbstractJmeterElementConverter elementConverter = elementConverterClass.getConstructor().newInstance(); AbstractJmeterElementConverter<? extends MsTestElement> elementConverter = elementConverterClass.getDeclaredConstructor().newInstance();
// 设置获取转换器的方法 // 设置获取转换器的方法
Function<Class<? extends MsTestElement>, AbstractJmeterElementConverter> getConverterFunc = JmeterElementConverterRegister::getConverter; elementConverter.setGetConverterFunc(JmeterElementConverterRegister::getConverter);
elementConverter.setGetConverterFunc(getConverterFunc);
// 注册到解析器集合中 // 注册到解析器集合中
parserMap.put(elementConverter.testElementClass, elementConverter); parserMap.put(elementConverter.testElementClass, elementConverter);
} catch (Exception e) { } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
PluginLogUtils.error("注册转换器失败: " + elementConverterClass); NoSuchMethodException e) {
PluginLogUtils.error(e); handleRegistrationException(elementConverterClass, e);
} }
} }
/** /**
* 获取对应组件的转换器 * 获取对应组件的转换器
*
* @param msTestElementClass 组件的类
* @return 转换器
*/ */
public static AbstractJmeterElementConverter getConverter(Class<? extends MsTestElement> msTestElementClass) { public static AbstractJmeterElementConverter getConverter(Class<? extends MsTestElement> 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);
} }
} }