1 | |
package org.apache.maven.plugin.surefire.report; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.io.output.DeferredFileOutputStream; |
23 | |
import org.apache.maven.plugin.surefire.runorder.StatisticsReporter; |
24 | |
import org.apache.maven.surefire.report.ConsoleLogger; |
25 | |
import org.apache.maven.surefire.report.ConsoleOutputReceiver; |
26 | |
import org.apache.maven.surefire.report.ReportEntry; |
27 | |
import org.apache.maven.surefire.report.RunListener; |
28 | |
import org.apache.maven.surefire.report.RunStatistics; |
29 | |
import org.apache.maven.surefire.util.NestedRuntimeException; |
30 | |
|
31 | |
import java.io.File; |
32 | |
import java.io.IOException; |
33 | |
import java.util.List; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class TestSetRunListener |
42 | |
implements RunListener, ConsoleOutputReceiver, ConsoleLogger |
43 | |
{ |
44 | |
private final RunStatistics globalStatistics; |
45 | |
|
46 | |
private final TestSetStats detailsForThis; |
47 | |
|
48 | 0 | private DeferredFileOutputStream testStdOut = initDeferred( "stdout" ); |
49 | |
|
50 | 0 | private DeferredFileOutputStream testStdErr = initDeferred( "stderr" ); |
51 | |
|
52 | |
private DeferredFileOutputStream initDeferred( String channel ) |
53 | |
{ |
54 | 0 | return new DeferredFileOutputStream( 1000000, channel , "deferred", null ); |
55 | |
} |
56 | |
|
57 | |
|
58 | |
private final TestcycleConsoleOutputReceiver consoleOutputReceiver; |
59 | |
|
60 | |
private final boolean briefOrPlainFormat; |
61 | |
|
62 | |
private final StatelessXmlReporter simpleXMLReporter; |
63 | |
|
64 | |
private final ConsoleReporter consoleReporter; |
65 | |
|
66 | |
private final FileReporter fileReporter; |
67 | |
|
68 | |
private final StatisticsReporter statisticsReporter; |
69 | |
|
70 | |
public TestSetRunListener( ConsoleReporter consoleReporter, FileReporter fileReporter, |
71 | |
StatelessXmlReporter simpleXMLReporter, |
72 | |
TestcycleConsoleOutputReceiver consoleOutputReceiver, |
73 | |
StatisticsReporter statisticsReporter, RunStatistics globalStats, boolean trimStackTrace, |
74 | |
boolean isPlainFormat, boolean briefOrPlainFormat ) |
75 | 0 | { |
76 | 0 | this.consoleReporter = consoleReporter; |
77 | 0 | this.fileReporter = fileReporter; |
78 | 0 | this.statisticsReporter = statisticsReporter; |
79 | 0 | this.simpleXMLReporter = simpleXMLReporter; |
80 | 0 | this.consoleOutputReceiver = consoleOutputReceiver; |
81 | 0 | this.briefOrPlainFormat = briefOrPlainFormat; |
82 | 0 | this.detailsForThis = new TestSetStats( trimStackTrace, isPlainFormat ); |
83 | 0 | this.globalStatistics = globalStats; |
84 | 0 | } |
85 | |
|
86 | |
public void info( String message ) |
87 | |
{ |
88 | 0 | if ( consoleReporter != null ) |
89 | |
{ |
90 | 0 | consoleReporter.writeMessage( message ); |
91 | |
} |
92 | 0 | } |
93 | |
|
94 | |
public void writeTestOutput( byte[] buf, int off, int len, boolean stdout ) |
95 | |
{ |
96 | |
try |
97 | |
{ |
98 | 0 | if ( stdout ) |
99 | |
{ |
100 | 0 | testStdOut.write( buf, off, len ); |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 0 | testStdErr.write( buf, off, len ); |
105 | |
} |
106 | |
} |
107 | 0 | catch ( IOException e ) |
108 | |
{ |
109 | 0 | throw new NestedRuntimeException( e ); |
110 | 0 | } |
111 | 0 | consoleOutputReceiver.writeTestOutput( buf, off, len, stdout ); |
112 | 0 | } |
113 | |
|
114 | |
public void testSetStarting( ReportEntry report ) |
115 | |
{ |
116 | 0 | detailsForThis.testSetStart(); |
117 | 0 | if ( consoleReporter != null ) |
118 | |
{ |
119 | 0 | consoleReporter.testSetStarting( report ); |
120 | |
} |
121 | 0 | consoleOutputReceiver.testSetStarting( report ); |
122 | 0 | } |
123 | |
|
124 | |
public void clearCapture() |
125 | |
{ |
126 | 0 | testStdOut = initDeferred( "stdout" ); |
127 | 0 | testStdErr = initDeferred( "stderr" ); |
128 | 0 | } |
129 | |
|
130 | |
public void testSetCompleted( ReportEntry report ) |
131 | |
{ |
132 | 0 | WrappedReportEntry wrap = wrapTestSet( report ); |
133 | 0 | List<String> testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : null; |
134 | 0 | if ( consoleReporter != null ) |
135 | |
{ |
136 | 0 | consoleReporter.testSetCompleted( wrap, detailsForThis, testResults ); |
137 | |
} |
138 | 0 | consoleOutputReceiver.testSetCompleted( wrap ); |
139 | 0 | if ( fileReporter != null ) |
140 | |
{ |
141 | 0 | fileReporter.testSetCompleted( wrap, detailsForThis, testResults ); |
142 | |
} |
143 | 0 | if ( simpleXMLReporter != null ) |
144 | |
{ |
145 | 0 | simpleXMLReporter.testSetCompleted( wrap, detailsForThis ); |
146 | |
} |
147 | 0 | if ( statisticsReporter != null ) |
148 | |
{ |
149 | 0 | statisticsReporter.testSetCompleted(); |
150 | |
} |
151 | 0 | if ( consoleReporter != null ) |
152 | |
{ |
153 | 0 | consoleReporter.reset(); |
154 | |
} |
155 | |
|
156 | 0 | globalStatistics.add( detailsForThis ); |
157 | 0 | detailsForThis.reset(); |
158 | |
|
159 | 0 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public void testStarting( ReportEntry report ) |
166 | |
{ |
167 | 0 | detailsForThis.testStart(); |
168 | |
|
169 | 0 | } |
170 | |
|
171 | |
public void testSucceeded( ReportEntry reportEntry ) |
172 | |
{ |
173 | 0 | WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.success ); |
174 | 0 | detailsForThis.testSucceeded( wrapped ); |
175 | 0 | if ( statisticsReporter != null ) |
176 | |
{ |
177 | 0 | statisticsReporter.testSucceeded( reportEntry ); |
178 | |
} |
179 | 0 | clearCapture(); |
180 | 0 | } |
181 | |
|
182 | |
public void testError( ReportEntry reportEntry ) |
183 | |
{ |
184 | 0 | WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.error ); |
185 | 0 | detailsForThis.testError( wrapped ); |
186 | 0 | if ( statisticsReporter != null ) |
187 | |
{ |
188 | 0 | statisticsReporter.testError( reportEntry ); |
189 | |
} |
190 | 0 | globalStatistics.addErrorSource( reportEntry.getStackTraceWriter() ); |
191 | 0 | clearCapture(); |
192 | 0 | } |
193 | |
|
194 | |
public void testFailed( ReportEntry reportEntry ) |
195 | |
{ |
196 | 0 | WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.failure ); |
197 | 0 | detailsForThis.testFailure( wrapped ); |
198 | 0 | if ( statisticsReporter != null ) |
199 | |
{ |
200 | 0 | statisticsReporter.testFailed( reportEntry ); |
201 | |
} |
202 | 0 | globalStatistics.addFailureSource( reportEntry.getStackTraceWriter() ); |
203 | 0 | clearCapture(); |
204 | 0 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public void testSkipped( ReportEntry reportEntry ) |
211 | |
{ |
212 | 0 | WrappedReportEntry wrapped = wrap( reportEntry, ReportEntryType.skipped ); |
213 | 0 | detailsForThis.testSkipped( wrapped ); |
214 | 0 | if ( statisticsReporter != null ) |
215 | |
{ |
216 | 0 | statisticsReporter.testSkipped( reportEntry ); |
217 | |
} |
218 | 0 | clearCapture(); |
219 | 0 | } |
220 | |
|
221 | |
public void testAssumptionFailure( ReportEntry report ) |
222 | |
{ |
223 | 0 | testSkipped( report ); |
224 | 0 | } |
225 | |
|
226 | |
private WrappedReportEntry wrap( ReportEntry other, ReportEntryType reportEntryType ) |
227 | |
{ |
228 | |
final int estimatedElapsed; |
229 | 0 | if ( reportEntryType != ReportEntryType.skipped ) |
230 | |
{ |
231 | 0 | if ( other.getElapsed() != null ) |
232 | |
{ |
233 | 0 | estimatedElapsed = other.getElapsed(); |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 0 | estimatedElapsed = detailsForThis.getElapsedSinceLastStart(); |
238 | |
} |
239 | |
} |
240 | |
else |
241 | |
{ |
242 | 0 | estimatedElapsed = 0; |
243 | |
} |
244 | |
|
245 | 0 | return new WrappedReportEntry( other, reportEntryType, estimatedElapsed, testStdOut, testStdErr ); |
246 | |
} |
247 | |
|
248 | |
private WrappedReportEntry wrapTestSet( ReportEntry other ) |
249 | |
{ |
250 | 0 | return new WrappedReportEntry( other, null, other.getElapsed() != null |
251 | |
? other.getElapsed() |
252 | |
: detailsForThis.getElapsedSinceTestSetStart(), testStdOut, testStdErr ); |
253 | |
} |
254 | |
|
255 | |
public void close() |
256 | |
{ |
257 | 0 | if ( consoleOutputReceiver != null ) |
258 | |
{ |
259 | 0 | consoleOutputReceiver.close(); |
260 | |
} |
261 | 0 | } |
262 | |
} |