1 package org.apache.maven.plugin.surefire.booterclient;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.PrintStream;
25 import java.util.List;
26 import java.util.Properties;
27 import java.util.StringTokenizer;
28 import org.apache.maven.plugin.surefire.booterclient.output.ForkClient;
29 import org.apache.maven.surefire.booter.ForkingRunListener;
30 import org.apache.maven.surefire.report.CategorizedReportEntry;
31 import org.apache.maven.surefire.report.ConsoleLogger;
32 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
33 import org.apache.maven.surefire.report.LegacyPojoStackTraceWriter;
34 import org.apache.maven.surefire.report.ReportEntry;
35 import org.apache.maven.surefire.report.ReporterException;
36 import org.apache.maven.surefire.report.RunListener;
37 import org.apache.maven.surefire.report.SimpleReportEntry;
38 import org.apache.maven.surefire.report.StackTraceWriter;
39
40 import junit.framework.Assert;
41 import junit.framework.TestCase;
42
43
44
45
46 public class ForkingRunListenerTest
47 extends TestCase
48 {
49
50 private final ByteArrayOutputStream content;
51
52 private final PrintStream printStream;
53
54 final Integer defaultChannel = 17;
55
56 final Integer anotherChannel = 18;
57
58 public ForkingRunListenerTest()
59 {
60 this.content = new ByteArrayOutputStream();
61 printStream = new PrintStream( content );
62 }
63
64 private void reset()
65 {
66 printStream.flush();
67 content.reset();
68 }
69
70
71 public void testHeaderCreation()
72 {
73 final byte[] header = ForkingRunListener.createHeader( (byte) 'F', 0xCAFE );
74 String asString = new String( header );
75 assertEquals( "F,cafe,", asString );
76 }
77
78 public void testHeaderCreationShort()
79 {
80 final byte[] header = ForkingRunListener.createHeader( (byte) 'F', 0xE );
81 String asString = new String( header );
82 assertEquals( "F,000e,", asString );
83 }
84
85 public void testSetStarting()
86 throws ReporterException, IOException
87 {
88 final StandardTestRun standardTestRun = new StandardTestRun();
89 ReportEntry expected = createDefaultReportEntry();
90 standardTestRun.run().testSetStarting( expected );
91 standardTestRun.assertExpected( MockReporter.SET_STARTING, expected );
92 }
93
94 public void testSetCompleted()
95 throws ReporterException, IOException
96 {
97 final StandardTestRun standardTestRun = new StandardTestRun();
98 ReportEntry expected = createDefaultReportEntry();
99 standardTestRun.run().testSetCompleted( expected );
100 standardTestRun.assertExpected( MockReporter.SET_COMPLETED, expected );
101 }
102
103 public void testStarting()
104 throws ReporterException, IOException
105 {
106 final StandardTestRun standardTestRun = new StandardTestRun();
107 ReportEntry expected = createDefaultReportEntry();
108 standardTestRun.run().testStarting( expected );
109 standardTestRun.assertExpected( MockReporter.TEST_STARTING, expected );
110 }
111
112 public void testStringTokenizer()
113 {
114 String test = "5,11,com.abc.TestClass,testMethod,null,22,,,";
115 StringTokenizer tok = new StringTokenizer( test, "," );
116 assertEquals( "5", tok.nextToken() );
117 assertEquals( "11", tok.nextToken() );
118 assertEquals( "com.abc.TestClass", tok.nextToken() );
119 assertEquals( "testMethod", tok.nextToken() );
120 assertEquals( "null", tok.nextToken() );
121 assertEquals( "22", tok.nextToken() );
122 assertFalse( tok.hasMoreTokens() );
123 }
124
125 public void testSucceded()
126 throws ReporterException, IOException
127 {
128 final StandardTestRun standardTestRun = new StandardTestRun();
129 ReportEntry expected = createDefaultReportEntry();
130 standardTestRun.run().testSucceeded( expected );
131 standardTestRun.assertExpected( MockReporter.TEST_SUCCEEDED, expected );
132 }
133
134 public void testFailed()
135 throws ReporterException, IOException
136 {
137 final StandardTestRun standardTestRun = new StandardTestRun();
138 ReportEntry expected = createReportEntryWithStackTrace();
139 standardTestRun.run().testFailed( expected );
140 standardTestRun.assertExpected( MockReporter.TEST_FAILED, expected );
141 }
142
143 public void testFailedWithCommaInMessage()
144 throws ReporterException, IOException
145 {
146 final StandardTestRun standardTestRun = new StandardTestRun();
147 ReportEntry expected = createReportEntryWithSpecialMessage( "We, the people" );
148 standardTestRun.run().testFailed( expected );
149 standardTestRun.assertExpected( MockReporter.TEST_FAILED, expected );
150 }
151
152 public void testFailedWithUnicodeEscapeInMessage()
153 throws ReporterException, IOException
154 {
155 final StandardTestRun standardTestRun = new StandardTestRun();
156 ReportEntry expected = createReportEntryWithSpecialMessage( "We, \\u0177 people" );
157 standardTestRun.run().testFailed( expected );
158 standardTestRun.assertExpected( MockReporter.TEST_FAILED, expected );
159 }
160
161 public void testFailure()
162 throws ReporterException, IOException
163 {
164 final StandardTestRun standardTestRun = new StandardTestRun();
165 ReportEntry expected = createDefaultReportEntry();
166 standardTestRun.run().testError( expected );
167 standardTestRun.assertExpected( MockReporter.TEST_ERROR, expected );
168 }
169
170 public void testSkipped()
171 throws ReporterException, IOException
172 {
173 final StandardTestRun standardTestRun = new StandardTestRun();
174 ReportEntry expected = createDefaultReportEntry();
175 standardTestRun.run().testSkipped( expected );
176 standardTestRun.assertExpected( MockReporter.TEST_SKIPPED, expected );
177 }
178
179 public void testAssumptionFailure()
180 throws ReporterException, IOException
181 {
182 final StandardTestRun standardTestRun = new StandardTestRun();
183 ReportEntry expected = createDefaultReportEntry();
184 standardTestRun.run().testAssumptionFailure( expected );
185 standardTestRun.assertExpected( MockReporter.TEST_ASSUMPTION_FAIL, expected );
186 }
187
188 public void testConsole()
189 throws ReporterException, IOException
190 {
191 final StandardTestRun standardTestRun = new StandardTestRun();
192 ConsoleLogger directConsoleReporter = (ConsoleLogger) standardTestRun.run();
193 directConsoleReporter.info( "HeyYou" );
194 standardTestRun.assertExpected( MockReporter.CONSOLE_OUTPUT, "HeyYou" );
195 }
196
197 public void testConsoleOutput()
198 throws ReporterException, IOException
199 {
200 final StandardTestRun standardTestRun = new StandardTestRun();
201 ConsoleOutputReceiver directConsoleReporter = (ConsoleOutputReceiver) standardTestRun.run();
202 directConsoleReporter.writeTestOutput( "HeyYou".getBytes(), 0, 6, true );
203 standardTestRun.assertExpected( MockReporter.STDOUT, "HeyYou" );
204 }
205
206 public void testSystemProperties()
207 throws ReporterException, IOException
208 {
209 final StandardTestRun standardTestRun = new StandardTestRun();
210 standardTestRun.run();
211
212 reset();
213 createForkingRunListener( defaultChannel );
214
215 TestSetMockReporterFactory providerReporterFactory = new TestSetMockReporterFactory();
216 final Properties testVmSystemProperties = new Properties();
217 ForkClient forkStreamClient = new ForkClient( providerReporterFactory, testVmSystemProperties );
218
219 forkStreamClient.consumeMultiLineContent( content.toString( "utf-8" ) );
220
221 assertTrue( testVmSystemProperties.size() > 1 );
222 }
223
224 public void testMultipleEntries()
225 throws ReporterException, IOException
226 {
227
228 final StandardTestRun standardTestRun = new StandardTestRun();
229 standardTestRun.run();
230
231 reset();
232 RunListener forkingReporter = createForkingRunListener( defaultChannel );
233
234 ReportEntry reportEntry = createDefaultReportEntry();
235 forkingReporter.testSetStarting( reportEntry );
236 forkingReporter.testStarting( reportEntry );
237 forkingReporter.testSucceeded( reportEntry );
238 forkingReporter.testSetCompleted( reportEntry );
239
240 TestSetMockReporterFactory providerReporterFactory = new TestSetMockReporterFactory();
241 ForkClient forkStreamClient = new ForkClient( providerReporterFactory, new Properties() );
242
243 forkStreamClient.consumeMultiLineContent( content.toString( "utf-8" ) );
244
245 final MockReporter reporter = (MockReporter) forkStreamClient.getReporter( defaultChannel );
246 final List<String> events = reporter.getEvents();
247 assertEquals( MockReporter.SET_STARTING, events.get( 0 ) );
248 assertEquals( MockReporter.TEST_STARTING, events.get( 1 ) );
249 assertEquals( MockReporter.TEST_SUCCEEDED, events.get( 2 ) );
250 assertEquals( MockReporter.SET_COMPLETED, events.get( 3 ) );
251 }
252
253 public void test2DifferentChannels()
254 throws ReporterException, IOException
255 {
256 reset();
257 ReportEntry expected = createDefaultReportEntry();
258 final SimpleReportEntry secondExpected = createAnotherDefaultReportEntry();
259
260 new ForkingRunListener( printStream, defaultChannel, false ).testStarting( expected );
261 new ForkingRunListener( printStream, anotherChannel, false ).testSkipped( secondExpected );
262
263 TestSetMockReporterFactory providerReporterFactory = new TestSetMockReporterFactory();
264 final ForkClient forkStreamClient = new ForkClient( providerReporterFactory, new Properties() );
265 forkStreamClient.consumeMultiLineContent( content.toString( "utf-8" ) );
266
267 MockReporter reporter = (MockReporter) forkStreamClient.getReporter( defaultChannel );
268 Assert.assertEquals( MockReporter.TEST_STARTING, reporter.getFirstEvent() );
269 Assert.assertEquals( expected, reporter.getFirstData() );
270 Assert.assertEquals( 1, reporter.getEvents().size() );
271
272 MockReporter reporter2 = (MockReporter) forkStreamClient.getReporter( anotherChannel );
273 Assert.assertEquals( MockReporter.TEST_SKIPPED, reporter2.getFirstEvent() );
274 Assert.assertEquals( secondExpected, reporter2.getFirstData() );
275 Assert.assertEquals( 1, reporter2.getEvents().size() );
276 }
277
278
279
280 private SimpleReportEntry createDefaultReportEntry()
281 {
282 return new SimpleReportEntry( "com.abc.TestClass", "testMethod", 22 );
283 }
284
285 private SimpleReportEntry createAnotherDefaultReportEntry()
286 {
287 return new SimpleReportEntry( "com.abc.AnotherTestClass", "testAnotherMethod", 42 );
288 }
289
290 private SimpleReportEntry createReportEntryWithStackTrace()
291 {
292 try
293 {
294 throw new RuntimeException();
295 }
296 catch ( RuntimeException e )
297 {
298 StackTraceWriter stackTraceWriter =
299 new LegacyPojoStackTraceWriter( "org.apache.tests.TestClass", "testMethod11", e );
300 return new CategorizedReportEntry( "com.abc.TestClass", "testMethod", "aGroup", stackTraceWriter, 77 );
301 }
302 }
303
304 private SimpleReportEntry createReportEntryWithSpecialMessage( String message )
305 {
306 try
307 {
308 throw new RuntimeException( message );
309 }
310 catch ( RuntimeException e )
311 {
312 StackTraceWriter stackTraceWriter =
313 new LegacyPojoStackTraceWriter( "org.apache.tests.TestClass", "testMethod11", e );
314 return new CategorizedReportEntry( "com.abc.TestClass", "testMethod", "aGroup", stackTraceWriter, 77 );
315 }
316 }
317
318 private RunListener createForkingRunListener( Integer testSetCHannel )
319 {
320 return new ForkingRunListener( printStream, testSetCHannel, false );
321 }
322
323 private class StandardTestRun
324 {
325 private MockReporter reporter;
326
327 public RunListener run()
328 throws ReporterException
329 {
330 reset();
331 return createForkingRunListener( defaultChannel );
332 }
333
334 public void clientReceiveContent()
335 throws ReporterException, IOException
336 {
337 TestSetMockReporterFactory providerReporterFactory = new TestSetMockReporterFactory();
338 final ForkClient forkStreamClient = new ForkClient( providerReporterFactory, new Properties() );
339 forkStreamClient.consumeMultiLineContent( content.toString( ) );
340 reporter = (MockReporter) forkStreamClient.getReporter( defaultChannel );
341 }
342
343
344 public String getFirstEvent()
345 {
346 return reporter.getEvents().get( 0 );
347 }
348
349 public ReportEntry getFirstData()
350 {
351 return (ReportEntry) reporter.getData().get( 0 );
352 }
353
354 private void assertExpected( String actionCode, ReportEntry expected )
355 throws IOException, ReporterException
356 {
357 clientReceiveContent();
358 assertEquals( actionCode, getFirstEvent() );
359 final ReportEntry firstData = getFirstData();
360 assertEquals( expected.getSourceName(), firstData.getSourceName() );
361 assertEquals( expected.getName(), firstData.getName() );
362
363 assertEquals( expected.getElapsed(), firstData.getElapsed() );
364 assertEquals( expected.getGroup(), firstData.getGroup() );
365 if ( expected.getStackTraceWriter() != null )
366 {
367
368 assertEquals( expected.getStackTraceWriter().getThrowable().getLocalizedMessage(),
369 firstData.getStackTraceWriter().getThrowable().getLocalizedMessage() );
370 assertEquals( expected.getStackTraceWriter().writeTraceToString(),
371 firstData.getStackTraceWriter().writeTraceToString() );
372 }
373 }
374
375 private void assertExpected( String actionCode, String expected )
376 throws IOException, ReporterException
377 {
378 clientReceiveContent();
379 assertEquals( actionCode, getFirstEvent() );
380 final String firstData = (String) reporter.getData().get( 0 );
381 assertEquals( expected, firstData );
382 }
383
384 }
385 }