1 | |
package org.apache.maven.surefire.booter; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.PrintStream; |
23 | |
import java.util.Enumeration; |
24 | |
import java.util.Properties; |
25 | |
import org.apache.maven.surefire.report.ConsoleLogger; |
26 | |
import org.apache.maven.surefire.report.ConsoleOutputReceiver; |
27 | |
import org.apache.maven.surefire.report.ReportEntry; |
28 | |
import org.apache.maven.surefire.report.RunListener; |
29 | |
import org.apache.maven.surefire.report.SafeThrowable; |
30 | |
import org.apache.maven.surefire.report.StackTraceWriter; |
31 | |
import org.apache.maven.surefire.util.internal.ByteBuffer; |
32 | |
import org.apache.maven.surefire.util.internal.StringUtils; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class ForkingRunListener |
52 | |
implements RunListener, ConsoleLogger, ConsoleOutputReceiver |
53 | |
{ |
54 | |
public static final byte BOOTERCODE_TESTSET_STARTING = (byte) '1'; |
55 | |
|
56 | |
public static final byte BOOTERCODE_TESTSET_COMPLETED = (byte) '2'; |
57 | |
|
58 | |
public static final byte BOOTERCODE_STDOUT = (byte) '3'; |
59 | |
|
60 | |
public static final byte BOOTERCODE_STDERR = (byte) '4'; |
61 | |
|
62 | |
public static final byte BOOTERCODE_TEST_STARTING = (byte) '5'; |
63 | |
|
64 | |
public static final byte BOOTERCODE_TEST_SUCCEEDED = (byte) '6'; |
65 | |
|
66 | |
public static final byte BOOTERCODE_TEST_ERROR = (byte) '7'; |
67 | |
|
68 | |
public static final byte BOOTERCODE_TEST_FAILED = (byte) '8'; |
69 | |
|
70 | |
public static final byte BOOTERCODE_TEST_SKIPPED = (byte) '9'; |
71 | |
|
72 | |
public static final byte BOOTERCODE_TEST_ASSUMPTIONFAILURE = (byte) 'G'; |
73 | |
|
74 | |
public static final byte BOOTERCODE_CONSOLE = (byte) 'H'; |
75 | |
|
76 | |
public static final byte BOOTERCODE_SYSPROPS = (byte) 'I'; |
77 | |
|
78 | |
public static final byte BOOTERCODE_NEXT_TEST = (byte) 'N'; |
79 | |
|
80 | |
public static final byte BOOTERCODE_ERROR = (byte) 'X'; |
81 | |
|
82 | |
public static final byte BOOTERCODE_BYE = (byte) 'Z'; |
83 | |
|
84 | |
|
85 | |
private final PrintStream target; |
86 | |
|
87 | |
private final Integer testSetChannelId; |
88 | |
|
89 | |
private final boolean trimStackTraces; |
90 | |
|
91 | |
private final byte[] stdOutHeader; |
92 | |
|
93 | |
private final byte[] stdErrHeader; |
94 | |
|
95 | |
public ForkingRunListener( PrintStream target, int testSetChannelId, boolean trimStackTraces ) |
96 | 1 | { |
97 | 1 | this.target = target; |
98 | 1 | this.testSetChannelId = new Integer( testSetChannelId ); |
99 | 1 | this.trimStackTraces = trimStackTraces; |
100 | 1 | stdOutHeader = createHeader( BOOTERCODE_STDOUT, testSetChannelId ); |
101 | 1 | stdErrHeader = createHeader( BOOTERCODE_STDERR, testSetChannelId ); |
102 | 1 | sendProps(); |
103 | 1 | } |
104 | |
|
105 | |
public void testSetStarting( ReportEntry report ) |
106 | |
{ |
107 | 0 | target.print( toString( BOOTERCODE_TESTSET_STARTING, report, testSetChannelId ) ); |
108 | 0 | } |
109 | |
|
110 | |
public void testSetCompleted( ReportEntry report ) |
111 | |
{ |
112 | 0 | target.print( toString( BOOTERCODE_TESTSET_COMPLETED, report, testSetChannelId ) ); |
113 | 0 | } |
114 | |
|
115 | |
public void testStarting( ReportEntry report ) |
116 | |
{ |
117 | 0 | target.print( toString( BOOTERCODE_TEST_STARTING, report, testSetChannelId ) ); |
118 | 0 | } |
119 | |
|
120 | |
public void testSucceeded( ReportEntry report ) |
121 | |
{ |
122 | 0 | target.print( toString( BOOTERCODE_TEST_SUCCEEDED, report, testSetChannelId ) ); |
123 | 0 | } |
124 | |
|
125 | |
public void testAssumptionFailure( ReportEntry report ) |
126 | |
{ |
127 | 0 | target.print( toString( BOOTERCODE_TEST_ASSUMPTIONFAILURE, report, testSetChannelId ) ); |
128 | 0 | } |
129 | |
|
130 | |
public void testError( ReportEntry report ) |
131 | |
{ |
132 | 0 | target.print( toString( BOOTERCODE_TEST_ERROR, report, testSetChannelId ) ); |
133 | 0 | } |
134 | |
|
135 | |
public void testFailed( ReportEntry report ) |
136 | |
{ |
137 | 0 | target.print( toString( BOOTERCODE_TEST_FAILED, report, testSetChannelId ) ); |
138 | 0 | } |
139 | |
|
140 | |
public void testSkipped( ReportEntry report ) |
141 | |
{ |
142 | 0 | target.print( toString( BOOTERCODE_TEST_SKIPPED, report, testSetChannelId ) ); |
143 | 0 | } |
144 | |
|
145 | |
void sendProps() |
146 | |
{ |
147 | 1 | Properties systemProperties = System.getProperties(); |
148 | |
|
149 | 1 | if ( systemProperties != null ) |
150 | |
{ |
151 | 1 | Enumeration propertyKeys = systemProperties.propertyNames(); |
152 | |
|
153 | 59 | while ( propertyKeys.hasMoreElements() ) |
154 | |
{ |
155 | 58 | String key = (String) propertyKeys.nextElement(); |
156 | |
|
157 | 58 | String value = systemProperties.getProperty( key ); |
158 | |
|
159 | 58 | if ( value == null ) |
160 | |
{ |
161 | 0 | value = "null"; |
162 | |
} |
163 | 58 | target.print( toPropertyString( key, value ) ); |
164 | 58 | } |
165 | |
} |
166 | 1 | } |
167 | |
|
168 | |
public void writeTestOutput( byte[] buf, int off, int len, boolean stdout ) |
169 | |
{ |
170 | 0 | byte[] header = stdout ? stdOutHeader : stdErrHeader; |
171 | 0 | byte[] content = |
172 | |
new byte[buf.length * 6 + 1]; |
173 | 0 | int i = StringUtils.escapeJavaStyleString( content, 0, buf, off, len ); |
174 | 0 | content[i++] = (byte) '\n'; |
175 | |
|
176 | 0 | synchronized ( target ) |
177 | |
{ |
178 | 0 | target.write( header, 0, header.length ); |
179 | 0 | target.write( content, 0, i ); |
180 | 0 | } |
181 | 0 | } |
182 | |
|
183 | |
public static byte[] createHeader( byte booterCode, int testSetChannel ) |
184 | |
{ |
185 | 2 | byte[] header = new byte[7]; |
186 | 2 | header[0] = booterCode; |
187 | 2 | header[1] = (byte) ','; |
188 | 2 | header[6] = (byte) ','; |
189 | |
|
190 | 2 | int i = testSetChannel; |
191 | 2 | int charPos = 6; |
192 | 2 | int radix = 1 << 4; |
193 | 2 | int mask = radix - 1; |
194 | |
do |
195 | |
{ |
196 | 2 | header[--charPos] = (byte) digits[i & mask]; |
197 | 2 | i >>>= 4; |
198 | |
} |
199 | 2 | while ( i != 0 ); |
200 | |
|
201 | 8 | while ( charPos > 2 ) |
202 | |
{ |
203 | 6 | header[--charPos] = (byte) '0'; |
204 | |
} |
205 | 2 | return header; |
206 | |
} |
207 | |
|
208 | 1 | private final static char[] digits = |
209 | |
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', |
210 | |
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; |
211 | |
|
212 | |
|
213 | |
public void info( String message ) |
214 | |
{ |
215 | 2 | byte[] buf = message.getBytes(); |
216 | 2 | ByteBuffer byteBuffer = new ByteBuffer( 7 + buf.length * 6 ); |
217 | 2 | byteBuffer.append( BOOTERCODE_CONSOLE ); |
218 | 2 | byteBuffer.comma(); |
219 | 2 | byteBuffer.append( testSetChannelId ); |
220 | 2 | byteBuffer.comma(); |
221 | 2 | final int i = |
222 | |
StringUtils.escapeJavaStyleString( byteBuffer.getData(), byteBuffer.getlength(), buf, 0, buf.length ); |
223 | 2 | byteBuffer.advance( i ); |
224 | 2 | byteBuffer.append( '\n' ); |
225 | 2 | synchronized ( target ) |
226 | |
{ |
227 | 2 | target.write( byteBuffer.getData(), 0, byteBuffer.getlength() ); |
228 | 2 | target.flush(); |
229 | 2 | } |
230 | 2 | } |
231 | |
|
232 | |
private String toPropertyString( String key, String value ) |
233 | |
{ |
234 | 58 | StringBuffer stringBuffer = new StringBuffer(); |
235 | 58 | append( stringBuffer, BOOTERCODE_SYSPROPS ); |
236 | 58 | comma( stringBuffer ); |
237 | 58 | append( stringBuffer, Integer.toHexString( testSetChannelId.intValue() ) ); |
238 | 58 | comma( stringBuffer ); |
239 | 58 | StringUtils.escapeJavaStyleString( stringBuffer, key ); |
240 | 58 | append( stringBuffer, "," ); |
241 | 58 | StringUtils.escapeJavaStyleString( stringBuffer, value ); |
242 | 58 | stringBuffer.append( "\n" ); |
243 | 58 | return stringBuffer.toString(); |
244 | |
} |
245 | |
|
246 | |
private String toString( byte operationCode, ReportEntry reportEntry, Integer testSetChannelId ) |
247 | |
{ |
248 | 0 | StringBuffer stringBuffer = new StringBuffer(); |
249 | 0 | append( stringBuffer, operationCode ); |
250 | 0 | comma( stringBuffer ); |
251 | 0 | append( stringBuffer, Integer.toHexString( testSetChannelId.intValue() ) ); |
252 | 0 | comma( stringBuffer ); |
253 | 0 | nullableEncoding( stringBuffer, reportEntry.getSourceName() ); |
254 | 0 | comma( stringBuffer ); |
255 | 0 | nullableEncoding( stringBuffer, reportEntry.getName() ); |
256 | 0 | comma( stringBuffer ); |
257 | 0 | nullableEncoding( stringBuffer, reportEntry.getGroup() ); |
258 | 0 | comma( stringBuffer ); |
259 | 0 | nullableEncoding( stringBuffer, reportEntry.getMessage() ); |
260 | 0 | comma( stringBuffer ); |
261 | 0 | nullableEncoding( stringBuffer, reportEntry.getElapsed() ); |
262 | 0 | encode( stringBuffer, reportEntry.getStackTraceWriter() ); |
263 | 0 | stringBuffer.append( "\n" ); |
264 | 0 | return stringBuffer.toString(); |
265 | |
} |
266 | |
|
267 | |
private static void comma( StringBuffer stringBuffer ) |
268 | |
{ |
269 | 116 | stringBuffer.append( "," ); |
270 | 116 | } |
271 | |
|
272 | |
private ForkingRunListener append( StringBuffer stringBuffer, String message ) |
273 | |
{ |
274 | 116 | stringBuffer.append( encode( message ) ); |
275 | 116 | return this; |
276 | |
} |
277 | |
|
278 | |
private ForkingRunListener append( StringBuffer stringBuffer, byte b ) |
279 | |
{ |
280 | 58 | stringBuffer.append( (char) b ); |
281 | 58 | return this; |
282 | |
} |
283 | |
|
284 | |
private void nullableEncoding( StringBuffer stringBuffer, Integer source ) |
285 | |
{ |
286 | 0 | if ( source == null ) |
287 | |
{ |
288 | 0 | stringBuffer.append( "null" ); |
289 | |
} |
290 | |
else |
291 | |
{ |
292 | 0 | stringBuffer.append( source.toString() ); |
293 | |
} |
294 | 0 | } |
295 | |
|
296 | |
private String encode( String source ) |
297 | |
{ |
298 | 116 | return source; |
299 | |
} |
300 | |
|
301 | |
|
302 | |
private static void nullableEncoding( StringBuffer stringBuffer, String source ) |
303 | |
{ |
304 | 0 | if ( source == null || source.length() == 0 ) |
305 | |
{ |
306 | 0 | stringBuffer.append( "null" ); |
307 | |
} |
308 | |
else |
309 | |
{ |
310 | 0 | StringUtils.escapeJavaStyleString( stringBuffer, source ); |
311 | |
} |
312 | 0 | } |
313 | |
|
314 | |
private void encode( StringBuffer stringBuffer, StackTraceWriter stackTraceWriter ) |
315 | |
{ |
316 | 0 | encode( stringBuffer, stackTraceWriter, trimStackTraces ); |
317 | 0 | } |
318 | |
|
319 | |
public static void encode( StringBuffer stringBuffer, StackTraceWriter stackTraceWriter, boolean trimStackTraces ) |
320 | |
{ |
321 | 0 | if ( stackTraceWriter != null ) |
322 | |
{ |
323 | 0 | comma( stringBuffer ); |
324 | |
|
325 | 0 | final SafeThrowable throwable = stackTraceWriter.getThrowable(); |
326 | 0 | if ( throwable != null ) |
327 | |
{ |
328 | 0 | String message = throwable.getLocalizedMessage(); |
329 | 0 | nullableEncoding( stringBuffer, message ); |
330 | |
} |
331 | 0 | comma( stringBuffer ); |
332 | 0 | nullableEncoding( stringBuffer, stackTraceWriter.smartTrimmedStackTrace() ); |
333 | 0 | comma( stringBuffer ); |
334 | 0 | nullableEncoding( stringBuffer, trimStackTraces |
335 | |
? stackTraceWriter.writeTrimmedTraceToString() |
336 | |
: stackTraceWriter.writeTraceToString() ); |
337 | |
} |
338 | 0 | } |
339 | |
} |