1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.junitcore;
20
21 import java.util.Map;
22
23 import org.apache.maven.surefire.api.report.StackTraceWriter;
24 import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
25 import org.apache.maven.surefire.common.junit4.JUnit4StackTraceWriter;
26 import org.junit.runner.Description;
27 import org.junit.runner.Result;
28 import org.junit.runner.notification.Failure;
29
30 import static org.apache.maven.surefire.api.util.internal.TestClassMethodNameUtils.extractClassName;
31
32
33
34
35
36
37
38
39
40
41 final class JUnitCoreRunListener extends JUnit4RunListener {
42 private final Map<String, TestSet> classMethodCounts;
43
44
45
46
47
48 JUnitCoreRunListener(ConcurrentRunListener reporter, Map<String, TestSet> classMethodCounts) {
49 super(reporter);
50 this.classMethodCounts = classMethodCounts;
51 }
52
53
54
55
56
57
58 @Override
59 public void testRunStarted(Description description) throws Exception {
60 fillTestCountMap(description);
61 reporter.testSetStarting(null);
62 }
63
64 @Override
65 public void testRunFinished(Result result) throws Exception {
66 try {
67 reporter.testSetCompleted(null);
68 } finally {
69 classMethodCounts.clear();
70 }
71 }
72
73 private void fillTestCountMap(Description testDesc) {
74 for (Description child : testDesc.getChildren()) {
75 if (!asTestLeaf(child)) {
76 fillTestCountMap(child);
77 }
78 }
79 }
80
81 private boolean asTestLeaf(Description description) {
82 if (description.isTest()) {
83 final String testClassName = extractClassName(description.getDisplayName());
84 if (testClassName != null) {
85 final TestSet testSet;
86 if (classMethodCounts.containsKey(testClassName)) {
87 testSet = classMethodCounts.get(testClassName);
88 } else {
89 testSet = new TestSet(testClassName, getRunMode(), classMethodIndexer);
90 classMethodCounts.put(testClassName, testSet);
91 }
92 testSet.incrementTestMethodCount();
93 }
94 return true;
95 } else {
96 return false;
97 }
98 }
99
100 @Override
101 protected StackTraceWriter createStackTraceWriter(Failure failure) {
102 return new JUnit4StackTraceWriter(failure);
103 }
104 }