1 package org.apache.maven.surefire.junitcore;
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.PrintStream;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
27 import org.apache.maven.surefire.api.report.ConsoleStream;
28 import org.apache.maven.surefire.api.report.DefaultDirectConsoleReporter;
29 import org.apache.maven.surefire.api.report.ReporterFactory;
30 import org.apache.maven.surefire.api.report.RunListener;
31 import org.apache.maven.surefire.report.RunStatistics;
32 import org.apache.maven.surefire.api.testset.TestSetFailedException;
33
34 import junit.framework.Assert;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37 import org.junit.Ignore;
38 import org.junit.Test;
39 import org.junit.runner.Computer;
40 import org.junit.runner.JUnitCore;
41
42
43
44
45 public class ConcurrentRunListenerTest
46 extends TestCase
47 {
48
49 public void testNoErrorsCounting()
50 throws Exception
51 {
52 runClasses( 3, 0, 0, DummyAllOk.class );
53 }
54
55 public void testNoErrorsCounting2()
56 throws Exception
57 {
58 runClasses( 2, 0, 0, Dummy3.class );
59 }
60
61 public void testOneIgnoreCounting()
62 throws Exception
63 {
64 runClasses( 3, 1, 0, DummyWithOneIgnore.class );
65 }
66
67 public void testOneFailureCounting()
68 throws Exception
69 {
70 runClasses( 3, 0, 1, DummyWithFailure.class );
71 }
72
73 public void testWithErrorsCountingDemultiplexed()
74 throws Exception
75 {
76 runClasses( 6, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class );
77 }
78
79 public void testJunitResultCountingDemultiplexed()
80 throws Exception
81 {
82 runClasses( 8, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class );
83 }
84
85 public void testJunitResultCountingJUnit3Demultiplexed()
86 throws Exception
87 {
88 runClasses( 3, 0, 0, Junit3Tc1.class, Junit3Tc2.class );
89 }
90
91 public void testJunitResultCountingJUnit3OddTest()
92 throws Exception
93 {
94 runClasses( 2, 0, 0, Junit3OddTest1.class );
95 }
96
97 public void testJunit3WithNestedSuite()
98 throws TestSetFailedException
99 {
100 runClasses( 4, 0, 0, Junit3WithNestedSuite.class );
101 }
102
103 public void testJunit3NestedSuite()
104 throws Exception
105 {
106 runClasses( 2, 0, 0, Junit3OddTest1.class );
107 }
108
109 public void testSimpleOutput()
110 throws Exception
111 {
112 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
113 PrintStream collector = new PrintStream( byteArrayOutputStream );
114 PrintStream orgOur = System.out;
115 System.setOut( collector );
116
117 RunStatistics result = runClasses( Dummy3.class );
118 assertReporter( result, 2, 0, 0, "msgs" );
119
120 String foo = new String( byteArrayOutputStream.toByteArray() );
121 assertNotNull( foo );
122
123 System.setOut( orgOur );
124 }
125
126 public void testOutputOrdering()
127 throws Exception
128 {
129 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
130 PrintStream collector = new PrintStream( byteArrayOutputStream );
131 PrintStream orgOur = System.out;
132 System.setOut( collector );
133
134 RunStatistics result = runClasses( DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class );
135 assertReporter( result, 8, 1, 1, "msgs" );
136
137 String foo = new String( byteArrayOutputStream.toByteArray() );
138 assertNotNull( foo );
139
140 System.setOut( orgOur );
141
142
143
144 }
145
146 private void runClasses( int success, int ignored, int failure, Class<?>... classes )
147 throws TestSetFailedException
148 {
149 DefaultReporterFactory reporterFactory = createReporterFactory();
150 HashMap<String, TestSet> classMethodCounts = new HashMap<>();
151 final ConsoleStream defaultConsoleReporter = new DefaultDirectConsoleReporter( System.out );
152 RunListener reporter =
153 new ClassesParallelRunListener( classMethodCounts, reporterFactory, defaultConsoleReporter );
154 JUnitCoreRunListener runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
155 RunStatistics result = runClasses( reporterFactory, runListener, classes );
156 assertReporter( result, success, ignored, failure, "classes" );
157 classMethodCounts.clear();
158
159 reporterFactory = createReporterFactory();
160 reporter = new MethodsParallelRunListener( classMethodCounts, reporterFactory, true, defaultConsoleReporter );
161 runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
162 result = runClasses( reporterFactory, runListener, classes );
163 assertReporter( result, success, ignored, failure, "methods" );
164 }
165
166 private RunStatistics runClasses( Class<?>... classes )
167 throws TestSetFailedException
168 {
169 HashMap<String, TestSet> classMethodCounts = new HashMap<>();
170 final DefaultReporterFactory reporterManagerFactory = createReporterFactory();
171 org.junit.runner.notification.RunListener demultiplexingRunListener =
172 createRunListener( reporterManagerFactory, classMethodCounts );
173
174 JUnitCore jUnitCore = new JUnitCore();
175
176 jUnitCore.addListener( demultiplexingRunListener );
177 Computer computer = new Computer();
178
179 jUnitCore.run( computer, classes );
180 reporterManagerFactory.close();
181 return reporterManagerFactory.getGlobalRunStatistics();
182 }
183
184 private RunStatistics runClasses( DefaultReporterFactory reporterManagerFactory,
185 org.junit.runner.notification.RunListener demultiplexingRunListener,
186 Class<?>... classes )
187 throws TestSetFailedException
188 {
189
190 JUnitCore jUnitCore = new JUnitCore();
191
192 jUnitCore.addListener( demultiplexingRunListener );
193 Computer computer = new Computer();
194
195 jUnitCore.run( computer, classes );
196 return reporterManagerFactory.getGlobalRunStatistics();
197 }
198
199 private org.junit.runner.notification.RunListener createRunListener( ReporterFactory reporterFactory,
200 Map<String, TestSet> testSetMap )
201 throws TestSetFailedException
202 {
203 return new JUnitCoreRunListener(
204 new ClassesParallelRunListener( testSetMap, reporterFactory,
205 new DefaultDirectConsoleReporter( System.out ) ), testSetMap );
206 }
207
208
209
210
211
212 public static class DummyWithOneIgnore
213 {
214 @Test
215 public void testNotMuch()
216 {
217 }
218
219 @Ignore
220 @Test
221 public void testStub1()
222 {
223 }
224
225 @Test
226 public void testStub2()
227 {
228 }
229 }
230
231
232
233
234 public static class DummyWithFailure
235 {
236 @Test
237 public void testBeforeFail()
238 {
239 }
240
241 @Test
242 public void testWillFail()
243 {
244 Assert.fail( "We will fail" );
245 }
246
247 @Test
248 public void testAfterFail()
249 {
250 }
251 }
252
253
254
255
256 public static class DummyAllOk
257 {
258
259 @Test
260 public void testNotMuchA()
261 {
262 }
263
264 @Test
265 public void testStub1A()
266 {
267 }
268
269 @Test
270 public void testStub2A()
271 {
272 }
273 }
274
275
276
277
278 public static class Dummy3
279 {
280
281 @Test
282 public void testNotMuchA()
283 {
284 System.out.println( "tNMA1" );
285 System.err.println( "tNMA1err" );
286 }
287
288 @Test
289 public void testStub2A()
290 {
291 System.out.println( "tS2A" );
292 System.err.println( "tS2AErr" );
293 }
294 }
295
296
297
298
299 public static class Junit3Tc1
300 extends TestCase
301 {
302
303 public Junit3Tc1()
304 {
305 super( "testNotMuchJunit3TC1" );
306 }
307
308 public void testNotMuchJunit3TC1()
309 {
310 System.out.println( "Junit3TC1" );
311 }
312
313
314 public static junit.framework.Test suite()
315 {
316 TestSuite suite = new TestSuite();
317 suite.addTest( new Junit3Tc1() );
318 return suite;
319 }
320 }
321
322
323
324
325 public static class Junit3Tc2
326 extends TestCase
327 {
328 public Junit3Tc2( String testMethod )
329 {
330 super( testMethod );
331 }
332
333 public void testNotMuchJunit3TC2()
334 {
335 System.out.println( "Junit3TC2" );
336 }
337
338 public void testStubJ3TC2A()
339 {
340 System.out.println( "testStubJ3TC2A" );
341 }
342
343 public static junit.framework.Test suite()
344 {
345 TestSuite suite = new TestSuite();
346 suite.addTest( new Junit3Tc2( "testNotMuchJunit3TC2" ) );
347 suite.addTest( new Junit3Tc2( "testStubJ3TC2A" ) );
348 return suite;
349 }
350 }
351
352
353
354
355 public static class Junit3OddTest1
356 extends TestCase
357 {
358 public static junit.framework.Test suite()
359 {
360 TestSuite suite = new TestSuite();
361
362 suite.addTest( new Junit3OddTest1( "testMe" ) );
363 suite.addTest( new Junit3OddTest1( "testMe2" ) );
364
365 return suite;
366 }
367
368 public Junit3OddTest1( String name )
369 {
370 super( name );
371 }
372
373 public void testMe()
374 {
375 assertTrue( true );
376 }
377 }
378
379
380
381
382 public static class Junit3WithNestedSuite
383 extends TestCase
384 {
385 public static junit.framework.Test suite()
386 {
387 TestSuite suite = new TestSuite();
388
389 suite.addTest( new Junit3WithNestedSuite( "testMe" ) );
390 suite.addTest( new Junit3WithNestedSuite( "testMe2" ) );
391 suite.addTestSuite( Junit3Tc2.class );
392 return suite;
393 }
394
395 public Junit3WithNestedSuite( String name )
396 {
397 super( name );
398 }
399
400 public void testMe2()
401 {
402 assertTrue( true );
403 }
404 }
405
406
407 private DefaultReporterFactory createReporterFactory()
408 {
409 return JUnitCoreTester.defaultNoXml();
410 }
411
412
413 private void assertReporter( RunStatistics result, int success, int ignored, int failure, String message )
414 {
415 assertEquals( message, success, result.getCompletedCount() );
416 assertEquals( message, ignored, result.getSkipped() );
417 }
418
419 }