Skip to content

Commit 4e1d09d

Browse files
committed
Refactor JUnit 4 and JUnit 5 parts
1 parent 22d5905 commit 4e1d09d

File tree

14 files changed

+393
-231
lines changed

14 files changed

+393
-231
lines changed

core/src/main/java/org/quickperf/issue/PerfIssuesEvaluator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ private PerfIssuesEvaluator() {}
3333
public static final PerfIssuesEvaluator INSTANCE = new PerfIssuesEvaluator();
3434

3535
public Collection<PerfIssuesToFormat> evaluatePerfIssues(SetOfAnnotationConfigs testAnnotationConfigs, TestExecutionContext testExecutionContext) {
36-
if(testExecutionContext.isQuickPerfDisabled()) {
37-
return Collections.emptyList();
38-
}
3936
Map<Annotation, PerfRecord> perfRecordByAnnotation
4037
= buildPerfRecordByAnnotation(testAnnotationConfigs, testExecutionContext);
4138

core/src/main/java/org/quickperf/reporter/ConsoleReporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void displayQuickPerfDebugInfos() {
6363
printExecutionOrders(executionOrderListAfter);
6464
}
6565

66-
public void displayQuickPerfAnnotations(Annotation[] perfAnnotations) {
66+
void displayQuickPerfAnnotations(Annotation[] perfAnnotations) {
6767

6868
List<Annotation> perfAnnotationsWithoutDisplayAppliedAnnotations =
6969
removeDisplayAppliedAnnotations(perfAnnotations);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2019-2019 the original author or authors.
12+
*/
13+
14+
package org.quickperf.testlauncher;
15+
16+
import org.quickperf.WorkingFolder;
17+
18+
import java.lang.reflect.Method;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
public class MainClassArguments {
23+
24+
private final String className;
25+
26+
private final String methodName;
27+
28+
private final String workingFolderPath;
29+
30+
private MainClassArguments(String className, String methodName, String workingFolderPath) {
31+
this.className = className;
32+
this.methodName = methodName;
33+
this.workingFolderPath = workingFolderPath;
34+
}
35+
36+
public String getClassName() {
37+
return className;
38+
}
39+
40+
public String getMethodName() {
41+
return methodName;
42+
}
43+
44+
public String getWorkingFolderPath() {
45+
return workingFolderPath;
46+
}
47+
48+
public static MainClassArguments buildFrom(Method testMethod
49+
, WorkingFolder workingFolder) {
50+
String className = retrieveClassNameOf(testMethod);
51+
String methodName = testMethod.getName();
52+
String workingFolderPath = workingFolder.getPath();
53+
return new MainClassArguments(className, methodName, workingFolderPath);
54+
}
55+
56+
private static String retrieveClassNameOf(Method method) {
57+
Class<?> classOfTestMethod = method.getDeclaringClass();
58+
return classOfTestMethod.getName();
59+
}
60+
61+
public static MainClassArguments buildFromMainArguments(String... args) {
62+
String className = args[0];
63+
String methodName = args[1];
64+
String workingFolderPath = args[2];
65+
return new MainClassArguments(className, methodName, workingFolderPath);
66+
}
67+
68+
public List<String> buildMainClassArgumentsForJvmCommand() {
69+
List<String> arguments = new ArrayList<>(3);
70+
arguments.add(className);
71+
arguments.add(methodName);
72+
arguments.add(workingFolderPath);
73+
return arguments;
74+
}
75+
76+
}

core/src/main/java/org/quickperf/testlauncher/NewJvmTestLauncher.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
import org.apache.commons.io.IOUtils;
1717
import org.quickperf.SystemProperties;
18+
import org.quickperf.TestExecutionContext;
1819
import org.quickperf.WorkingFolder;
20+
import org.quickperf.issue.BusinessOrTechnicalIssue;
21+
import org.quickperf.repository.BusinessOrTechnicalIssueRepository;
1922

2023
import java.io.File;
2124
import java.io.IOException;
@@ -28,27 +31,36 @@ public class NewJvmTestLauncher {
2831

2932
public static final NewJvmTestLauncher INSTANCE = new NewJvmTestLauncher();
3033

34+
private final BusinessOrTechnicalIssueRepository businessOrTechnicalIssueRepository = BusinessOrTechnicalIssueRepository.INSTANCE;
35+
3136
private NewJvmTestLauncher() { }
3237

33-
public void run(Method testMethod
34-
, WorkingFolder workingFolder
35-
, AllJvmOptions jvmOptions
36-
, Class<?> mainClassToLaunchTestInANewJvm) {
38+
public BusinessOrTechnicalIssue executeTestMethodInNewJwm( Method testMethod
39+
, TestExecutionContext testExecutionContext
40+
, Class<?> mainClassToLaunchTestInANewJvm) {
41+
WorkingFolder workingFolder = testExecutionContext.getWorkingFolder();
42+
AllJvmOptions jvmOptions = testExecutionContext.getJvmOptions();
43+
44+
executeTestInNewJvm(testMethod, workingFolder, jvmOptions, mainClassToLaunchTestInANewJvm);
45+
46+
return businessOrTechnicalIssueRepository.findFrom(workingFolder);
47+
}
3748

38-
String className = retrieveClassNameFrom(testMethod);
39-
String methodName = testMethod.getName();
49+
private void executeTestInNewJvm(Method testMethod
50+
, WorkingFolder workingFolder
51+
, AllJvmOptions jvmOptions
52+
, Class<?> mainClassToLaunchTestInANewJvm) {
53+
54+
MainClassArguments mainClassArguments = MainClassArguments.buildFrom(testMethod, workingFolder);
4055

4156
List<String> jvmOptionsAsStrings = jvmOptions.asStrings(workingFolder);
4257

43-
List<String> jvmCommand
44-
= buildCommandOfLaunchingJUnitCoreInANewJvm(
45-
className
46-
, methodName
47-
, jvmOptionsAsStrings
48-
, workingFolder.getPath()
49-
, mainClassToLaunchTestInANewJvm);
58+
List<String> jvmCommand = buildCommand( mainClassArguments
59+
, jvmOptionsAsStrings
60+
, workingFolder.getPath()
61+
, mainClassToLaunchTestInANewJvm);
5062

51-
CmdExecutionResult cmdExecutionResult = tryWith(jvmCommand, workingFolder);
63+
CmdExecutionResult cmdExecutionResult = tryWith(jvmCommand);
5264

5365
System.out.println(cmdExecutionResult.getMessage());
5466

@@ -58,35 +70,27 @@ public void run(Method testMethod
5870

5971
}
6072

61-
private static List<String> buildCommandOfLaunchingJUnitCoreInANewJvm(
62-
String className
63-
, String methodName
64-
, List<String> jvmParamsAsString
65-
, String workingFolderPath
66-
, Class<?> mainClassToLaunchTest) {
73+
private static List<String> buildCommand(MainClassArguments mainClassArguments
74+
, List<String> jvmOptionsAsStrings
75+
, String workingFolderPath
76+
, Class<?> mainClassToLaunchTest) {
6777
List<String> command = new ArrayList<>();
6878
command.add(retrieveJavaExePath());
69-
command.addAll(jvmParamsAsString);
79+
command.addAll(jvmOptionsAsStrings);
7080
command.add(SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM
71-
.buildForJvm("true")
81+
.buildForJvm("true")
7282
);
7383
command.add(SystemProperties.WORKING_FOLDER
74-
.buildForJvm(workingFolderPath)
84+
.buildForJvm(workingFolderPath)
7585
);
7686
command.add("-cp");
7787
command.add(retrieveCurrentClassPath());
7888
command.add(mainClassToLaunchTest.getCanonicalName());
79-
command.add(className);
80-
command.add(methodName);
81-
command.add(workingFolderPath);
89+
List<String> mainClassArgumentsAsStringList = mainClassArguments.buildMainClassArgumentsForJvmCommand();
90+
command.addAll(mainClassArgumentsAsStringList);
8291
return command;
8392
}
8493

85-
private static String retrieveClassNameFrom(Method method) {
86-
Class<?> classOfTestMethod = method.getDeclaringClass();
87-
return classOfTestMethod.getName();
88-
}
89-
9094
private static String retrieveJavaExePath() {
9195
String javaHomeDirectoryPath = System.getProperty("java.home");
9296
return javaHomeDirectoryPath
@@ -126,7 +130,7 @@ private String getErrorMessage() {
126130

127131
}
128132

129-
private static CmdExecutionResult tryWith(List<String> cmd, WorkingFolder workingFolder) {
133+
private static CmdExecutionResult tryWith(List<String> cmd) {
130134

131135
try {
132136
final Process process = new ProcessBuilder(cmd).start();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2019-2019 the original author or authors.
12+
*/
13+
14+
package org.quickperf.testlauncher;
15+
16+
import org.quickperf.issue.BusinessOrTechnicalIssue;
17+
import org.quickperf.repository.BusinessOrTechnicalIssueRepository;
18+
19+
public class TestRunnerFromMain {
20+
21+
public static final TestRunnerFromMain INSTANCE = new TestRunnerFromMain();
22+
23+
private TestRunnerFromMain() { }
24+
25+
public interface FrameworkTestRunner {
26+
27+
BusinessOrTechnicalIssue executeTestMethod(Class<?> testClass, String methodName);
28+
29+
}
30+
31+
public void executeTestMethod(FrameworkTestRunner frameworkTestRunner, String... mainArgs) throws ClassNotFoundException {
32+
33+
MainClassArguments mainClassArguments = MainClassArguments.buildFromMainArguments(mainArgs);
34+
35+
BusinessOrTechnicalIssue businessOrTechnicalIssue = executeTestMethod(frameworkTestRunner, mainClassArguments);
36+
37+
BusinessOrTechnicalIssueRepository businessOrTechnicalIssueRepository = BusinessOrTechnicalIssueRepository.INSTANCE;
38+
39+
String workingFolderPath = mainClassArguments.getWorkingFolderPath();
40+
businessOrTechnicalIssueRepository.save(businessOrTechnicalIssue
41+
, workingFolderPath);
42+
43+
// To be sure that Tomcat or Jetty web server will stop
44+
System.exit(0);
45+
46+
}
47+
48+
private BusinessOrTechnicalIssue executeTestMethod(FrameworkTestRunner frameworkTestRunner, MainClassArguments mainClassArguments) throws ClassNotFoundException {
49+
String className = mainClassArguments.getClassName();
50+
Class<?> testClass = Class.forName(className);
51+
52+
String methodName = mainClassArguments.getMethodName();
53+
54+
return frameworkTestRunner.executeTestMethod(testClass, methodName);
55+
}
56+
57+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2019-2019 the original author or authors.
12+
*/
13+
14+
package org.quickperf.junit4;
15+
16+
import org.junit.runner.JUnitCore;
17+
import org.junit.runner.Request;
18+
import org.junit.runner.Result;
19+
import org.junit.runner.notification.Failure;
20+
import org.quickperf.issue.BusinessOrTechnicalIssue;
21+
import org.quickperf.testlauncher.TestRunnerFromMain;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
class JUnit4TestRunner implements TestRunnerFromMain.FrameworkTestRunner {
27+
28+
@Override
29+
public BusinessOrTechnicalIssue executeTestMethod(Class<?> testClass, String methodName) {
30+
31+
Request junitRequestOfMethod = Request.method(testClass, methodName);
32+
33+
Result testResult = new JUnitCore().run(junitRequestOfMethod);
34+
35+
List<Failure> jUnit4Failures = testResult.getFailures();
36+
37+
List<Throwable> jUnit4failuresAsThrowables = convertJUnit4FailuresToThrowables(jUnit4Failures);
38+
39+
return BusinessOrTechnicalIssue.buildFrom(jUnit4failuresAsThrowables);
40+
41+
}
42+
43+
private List<Throwable> convertJUnit4FailuresToThrowables(List<Failure> jUnit4Failures) {
44+
List<Throwable> throwables = new ArrayList<>();
45+
for (Failure failure : jUnit4Failures) {
46+
Throwable throwable = failure.getException();
47+
throwables.add(throwable);
48+
}
49+
return throwables;
50+
}
51+
52+
}

0 commit comments

Comments
 (0)