From 0793e0894612007488f635d3d2ab62f81debc9b8 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 28 Nov 2022 17:22:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8C=E5=90=8D=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=BF=87=E7=A8=8B=E4=BA=A7=E7=94=9F=E5=A4=A7?= =?UTF-8?q?=E9=87=8F=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1016451 --user=赵勇 [接口测试]github#17539我把metersphere的接口自动化中的50个场景导入到一个新场景中运行时控制台报错。这些场景单独运行都是没问题的,就是导入到一个新的场景中控制台就报这个错了,并且运行时也有大量报错了 https://www.tapd.cn/55049933/s/1306709 --- .../api/dto/definition/request/ElementUtil.java | 11 ++++++++--- .../processors/post/MsJDBCPostProcessor.java | 12 +++++++----- .../request/processors/pre/MsJDBCPreProcessor.java | 13 +++++++------ .../definition/request/sampler/MsJDBCSampler.java | 14 ++++++++------ 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index 02225aedf8..a407d78b20 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -801,9 +801,11 @@ public class ElementUtil { } } List finalDataName = dataName; - List collect = envConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getName().equals(finalDataName.get(0))).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(collect)) { - return collect.get(0); + if (CollectionUtils.isNotEmpty(dataName)) { + List collect = envConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getName().equals(finalDataName.get(0))).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(collect)) { + return collect.get(0); + } } } catch (Exception e) { LogUtil.error(e); @@ -811,4 +813,7 @@ public class ElementUtil { return null; } + public static String getDataSourceName(String name) { + return StringUtils.join(name, "-", UUID.randomUUID().toString()); + } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java index e9da251fa8..e542e41b21 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java @@ -146,8 +146,9 @@ public class MsJDBCPostProcessor extends MsTestElement { MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + ":" + message : message); } } - final HashTree samplerHashTree = tree.add(jdbcPostProcessor(config)); - tree.add(jdbcDataSource()); + JDBCPostProcessor jdbcPostProcessor = jdbcPostProcessor(config); + final HashTree samplerHashTree = tree.add(jdbcPostProcessor); + tree.add(jdbcDataSource(jdbcPostProcessor.getDataSource())); Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables()); if (arguments != null) { tree.add(arguments); @@ -260,7 +261,8 @@ public class MsJDBCPostProcessor extends MsTestElement { ElementUtil.setBaseParams(jdbcPostProcessor, this.getParent(), config, this.getId(), this.getIndex()); // request.getDataSource() 是ID,需要转换为Name - jdbcPostProcessor.setProperty("dataSource", this.dataSource.getName()); + jdbcPostProcessor.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName())); + jdbcPostProcessor.setProperty("dataSource", jdbcPostProcessor.getDataSource()); jdbcPostProcessor.setProperty("query", this.getQuery()); jdbcPostProcessor.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout())); jdbcPostProcessor.setProperty("resultVariable", this.getResultVariable()); @@ -270,7 +272,7 @@ public class MsJDBCPostProcessor extends MsTestElement { return jdbcPostProcessor; } - private DataSourceElement jdbcDataSource() { + private DataSourceElement jdbcDataSource(String name) { DataSourceElement dataSourceElement = new DataSourceElement(); dataSourceElement.setEnabled(true); dataSourceElement.setName(this.getName() + " JDBCDataSource"); @@ -279,7 +281,7 @@ public class MsJDBCPostProcessor extends MsTestElement { dataSourceElement.setProperty("autocommit", true); dataSourceElement.setProperty("keepAlive", true); dataSourceElement.setProperty("preinit", false); - dataSourceElement.setProperty("dataSource", dataSource.getName()); + dataSourceElement.setProperty("dataSource", name); dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl()); dataSourceElement.setProperty("driver", dataSource.getDriver()); dataSourceElement.setProperty("username", dataSource.getUsername()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java index 90a20ff10e..d4b4037cb9 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java @@ -146,8 +146,9 @@ public class MsJDBCPreProcessor extends MsTestElement { MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + ":" + message : message); } } - final HashTree samplerHashTree = tree.add(jdbcPreProcessor(config)); - tree.add(jdbcDataSource()); + JDBCPreProcessor preProcessor = jdbcPreProcessor(config); + final HashTree samplerHashTree = tree.add(preProcessor); + tree.add(jdbcDataSource(preProcessor.getDataSource())); Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables()); if (arguments != null) { tree.add(arguments); @@ -259,8 +260,8 @@ public class MsJDBCPreProcessor extends MsTestElement { jdbcPreProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); ElementUtil.setBaseParams(jdbcPreProcessor, this.getParent(), config, this.getId(), this.getIndex()); - - jdbcPreProcessor.setProperty("dataSource", this.dataSource.getName()); + jdbcPreProcessor.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName())); + jdbcPreProcessor.setProperty("dataSource", jdbcPreProcessor.getDataSource()); jdbcPreProcessor.setProperty("query", this.getQuery()); jdbcPreProcessor.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout())); jdbcPreProcessor.setProperty("resultVariable", this.getResultVariable()); @@ -270,7 +271,7 @@ public class MsJDBCPreProcessor extends MsTestElement { return jdbcPreProcessor; } - private DataSourceElement jdbcDataSource() { + private DataSourceElement jdbcDataSource(String sourceName) { DataSourceElement dataSourceElement = new DataSourceElement(); dataSourceElement.setEnabled(true); dataSourceElement.setName(this.getName() + " JDBCDataSource"); @@ -279,7 +280,7 @@ public class MsJDBCPreProcessor extends MsTestElement { dataSourceElement.setProperty("autocommit", true); dataSourceElement.setProperty("keepAlive", true); dataSourceElement.setProperty("preinit", false); - dataSourceElement.setProperty("dataSource", dataSource.getName()); + dataSourceElement.setProperty("dataSource", sourceName); dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl()); dataSourceElement.setProperty("driver", dataSource.getDriver()); dataSourceElement.setProperty("username", dataSource.getUsername()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index db41a5c1e5..f9d96b6aa5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -161,10 +161,11 @@ public class MsJDBCSampler extends MsTestElement { String message = "数据源为空请选择数据源"; MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + ":" + message : message); } - final HashTree samplerHashTree = tree.add(jdbcSampler(config)); - tree.add(jdbcDataSource()); + JDBCSampler jdbcSampler = jdbcSampler(config); + final HashTree samplerHashTree = tree.add(jdbcSampler); + tree.add(jdbcDataSource(jdbcSampler.getDataSource())); Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables()); - if (arguments != null) { + if (arguments != null && !arguments.getArguments().isEmpty()) { tree.add(arguments); } // 环境通用请求头 @@ -332,7 +333,8 @@ public class MsJDBCSampler extends MsTestElement { sampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName()); sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); ElementUtil.setBaseParams(sampler, this.getParent(), config, this.getId(), this.getIndex()); - sampler.setProperty("dataSource", this.dataSource.getName()); + sampler.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName())); + sampler.setProperty("dataSource", sampler.getDataSource()); sampler.setProperty("query", this.getQuery()); sampler.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout())); sampler.setProperty("resultVariable", this.getResultVariable()); @@ -342,7 +344,7 @@ public class MsJDBCSampler extends MsTestElement { return sampler; } - private DataSourceElement jdbcDataSource() { + private DataSourceElement jdbcDataSource(String dataSourceName) { DataSourceElement dataSourceElement = new DataSourceElement(); dataSourceElement.setEnabled(true); dataSourceElement.setName(this.getName() + " JDBCDataSource"); @@ -351,7 +353,7 @@ public class MsJDBCSampler extends MsTestElement { dataSourceElement.setProperty("autocommit", true); dataSourceElement.setProperty("keepAlive", true); dataSourceElement.setProperty("preinit", false); - dataSourceElement.setProperty("dataSource", dataSource.getName()); + dataSourceElement.setProperty("dataSource", dataSourceName); dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl()); dataSourceElement.setProperty("driver", dataSource.getDriver()); dataSourceElement.setProperty("username", dataSource.getUsername());