diff --git a/backend/src/main/java/io/metersphere/commons/constants/FileType.java b/backend/src/main/java/io/metersphere/commons/constants/FileType.java new file mode 100644 index 0000000000..6236b13cba --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/constants/FileType.java @@ -0,0 +1,5 @@ +package io.metersphere.commons.constants; + +public enum FileType { + JMX +} diff --git a/backend/src/main/java/io/metersphere/engine/EngineContext.java b/backend/src/main/java/io/metersphere/engine/EngineContext.java index a6eaf79851..bcfd804476 100644 --- a/backend/src/main/java/io/metersphere/engine/EngineContext.java +++ b/backend/src/main/java/io/metersphere/engine/EngineContext.java @@ -1,13 +1,13 @@ package io.metersphere.engine; -import java.io.InputStream; import java.util.HashMap; import java.util.Map; public class EngineContext { private String engineId; private String engineType; - private InputStream inputStream; + private String fileType; + private String content; private Map properties = new HashMap<>(); public String getEngineId() { @@ -26,14 +26,6 @@ public class EngineContext { this.engineType = engineType; } - public InputStream getInputStream() { - return inputStream; - } - - public void setInputStream(InputStream inputStream) { - this.inputStream = inputStream; - } - public void addProperty(String key, Object value) { this.properties.put(key, value); } @@ -41,4 +33,20 @@ public class EngineContext { public Object getProperty(String key) { return this.properties.get(key); } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getFileType() { + return fileType; + } + + public void setFileType(String fileType) { + this.fileType = fileType; + } } diff --git a/backend/src/main/java/io/metersphere/engine/EngineFactory.java b/backend/src/main/java/io/metersphere/engine/EngineFactory.java index 5d133b5730..63a4d2059b 100644 --- a/backend/src/main/java/io/metersphere/engine/EngineFactory.java +++ b/backend/src/main/java/io/metersphere/engine/EngineFactory.java @@ -6,6 +6,7 @@ import io.metersphere.base.domain.FileContent; import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.commons.constants.EngineType; +import io.metersphere.commons.constants.FileType; import io.metersphere.commons.exception.MSException; import io.metersphere.engine.docker.DockerTestEngine; import io.metersphere.engine.kubernetes.KubernetesTestEngine; @@ -14,7 +15,6 @@ import io.metersphere.parse.EngineSourceParserFactory; import org.apache.commons.lang3.StringUtils; import java.io.ByteArrayInputStream; -import java.io.InputStream; public class EngineFactory { public static Engine createEngine(String engineType) { @@ -32,8 +32,8 @@ public class EngineFactory { public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, FileContent fileContent) throws Exception { final EngineContext engineContext = new EngineContext(); engineContext.setEngineId(loadTest.getId()); - engineContext.setInputStream(new ByteArrayInputStream(fileContent.getFile())); - engineContext.setEngineType(fileMetadata.getType()); + engineContext.setEngineType(fileMetadata.getEngine()); + engineContext.setFileType(fileMetadata.getType()); if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) { final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration()); @@ -44,14 +44,14 @@ public class EngineFactory { } } - final EngineSourceParser engineSourceParser = EngineSourceParserFactory.createEngineSourceParser(engineContext.getEngineType()); + final EngineSourceParser engineSourceParser = EngineSourceParserFactory.createEngineSourceParser(engineContext.getFileType()); if (engineSourceParser == null) { MSException.throwException("未知的文件类型!"); } - final InputStream inputStream = engineSourceParser.parse(engineContext, engineContext.getInputStream()); - engineContext.setInputStream(inputStream); + String content = engineSourceParser.parse(engineContext, new ByteArrayInputStream(fileContent.getFile())); + engineContext.setContent(content); return engineContext; } diff --git a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java index ef5f2f50e4..1665b9f1f0 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -4,9 +4,13 @@ import io.metersphere.engine.Engine; import io.metersphere.engine.EngineContext; public class DockerTestEngine implements Engine { + private EngineContext context; + @Override public boolean init(EngineContext context) { - return false; + // todo 初始化操作 + this.context = context; + return true; } @Override diff --git a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java index 61f363d502..fa67d5484d 100644 --- a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java @@ -4,11 +4,16 @@ import io.metersphere.engine.Engine; import io.metersphere.engine.EngineContext; public class KubernetesTestEngine implements Engine { + private EngineContext context; + @Override public boolean init(EngineContext context) { - return false; + // todo 初始化操作 + this.context = context; + return true; } + @Override public void start() { diff --git a/backend/src/main/java/io/metersphere/parse/EngineSourceParser.java b/backend/src/main/java/io/metersphere/parse/EngineSourceParser.java index ef3a8d8c0e..25de3624d2 100644 --- a/backend/src/main/java/io/metersphere/parse/EngineSourceParser.java +++ b/backend/src/main/java/io/metersphere/parse/EngineSourceParser.java @@ -5,5 +5,5 @@ import io.metersphere.engine.EngineContext; import java.io.InputStream; public interface EngineSourceParser { - InputStream parse(EngineContext context, InputStream source) throws Exception; + String parse(EngineContext context, InputStream source) throws Exception; } diff --git a/backend/src/main/java/io/metersphere/parse/EngineSourceParserFactory.java b/backend/src/main/java/io/metersphere/parse/EngineSourceParserFactory.java index 5fd673fdbc..6659d8d17d 100644 --- a/backend/src/main/java/io/metersphere/parse/EngineSourceParserFactory.java +++ b/backend/src/main/java/io/metersphere/parse/EngineSourceParserFactory.java @@ -1,13 +1,13 @@ package io.metersphere.parse; -import io.metersphere.commons.constants.EngineType; +import io.metersphere.commons.constants.FileType; import io.metersphere.parse.xml.XmlEngineSourceParse; public class EngineSourceParserFactory { public static EngineSourceParser createEngineSourceParser(String type) { - final EngineType engineType = EngineType.valueOf(type); + final FileType engineType = FileType.valueOf(type); - if (EngineType.JMX.equals(engineType)) { + if (FileType.JMX.equals(engineType)) { return new XmlEngineSourceParse(); } diff --git a/backend/src/main/java/io/metersphere/parse/xml/XmlEngineSourceParse.java b/backend/src/main/java/io/metersphere/parse/xml/XmlEngineSourceParse.java index 42648601bb..db8dfe02ae 100644 --- a/backend/src/main/java/io/metersphere/parse/xml/XmlEngineSourceParse.java +++ b/backend/src/main/java/io/metersphere/parse/xml/XmlEngineSourceParse.java @@ -13,7 +13,7 @@ import java.io.InputStream; public class XmlEngineSourceParse implements EngineSourceParser { @Override - public InputStream parse(EngineContext context, InputStream source) throws Exception { + public String parse(EngineContext context, InputStream source) throws Exception { final InputSource inputSource = new InputSource(source); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); @@ -21,7 +21,7 @@ public class XmlEngineSourceParse implements EngineSourceParser { DocumentBuilder docBuilder = factory.newDocumentBuilder(); final Document document = docBuilder.parse(inputSource); - final DocumentParser documentParser = createDocumentParser(context.getEngineType()); + final DocumentParser documentParser = createDocumentParser(context.getFileType()); return documentParser.parse(context, document); } diff --git a/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParser.java b/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParser.java index 3e4813760d..2ab6f67a72 100644 --- a/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParser.java +++ b/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParser.java @@ -6,5 +6,5 @@ import org.w3c.dom.Document; import java.io.InputStream; public interface DocumentParser { - InputStream parse(EngineContext context, Document document) throws Exception; + String parse(EngineContext context, Document document) throws Exception; } diff --git a/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParserFactory.java b/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParserFactory.java index 218499951b..d5772d174a 100644 --- a/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParserFactory.java +++ b/backend/src/main/java/io/metersphere/parse/xml/reader/DocumentParserFactory.java @@ -1,16 +1,18 @@ package io.metersphere.parse.xml.reader; -import io.metersphere.commons.constants.EngineType; +import io.metersphere.commons.constants.FileType; import io.metersphere.parse.xml.reader.jmx.JmeterDocumentParser; public class DocumentParserFactory { public static DocumentParser createDocumentParser(String type) { - final EngineType engineType = EngineType.valueOf(type); + final FileType fileType = FileType.valueOf(type); - if (EngineType.JMX.equals(engineType)) { - return new JmeterDocumentParser(); + switch (fileType) { + case JMX: + return new JmeterDocumentParser(); + default: + break; } - return null; } } diff --git a/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java b/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java index 2f44539943..a1854fe572 100644 --- a/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java +++ b/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java @@ -15,10 +15,7 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import java.io.ByteArrayInputStream; -import java.io.InputStream; import java.io.StringWriter; -import java.nio.charset.StandardCharsets; public class JmeterDocumentParser implements DocumentParser { private final static String HASH_TREE_ELEMENT = "hashTree"; @@ -32,7 +29,7 @@ public class JmeterDocumentParser implements DocumentParser { private EngineContext context; @Override - public InputStream parse(EngineContext context, Document document) throws Exception { + public String parse(EngineContext context, Document document) throws Exception { this.context = context; final Element jmeterTestPlan = document.getDocumentElement(); @@ -49,18 +46,17 @@ public class JmeterDocumentParser implements DocumentParser { } } - return documentToInputStream(document); + return documentToString(document); } - private InputStream documentToInputStream(Document document) throws TransformerException { + private String documentToString(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); - final String resultStr = writer.toString(); - return new ByteArrayInputStream(resultStr.getBytes(StandardCharsets.UTF_8)); + return writer.toString(); } private void parseHashTree(Element hashTree) {