Skip to content

Commit 58bc1ae

Browse files
authored
Merge pull request #6 from CodinGame/refact_due_to_json_specification_change
Refact: take into account the new json specifications
2 parents 98889ce + 6ab96d3 commit 58bc1ae

File tree

9 files changed

+111
-181
lines changed

9 files changed

+111
-181
lines changed

src/main/java/com/codingame/codemachine/runner/junit/JUnitTest.java

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@
1111
import java.io.File;
1212
import java.io.IOException;
1313
import java.io.PrintStream;
14-
import java.util.ArrayList;
15-
import java.util.List;
1614
import java.util.regex.Matcher;
1715
import java.util.regex.Pattern;
1816

19-
public class JUnitTest {
17+
class JUnitTest {
2018
private static final String DEFAULT_OUTPUT = "-";
2119
private static final Pattern COMMAND_PATTERN = Pattern.compile("(?<class>[^#]+)(?:#(?<method>[^#]+))?");
2220

2321
private final PrintStream realOut;
2422
private final PrintStream realErr;
2523
private final JUnitCore jUnitCore;
26-
private List<TestResultDto> results;
24+
private TestResultDto result;
2725
private boolean oneFailure;
2826

2927
JUnitTest() {
@@ -33,73 +31,65 @@ public class JUnitTest {
3331
oneFailure = false;
3432
}
3533

36-
boolean isOneFailure() {
34+
private boolean isOneFailure() {
3735
return this.oneFailure;
3836
}
3937

40-
int run(String... args) {
41-
List<TestCase> testCases = findRequests(args);
42-
runTestCases(testCases);
38+
int run(String testcaseSpecification) {
39+
TestCase testCase = findRequest(testcaseSpecification);
40+
runTestCase(testCase);
4341

4442
int statusCode = isOneFailure() ? 1 : 0;
4543
statusCode = generateResult() ? statusCode : 3;
4644

4745
return statusCode;
4846
}
4947

50-
List<TestCase> findRequests(String... args) {
51-
List<TestCase> requests = new ArrayList<>();
52-
for (String arg : args) {
53-
Matcher matcher = COMMAND_PATTERN.matcher(arg);
54-
if (matcher.matches()) {
55-
try {
56-
Class<?> clazz = Class.forName(matcher.group("class"));
57-
String method = matcher.group("method");
58-
if (method != null) {
59-
requests.add(TestCase.createTestCase(Request.method(clazz, method), arg));
60-
}
61-
else {
62-
requests.add(TestCase.createTestCase(Request.aClass(clazz), arg));
63-
}
48+
TestCase findRequest(String testcaseSpecification) {
49+
TestCase request = null;
50+
Matcher matcher = COMMAND_PATTERN.matcher(testcaseSpecification);
51+
if (matcher.matches()) {
52+
try {
53+
Class<?> clazz = Class.forName(matcher.group("class"));
54+
String method = matcher.group("method");
55+
if (method != null) {
56+
request = TestCase.createTestCase(Request.method(clazz, method), testcaseSpecification);
6457
}
65-
catch (ClassNotFoundException ignored) {
66-
requests.add(TestCase.createTestCase());
58+
else {
59+
request = TestCase.createTestCase(Request.aClass(clazz), testcaseSpecification);
6760
}
6861
}
62+
catch (ClassNotFoundException ignored) {
63+
request = TestCase.createTestCase();
64+
}
6965
}
70-
return requests;
71-
}
72-
73-
void runTestCases(List<TestCase> testCases) {
74-
results = new ArrayList<>();
75-
jUnitCore.addListener(new TestResultProvider(results));
76-
77-
testCases.forEach(this::runTestCase);
66+
return request;
7867
}
7968

8069
private void runTestCase(TestCase testCase) {
8170
if (testCase.exists()) {
71+
result = new TestResultDto();
72+
jUnitCore.addListener(new TestResultProvider(result));
8273
if (!testCase.run(jUnitCore)) {
8374
oneFailure = true;
8475
}
8576
}
8677
else {
87-
results.add(createTestNotFoundResult(testCase.description()));
78+
result = createTestNotFoundResult();
8879
oneFailure = true;
8980
}
9081
}
9182

92-
private TestResultDto createTestNotFoundResult(String testReference) {
83+
private TestResultDto createTestNotFoundResult() {
9384
TestResultDto result = new TestResultDto();
9485
result.setSuccess(false);
9586
result.setNotFound(true);
96-
result.setTestReference(testReference);
9787
return result;
9888
}
9989

10090
private boolean generateResult() {
10191
String resultOutput = System.getProperty("codingame.junit-runner.output", DEFAULT_OUTPUT);
102-
String resultStr = new Gson().toJson(results);
92+
String resultStr = new Gson().toJson(result);
10393
if (DEFAULT_OUTPUT.equals(resultOutput)) {
10494
realOut.println(resultStr);
10595
}

src/main/java/com/codingame/codemachine/runner/junit/JUnitTestListRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class JUnitTestListRunner {
44
public static void main(String... args) {
55
JUnitTest jUnitTest = new JUnitTest();
6-
int statusCode = jUnitTest.run(args);
6+
int statusCode = jUnitTest.run(args[0]);
77
System.exit(statusCode);
88
}
99
}

src/main/java/com/codingame/codemachine/runner/junit/TestResultDtoFactory.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,10 @@ private RunLogDto parseThrowable(Throwable t) {
3030
return runLog;
3131
}
3232

33-
TestResultDto create(boolean success, Description description, Throwable t) {
33+
TestResultDto create(boolean success, Throwable t) {
3434
TestResultDto result = new TestResultDto();
3535
result.setSuccess(success);
3636
result.setNotFound(false);
37-
String testReference = description.getClassName();
38-
if (description.getMethodName() != null) {
39-
testReference += "#" + description.getMethodName();
40-
}
41-
result.setTestReference(testReference);
4237
if (t != null) {
4338
result.setLogs(singletonList(parseThrowable(t)));
4439
}

src/main/java/com/codingame/codemachine/runner/junit/TestResultProvider.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
import java.io.ByteArrayOutputStream;
99
import java.io.PrintStream;
10-
import java.util.List;
1110

1211
public class TestResultProvider extends RunListener {
1312
private final TestResultDtoFactory testResultDtoFactory;
14-
private final List<TestResultDto> results;
13+
private final TestResultDto result;
1514

1615
private ByteArrayOutputStream out;
1716
private ByteArrayOutputStream err;
1817
private TestResultDto currentResult;
1918

20-
TestResultProvider(List<TestResultDto> results) {
21-
this(results, null);
19+
TestResultProvider(TestResultDto result) {
20+
this(result, null);
2221
}
2322

24-
TestResultProvider(List<TestResultDto> results, TestResultDtoFactory testResultDtoFactory) {
25-
this.results = results;
23+
TestResultProvider(TestResultDto result, TestResultDtoFactory testResultDtoFactory) {
24+
this.result = result;
25+
this.result.setSuccess(true);
2626
this.testResultDtoFactory = testResultDtoFactory != null ? testResultDtoFactory : new TestResultDtoFactory();
2727
}
2828

@@ -35,16 +35,17 @@ public void testStarted(Description description) {
3535
}
3636

3737
public void testFailure(Failure failure) {
38-
currentResult = testResultDtoFactory.create(false, failure.getDescription(), failure.getException());
38+
currentResult = testResultDtoFactory.create(false, failure.getException());
3939
}
4040

4141
public void testFinished(Description description) throws Exception {
4242
if (currentResult == null) {
43-
currentResult = testResultDtoFactory.create(true, description, null);
43+
currentResult = testResultDtoFactory.create(true, null);
4444
}
45-
currentResult.setProgramStderr(new String(err.toByteArray()));
46-
currentResult.setProgramStdout(new String(out.toByteArray()));
47-
results.add(currentResult);
45+
result.appendStderr(new String(err.toByteArray()));
46+
result.appendStdout(new String(out.toByteArray()));
47+
result.appendLogs(currentResult.getLogs());
48+
result.setSuccess(result.isSuccess() && currentResult.isSuccess());
4849
err.close();
4950
out.close();
5051
}
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
package com.codingame.codemachine.runner.junit.core;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
public class TestResultDto {
6-
private String testReference;
77
private boolean success;
88
private boolean notFound;
99
private List<RunLogDto> logs;
10-
private String programStdout;
11-
private String programStderr;
12-
13-
public String getTestReference() {
14-
return testReference;
15-
}
16-
17-
public void setTestReference(String testReference) {
18-
this.testReference = testReference;
19-
}
10+
private String stdout = "";
11+
private String stderr = "";
2012

2113
public boolean isSuccess() {
2214
return success;
@@ -27,13 +19,20 @@ public void setSuccess(boolean success) {
2719
}
2820

2921
public List<RunLogDto> getLogs() {
22+
if (logs == null) {
23+
setLogs(new ArrayList<>());
24+
}
3025
return logs;
3126
}
3227

3328
public void setLogs(List<RunLogDto> logs) {
3429
this.logs = logs;
3530
}
3631

32+
public void appendLogs(List<RunLogDto> logs) {
33+
getLogs().addAll(logs);
34+
}
35+
3736
public boolean isNotFound() {
3837
return notFound;
3938
}
@@ -42,19 +41,27 @@ public void setNotFound(boolean notFound) {
4241
this.notFound = notFound;
4342
}
4443

45-
public String getProgramStdout() {
46-
return programStdout;
44+
public String getStdout() {
45+
return stdout;
46+
}
47+
48+
public void setStdout(String stdout) {
49+
this.stdout = stdout;
50+
}
51+
52+
public void appendStdout(String stdsout) {
53+
setStdout(getStdout() + stdsout);
4754
}
4855

49-
public void setProgramStdout(String programStdout) {
50-
this.programStdout = programStdout;
56+
public String getStderr() {
57+
return stderr;
5158
}
5259

53-
public String getProgramStderr() {
54-
return programStderr;
60+
public void setStderr(String stderr) {
61+
this.stderr = stderr;
5562
}
5663

57-
public void setProgramStderr(String programStderr) {
58-
this.programStderr = programStderr;
64+
public void appendStderr(String stderr) {
65+
setStderr(getStderr() + stderr);
5966
}
6067
}

src/main/resources/junit-runner

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ find * -name "*.java" -print0 | xargs -0 /usr/src/codingame/java-compiler/cgjava
1010
compilationExitCode=$?
1111

1212
if [ $compilationExitCode -eq 0 ]; then
13-
java -cp "/project/workspace:$classpath:/usr/src/codingame/junit-runner/junit-runner.jar" -Dcodingame.junit-runner.output=/project/results/executions.json com.codingame.codemachine.runner.junit.JUnitTestListRunner $*
13+
java -cp "/project/workspace:$classpath:/usr/src/codingame/junit-runner/junit-runner.jar" -Dcodingame.junit-runner.output=/project/results/execution.json com.codingame.codemachine.runner.junit.JUnitTestListRunner $*
1414
executionExitCode=$?
1515
fi
1616

src/test/java/com/codingame/codemachine/runner/junit/TestJunitTest.java

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import org.junit.Before;
55
import org.junit.Test;
66

7-
import java.util.List;
8-
97
import static org.assertj.core.api.Assertions.assertThat;
108

119
public class TestJunitTest {
@@ -26,9 +24,8 @@ public void should_find_a_test_class() throws ClassNotFoundException {
2624
@Test
2725
public void should_not_find_test_class_if_it_does_not_exist() throws ClassNotFoundException {
2826
String className = "unknown.ClassTest";
29-
List<TestCase> testCases = jUnitTest.findRequests(className);
30-
assertThat(testCases).hasSize(1);
31-
assertThat(testCases.get(0).exists()).isFalse();
27+
TestCase testCase = jUnitTest.findRequest(className);
28+
assertThat(testCase.exists()).isFalse();
3229
}
3330

3431
@Test
@@ -38,32 +35,6 @@ public void should_find_a_test_method() {
3835
checkTestCase(className, methodName);
3936
}
4037

41-
@Test
42-
public void should_find_many_test_methods() {
43-
String className = "resources.simple.com.codingame.core.MyFirstTest";
44-
String methodName0 = "myFirstTest";
45-
String methodName1 = "aSecondTest";
46-
List<TestCase> testCases =
47-
jUnitTest.findRequests(className + "#" + methodName0, className + "#" + methodName1);
48-
assertThat(testCases).hasSize(2);
49-
assertThat(testCases.get(0).exists()).isTrue();
50-
assertThat(testCases.get(0).description()).isEqualTo(className + "#" + methodName0);
51-
assertThat(testCases.get(1).exists()).isTrue();
52-
assertThat(testCases.get(1).description()).isEqualTo(className + "#" + methodName1);
53-
}
54-
55-
@Test
56-
public void should_find_many_test_methods_even_with_errors() {
57-
String className = "resources.simple.com.codingame.core.MyFirstTest";
58-
String methodName0 = "myFirstTest";
59-
String methodName1 = "unknown";
60-
List<TestCase> testCases =
61-
jUnitTest.findRequests(className + "#" + methodName0, className + "#" + methodName1);
62-
assertThat(testCases).hasSize(2);
63-
assertThat(testCases.get(0).exists()).isTrue();
64-
assertThat(testCases.get(1).exists()).isFalse();
65-
}
66-
6738
@Test
6839
public void should_find_a_test_method_regardless_of_runWith() {
6940
String className = "resources.run_with.io.vertx.blog.first.MyFirstVerticleTest";
@@ -75,42 +46,29 @@ public void should_find_a_test_method_regardless_of_runWith() {
7546
public void does_not_work_with_parameterized_test_class() {
7647
String className = "resources.parameterized.com.codingame.core.FibonacciTest";
7748
String methodName = "test";
78-
List<TestCase> testCases = jUnitTest.findRequests(className + "#" + methodName);
79-
assertThat(testCases).hasSize(1);
80-
assertThat(testCases.get(0).exists()).isFalse();
49+
TestCase testCase = jUnitTest.findRequest(className + "#" + methodName);
50+
assertThat(testCase).isNotNull();
51+
assertThat(testCase.exists()).isFalse();
8152
}
8253

8354
private void checkTestCase(String className) throws ClassNotFoundException {
84-
List<TestCase> testCases = jUnitTest.findRequests(className);
85-
assertThat(testCases).hasSize(1);
55+
TestCase testCase = jUnitTest.findRequest(className);
56+
assertThat(testCase).isNotNull();
8657

8758
Class<?> clazz = Class.forName(className);
8859
int expectedTestCount = clazz.getDeclaredMethods().length;
89-
assertThat(testCases.get(0).exists()).isTrue();
90-
assertThat(testCases.get(0).description()).isEqualTo(className);
91-
int testCount = testCases.get(0).request().getRunner().testCount();
60+
assertThat(testCase.exists()).isTrue();
61+
assertThat(testCase.description()).isEqualTo(className);
62+
int testCount = testCase.request().getRunner().testCount();
9263
assertThat(testCount).isEqualTo(expectedTestCount);
9364
}
9465

9566
private void checkTestCase(String className, String methodName) {
96-
List<TestCase> testCases = jUnitTest.findRequests(className + "#" + methodName);
97-
assertThat(testCases).hasSize(1);
98-
assertThat(testCases.get(0).exists()).isTrue();
99-
assertThat(testCases.get(0).description()).isEqualTo(className + "#" + methodName);
100-
int testCount = testCases.get(0).request().getRunner().testCount();
67+
TestCase testCase = jUnitTest.findRequest(className + "#" + methodName);
68+
assertThat(testCase).isNotNull();
69+
assertThat(testCase.exists()).isTrue();
70+
assertThat(testCase.description()).isEqualTo(className + "#" + methodName);
71+
int testCount = testCase.request().getRunner().testCount();
10172
assertThat(testCount).isEqualTo(1);
10273
}
103-
104-
@Test
105-
public void should_run_only_known_test_methods() {
106-
String className = "resources.simple.com.codingame.core.MyFirstTest";
107-
String methodName0 = "myFirstTest";
108-
String methodName1 = "unknown";
109-
110-
JUnitTest jUnitTest = new JUnitTest();
111-
List<TestCase> testCases = jUnitTest.findRequests(className + "#" + methodName0, className + "#" + methodName1);
112-
jUnitTest.runTestCases(testCases);
113-
114-
assertThat(jUnitTest.isOneFailure()).isTrue();
115-
}
11674
}

0 commit comments

Comments
 (0)