1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
23 import org.apache.maven.surefire.common.junit48.JUnit46StackTraceWriter;
24 import org.apache.maven.surefire.report.RunListener;
25 import org.apache.maven.surefire.report.StackTraceWriter;
26 import org.junit.runner.Description;
27 import org.junit.runner.Result;
28 import org.junit.runner.notification.Failure;
29
30 import java.util.Map;
31
32
33
34
35
36
37
38
39
40
41 public class JUnitCoreRunListener
42 extends JUnit4RunListener
43 {
44 private final Map<String, TestSet> classMethodCounts;
45
46
47
48
49
50 public JUnitCoreRunListener( RunListener reporter, Map<String, TestSet> classMethodCounts )
51 {
52 super( reporter );
53 this.classMethodCounts = classMethodCounts;
54 }
55
56
57
58
59
60
61 @Override
62 public void testRunStarted( Description description )
63 throws Exception
64 {
65 fillTestCountMap( description );
66 reporter.testSetStarting( null );
67 }
68
69 @Override
70 public void testRunFinished( Result result )
71 throws Exception
72 {
73 try
74 {
75 reporter.testSetCompleted( null );
76 }
77 finally
78 {
79 classMethodCounts.clear();
80 }
81 }
82
83 private void fillTestCountMap( Description testDesc )
84 {
85 for ( Description child : testDesc.getChildren() )
86 {
87 if ( !asTestLeaf( child ) )
88 {
89 fillTestCountMap( child );
90 }
91 }
92 }
93
94 private boolean asTestLeaf( Description description )
95 {
96 if ( description.isTest() )
97 {
98 final String testClassName = extractDescriptionClassName( description );
99 if ( testClassName != null )
100 {
101 final TestSet testSet;
102 if ( classMethodCounts.containsKey( testClassName ) )
103 {
104 testSet = classMethodCounts.get( testClassName );
105 }
106 else
107 {
108 testSet = new TestSet( testClassName );
109 classMethodCounts.put( testClassName, testSet );
110 }
111 testSet.incrementTestMethodCount();
112 }
113 return true;
114 }
115 else
116 {
117 return false;
118 }
119 }
120
121 @Override
122 protected StackTraceWriter createStackTraceWriter( Failure failure )
123 {
124 return new JUnit46StackTraceWriter( failure );
125 }
126
127 @Override
128 protected String extractDescriptionClassName( Description description )
129 {
130 return description.getClassName();
131 }
132
133 @Override
134 protected String extractDescriptionMethodName( Description description )
135 {
136 return description.getMethodName();
137 }
138 }