View Javadoc

1   package org.apache.maven.surefire.junitcore;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.DefaultConsoleReporter;
28  import org.apache.maven.surefire.report.ReporterFactory;
29  import org.apache.maven.surefire.report.RunListener;
30  import org.apache.maven.surefire.report.RunStatistics;
31  import org.apache.maven.surefire.testset.TestSetFailedException;
32  
33  import junit.framework.Assert;
34  import junit.framework.TestCase;
35  import junit.framework.TestSuite;
36  import org.junit.Ignore;
37  import org.junit.Test;
38  import org.junit.runner.Computer;
39  import org.junit.runner.JUnitCore;
40  
41  /*
42   * @author Kristian Rosenvold
43   */
44  public class ConcurrentRunListenerTest
45      extends TestCase
46  {
47      // Tests are in order of increasing complexity
48      public void testNoErrorsCounting()
49          throws Exception
50      {
51          runClasses( 3, 0, 0, DummyAllOk.class );
52      }
53  
54      public void testNoErrorsCounting2()
55          throws Exception
56      {
57          runClasses( 2, 0, 0, Dummy3.class );
58      }
59  
60      public void testOneIgnoreCounting()
61          throws Exception
62      {
63          runClasses( 3, 1, 0, DummyWithOneIgnore.class );
64      }
65  
66      public void testOneFailureCounting()
67          throws Exception
68      {
69          runClasses( 3, 0, 1, DummyWithFailure.class );
70      }
71  
72      public void testWithErrorsCountingDemultiplexed()
73          throws Exception
74      {
75          runClasses( 6, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class );
76      }
77  
78      public void testJunitResultCountingDemultiplexed()
79          throws Exception
80      {
81          runClasses( 8, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class );
82      }
83  
84      public void testJunitResultCountingJUnit3Demultiplexed()
85          throws Exception
86      {
87          runClasses( 3, 0, 0, Junit3Tc1.class, Junit3Tc2.class );
88      }
89  
90      public void testJunitResultCountingJUnit3OddTest()
91          throws Exception
92      {
93          runClasses( 2, 0, 0, Junit3OddTest1.class );
94      }
95  
96      public void testJunit3WithNestedSuite()
97          throws TestSetFailedException
98      {
99          runClasses( 4, 0, 0, Junit3WithNestedSuite.class );
100     }
101 
102     public void testJunit3NestedSuite()
103         throws Exception
104     {
105         runClasses( 2, 0, 0, Junit3OddTest1.class );
106     }
107 
108     public void testSimpleOutput()
109         throws Exception
110     {
111         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
112         PrintStream collector = new PrintStream( byteArrayOutputStream );
113         PrintStream orgOur = System.out;
114         System.setOut( collector );
115 
116         RunStatistics result = runClasses( Dummy3.class );
117         assertReporter( result, 2, 0, 0, "msgs" );
118 
119         String foo = new String( byteArrayOutputStream.toByteArray() );
120         assertNotNull( foo );
121 
122         System.setOut( orgOur );
123     }
124 
125     public void testOutputOrdering()
126         throws Exception
127     {
128         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
129         PrintStream collector = new PrintStream( byteArrayOutputStream );
130         PrintStream orgOur = System.out;
131         System.setOut( collector );
132 
133         RunStatistics result = runClasses( DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class );
134         assertReporter( result, 8, 1, 1, "msgs" );
135 
136         String foo = new String( byteArrayOutputStream.toByteArray() );
137         assertNotNull( foo );
138 
139         System.setOut( orgOur );
140 
141 //        final List<String> stringList = result.getEvents();
142 //        assertEquals( 23, stringList.size() );
143     }
144 
145     private void runClasses( int success, int ignored, int failure, Class<?>... classes )
146         throws TestSetFailedException
147     {
148         DefaultReporterFactory reporterFactory = createReporterFactory();
149         HashMap<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
150         final DefaultConsoleReporter defaultConsoleReporter = new DefaultConsoleReporter( System.out );
151         RunListener reporter =
152             new ClassesParallelRunListener( classMethodCounts, reporterFactory, defaultConsoleReporter );
153         JUnitCoreRunListener runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
154         RunStatistics result = runClasses( reporterFactory, runListener, classes );
155         assertReporter( result, success, ignored, failure, "classes" );
156         classMethodCounts.clear();
157 
158         reporterFactory = createReporterFactory();
159         reporter = new MethodsParallelRunListener( classMethodCounts, reporterFactory, true, defaultConsoleReporter );
160         runListener = new JUnitCoreRunListener( reporter, classMethodCounts );
161         result = runClasses( reporterFactory, runListener, classes );
162         assertReporter( result, success, ignored, failure, "methods" );
163     }
164 
165     private RunStatistics runClasses( Class<?>... classes )
166         throws TestSetFailedException
167     {
168         HashMap<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
169         final DefaultReporterFactory reporterManagerFactory = createReporterFactory();
170         org.junit.runner.notification.RunListener demultiplexingRunListener =
171             createRunListener( reporterManagerFactory, classMethodCounts );
172 
173         JUnitCore jUnitCore = new JUnitCore();
174 
175         jUnitCore.addListener( demultiplexingRunListener );
176         Computer computer = new Computer();
177 
178         jUnitCore.run( computer, classes );
179         reporterManagerFactory.close();
180         return reporterManagerFactory.getGlobalRunStatistics();
181     }
182 
183     private RunStatistics runClasses( DefaultReporterFactory reporterManagerFactory,
184                                       org.junit.runner.notification.RunListener demultiplexingRunListener,
185                                       Class<?>... classes )
186         throws TestSetFailedException
187     {
188 
189         JUnitCore jUnitCore = new JUnitCore();
190 
191         jUnitCore.addListener( demultiplexingRunListener );
192         Computer computer = new Computer();
193 
194         jUnitCore.run( computer, classes );
195         return reporterManagerFactory.getGlobalRunStatistics();
196     }
197 
198     private org.junit.runner.notification.RunListener createRunListener( ReporterFactory reporterFactory,
199                                                                          Map<String, TestSet> testSetMap )
200         throws TestSetFailedException
201     {
202         return new JUnitCoreRunListener(
203             new ClassesParallelRunListener( testSetMap, reporterFactory, new DefaultConsoleReporter( System.out ) ),
204             testSetMap );
205     }
206 
207 
208     public static class DummyWithOneIgnore
209     {
210         @Test
211         public void testNotMuch()
212         {
213         }
214 
215         @Ignore
216         @Test
217         public void testStub1()
218         {
219         }
220 
221         @Test
222         public void testStub2()
223         {
224         }
225     }
226 
227     public static class DummyWithFailure
228     {
229         @Test
230         public void testBeforeFail()
231         {
232         }
233 
234         @Test
235         public void testWillFail()
236         {
237             Assert.fail( "We will fail" );
238         }
239 
240         @Test
241         public void testAfterFail()
242         {
243         }
244     }
245 
246     public static class DummyAllOk
247     {
248 
249         @Test
250         public void testNotMuchA()
251         {
252         }
253 
254         @Test
255         public void testStub1A()
256         {
257         }
258 
259         @Test
260         public void testStub2A()
261         {
262         }
263     }
264 
265     public static class Dummy3
266     {
267 
268         @Test
269         public void testNotMuchA()
270         {
271             System.out.println( "tNMA1" );
272             System.err.println( "tNMA1err" );
273         }
274 
275         @Test
276         public void testStub2A()
277         {
278             System.out.println( "tS2A" );
279             System.err.println( "tS2AErr" );
280         }
281     }
282 
283     public static class Junit3Tc1
284         extends TestCase
285     {
286 
287         public Junit3Tc1()
288         {
289             super( "testNotMuchJunit3TC1" );
290         }
291 
292         public void testNotMuchJunit3TC1()
293         {
294             System.out.println( "Junit3TC1" );
295         }
296 
297 
298         public static junit.framework.Test suite()
299         {
300             TestSuite suite = new TestSuite();
301             suite.addTest( new Junit3Tc1() );
302             return suite;
303         }
304     }
305 
306     public static class Junit3Tc2
307         extends TestCase
308     {
309         public Junit3Tc2( String testMethod )
310         {
311             super( testMethod );
312         }
313 
314         public void testNotMuchJunit3TC2()
315         {
316             System.out.println( "Junit3TC2" );
317         }
318 
319         public void testStubJ3TC2A()
320         {
321             System.out.println( "testStubJ3TC2A" );
322         }
323 
324         public static junit.framework.Test suite()
325         {
326             TestSuite suite = new TestSuite();
327             suite.addTest( new Junit3Tc2( "testNotMuchJunit3TC2" ) );
328             suite.addTest( new Junit3Tc2( "testStubJ3TC2A" ) );
329             return suite;
330         }
331     }
332 
333     public static class Junit3OddTest1
334         extends TestCase
335     {
336         public static junit.framework.Test suite()
337         {
338             TestSuite suite = new TestSuite();
339 
340             suite.addTest( new Junit3OddTest1( "testMe" ) );
341             suite.addTest( new Junit3OddTest1( "testMe" ) );
342 
343             return suite;
344         }
345 
346         public Junit3OddTest1( String name )
347         {
348             super( name );
349         }
350 
351         public void testMe()
352         {
353             assertTrue( true );
354         }
355     }
356 
357     public static class Junit3WithNestedSuite
358         extends TestCase
359     {
360         public static junit.framework.Test suite()
361         {
362             TestSuite suite = new TestSuite();
363 
364             suite.addTest( new Junit3WithNestedSuite( "testMe2" ) );
365             suite.addTest( new Junit3WithNestedSuite( "testMe2" ) );
366             suite.addTestSuite( Junit3Tc2.class );
367             return suite;
368         }
369 
370         public Junit3WithNestedSuite( String name )
371         {
372             super( name );
373         }
374 
375         public void testMe2()
376         {
377             assertTrue( true );
378         }
379     }
380 
381 
382     private DefaultReporterFactory createReporterFactory()
383     {
384         return DefaultReporterFactory.defaultNoXml();
385     }
386 
387 
388     private void assertReporter( RunStatistics result, int success, int ignored, int failure, String message )
389     {
390         assertEquals( message, success, result.getCompletedCount() );
391         assertEquals( message, failure, result.getFailureSources().size() );
392         assertEquals( message, ignored, result.getSkipped() );
393     }
394 
395 }