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