mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-02 12:09:13 +08:00
refactor(接口测试): 微调脚本解析器
This commit is contained in:
parent
c0ade8e3aa
commit
b85aa15910
@ -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<T extends MsTestElement> {
|
||||
* 获取转换器的函数
|
||||
* 主应用在实例化转换器的时候会设置
|
||||
*/
|
||||
@Setter
|
||||
private Function<Class<? extends MsTestElement>, AbstractJmeterElementConverter> getConverterFunc;
|
||||
|
||||
public void setGetConverterFunc(Function<Class<? extends MsTestElement>, AbstractJmeterElementConverter> getConverterFunc) {
|
||||
this.getConverterFunc = getConverterFunc;
|
||||
}
|
||||
|
||||
public AbstractJmeterElementConverter() {
|
||||
Type genericSuperclass = getClass().getGenericSuperclass();
|
||||
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) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Class<? extends MsTestElement>, AbstractJmeterElementConverter<?>> parserMap = new HashMap<>();
|
||||
private static final Map<Class<? extends MsTestElement>, AbstractJmeterElementConverter<? extends MsTestElement>> 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<? extends AbstractJmeterElementConverter> elementConverterClass) {
|
||||
public static void register(Class<? extends AbstractJmeterElementConverter<? extends MsTestElement>> elementConverterClass) {
|
||||
try {
|
||||
AbstractJmeterElementConverter elementConverter = elementConverterClass.getConstructor().newInstance();
|
||||
AbstractJmeterElementConverter<? extends MsTestElement> elementConverter = elementConverterClass.getDeclaredConstructor().newInstance();
|
||||
// 设置获取转换器的方法
|
||||
Function<Class<? extends MsTestElement>, 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<? 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user