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