2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the
7
* "License"); you may not use this file except in compliance
8
* with the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing,
13
* software distributed under the License is distributed on an
14
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
* KIND, either express or implied. See the License for the
16
* specific language governing permissions and limitations
20
package org.apache.maven.surefire.junitcore;
23
import java.io.ByteArrayOutputStream;
25
import java.io.PrintStream;
26
import java.util.HashMap;
28
import org.apache.maven.plugin.surefire.report.FileReporterFactory;
29
import org.apache.maven.surefire.booter.StartupReportConfiguration;
30
import org.apache.maven.surefire.report.DefaultConsoleReporter;
31
import org.apache.maven.surefire.report.ReporterConfiguration;
32
import org.apache.maven.surefire.report.ReporterFactory;
33
import org.apache.maven.surefire.report.RunListener;
34
import org.apache.maven.surefire.report.RunStatistics;
35
import org.apache.maven.surefire.testset.TestSetFailedException;
37
import junit.framework.Assert;
38
import junit.framework.TestCase;
39
import junit.framework.TestSuite;
40
import org.junit.Ignore;
41
import org.junit.Test;
42
import org.junit.runner.Computer;
43
import org.junit.runner.JUnitCore;
46
* @author Kristian Rosenvold
49
public class ConcurrentReporterManagerTest
52
// Tests are in order of increasing complexity
53
public void testNoErrorsCounting()
56
runClasses( 3, 0, 0, DummyAllOk.class );
59
public void testNoErrorsCounting2()
62
runClasses( 2, 0, 0, Dummy3.class );
65
public void testOneIgnoreCounting()
68
runClasses( 3, 1, 0, DummyWithOneIgnore.class );
71
public void testOneFailureCounting()
74
runClasses( 3, 0, 1, DummyWithFailure.class );
77
public void testWithErrorsCountingDemultiplexed()
80
runClasses( 6, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class );
83
public void testJunitResultCountingDemultiplexed()
86
runClasses( 8, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class );
89
public void testJunitResultCountingJUnit3Demultiplexed()
92
runClasses( 3, 0, 0, Junit3Tc1.class, Junit3Tc2.class );
95
public void testJunitResultCountingJUnit3OddTest()
98
runClasses( 2, 0, 0, Junit3OddTest1.class );
101
public void testJunit3WithNestedSuite()
102
throws TestSetFailedException
104
runClasses( 4, 0, 0, Junit3WithNestedSuite.class );
107
public void testJunit3NestedSuite()
110
runClasses( 2, 0, 0, Junit3OddTest1.class );
114
public void testSimpleOutput()
117
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
118
PrintStream collector = new PrintStream( byteArrayOutputStream );
119
PrintStream orgOur = System.out;
120
System.setOut( collector );
122
RunStatistics result = runClasses( Dummy3.class );
123
assertReporter( result, 2, 0, 0, "msgs" );
125
String foo = new String( byteArrayOutputStream.toByteArray() );
126
assertNotNull( foo );
128
System.setOut( orgOur );
131
public void testOutputOrdering()
134
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
135
PrintStream collector = new PrintStream( byteArrayOutputStream );
136
PrintStream orgOur = System.out;
137
System.setOut( collector );
139
RunStatistics result = runClasses( DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class );
140
assertReporter( result, 8, 1, 1, "msgs" );
142
String foo = new String( byteArrayOutputStream.toByteArray() );
143
assertNotNull( foo );
145
System.setOut( orgOur );
147
// final List<String> stringList = result.getEvents();
148
// assertEquals( 23, stringList.size() );
151
private void runClasses( int success, int ignored, int failure, Class... classes )
152
throws TestSetFailedException
154
ReporterFactory reporterFactory = createReporterFactory();
155
HashMap<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
156
final DefaultConsoleReporter defaultConsoleReporter = new DefaultConsoleReporter( System.out );
157
RunListener reporter =
158
new ClassesParallelRunListener( classMethodCounts, reporterFactory, defaultConsoleReporter );
159
JUnitCoreRunListener runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
160
RunStatistics result = runClasses( reporterFactory, runListener, classes );
161
assertReporter( result, success, ignored, failure, "classes" );
162
classMethodCounts.clear();
164
reporterFactory = createReporterFactory();
165
reporter = new MethodsParallelRunListener( classMethodCounts, reporterFactory, true, defaultConsoleReporter );
166
runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
167
result = runClasses( reporterFactory, runListener, classes );
168
assertReporter( result, success, ignored, failure, "methods" );
171
private RunStatistics runClasses( Class... classes )
172
throws TestSetFailedException
174
HashMap<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
175
final ReporterFactory reporterManagerFactory = createReporterFactory();
176
org.junit.runner.notification.RunListener demultiplexingRunListener =
177
createRunListener( reporterManagerFactory, classMethodCounts );
179
JUnitCore jUnitCore = new JUnitCore();
181
jUnitCore.addListener( demultiplexingRunListener );
182
Computer computer = new Computer();
184
jUnitCore.run( computer, classes );
185
reporterManagerFactory.close();
186
return reporterManagerFactory.getGlobalRunStatistics();
189
private RunStatistics runClasses( ReporterFactory reporterManagerFactory,
190
org.junit.runner.notification.RunListener demultiplexingRunListener,
192
throws TestSetFailedException
195
JUnitCore jUnitCore = new JUnitCore();
197
jUnitCore.addListener( demultiplexingRunListener );
198
Computer computer = new Computer();
200
jUnitCore.run( computer, classes );
201
return reporterManagerFactory.getGlobalRunStatistics();
204
private org.junit.runner.notification.RunListener createRunListener( ReporterFactory reporterFactory,
205
Map<String, TestSet> testSetMap )
206
throws TestSetFailedException
208
return new JUnitCoreRunListener(
209
new ClassesParallelRunListener( testSetMap, reporterFactory, new DefaultConsoleReporter( System.out ) ),
214
public static class DummyWithOneIgnore
217
public void testNotMuch()
224
public void testStub1()
229
public void testStub2()
234
public static class DummyWithFailure
238
public void testBeforeFail()
244
public void testWillFail()
246
Assert.fail( "We will fail" );
250
public void testAfterFail()
255
public static class DummyAllOk
259
public void testNotMuchA()
265
public void testStub1A()
270
public void testStub2A()
275
public static class Dummy3
279
public void testNotMuchA()
281
System.out.println( "tNMA1" );
282
System.err.println( "tNMA1err" );
286
public void testStub2A()
288
System.out.println( "tS2A" );
289
System.err.println( "tS2AErr" );
293
public static class Junit3Tc1
299
super( "testNotMuchJunit3TC1" );
302
public void testNotMuchJunit3TC1()
304
System.out.println( "Junit3TC1" );
308
public static junit.framework.Test suite()
310
TestSuite suite = new TestSuite();
311
suite.addTest( new Junit3Tc1() );
316
public static class Junit3Tc2
319
public Junit3Tc2( String testMethod )
324
public void testNotMuchJunit3TC2()
326
System.out.println( "Junit3TC2" );
329
public void testStubJ3TC2A()
331
System.out.println( "testStubJ3TC2A" );
335
public static junit.framework.Test suite()
337
TestSuite suite = new TestSuite();
338
suite.addTest( new Junit3Tc2( "testNotMuchJunit3TC2" ) );
339
suite.addTest( new Junit3Tc2( "testStubJ3TC2A" ) );
344
public static class Junit3OddTest1
349
public static junit.framework.Test suite()
351
TestSuite suite = new TestSuite();
353
suite.addTest( new Junit3OddTest1( "testMe" ) );
354
suite.addTest( new Junit3OddTest1( "testMe" ) );
360
public Junit3OddTest1( String name )
371
public static class Junit3WithNestedSuite
376
public static junit.framework.Test suite()
378
TestSuite suite = new TestSuite();
380
suite.addTest( new Junit3WithNestedSuite( "testMe2" ) );
381
suite.addTest( new Junit3WithNestedSuite( "testMe2" ) );
382
suite.addTestSuite( Junit3Tc2.class );
387
public Junit3WithNestedSuite( String name )
392
public void testMe2()
399
private ReporterFactory createReporterFactory()
401
return new FileReporterFactory( StartupReportConfiguration.defaultNoXml() );
404
public static ReporterConfiguration getTestReporterConfiguration()
406
return new ReporterConfiguration( new File( "." ), Boolean.TRUE );
410
private void assertReporter( RunStatistics result, int success, int ignored, int failure, String message )
412
assertEquals( message, success, result.getCompletedCount() );
413
assertEquals( message, failure, result.getFailureSources().size() );
414
assertEquals( message, ignored, result.getSkipped() );