-
Notifications
You must be signed in to change notification settings - Fork 531
/
TestSetStats.java
263 lines (223 loc) · 8.56 KB
/
TestSetStats.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugin.surefire.report;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.shared.utils.logging.MessageBuilder;
import static org.apache.maven.surefire.api.report.CategorizedReportEntry.GROUP_PREFIX;
import static org.apache.maven.surefire.shared.utils.logging.MessageUtils.buffer;
/**
* Maintains per-thread test result state. Not thread safe.
*/
public class TestSetStats {
private static final String TESTS = "Tests ";
private static final String RUN = "run: ";
private static final String TESTS_RUN = "Tests run: ";
private static final String FAILURES = "Failures: ";
private static final String ERRORS = "Errors: ";
private static final String SKIPPED = "Skipped: ";
private static final String FAILURE_MARKER = " <<< FAILURE!";
private static final String IN_MARKER = " -- in ";
private static final String COMMA = ", ";
private final Queue<WrappedReportEntry> reportEntries = new ConcurrentLinkedQueue<>();
private final boolean trimStackTrace;
private final boolean plainFormat;
private long testSetStartAt;
private long testStartAt;
private int completedCount;
private int errors;
private int failures;
private int skipped;
private long lastStartAt;
public TestSetStats(boolean trimStackTrace, boolean plainFormat) {
this.trimStackTrace = trimStackTrace;
this.plainFormat = plainFormat;
}
public int getElapsedSinceTestSetStart() {
return testSetStartAt > 0 ? (int) (System.currentTimeMillis() - testSetStartAt) : 0;
}
public int getElapsedSinceLastStart() {
return lastStartAt > 0 ? (int) (System.currentTimeMillis() - lastStartAt) : 0;
}
public void testSetStart() {
testSetStartAt = System.currentTimeMillis();
lastStartAt = testSetStartAt;
}
public void testStart() {
testStartAt = System.currentTimeMillis();
lastStartAt = testStartAt;
}
private void finishTest(WrappedReportEntry reportEntry) {
reportEntries.add(reportEntry);
incrementCompletedCount();
// SUREFIRE-398 skipped tests call endTest without calling testStarting
// if startTime = 0, set it to endTime, so the diff will be 0
if (testStartAt == 0) {
testStartAt = System.currentTimeMillis();
}
}
public void testSucceeded(WrappedReportEntry reportEntry) {
finishTest(reportEntry);
}
public void testError(WrappedReportEntry reportEntry) {
errors += 1;
finishTest(reportEntry);
}
public void testFailure(WrappedReportEntry reportEntry) {
failures += 1;
finishTest(reportEntry);
}
public void testSkipped(WrappedReportEntry reportEntry) {
skipped += 1;
finishTest(reportEntry);
}
public void reset() {
completedCount = 0;
errors = 0;
failures = 0;
skipped = 0;
for (WrappedReportEntry entry : reportEntries) {
entry.getStdout().free();
entry.getStdErr().free();
}
reportEntries.clear();
}
public int getCompletedCount() {
return completedCount;
}
public int getErrors() {
return errors;
}
public int getFailures() {
return failures;
}
public int getSkipped() {
return skipped;
}
private void incrementCompletedCount() {
completedCount += 1;
}
public String getTestSetSummary(WrappedReportEntry reportEntry, boolean phrasedClassName) {
String summary = TESTS_RUN
+ completedCount
+ COMMA
+ FAILURES
+ failures
+ COMMA
+ ERRORS
+ errors
+ COMMA
+ SKIPPED
+ skipped
+ COMMA
+ reportEntry.getElapsedTimeVerbose();
if (failures > 0 || errors > 0) {
summary += FAILURE_MARKER;
}
summary += IN_MARKER;
summary += phrasedClassName ? reportEntry.getReportNameWithGroup() : reportEntry.getNameWithGroup();
return summary;
}
public String getColoredTestSetSummary(WrappedReportEntry reportEntry, boolean phrasedClassName) {
final boolean isSuccessful = failures == 0 && errors == 0 && skipped == 0;
final boolean isFailure = failures > 0;
final boolean isError = errors > 0;
final boolean isFailureOrError = isFailure | isError;
final boolean isSkipped = skipped > 0;
final MessageBuilder builder = buffer();
if (isSuccessful) {
if (completedCount == 0) {
builder.strong(TESTS_RUN).strong(completedCount);
} else {
builder.success(TESTS_RUN).success(completedCount);
}
} else {
if (isFailureOrError) {
builder.failure(TESTS).strong(RUN).strong(completedCount);
} else {
builder.warning(TESTS).strong(RUN).strong(completedCount);
}
}
builder.a(COMMA);
if (isFailure) {
builder.failure(FAILURES).failure(failures);
} else {
builder.a(FAILURES).a(failures);
}
builder.a(COMMA);
if (isError) {
builder.failure(ERRORS).failure(errors);
} else {
builder.a(ERRORS).a(errors);
}
builder.a(COMMA);
if (isSkipped) {
builder.warning(SKIPPED).warning(skipped);
} else {
builder.a(SKIPPED).a(skipped);
}
builder.a(COMMA).a(reportEntry.getElapsedTimeVerbose());
if (isFailureOrError) {
builder.failure(FAILURE_MARKER);
}
builder.a(IN_MARKER);
return concatenateWithTestGroup(builder, reportEntry, phrasedClassName);
}
public List<String> getTestResults() {
List<String> result = new ArrayList<>();
for (WrappedReportEntry testResult : reportEntries) {
if (testResult.isErrorOrFailure()) {
result.add(testResult.getOutput(trimStackTrace));
} else if (plainFormat && testResult.isSkipped()) {
result.add(testResult.getSourceName() + " skipped");
} else if (plainFormat && testResult.isSucceeded()) {
result.add(testResult.getElapsedTimeSummary());
}
}
// This should be Map with an enum and the enums will be displayed with colors on console.
return result;
}
public Collection<WrappedReportEntry> getReportEntries() {
return reportEntries;
}
/**
* Append the test set message for a report.
* e.g. "org.foo.BarTest ( of group )" or phrased text "test class description ( of group )".
*
* @param builder MessageBuilder with preceded text inside
* @param report report whose test set is starting
* @return the message
*/
static String concatenateWithTestGroup(MessageBuilder builder, ReportEntry report, boolean phrasedClassName) {
if (phrasedClassName && report.getReportNameWithGroup() != null) {
return builder.strong(report.getReportNameWithGroup()).toString();
} else {
String testClass = report.getNameWithGroup();
int indexOfGroup = testClass.indexOf(GROUP_PREFIX);
int delimiter = testClass.lastIndexOf('.', indexOfGroup == -1 ? testClass.length() : indexOfGroup);
String pkg = testClass.substring(0, 1 + delimiter);
String cls = testClass.substring(1 + delimiter);
return builder.a(pkg).strong(cls).toString();
}
}
}