Skip to content

Commit 19698d6

Browse files
authored
Merge pull request #8 from CodinGame/pr/7
Do not generate json anymore, use new command API
2 parents 58bc1ae + 081ce7d commit 19698d6

13 files changed

+51
-479
lines changed

Diff for: pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
</properties>
1111

1212
<dependencies>
13-
<dependency>
14-
<groupId>com.google.code.gson</groupId>
15-
<artifactId>gson</artifactId>
16-
<version>2.7</version>
17-
</dependency>
1813
<dependency>
1914
<groupId>junit</groupId>
2015
<artifactId>junit</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,45 @@
11
package com.codingame.codemachine.runner.junit;
22

3-
import com.codingame.codemachine.runner.junit.core.TestResultDto;
4-
import com.google.gson.Gson;
5-
import org.apache.commons.io.FileUtils;
6-
import org.junit.internal.runners.ErrorReportingRunner;
73
import org.junit.runner.JUnitCore;
84
import org.junit.runner.Request;
9-
import org.junit.runner.Runner;
105

11-
import java.io.File;
12-
import java.io.IOException;
13-
import java.io.PrintStream;
146
import java.util.regex.Matcher;
157
import java.util.regex.Pattern;
168

179
class JUnitTest {
18-
private static final String DEFAULT_OUTPUT = "-";
1910
private static final Pattern COMMAND_PATTERN = Pattern.compile("(?<class>[^#]+)(?:#(?<method>[^#]+))?");
2011

21-
private final PrintStream realOut;
22-
private final PrintStream realErr;
23-
private final JUnitCore jUnitCore;
24-
private TestResultDto result;
25-
private boolean oneFailure;
26-
27-
JUnitTest() {
28-
realOut = System.out;
29-
realErr = System.err;
30-
jUnitCore = new JUnitCore();
31-
oneFailure = false;
32-
}
33-
34-
private boolean isOneFailure() {
35-
return this.oneFailure;
36-
}
37-
3812
int run(String testcaseSpecification) {
39-
TestCase testCase = findRequest(testcaseSpecification);
40-
runTestCase(testCase);
41-
42-
int statusCode = isOneFailure() ? 1 : 0;
43-
statusCode = generateResult() ? statusCode : 3;
44-
45-
return statusCode;
13+
Request request = findRequest(testcaseSpecification);
14+
if (request == null) {
15+
System.err.println(String.format("Testcase \"%s\" not found", testcaseSpecification));
16+
return 2;
17+
}
18+
return runTestCase(request);
4619
}
4720

48-
TestCase findRequest(String testcaseSpecification) {
49-
TestCase request = null;
21+
Request findRequest(String testcaseSpecification) {
22+
Request request = null;
5023
Matcher matcher = COMMAND_PATTERN.matcher(testcaseSpecification);
5124
if (matcher.matches()) {
5225
try {
5326
Class<?> clazz = Class.forName(matcher.group("class"));
5427
String method = matcher.group("method");
5528
if (method != null) {
56-
request = TestCase.createTestCase(Request.method(clazz, method), testcaseSpecification);
29+
request = Request.method(clazz, method);
5730
}
5831
else {
59-
request = TestCase.createTestCase(Request.aClass(clazz), testcaseSpecification);
32+
request = Request.aClass(clazz);
6033
}
6134
}
62-
catch (ClassNotFoundException ignored) {
63-
request = TestCase.createTestCase();
64-
}
35+
catch (ClassNotFoundException ignored) {}
6536
}
6637
return request;
6738
}
6839

69-
private void runTestCase(TestCase testCase) {
70-
if (testCase.exists()) {
71-
result = new TestResultDto();
72-
jUnitCore.addListener(new TestResultProvider(result));
73-
if (!testCase.run(jUnitCore)) {
74-
oneFailure = true;
75-
}
76-
}
77-
else {
78-
result = createTestNotFoundResult();
79-
oneFailure = true;
80-
}
81-
}
82-
83-
private TestResultDto createTestNotFoundResult() {
84-
TestResultDto result = new TestResultDto();
85-
result.setSuccess(false);
86-
result.setNotFound(true);
87-
return result;
88-
}
89-
90-
private boolean generateResult() {
91-
String resultOutput = System.getProperty("codingame.junit-runner.output", DEFAULT_OUTPUT);
92-
String resultStr = new Gson().toJson(result);
93-
if (DEFAULT_OUTPUT.equals(resultOutput)) {
94-
realOut.println(resultStr);
95-
}
96-
else {
97-
try {
98-
FileUtils.writeStringToFile(new File(resultOutput), resultStr);
99-
}
100-
catch (IOException e) {
101-
realErr.println(e.getMessage());
102-
return false;
103-
}
104-
}
105-
return true;
40+
private int runTestCase(Request request) {
41+
JUnitCore jUnitCore = new JUnitCore();
42+
jUnitCore.addListener(new TestResultFormatter());
43+
return jUnitCore.run(request).wasSuccessful() ? 0 : 1;
10644
}
107-
108-
static class TestCase {
109-
110-
static TestCase createTestCase() {
111-
return new TestCase(null, null);
112-
}
113-
114-
static TestCase createTestCase(Request request, String description) {
115-
TestCase testCase = new TestCase(null, null);
116-
if (request != null) {
117-
Runner runner = request.getRunner();
118-
if (!(runner instanceof ErrorReportingRunner)) {
119-
testCase = new TestCase(request, description);
120-
}
121-
}
122-
return testCase;
123-
}
124-
125-
private final Request request;
126-
private final String description;
127-
128-
private TestCase(Request request, String description) {
129-
this.request = request;
130-
this.description = description;
131-
}
132-
133-
Request request() {
134-
return this.request;
135-
}
136-
137-
String description() {
138-
return this.description;
139-
}
140-
141-
boolean exists() {
142-
return request() != null;
143-
}
144-
145-
boolean run(final JUnitCore jUnitCore) {
146-
return jUnitCore.run(request).wasSuccessful();
147-
}
148-
}
149-
15045
}

Diff for: src/main/java/com/codingame/codemachine/runner/junit/JUnitTestListRunner.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
public class JUnitTestListRunner {
44
public static void main(String... args) {
5-
JUnitTest jUnitTest = new JUnitTest();
6-
int statusCode = jUnitTest.run(args[0]);
7-
System.exit(statusCode);
5+
System.exit(new JUnitTest().run(args[0]));
86
}
97
}

Diff for: src/main/java/com/codingame/codemachine/runner/junit/TestResultDtoFactory.java

-42
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.codingame.codemachine.runner.junit;
2+
3+
import org.junit.runner.notification.Failure;
4+
import org.junit.runner.notification.RunListener;
5+
6+
public class TestResultFormatter extends RunListener {
7+
@Override
8+
public void testFailure(Failure failure) {
9+
failure.getException().printStackTrace();
10+
}
11+
}

Diff for: src/main/java/com/codingame/codemachine/runner/junit/TestResultProvider.java

-52
This file was deleted.

Diff for: src/main/java/com/codingame/codemachine/runner/junit/core/RunLogDto.java

-33
This file was deleted.

Diff for: src/main/java/com/codingame/codemachine/runner/junit/core/RunLogStackTraceDto.java

-31
This file was deleted.

0 commit comments

Comments
 (0)