Skip to content

Commit 54680b5

Browse files
author
Sylvain Fraïssé
committed
refact: JUnitTesListRunner uses JUnitTest to run test cases
1 parent bdca505 commit 54680b5

File tree

1 file changed

+2
-157
lines changed

1 file changed

+2
-157
lines changed
Lines changed: 2 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,9 @@
11
package com.codingame.codemachine.runner.junit;
22

3-
import com.codingame.codemachine.runner.junit.core.RunLogDto;
4-
import com.codingame.codemachine.runner.junit.core.RunLogStackTraceDto;
5-
import com.codingame.codemachine.runner.junit.core.TestResultDto;
6-
import com.google.gson.Gson;
7-
import org.apache.commons.io.FileUtils;
8-
import org.junit.runner.Description;
9-
import org.junit.runner.JUnitCore;
10-
import org.junit.runner.Request;
11-
import org.junit.runner.Result;
12-
import org.junit.runner.notification.Failure;
13-
import org.junit.runner.notification.RunListener;
14-
15-
import java.io.ByteArrayOutputStream;
16-
import java.io.File;
17-
import java.io.IOException;
18-
import java.io.PrintStream;
19-
import java.util.ArrayList;
20-
import java.util.List;
21-
import java.util.regex.Matcher;
22-
import java.util.regex.Pattern;
23-
24-
import static java.util.Collections.singletonList;
25-
263
public class JUnitTestListRunner {
27-
private static final String DEFAULT_OUTPUT = "-";
28-
29-
private static RunLogDto parseThrowable(Throwable t) {
30-
RunLogDto runLog = new RunLogDto();
31-
runLog.setMessage(t.getMessage());
32-
List<RunLogStackTraceDto> stack = new ArrayList<>();
33-
for (StackTraceElement item : t.getStackTrace()) {
34-
RunLogStackTraceDto stackItem = new RunLogStackTraceDto();
35-
stackItem.setLine(item.getLineNumber());
36-
stackItem.setContainer(item.getClassName());
37-
stackItem.setFunction(item.getMethodName());
38-
stack.add(stackItem);
39-
}
40-
runLog.setStacktrace(stack);
41-
42-
if (t.getCause() != null) {
43-
runLog.setCause(parseThrowable(t.getCause()));
44-
}
45-
return runLog;
46-
}
47-
48-
private static TestResultDto parseTestResult(boolean success, Description description, Throwable t) {
49-
TestResultDto result = new TestResultDto();
50-
result.setSuccess(success);
51-
result.setNotFound(false);
52-
result.setTestReference(description.getClassName() + "#" + description.getMethodName());
53-
if (t != null) {
54-
result.setLogs(singletonList(parseThrowable(t)));
55-
}
56-
return result;
57-
}
58-
59-
private static TestResultDto createTestNotFoundResult(String testReference) {
60-
TestResultDto result = new TestResultDto();
61-
result.setSuccess(false);
62-
result.setNotFound(true);
63-
result.setTestReference(testReference);
64-
return result;
65-
}
66-
67-
private static final Pattern COMMAND_PATTERN = Pattern.compile("(?<class>[^#]+)(?:#(?<method>[^#]+))?");
68-
69-
private static ByteArrayOutputStream out, err;
70-
714
public static void main(String... args) {
72-
PrintStream realOut = System.out;
73-
PrintStream realErr = System.err;
74-
75-
List<TestResultDto> results = new ArrayList<>();
76-
77-
JUnitCore jUnitCore = new JUnitCore();
78-
jUnitCore.addListener(new RunListener() {
79-
private TestResultDto currentResult;
80-
81-
public void testStarted(Description description) {
82-
out = new ByteArrayOutputStream();
83-
err = new ByteArrayOutputStream();
84-
System.setOut(new PrintStream(out, true));
85-
System.setErr(new PrintStream(err, true));
86-
currentResult = null;
87-
}
88-
89-
public void testFailure(Failure failure) throws Exception {
90-
currentResult = parseTestResult(false, failure.getDescription(), failure.getException());
91-
}
92-
93-
public void testFinished(Description description) throws Exception {
94-
if (currentResult == null) {
95-
currentResult = parseTestResult(true, description, null);
96-
}
97-
currentResult.setProgramStderr(new String(err.toByteArray()));
98-
currentResult.setProgramStdout(new String(out.toByteArray()));
99-
err.close();
100-
out.close();
101-
results.add(currentResult);
102-
}
103-
});
104-
105-
boolean successful = true;
106-
for (String arg : args) {
107-
Matcher matcher = COMMAND_PATTERN.matcher(arg);
108-
if (matcher.matches()) {
109-
try {
110-
Class<?> clazz = Class.forName(matcher.group("class"));
111-
String method = matcher.group("method");
112-
Request request = null;
113-
if (method != null) {
114-
try {
115-
clazz.getMethod(method);
116-
request = Request.method(clazz, method);
117-
}
118-
catch (NoSuchMethodException | SecurityException ignored) {
119-
}
120-
}
121-
else {
122-
request = Request.aClass(clazz);
123-
}
124-
125-
if (request != null) {
126-
Result result = jUnitCore.run(request);
127-
if (!result.wasSuccessful()) {
128-
successful = false;
129-
}
130-
}
131-
else {
132-
results.add(createTestNotFoundResult(arg));
133-
successful = false;
134-
}
135-
}
136-
catch (ClassNotFoundException e) {
137-
results.add(createTestNotFoundResult(arg));
138-
successful = false;
139-
}
140-
}
141-
else {
142-
results.add(createTestNotFoundResult(arg));
143-
successful = false;
144-
}
145-
}
146-
int statusCode = successful ? 0 : 1;
147-
148-
String resultOutput = System.getProperty("codingame.junit-runner.output", DEFAULT_OUTPUT);
149-
String resultStr = new Gson().toJson(results);
150-
if (DEFAULT_OUTPUT.equals(resultOutput)) {
151-
realOut.println(resultStr);
152-
}
153-
else {
154-
try {
155-
FileUtils.writeStringToFile(new File(resultOutput), resultStr);
156-
} catch (IOException e) {
157-
realErr.println(e.getMessage());
158-
statusCode = 3;
159-
}
160-
}
161-
5+
JUnitTest jUnitTest = new JUnitTest();
6+
int statusCode = jUnitTest.run(args);
1627
System.exit(statusCode);
1638
}
1649
}

0 commit comments

Comments
 (0)