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 java.util.Map; |
23 | |
import org.apache.maven.surefire.report.ConsoleLogger; |
24 | |
import org.apache.maven.surefire.report.ConsoleOutputReceiver; |
25 | |
import org.apache.maven.surefire.report.ReportEntry; |
26 | |
import org.apache.maven.surefire.report.ReporterFactory; |
27 | |
import org.apache.maven.surefire.report.RunListener; |
28 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 26 | public abstract class ConcurrentRunListener |
42 | |
implements RunListener, ConsoleOutputReceiver |
43 | |
{ |
44 | |
private final Map<String, TestSet> classMethodCounts; |
45 | |
|
46 | |
private final ThreadLocal<RunListener> reporterManagerThreadLocal; |
47 | |
|
48 | |
private final boolean reportImmediately; |
49 | |
|
50 | |
private final ReporterFactory reporterFactory; |
51 | |
|
52 | |
private final ConsoleLogger consoleLogger; |
53 | |
|
54 | |
ConcurrentRunListener( ReporterFactory reporterFactory, ConsoleLogger consoleLogger, boolean reportImmediately, |
55 | |
Map<String, TestSet> classMethodCounts ) |
56 | |
throws TestSetFailedException |
57 | 26 | { |
58 | 26 | this.reportImmediately = reportImmediately; |
59 | 26 | this.reporterFactory = reporterFactory; |
60 | 26 | this.classMethodCounts = classMethodCounts; |
61 | 26 | this.consoleLogger = consoleLogger; |
62 | |
|
63 | 26 | this.reporterManagerThreadLocal = new ThreadLocal<RunListener>() |
64 | 52 | { |
65 | |
@Override |
66 | |
protected RunListener initialValue() |
67 | |
{ |
68 | 26 | return ConcurrentRunListener.this.reporterFactory.createReporter(); |
69 | |
} |
70 | |
}; |
71 | 26 | } |
72 | |
|
73 | |
public void testSetStarting( ReportEntry description ) |
74 | |
{ |
75 | 26 | } |
76 | |
|
77 | |
public void testSetCompleted( ReportEntry result ) |
78 | |
{ |
79 | 26 | final RunListener reporterManager = getRunListener(); |
80 | 26 | for ( TestSet testSet : classMethodCounts.values() ) |
81 | |
{ |
82 | 38 | testSet.replay( reporterManager ); |
83 | 38 | } |
84 | 26 | reporterManagerThreadLocal.remove(); |
85 | 26 | } |
86 | |
|
87 | |
public void testFailed( ReportEntry failure ) |
88 | |
{ |
89 | 7 | final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure ); |
90 | 7 | if ( testMethod != null ) |
91 | |
{ |
92 | 7 | testMethod.testFailure( failure ); |
93 | 7 | testMethod.detachFromCurrentThread(); |
94 | |
} |
95 | 7 | } |
96 | |
|
97 | |
public void testError( ReportEntry failure ) |
98 | |
{ |
99 | 3 | final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure ); |
100 | 3 | if ( testMethod != null ) |
101 | |
{ |
102 | 2 | testMethod.testError( failure ); |
103 | 2 | testMethod.detachFromCurrentThread(); |
104 | |
} |
105 | 3 | } |
106 | |
|
107 | |
public void testSkipped( ReportEntry description ) |
108 | |
{ |
109 | 7 | TestSet testSet = getTestSet( description ); |
110 | 7 | TestMethod testMethod = testSet.createThreadAttachedTestMethod( description ); |
111 | 7 | testMethod.testIgnored( description ); |
112 | 7 | testSet.incrementFinishedTests( getRunListener(), reportImmediately ); |
113 | 7 | testMethod.detachFromCurrentThread(); |
114 | 7 | } |
115 | |
|
116 | |
public void testAssumptionFailure( ReportEntry failure ) |
117 | |
{ |
118 | 0 | final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure ); |
119 | 0 | if ( testMethod != null ) |
120 | |
{ |
121 | 0 | testMethod.testIgnored( failure ); |
122 | 0 | testMethod.detachFromCurrentThread(); |
123 | |
} |
124 | 0 | } |
125 | |
|
126 | |
public void testStarting( ReportEntry description ) |
127 | |
{ |
128 | 77 | TestSet testSet = getTestSet( description ); |
129 | 77 | testSet.createThreadAttachedTestMethod( description ); |
130 | |
|
131 | 77 | checkIfTestSetCanBeReported( testSet ); |
132 | 77 | testSet.attachToThread(); |
133 | 77 | } |
134 | |
|
135 | |
public void testSucceeded( ReportEntry report ) |
136 | |
{ |
137 | 70 | TestMethod testMethod = getTestMethod(); |
138 | 70 | if ( null != testMethod ) |
139 | |
{ |
140 | 70 | testMethod.testFinished(); |
141 | 70 | testMethod.getTestSet().incrementFinishedTests( getRunListener(), reportImmediately ); |
142 | 70 | testMethod.detachFromCurrentThread(); |
143 | |
} |
144 | 70 | } |
145 | |
|
146 | |
private TestMethod getOrCreateThreadAttachedTestMethod( ReportEntry description ) |
147 | |
{ |
148 | 10 | TestMethod threadTestMethod = TestMethod.getThreadTestMethod(); |
149 | 10 | if ( threadTestMethod != null ) |
150 | |
{ |
151 | 7 | return threadTestMethod; |
152 | |
} |
153 | 3 | TestSet testSet = getTestSet( description ); |
154 | 3 | if ( testSet == null ) |
155 | |
{ |
156 | 1 | consoleLogger.info( description.getName() ); |
157 | 1 | consoleLogger.info( description.getStackTraceWriter().writeTraceToString() ); |
158 | 1 | return null; |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | 2 | return testSet.createThreadAttachedTestMethod( description ); |
163 | |
} |
164 | |
} |
165 | |
|
166 | |
protected abstract void checkIfTestSetCanBeReported( TestSet testSetForTest ); |
167 | |
|
168 | |
TestMethod getTestMethod() |
169 | |
{ |
170 | 70 | return TestMethod.getThreadTestMethod(); |
171 | |
} |
172 | |
|
173 | |
TestSet getTestSet( ReportEntry description ) |
174 | |
{ |
175 | 87 | return classMethodCounts.get( description.getSourceName() ); |
176 | |
} |
177 | |
|
178 | |
RunListener getRunListener() |
179 | |
{ |
180 | 127 | return reporterManagerThreadLocal.get(); |
181 | |
} |
182 | |
|
183 | |
|
184 | |
public static ConcurrentRunListener createInstance( Map<String, TestSet> classMethodCounts, |
185 | |
ReporterFactory reporterManagerFactory, |
186 | |
boolean parallelClasses, boolean parallelBoth, |
187 | |
ConsoleLogger consoleLogger ) |
188 | |
throws TestSetFailedException |
189 | |
{ |
190 | 4 | if ( parallelClasses ) |
191 | |
{ |
192 | 1 | return new ClassesParallelRunListener( classMethodCounts, reporterManagerFactory, consoleLogger ); |
193 | |
} |
194 | 3 | return new MethodsParallelRunListener( classMethodCounts, reporterManagerFactory, !parallelBoth, |
195 | |
consoleLogger ); |
196 | |
} |
197 | |
|
198 | |
|
199 | |
public void writeTestOutput( byte[] buf, int off, int len, boolean stdout ) |
200 | |
{ |
201 | 1049 | TestMethod threadTestMethod = TestMethod.getThreadTestMethod(); |
202 | 1049 | if ( threadTestMethod != null ) |
203 | |
{ |
204 | 1002 | final LogicalStream logicalStream = threadTestMethod.getLogicalStream(); |
205 | 1002 | logicalStream.write( stdout, buf, off, len ); |
206 | 1002 | } |
207 | |
else |
208 | |
{ |
209 | |
|
210 | 47 | consoleLogger.info( new String( buf, off, len ) ); |
211 | |
} |
212 | 1049 | } |
213 | |
|
214 | |
} |