From 8fb7f5d64fa9da53d44aab0cc74887e2bccb38a4 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Wed, 28 Aug 2024 18:02:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=20logger=20?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/MeterSphereBuilder.java | 17 ++++++---- .../commons/utils/MeterSphereUtils.java | 33 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/metersphere/MeterSphereBuilder.java b/src/main/java/io/metersphere/MeterSphereBuilder.java index 9391f20..0826737 100644 --- a/src/main/java/io/metersphere/MeterSphereBuilder.java +++ b/src/main/java/io/metersphere/MeterSphereBuilder.java @@ -32,6 +32,7 @@ public class MeterSphereBuilder extends Builder implements SimpleBuildStep, Serializable { private static final String LOG_PREFIX = "[MeterSphere] "; + private MeterSphereUtils meterSphereUtils; private final String msEndpoint; private final String msAccessKey; @@ -62,7 +63,9 @@ public MeterSphereBuilder(String msEndpoint, String msAccessKey, String msSecret @Override public void perform(@Nonnull Run run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { - MeterSphereUtils.logger = listener.getLogger(); + // 初始化日志 + this.meterSphereUtils = new MeterSphereUtils(listener.getLogger()); + listener.getLogger().println("workspace=" + workspace); listener.getLogger().println("number=" + run.getNumber()); listener.getLogger().println("url=" + run.getUrl()); @@ -96,7 +99,7 @@ public void perform(@Nonnull Run run, @Nonnull FilePath workspace, @Nonnul boolean result = false; switch (method) { case Method.TEST_PLAN: - result = MeterSphereUtils.runTestPlan(run, client, realProjectId, mode, testPlanId, resourcePoolId, openMode); + result = meterSphereUtils.runTestPlan(run, client, realProjectId, mode, testPlanId, resourcePoolId, openMode); break; case Method.TEST_PLAN_NAME: String testPlanName = Util.replaceMacro(this.testPlanName, environment); @@ -111,7 +114,7 @@ public void perform(@Nonnull Run run, @Nonnull FilePath workspace, @Nonnul run.setResult(Result.FAILURE); return; } - result = MeterSphereUtils.runTestPlan(run, client, realProjectId, mode, first.get().getId(), resourcePoolId, openMode); + result = meterSphereUtils.runTestPlan(run, client, realProjectId, mode, first.get().getId(), resourcePoolId, openMode); break; case Method.SINGLE: testCases = client.getTestCases(realProjectId);//项目下 @@ -123,7 +126,7 @@ public void perform(@Nonnull Run run, @Nonnull FilePath workspace, @Nonnul run.setResult(Result.FAILURE); return; } - result = MeterSphereUtils.getTestStepsBySingle(client, realProjectId, firstCase.get(), testPlanId, resourcePoolId, openMode); + result = meterSphereUtils.getTestStepsBySingle(client, realProjectId, firstCase.get(), testPlanId, resourcePoolId, openMode); break; case Method.SINGLE_NAME: String testCaseName = Util.replaceMacro(this.testCaseName, environment); @@ -138,7 +141,7 @@ public void perform(@Nonnull Run run, @Nonnull FilePath workspace, @Nonnul run.setResult(Result.FAILURE); return; } - result = MeterSphereUtils.getTestStepsBySingle(client, realProjectId, firstCase.get(), testPlanId, resourcePoolId, openMode); + result = meterSphereUtils.getTestStepsBySingle(client, realProjectId, firstCase.get(), testPlanId, resourcePoolId, openMode); break; default: run.setResult(Result.FAILURE); @@ -425,7 +428,7 @@ public DescriptorImpl getDescriptor() { private void log(String msg) { - MeterSphereUtils.logger.println(LOG_PREFIX + msg); + meterSphereUtils.log(LOG_PREFIX + msg); } @DataBoundSetter @@ -556,4 +559,4 @@ public String getResourcePoolId() { public String getTestCaseName() { return testCaseName; } -} \ No newline at end of file +} diff --git a/src/main/java/io/metersphere/commons/utils/MeterSphereUtils.java b/src/main/java/io/metersphere/commons/utils/MeterSphereUtils.java index c0209ce..cfa95b1 100644 --- a/src/main/java/io/metersphere/commons/utils/MeterSphereUtils.java +++ b/src/main/java/io/metersphere/commons/utils/MeterSphereUtils.java @@ -16,14 +16,19 @@ import java.util.Map; public class MeterSphereUtils { - public static PrintStream logger; + private PrintStream logger; private static final String LOG_PREFIX = "[MeterSphere,代码测试]"; - private static void log(String msg) { + + public MeterSphereUtils(PrintStream logger) { + this.logger = logger; + } + + public void log(String msg) { logger.println(LOG_PREFIX + msg); } - public static boolean runUiTest(MeterSphereClient meterSphereClient, TestCaseDTO c, String openMode) { + public boolean runUiTest(MeterSphereClient meterSphereClient, TestCaseDTO c, String openMode) { String url = meterSphereClient.getBaseInfo(); boolean flag = true; String result = ""; @@ -67,7 +72,7 @@ public static boolean runUiTest(MeterSphereClient meterSphereClient, TestCaseDTO return flag; } - public static boolean runPerformTest(MeterSphereClient meterSphereClient, TestCaseDTO c, String testPlanId, String openMode) { + public boolean runPerformTest(MeterSphereClient meterSphereClient, TestCaseDTO c, String testPlanId, String openMode) { String url = meterSphereClient.getBaseInfo(); String reportId = ""; boolean flag = true; @@ -114,7 +119,7 @@ public static boolean runPerformTest(MeterSphereClient meterSphereClient, TestCa return flag; } - public static boolean runScenario(MeterSphereClient meterSphereClient, TestCaseDTO c, String projectId, String runMode, String resourcePoolId, String openMode) { + public boolean runScenario(MeterSphereClient meterSphereClient, TestCaseDTO c, String projectId, String runMode, String resourcePoolId, String openMode) { String url = meterSphereClient.getBaseInfo(); boolean flag = true; String reportId = null; @@ -164,7 +169,7 @@ public static boolean runScenario(MeterSphereClient meterSphereClient, TestCaseD return flag; } - public static boolean runDefinition(MeterSphereClient meterSphereClient, TestCaseDTO c, String testPlanId, String runMode) { + public boolean runDefinition(MeterSphereClient meterSphereClient, TestCaseDTO c, String testPlanId, String runMode) { boolean flag = true; String id = c.getId(); try { @@ -198,8 +203,8 @@ public static boolean runDefinition(MeterSphereClient meterSphereClient, TestCas return flag; } - public static boolean runTestPlan(Run run, MeterSphereClient meterSphereClient, String projectId, - String mode, String testPlanId, String resourcePoolId, String openMode) throws InterruptedException { + public boolean runTestPlan(Run run, MeterSphereClient meterSphereClient, String projectId, + String mode, String testPlanId, String resourcePoolId, String openMode) throws InterruptedException { log("测试计划开始执行"); String id = meterSphereClient.exeTestPlan(projectId, testPlanId, mode, resourcePoolId); log("生成测试报告id:" + id); @@ -233,26 +238,26 @@ public static boolean runTestPlan(Run run, MeterSphereClient meterSphereCl return flag; } - public static boolean getTestStepsBySingle(MeterSphereClient meterSphereClient, String projectId, TestCaseDTO testCase, - String testPlanId, String resourcePoolId, String openMode) { + public boolean getTestStepsBySingle(MeterSphereClient meterSphereClient, String projectId, TestCaseDTO testCase, + String testPlanId, String resourcePoolId, String openMode) { log("测试ID: " + testCase.getId()); log("测试名称: " + testCase.getName() + " [" + testCase.getType() + "]" + " [" + testCase.getVersionName() + "]"); boolean flag = true; switch (testCase.getType()) { case Results.PERFORMANCE: case Results.LOAD_TEST: - flag = MeterSphereUtils.runPerformTest(meterSphereClient, testCase, "", openMode); + flag = runPerformTest(meterSphereClient, testCase, "", openMode); break; case Results.SCENARIO: case Results.API_SCENARIO: - flag = MeterSphereUtils.runScenario(meterSphereClient, testCase, projectId, "scenario", resourcePoolId, openMode); + flag = runScenario(meterSphereClient, testCase, projectId, "scenario", resourcePoolId, openMode); break; case Results.DEFINITION: case Results.API_CASE: - flag = MeterSphereUtils.runDefinition(meterSphereClient, testCase, testPlanId, "JENKINS"); + flag = runDefinition(meterSphereClient, testCase, testPlanId, "JENKINS"); break; case Results.UI: - flag = MeterSphereUtils.runUiTest(meterSphereClient, testCase, openMode); + flag = runUiTest(meterSphereClient, testCase, openMode); break; default: break;