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 | |
import org.apache.maven.surefire.report.ReporterManager; |
22 | |
import org.apache.maven.surefire.report.ReporterManagerFactory; |
23 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
24 | |
import org.junit.runner.Description; |
25 | |
import org.junit.runner.Result; |
26 | |
import org.junit.runner.notification.Failure; |
27 | |
import org.junit.runner.notification.RunListener; |
28 | |
|
29 | |
import java.io.PrintStream; |
30 | |
import java.util.Map; |
31 | |
import java.util.concurrent.ConcurrentHashMap; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public abstract class ConcurrentReportingRunListener |
37 | |
extends RunListener |
38 | |
{ |
39 | 0 | private final PrintStream orgSystemOut = System.out; |
40 | |
|
41 | 0 | private final PrintStream orgSystemErr = System.err; |
42 | |
|
43 | 0 | protected Map<Class, TestSet> classMethodCounts = new ConcurrentHashMap<Class, TestSet>(); |
44 | |
|
45 | |
protected final ReporterManager reporterManager; |
46 | |
|
47 | |
protected final boolean reportImmediately; |
48 | |
|
49 | 0 | private final ConcurrentPrintStream out = new ConcurrentPrintStream( true ); |
50 | 0 | private final ConcurrentPrintStream err = new ConcurrentPrintStream( false ); |
51 | |
|
52 | |
public ConcurrentReportingRunListener( ReporterManagerFactory reporterFactory, boolean reportImmediately ) |
53 | |
throws TestSetFailedException |
54 | 0 | { |
55 | 0 | this.reportImmediately = reportImmediately; |
56 | 0 | reporterManager = reporterFactory.createReporterManager(); |
57 | |
|
58 | |
|
59 | |
|
60 | 0 | System.setOut( out ); |
61 | 0 | System.setErr( err ); |
62 | 0 | } |
63 | |
|
64 | |
@Override |
65 | |
public void testRunStarted( Description description ) |
66 | |
throws Exception |
67 | |
{ |
68 | 0 | TestMethod.fillTestCountMap( description, classMethodCounts ); |
69 | 0 | } |
70 | |
|
71 | |
@Override |
72 | |
public void testRunFinished( Result result ) |
73 | |
throws Exception |
74 | |
{ |
75 | 0 | for ( TestSet testSet : classMethodCounts.values() ) |
76 | |
{ |
77 | 0 | testSet.replay( reporterManager ); |
78 | |
} |
79 | 0 | System.setOut( orgSystemOut ); |
80 | 0 | System.setErr( orgSystemErr ); |
81 | |
|
82 | 0 | out.writeTo( orgSystemOut ); |
83 | 0 | err.writeTo( orgSystemErr ); |
84 | 0 | } |
85 | |
|
86 | |
protected TestMethod getTestMethod() |
87 | |
{ |
88 | 0 | return TestMethod.getThreadTestMethod(); |
89 | |
} |
90 | |
|
91 | |
protected void detachTestMethodFromThread() |
92 | |
{ |
93 | 0 | TestMethod.detachFromCurrentThread(); |
94 | 0 | } |
95 | |
|
96 | |
protected TestSet getTestSet( Description description ) |
97 | |
{ |
98 | 0 | return classMethodCounts.get( description.getTestClass() ); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public void testFailure( Failure failure ) |
103 | |
throws Exception |
104 | |
{ |
105 | 0 | getOrCreateTestMethod(failure.getDescription()).testFailure( failure ); |
106 | 0 | } |
107 | |
|
108 | |
private TestMethod getOrCreateTestMethod( Description description ) |
109 | |
{ |
110 | 0 | TestMethod threadTestMethod = TestMethod.getThreadTestMethod(); |
111 | 0 | if (threadTestMethod != null){ |
112 | 0 | return threadTestMethod; |
113 | |
} |
114 | 0 | TestSet testSet = getTestSet( description ); |
115 | 0 | return testSet.createTestMethod( description ); |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public void testAssumptionFailure( Failure failure ) |
120 | |
{ |
121 | 0 | TestMethod.getThreadTestMethod().testAssumptionFailure( failure ); |
122 | 0 | } |
123 | |
|
124 | |
@Override |
125 | |
public void testIgnored( Description description ) |
126 | |
throws Exception |
127 | |
{ |
128 | 0 | TestSet testSet = getTestSet( description ); |
129 | 0 | TestMethod testMethod = getTestSet( description ).createTestMethod( description ); |
130 | 0 | testMethod.testIgnored( description ); |
131 | 0 | testSet.incrementFinishedTests( reporterManager, reportImmediately ); |
132 | 0 | } |
133 | |
|
134 | |
@Override |
135 | |
public void testStarted( Description description ) |
136 | |
throws Exception |
137 | |
{ |
138 | 0 | TestSet testSet = getTestSet( description ); |
139 | 0 | testSet.createTestMethod( description ).attachToThread(); |
140 | 0 | checkIfTestSetCanBeReported( testSet ); |
141 | 0 | testSet.attachToThread(); |
142 | 0 | } |
143 | |
|
144 | |
public abstract void checkIfTestSetCanBeReported( TestSet testSetForTest ); |
145 | |
|
146 | |
@Override |
147 | |
public void testFinished( Description description ) |
148 | |
throws Exception |
149 | |
{ |
150 | 0 | getTestMethod().testFinished(); |
151 | 0 | TestSet.getThreadTestSet().incrementFinishedTests( reporterManager, reportImmediately ); |
152 | 0 | detachTestMethodFromThread(); |
153 | 0 | } |
154 | |
|
155 | |
public static ConcurrentReportingRunListener createInstance( ReporterManagerFactory reporterManagerFactory, |
156 | |
boolean parallelClasses, boolean parallelBoth ) |
157 | |
throws TestSetFailedException |
158 | |
{ |
159 | 0 | if ( parallelClasses ) |
160 | |
{ |
161 | 0 | return new ClassesParallelRunListener( reporterManagerFactory ); |
162 | |
} |
163 | 0 | return new MethodsParallelRunListener( reporterManagerFactory, !parallelBoth ); |
164 | |
} |
165 | |
|
166 | |
public static class ClassesParallelRunListener |
167 | |
extends ConcurrentReportingRunListener |
168 | |
{ |
169 | |
public ClassesParallelRunListener( ReporterManagerFactory reporterFactory ) |
170 | |
throws TestSetFailedException |
171 | |
{ |
172 | 0 | super( reporterFactory, false ); |
173 | 0 | } |
174 | |
|
175 | |
@Override |
176 | |
public void checkIfTestSetCanBeReported( TestSet testSetForTest ) |
177 | |
{ |
178 | 0 | TestSet currentlyAttached = TestSet.getThreadTestSet(); |
179 | 0 | if ( currentlyAttached != null && currentlyAttached != testSetForTest ) |
180 | |
{ |
181 | 0 | currentlyAttached.setAllScheduled( reporterManager ); |
182 | |
} |
183 | 0 | } |
184 | |
} |
185 | |
|
186 | |
public static class MethodsParallelRunListener |
187 | |
extends ConcurrentReportingRunListener |
188 | |
{ |
189 | |
private volatile TestSet lastStarted; |
190 | |
|
191 | 0 | private final Object lock = new Object(); |
192 | |
|
193 | |
public MethodsParallelRunListener( ReporterManagerFactory reporterFactory, boolean reportImmediately ) |
194 | |
throws TestSetFailedException |
195 | |
{ |
196 | 0 | super( reporterFactory, reportImmediately ); |
197 | 0 | } |
198 | |
|
199 | |
@Override |
200 | |
public void checkIfTestSetCanBeReported( TestSet testSetForTest ) |
201 | |
{ |
202 | 0 | synchronized ( lock ) |
203 | |
{ |
204 | 0 | if ( testSetForTest != lastStarted ) |
205 | |
{ |
206 | 0 | if ( lastStarted != null ) |
207 | |
{ |
208 | 0 | lastStarted.setAllScheduled( reporterManager ); |
209 | |
} |
210 | 0 | lastStarted = testSetForTest; |
211 | |
} |
212 | 0 | } |
213 | 0 | } |
214 | |
} |
215 | |
} |