Skip to content

Commit

Permalink
refactor: 调整 logger 实例化
Browse files Browse the repository at this point in the history
  • Loading branch information
liuruibin committed Aug 28, 2024
1 parent d1db925 commit 8fb7f5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
17 changes: 10 additions & 7 deletions src/main/java/io/metersphere/MeterSphereBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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);//项目下
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -425,7 +428,7 @@ public DescriptorImpl getDescriptor() {


private void log(String msg) {
MeterSphereUtils.logger.println(LOG_PREFIX + msg);
meterSphereUtils.log(LOG_PREFIX + msg);
}

@DataBoundSetter
Expand Down Expand Up @@ -556,4 +559,4 @@ public String getResourcePoolId() {
public String getTestCaseName() {
return testCaseName;
}
}
}
33 changes: 19 additions & 14 deletions src/main/java/io/metersphere/commons/utils/MeterSphereUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8fb7f5d

Please sign in to comment.