View Javadoc

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