refactor: 提取方法
This commit is contained in:
parent
b79c386d71
commit
9f6ec5c6dc
|
@ -21,6 +21,7 @@
|
|||
<nacos.version>1.1.3</nacos.version>
|
||||
<dubbo.version>2.7.8</dubbo.version>
|
||||
<groovy.version>3.0.8</groovy.version>
|
||||
<metersphere-jmeter-functions.version>1.2</metersphere-jmeter-functions.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -206,6 +207,12 @@
|
|||
<version>${jmeter.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.metersphere</groupId>
|
||||
<artifactId>metersphere-jmeter-functions</artifactId>
|
||||
<version>${metersphere-jmeter-functions.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>mssql-jdbc</artifactId>
|
||||
|
@ -577,6 +584,15 @@
|
|||
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
|
||||
<destFileName>ApacheJMeter_functions.jar</destFileName>
|
||||
</artifactItem>
|
||||
<artifactItem>
|
||||
<groupId>io.metersphere</groupId>
|
||||
<artifactId>metersphere-jmeter-functions</artifactId>
|
||||
<version>${metersphere-jmeter-functions.version}</version>
|
||||
<type>jar</type>
|
||||
<overWrite>true</overWrite>
|
||||
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
|
||||
<destFileName>metersphere-jmeter-functions.jar</destFileName>
|
||||
</artifactItem>
|
||||
<artifactItem>
|
||||
<groupId>org.python</groupId>
|
||||
<artifactId>jython-standalone</artifactId>
|
||||
|
|
|
@ -3,7 +3,7 @@ package io.metersphere.api.dto.definition.request;
|
|||
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
|
||||
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
|
||||
import io.metersphere.api.dto.ssl.MsKeyStore;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import lombok.Data;
|
||||
import org.apache.jmeter.config.Arguments;
|
||||
|
||||
|
@ -57,7 +57,7 @@ public class ParameterConfig {
|
|||
static public Arguments valueSupposeMock(Arguments arguments) {
|
||||
for(int i = 0; i < arguments.getArguments().size(); ++i) {
|
||||
String argValue = arguments.getArgument(i).getValue();
|
||||
arguments.getArgument(i).setValue(ScriptEngineUtils.calculate(argValue));
|
||||
arguments.getArgument(i).setValue(ScriptEngineUtils.buildFunctionCallString(argValue));
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import io.metersphere.commons.exception.MSException;
|
|||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.FileUtils;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import io.metersphere.track.service.TestPlanApiCaseService;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
@ -542,7 +542,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
Map<String, String> keyValueMap = new HashMap<>();
|
||||
this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).forEach(keyValue ->
|
||||
keyValueMap.put(keyValue.getName(), keyValue.getValue() != null && keyValue.getValue().startsWith("@") ?
|
||||
ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue())
|
||||
ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue())
|
||||
);
|
||||
try {
|
||||
Pattern p = Pattern.compile("(\\{)([\\w]+)(\\})");
|
||||
|
@ -567,7 +567,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
stringBuffer.append(keyValue.getName());
|
||||
if (keyValue.getValue() != null) {
|
||||
stringBuffer.append("=").append(keyValue.getValue().startsWith("@") ?
|
||||
ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue());
|
||||
ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue());
|
||||
}
|
||||
stringBuffer.append("&");
|
||||
});
|
||||
|
@ -577,7 +577,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
private Arguments httpArguments(List<KeyValue> list) {
|
||||
Arguments arguments = new Arguments();
|
||||
list.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> {
|
||||
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue());
|
||||
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue());
|
||||
if (keyValue.getValue() == null) {
|
||||
httpArgument.setValue("");
|
||||
}
|
||||
|
@ -599,7 +599,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
headerManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HeaderPanel"));
|
||||
// header 也支持 mock 参数
|
||||
headers.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
|
||||
headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.calculate(keyValue.getValue())))
|
||||
headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.buildFunctionCallString(keyValue.getValue())))
|
||||
);
|
||||
if (headerManager.getHeaders().size() > 0) {
|
||||
tree.add(headerManager);
|
||||
|
|
|
@ -20,7 +20,7 @@ import io.metersphere.commons.constants.DelimiterConstants;
|
|||
import io.metersphere.commons.constants.MsTestElementConstants;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
@ -273,7 +273,7 @@ public class MsTCPSampler extends MsTestElement {
|
|||
String findStr = m.group();
|
||||
if(findStr.length() > 3){
|
||||
findStr = findStr.substring(1,findStr.length()-2);
|
||||
String replaceStr = ScriptEngineUtils.calculate(findStr);
|
||||
String replaceStr = ScriptEngineUtils.buildFunctionCallString(findStr);
|
||||
if(StringUtils.equals(findStr,replaceStr)){
|
||||
replaceStr = "";
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSONObject;
|
|||
import io.metersphere.api.dto.scenario.request.BodyFile;
|
||||
import io.metersphere.commons.json.JSONSchemaGenerator;
|
||||
import io.metersphere.commons.utils.FileUtils;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -101,7 +101,7 @@ public class Body {
|
|||
jsonMockParse((JSONObject) value);
|
||||
} else if(value instanceof String) {
|
||||
if (StringUtils.isNotBlank((String) value)) {
|
||||
value = ScriptEngineUtils.calculate((String) value);
|
||||
value = ScriptEngineUtils.buildFunctionCallString((String) value);
|
||||
}
|
||||
jsonObject.put(key, value);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.metersphere.api.parse;
|
||||
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
@ -154,7 +154,7 @@ public class JmeterDocumentParser {
|
|||
if (!StringUtils.equals("?", u)) {
|
||||
u += "&";
|
||||
}
|
||||
u += k + "=" + ScriptEngineUtils.calculate(v);
|
||||
u += k + "=" + ScriptEngineUtils.buildFunctionCallString(v);
|
||||
return u;
|
||||
});
|
||||
ele.setTextContent(url + ((params != null && !"?".equals(params)) ? params : ""));
|
||||
|
@ -162,7 +162,7 @@ public class JmeterDocumentParser {
|
|||
case "Argument.value":
|
||||
String textContent = ele.getTextContent();
|
||||
if (StringUtils.startsWith(textContent, "@")) {
|
||||
ele.setTextContent(ScriptEngineUtils.calculate(textContent));
|
||||
ele.setTextContent(ScriptEngineUtils.buildFunctionCallString(textContent));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.metersphere.api.parse.old;
|
||||
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
@ -158,7 +158,7 @@ public class JmeterDocumentParser {
|
|||
}
|
||||
// 排除 jmeter 内置的函数
|
||||
if (!v.startsWith("$")) {
|
||||
v = ScriptEngineUtils.calculate(v);
|
||||
v = ScriptEngineUtils.buildFunctionCallString(v);
|
||||
// urlencoder
|
||||
try {
|
||||
v = URLEncoder.encode(v, "UTF-8");
|
||||
|
@ -176,7 +176,7 @@ public class JmeterDocumentParser {
|
|||
if (item.endsWith("/")) {
|
||||
item = item.substring(0, item.length() - 1);
|
||||
}
|
||||
url = url.replace("@" + item, ScriptEngineUtils.calculate("@" + item));
|
||||
url = url.replace("@" + item, ScriptEngineUtils.buildFunctionCallString("@" + item));
|
||||
}
|
||||
}
|
||||
ele.setTextContent(url + ((params != null && !params.equals("?")) ? params : ""));
|
||||
|
@ -184,7 +184,7 @@ public class JmeterDocumentParser {
|
|||
case "Argument.value":
|
||||
String textContent = ele.getTextContent();
|
||||
if (StringUtils.startsWith(textContent, "@")) {
|
||||
ele.setTextContent(ScriptEngineUtils.calculate(textContent));
|
||||
ele.setTextContent(ScriptEngineUtils.buildFunctionCallString(textContent));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -12,7 +12,7 @@ import io.metersphere.base.mapper.MockConfigMapper;
|
|||
import io.metersphere.base.mapper.MockExpectConfigMapper;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.JsonPathUtils;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import io.metersphere.commons.utils.SessionUtils;
|
||||
import io.metersphere.i18n.Translator;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -354,7 +354,7 @@ public class MockConfigService {
|
|||
values = "";
|
||||
} else {
|
||||
try {
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values;
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
@ -381,7 +381,7 @@ public class MockConfigService {
|
|||
values = "";
|
||||
} else {
|
||||
try {
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values;
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
@ -453,7 +453,7 @@ public class MockConfigService {
|
|||
values = "";
|
||||
} else {
|
||||
try {
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values;
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
@ -471,7 +471,7 @@ public class MockConfigService {
|
|||
values = "";
|
||||
} else {
|
||||
try {
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values;
|
||||
values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import com.github.fge.jsonschema.cfg.ValidationConfiguration;
|
|||
import com.github.fge.jsonschema.core.report.ProcessingReport;
|
||||
import com.github.fge.jsonschema.processors.syntax.SyntaxValidator;
|
||||
import com.google.gson.*;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
@ -125,7 +125,7 @@ public class JSONSchemaGenerator {
|
|||
concept.put(propertyName, object.get("default"));
|
||||
}
|
||||
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString()) && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
||||
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
concept.put(propertyName, value);
|
||||
}
|
||||
if (object.has("maxLength")) {
|
||||
|
@ -154,7 +154,7 @@ public class JSONSchemaGenerator {
|
|||
int value = object.get("mock").getAsJsonObject().get("mock").getAsInt();
|
||||
concept.put(propertyName, value);
|
||||
} catch (Exception e) {
|
||||
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
concept.put(propertyName, value);
|
||||
}
|
||||
}
|
||||
|
@ -198,7 +198,7 @@ public class JSONSchemaGenerator {
|
|||
concept.put(propertyName, value.floatValue());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
concept.put(propertyName, value);
|
||||
}
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ public class JSONSchemaGenerator {
|
|||
concept.put(propertyName, object.get("default"));
|
||||
}
|
||||
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
||||
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").toString());
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").toString());
|
||||
concept.put(propertyName, value);
|
||||
}
|
||||
} else if (propertyObjType.equals("array")) {
|
||||
|
@ -230,7 +230,7 @@ public class JSONSchemaGenerator {
|
|||
if (itemsObject.has("default")) {
|
||||
array.add(itemsObject.get("default"));
|
||||
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
||||
String value = ScriptEngineUtils.calculate(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
array.add(value);
|
||||
} else {
|
||||
array.add(null);
|
||||
|
@ -239,7 +239,7 @@ public class JSONSchemaGenerator {
|
|||
if (itemsObject.has("default")) {
|
||||
array.add(itemsObject.get("default"));
|
||||
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
||||
String value = ScriptEngineUtils.calculate(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||
array.add(value);
|
||||
} else {
|
||||
array.add(0);
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
package io.metersphere.commons.utils;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class ScriptEngineUtils {
|
||||
private static final String ENGINE_NAME = "rhino";
|
||||
private static ScriptEngine engine;
|
||||
|
||||
static {
|
||||
final ScriptEngineManager engineManager = new ScriptEngineManager();
|
||||
engine = engineManager.getEngineByName(ENGINE_NAME);
|
||||
try {
|
||||
String script = IOUtils.toString(ScriptEngineUtils.class.getResource("/javascript/func.js"), StandardCharsets.UTF_8);
|
||||
engine.eval(script);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// graal.js 禁止多线程同时访问,加上 synchronized
|
||||
public synchronized static String calculate(String input) {
|
||||
try {
|
||||
return engine.eval("calculate('" + input + "')").toString();
|
||||
} catch (ScriptException e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
return input;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSON;
|
|||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.ScriptEngineUtils;
|
||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||
import io.metersphere.config.KafkaProperties;
|
||||
import io.metersphere.i18n.Translator;
|
||||
import io.metersphere.performance.engine.EngineContext;
|
||||
|
@ -528,7 +528,7 @@ public class JmeterDocumentParser implements DocumentParser {
|
|||
elementProp.appendChild(createStringProp(document, "Argument.name", jsonObject.getString("name")));
|
||||
// 处理 mock data
|
||||
String value = jsonObject.getString("value");
|
||||
elementProp.appendChild(createStringProp(document, "Argument.value", ScriptEngineUtils.calculate(value)));
|
||||
elementProp.appendChild(createStringProp(document, "Argument.value", ScriptEngineUtils.buildFunctionCallString(value)));
|
||||
elementProp.appendChild(createStringProp(document, "Argument.metadata", "="));
|
||||
item.appendChild(elementProp);
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue