-
Notifications
You must be signed in to change notification settings - Fork 532
/
DefaultReporterFactoryTest.java
452 lines (379 loc) · 17.5 KB
/
DefaultReporterFactoryTest.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* 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.io.File;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import junit.framework.TestCase;
import org.apache.maven.plugin.surefire.StartupReportConfiguration;
import org.apache.maven.plugin.surefire.extensions.SurefireConsoleOutputReporter;
import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.report.SafeThrowable;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.api.report.TestOutputReportEntry;
import org.apache.maven.surefire.api.report.TestReportListener;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.report.RunStatistics;
import org.apache.maven.surefire.shared.utils.logging.MessageUtils;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.error;
import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.failure;
import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.flake;
import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.skipped;
import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.success;
import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.unknown;
import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.getTestResultType;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.reflect.Whitebox.getInternalState;
import static org.powermock.reflect.Whitebox.invokeMethod;
/**
*
*/
public class DefaultReporterFactoryTest extends TestCase {
private static final String TEST_ONE = "testOne";
private static final String TEST_TWO = "testTwo";
private static final String TEST_THREE = "testThree";
private static final String TEST_FOUR = "testFour";
private static final String TEST_FIVE = "testFive";
private static final String ASSERTION_FAIL = "assertionFail";
private static final String ERROR = "error";
public void testMergeTestHistoryResult() throws Exception {
MessageUtils.setColorEnabled(false);
File target = new File(System.getProperty("user.dir"), "target");
File reportsDirectory = new File(target, "tmp5");
StartupReportConfiguration reportConfig = new StartupReportConfiguration(
true,
true,
"PLAIN",
false,
reportsDirectory,
false,
null,
new File(reportsDirectory, "TESTHASH"),
false,
1,
null,
null,
false,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
DummyTestReporter reporter = new DummyTestReporter();
DefaultReporterFactory factory = new DefaultReporterFactory(reportConfig, reporter);
// First run, four tests failed and one passed
Queue<TestMethodStats> firstRunStats = new ArrayDeque<>();
firstRunStats.add(new TestMethodStats(TEST_ONE, ReportEntryType.ERROR, new DummyStackTraceWriter(ERROR)));
firstRunStats.add(new TestMethodStats(TEST_TWO, ReportEntryType.ERROR, new DummyStackTraceWriter(ERROR)));
firstRunStats.add(
new TestMethodStats(TEST_THREE, ReportEntryType.FAILURE, new DummyStackTraceWriter(ASSERTION_FAIL)));
firstRunStats.add(
new TestMethodStats(TEST_FOUR, ReportEntryType.FAILURE, new DummyStackTraceWriter(ASSERTION_FAIL)));
firstRunStats.add(new TestMethodStats(TEST_FIVE, ReportEntryType.SUCCESS, null));
// Second run, two tests passed
Queue<TestMethodStats> secondRunStats = new ArrayDeque<>();
secondRunStats.add(
new TestMethodStats(TEST_ONE, ReportEntryType.FAILURE, new DummyStackTraceWriter(ASSERTION_FAIL)));
secondRunStats.add(new TestMethodStats(TEST_TWO, ReportEntryType.SUCCESS, null));
secondRunStats.add(new TestMethodStats(TEST_THREE, ReportEntryType.ERROR, new DummyStackTraceWriter(ERROR)));
secondRunStats.add(new TestMethodStats(TEST_FOUR, ReportEntryType.SUCCESS, null));
// Third run, another test passed
Queue<TestMethodStats> thirdRunStats = new ArrayDeque<>();
thirdRunStats.add(new TestMethodStats(TEST_ONE, ReportEntryType.SUCCESS, null));
thirdRunStats.add(new TestMethodStats(TEST_THREE, ReportEntryType.ERROR, new DummyStackTraceWriter(ERROR)));
TestSetRunListener firstRunListener = mock(TestSetRunListener.class);
TestSetRunListener secondRunListener = mock(TestSetRunListener.class);
TestSetRunListener thirdRunListener = mock(TestSetRunListener.class);
when(firstRunListener.getTestMethodStats()).thenReturn(firstRunStats);
when(secondRunListener.getTestMethodStats()).thenReturn(secondRunStats);
when(thirdRunListener.getTestMethodStats()).thenReturn(thirdRunStats);
factory.addListener(firstRunListener);
factory.addListener(secondRunListener);
factory.addListener(thirdRunListener);
invokeMethod(factory, "mergeTestHistoryResult");
RunStatistics mergedStatistics = factory.getGlobalRunStatistics();
// Only TEST_THREE is a failing test, other three are flaky tests
assertEquals(5, mergedStatistics.getCompletedCount());
assertEquals(1, mergedStatistics.getErrors());
assertEquals(0, mergedStatistics.getFailures());
assertEquals(3, mergedStatistics.getFlakes());
assertEquals(0, mergedStatistics.getSkipped());
// Now test the result will be printed out correctly
factory.printTestFailures(flake);
String[] expectedFlakeOutput = {
"Flakes: ",
TEST_FOUR,
" Run 1: " + ASSERTION_FAIL,
" Run 2: PASS",
"",
TEST_ONE,
" Run 1: " + ERROR,
" Run 2: " + ASSERTION_FAIL,
" Run 3: PASS",
"",
TEST_TWO,
" Run 1: " + ERROR,
" Run 2: PASS",
""
};
assertEquals(asList(expectedFlakeOutput), reporter.getMessages());
reporter.reset();
factory.printTestFailures(error);
String[] expectedFailureOutput = {
"Errors: ", TEST_THREE, " Run 1: " + ASSERTION_FAIL, " Run 2: " + ERROR, " Run 3: " + ERROR, ""
};
assertEquals(asList(expectedFailureOutput), reporter.getMessages());
reporter.reset();
factory.printTestFailures(failure);
assertEquals(emptyList(), reporter.getMessages());
}
static final class DummyTestReporter implements ConsoleLogger {
private final List<String> messages = new ArrayList<>();
@Override
public boolean isDebugEnabled() {
return true;
}
@Override
public void debug(String message) {
messages.add(message);
}
@Override
public boolean isInfoEnabled() {
return true;
}
@Override
public void info(String message) {
messages.add(message);
}
@Override
public boolean isWarnEnabled() {
return true;
}
@Override
public void warning(String message) {
messages.add(message);
}
@Override
public boolean isErrorEnabled() {
return true;
}
@Override
public void error(String message) {
messages.add(message);
}
@Override
public void error(String message, Throwable t) {
messages.add(message + " " + t.getLocalizedMessage());
}
@Override
public void error(Throwable t) {
messages.add(t.getLocalizedMessage());
}
List<String> getMessages() {
return messages;
}
void reset() {
messages.clear();
}
}
public void testGetTestResultType() {
List<ReportEntryType> emptyList = new ArrayList<>();
assertEquals(unknown, getTestResultType(emptyList, 1));
List<ReportEntryType> successList = new ArrayList<>();
successList.add(ReportEntryType.SUCCESS);
successList.add(ReportEntryType.SUCCESS);
assertEquals(success, getTestResultType(successList, 1));
List<ReportEntryType> failureErrorList = new ArrayList<>();
failureErrorList.add(ReportEntryType.FAILURE);
failureErrorList.add(ReportEntryType.ERROR);
assertEquals(error, getTestResultType(failureErrorList, 1));
List<ReportEntryType> errorFailureList = new ArrayList<>();
errorFailureList.add(ReportEntryType.ERROR);
errorFailureList.add(ReportEntryType.FAILURE);
assertEquals(error, getTestResultType(errorFailureList, 1));
List<ReportEntryType> flakeList = new ArrayList<>();
flakeList.add(ReportEntryType.SUCCESS);
flakeList.add(ReportEntryType.FAILURE);
assertEquals(flake, getTestResultType(flakeList, 1));
assertEquals(failure, getTestResultType(flakeList, 0));
flakeList = new ArrayList<>();
flakeList.add(ReportEntryType.ERROR);
flakeList.add(ReportEntryType.SUCCESS);
flakeList.add(ReportEntryType.FAILURE);
assertEquals(flake, getTestResultType(flakeList, 1));
assertEquals(error, getTestResultType(flakeList, 0));
List<ReportEntryType> skippedList = new ArrayList<>();
skippedList.add(ReportEntryType.SKIPPED);
assertEquals(skipped, getTestResultType(skippedList, 1));
}
public void testLogger() {
MessageUtils.setColorEnabled(false);
File target = new File(System.getProperty("user.dir"), "target");
File reportsDirectory = new File(target, "tmp6");
StartupReportConfiguration reportConfig = new StartupReportConfiguration(
true,
true,
"PLAIN",
false,
reportsDirectory,
false,
null,
new File(reportsDirectory, "TESTHASH"),
false,
1,
null,
null,
false,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
DummyTestReporter reporter = new DummyTestReporter();
DefaultReporterFactory factory = new DefaultReporterFactory(reportConfig, reporter);
TestReportListener<TestOutputReportEntry> runListener = factory.createTestReportListener();
assertTrue(runListener.isDebugEnabled());
assertTrue(runListener.isInfoEnabled());
assertTrue(runListener.isWarnEnabled());
assertTrue(runListener.isErrorEnabled());
runListener.debug("msg");
assertEquals(1, reporter.getMessages().size());
assertEquals("msg", reporter.getMessages().get(0));
reporter.reset();
runListener.info("msg\n");
assertEquals(1, reporter.getMessages().size());
assertEquals("msg", reporter.getMessages().get(0));
reporter.reset();
runListener.warning("msg\r\n");
assertEquals(1, reporter.getMessages().size());
assertEquals("msg", reporter.getMessages().get(0));
reporter.reset();
runListener.error("msg");
assertEquals(1, reporter.getMessages().size());
assertEquals("msg", reporter.getMessages().get(0));
reporter.reset();
runListener.error("msg\n", new Exception("e"));
assertEquals(1, reporter.getMessages().size());
assertEquals("msg e", reporter.getMessages().get(0));
reporter.reset();
runListener.error(new Exception("e"));
assertEquals(1, reporter.getMessages().size());
assertEquals("e", reporter.getMessages().get(0));
reporter.reset();
}
public void testCreateReporterWithZeroStatistics() {
MessageUtils.setColorEnabled(false);
File target = new File(System.getProperty("user.dir"), "target");
File reportsDirectory = new File(target, "tmp7");
StartupReportConfiguration reportConfig = new StartupReportConfiguration(
true,
true,
"PLAIN",
false,
reportsDirectory,
false,
null,
new File(reportsDirectory, "TESTHASH"),
false,
0,
null,
null,
false,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
assertTrue(reportConfig.isUseFile());
assertTrue(reportConfig.isPrintSummary());
assertEquals("PLAIN", reportConfig.getReportFormat());
assertFalse(reportConfig.isRedirectTestOutputToFile());
assertEquals(reportsDirectory, reportConfig.getReportsDirectory());
assertFalse(reportConfig.isTrimStackTrace());
assertNull(reportConfig.getReportNameSuffix());
assertEquals(new File(reportsDirectory, "TESTHASH"), reportConfig.getStatisticsFile());
assertFalse(reportConfig.isRequiresRunHistory());
assertEquals(0, reportConfig.getRerunFailingTestsCount());
assertNull(reportConfig.getXsdSchemaLocation());
assertEquals(UTF_8, reportConfig.getEncoding());
assertFalse(reportConfig.isForking());
assertNotNull(reportConfig.getXmlReporter());
assertNotNull(reportConfig.getConsoleOutputReporter());
assertNotNull(reportConfig.getTestsetReporter());
assertNull(reportConfig.getStatisticsReporter());
DummyTestReporter reporter = new DummyTestReporter();
DefaultReporterFactory factory = new DefaultReporterFactory(reportConfig, reporter);
assertEquals(reportsDirectory, factory.getReportsDirectory());
TestSetRunListener runListener = (TestSetRunListener) factory.createTestReportListener();
Collection listeners = getInternalState(factory, "listeners");
assertEquals(1, listeners.size());
assertTrue(listeners.contains(runListener));
assertNotNull(runListener.getTestMethodStats());
factory.runStarting();
factory.close();
RunStatistics statistics = factory.getGlobalRunStatistics();
assertEquals(0, statistics.getCompletedCount());
assertEquals(new RunResult(0, 0, 0, 0), statistics.getRunResult());
assertEquals(0, statistics.getFailures());
assertEquals(0, statistics.getErrors());
assertEquals(0, statistics.getSkipped());
assertEquals(0, statistics.getFlakes());
assertEquals("Tests run: 0, Failures: 0, Errors: 0, Skipped: 0", statistics.getSummary());
assertEquals(0, statistics.getCompletedCount());
List<String> messages = reporter.getMessages();
assertEquals("", messages.get(0));
assertEquals("-------------------------------------------------------", messages.get(1));
assertEquals(" T E S T S", messages.get(2));
assertEquals("-------------------------------------------------------", messages.get(3));
assertEquals("", messages.get(4));
assertEquals("Results:", messages.get(5));
assertEquals("", messages.get(6));
assertEquals("Tests run: 0, Failures: 0, Errors: 0, Skipped: 0", messages.get(7));
assertEquals("", messages.get(8));
assertEquals(9, messages.size());
}
static class DummyStackTraceWriter implements StackTraceWriter {
private final String stackTrace;
DummyStackTraceWriter(String stackTrace) {
this.stackTrace = stackTrace;
}
@Override
public String writeTraceToString() {
return "";
}
@Override
public String writeTrimmedTraceToString() {
return "";
}
@Override
public String smartTrimmedStackTrace() {
return stackTrace;
}
@Override
public SafeThrowable getThrowable() {
return null;
}
}
}