|
1 | 1 | package com.codingame.codemachine.runner.junit;
|
2 | 2 |
|
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; |
7 | 3 | import org.junit.runner.JUnitCore;
|
8 | 4 | import org.junit.runner.Request;
|
9 |
| -import org.junit.runner.Runner; |
10 | 5 |
|
11 |
| -import java.io.File; |
12 |
| -import java.io.IOException; |
13 |
| -import java.io.PrintStream; |
14 | 6 | import java.util.regex.Matcher;
|
15 | 7 | import java.util.regex.Pattern;
|
16 | 8 |
|
17 | 9 | class JUnitTest {
|
18 |
| - private static final String DEFAULT_OUTPUT = "-"; |
19 | 10 | private static final Pattern COMMAND_PATTERN = Pattern.compile("(?<class>[^#]+)(?:#(?<method>[^#]+))?");
|
20 | 11 |
|
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 |
| - |
38 | 12 | 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); |
46 | 19 | }
|
47 | 20 |
|
48 |
| - TestCase findRequest(String testcaseSpecification) { |
49 |
| - TestCase request = null; |
| 21 | + Request findRequest(String testcaseSpecification) { |
| 22 | + Request request = null; |
50 | 23 | Matcher matcher = COMMAND_PATTERN.matcher(testcaseSpecification);
|
51 | 24 | if (matcher.matches()) {
|
52 | 25 | try {
|
53 | 26 | Class<?> clazz = Class.forName(matcher.group("class"));
|
54 | 27 | String method = matcher.group("method");
|
55 | 28 | if (method != null) {
|
56 |
| - request = TestCase.createTestCase(Request.method(clazz, method), testcaseSpecification); |
| 29 | + request = Request.method(clazz, method); |
57 | 30 | }
|
58 | 31 | else {
|
59 |
| - request = TestCase.createTestCase(Request.aClass(clazz), testcaseSpecification); |
| 32 | + request = Request.aClass(clazz); |
60 | 33 | }
|
61 | 34 | }
|
62 |
| - catch (ClassNotFoundException ignored) { |
63 |
| - request = TestCase.createTestCase(); |
64 |
| - } |
| 35 | + catch (ClassNotFoundException ignored) {} |
65 | 36 | }
|
66 | 37 | return request;
|
67 | 38 | }
|
68 | 39 |
|
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; |
106 | 44 | }
|
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 |
| - |
150 | 45 | }
|
0 commit comments