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.report.ConsoleStream;
28 import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
29 import org.apache.maven.surefire.report.ReporterFactory;
30 import org.apache.maven.surefire.report.RunListener;
31 import org.apache.maven.surefire.report.RunStatistics;
32 import org.apache.maven.surefire.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 public static class DummyWithOneIgnore
210 {
211 @Test
212 public void testNotMuch()
213 {
214 }
215
216 @Ignore
217 @Test
218 public void testStub1()
219 {
220 }
221
222 @Test
223 public void testStub2()
224 {
225 }
226 }
227
228 public static class DummyWithFailure
229 {
230 @Test
231 public void testBeforeFail()
232 {
233 }
234
235 @Test
236 public void testWillFail()
237 {
238 Assert.fail( "We will fail" );
239 }
240
241 @Test
242 public void testAfterFail()
243 {
244 }
245 }
246
247 public static class DummyAllOk
248 {
249
250 @Test
251 public void testNotMuchA()
252 {
253 }
254
255 @Test
256 public void testStub1A()
257 {
258 }
259
260 @Test
261 public void testStub2A()
262 {
263 }
264 }
265
266 public static class Dummy3
267 {
268
269 @Test
270 public void testNotMuchA()
271 {
272 System.out.println( "tNMA1" );
273 System.err.println( "tNMA1err" );
274 }
275
276 @Test
277 public void testStub2A()
278 {
279 System.out.println( "tS2A" );
280 System.err.println( "tS2AErr" );
281 }
282 }
283
284 public static class Junit3Tc1
285 extends TestCase
286 {
287
288 public Junit3Tc1()
289 {
290 super( "testNotMuchJunit3TC1" );
291 }
292
293 public void testNotMuchJunit3TC1()
294 {
295 System.out.println( "Junit3TC1" );
296 }
297
298
299 public static junit.framework.Test suite()
300 {
301 TestSuite suite = new TestSuite();
302 suite.addTest( new Junit3Tc1() );
303 return suite;
304 }
305 }
306
307 public static class Junit3Tc2
308 extends TestCase
309 {
310 public Junit3Tc2( String testMethod )
311 {
312 super( testMethod );
313 }
314
315 public void testNotMuchJunit3TC2()
316 {
317 System.out.println( "Junit3TC2" );
318 }
319
320 public void testStubJ3TC2A()
321 {
322 System.out.println( "testStubJ3TC2A" );
323 }
324
325 public static junit.framework.Test suite()
326 {
327 TestSuite suite = new TestSuite();
328 suite.addTest( new Junit3Tc2( "testNotMuchJunit3TC2" ) );
329 suite.addTest( new Junit3Tc2( "testStubJ3TC2A" ) );
330 return suite;
331 }
332 }
333
334 public static class Junit3OddTest1
335 extends TestCase
336 {
337 public static junit.framework.Test suite()
338 {
339 TestSuite suite = new TestSuite();
340
341 suite.addTest( new Junit3OddTest1( "testMe" ) );
342 suite.addTest( new Junit3OddTest1( "testMe2" ) );
343
344 return suite;
345 }
346
347 public Junit3OddTest1( String name )
348 {
349 super( name );
350 }
351
352 public void testMe()
353 {
354 assertTrue( true );
355 }
356 }
357
358 public static class Junit3WithNestedSuite
359 extends TestCase
360 {
361 public static junit.framework.Test suite()
362 {
363 TestSuite suite = new TestSuite();
364
365 suite.addTest( new Junit3WithNestedSuite( "testMe" ) );
366 suite.addTest( new Junit3WithNestedSuite( "testMe2" ) );
367 suite.addTestSuite( Junit3Tc2.class );
368 return suite;
369 }
370
371 public Junit3WithNestedSuite( String name )
372 {
373 super( name );
374 }
375
376 public void testMe2()
377 {
378 assertTrue( true );
379 }
380 }
381
382
383 private DefaultReporterFactory createReporterFactory()
384 {
385 return JUnitCoreTester.defaultNoXml();
386 }
387
388
389 private void assertReporter( RunStatistics result, int success, int ignored, int failure, String message )
390 {
391 assertEquals( message, success, result.getCompletedCount() );
392 assertEquals( message, ignored, result.getSkipped() );
393 }
394
395 }