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