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