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  package org.apache.maven.surefire.junitcore;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.PrintStream;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import junit.framework.Assert;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
30  import org.apache.maven.surefire.api.report.ReporterFactory;
31  import org.apache.maven.surefire.api.testset.TestSetFailedException;
32  import org.apache.maven.surefire.report.RunStatistics;
33  import org.junit.Ignore;
34  import org.junit.Test;
35  import org.junit.runner.Computer;
36  import org.junit.runner.JUnitCore;
37  
38  /**
39   * @author Kristian Rosenvold
40   */
41  public class ConcurrentRunListenerTest extends TestCase {
42      // Tests are in order of increasing complexity
43      public void testNoErrorsCounting() throws Exception {
44          runClasses(3, 0, 0, DummyAllOk.class);
45      }
46  
47      public void testNoErrorsCounting2() throws Exception {
48          runClasses(2, 0, 0, Dummy3.class);
49      }
50  
51      public void testOneIgnoreCounting() throws Exception {
52          runClasses(3, 1, 0, DummyWithOneIgnore.class);
53      }
54  
55      public void testOneFailureCounting() throws Exception {
56          runClasses(3, 0, 1, DummyWithFailure.class);
57      }
58  
59      public void testWithErrorsCountingDemultiplexed() throws Exception {
60          runClasses(6, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class);
61      }
62  
63      public void testJunitResultCountingDemultiplexed() throws Exception {
64          runClasses(8, 1, 1, DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class);
65      }
66  
67      public void testJunitResultCountingJUnit3Demultiplexed() throws Exception {
68          runClasses(3, 0, 0, Junit3Tc1.class, Junit3Tc2.class);
69      }
70  
71      public void testJunitResultCountingJUnit3OddTest() throws Exception {
72          runClasses(2, 0, 0, Junit3OddTest1.class);
73      }
74  
75      public void testJunit3WithNestedSuite() throws TestSetFailedException {
76          runClasses(4, 0, 0, Junit3WithNestedSuite.class);
77      }
78  
79      public void testJunit3NestedSuite() throws Exception {
80          runClasses(2, 0, 0, Junit3OddTest1.class);
81      }
82  
83      public void testSimpleOutput() throws Exception {
84          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
85          PrintStream collector = new PrintStream(byteArrayOutputStream);
86          PrintStream orgOur = System.out;
87          System.setOut(collector);
88  
89          RunStatistics result = runClasses(Dummy3.class);
90          assertReporter(result, 2, 0, 0, "msgs");
91  
92          String foo = new String(byteArrayOutputStream.toByteArray());
93          assertNotNull(foo);
94  
95          System.setOut(orgOur);
96      }
97  
98      public void testOutputOrdering() throws Exception {
99          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
100         PrintStream collector = new PrintStream(byteArrayOutputStream);
101         PrintStream orgOur = System.out;
102         System.setOut(collector);
103 
104         RunStatistics result = runClasses(DummyWithOneIgnore.class, DummyWithFailure.class, Dummy3.class);
105         assertReporter(result, 8, 1, 1, "msgs");
106 
107         String foo = new String(byteArrayOutputStream.toByteArray());
108         assertNotNull(foo);
109 
110         System.setOut(orgOur);
111 
112         //        final List<String> stringList = result.getEvents();
113         //        assertEquals( 23, stringList.size() );
114     }
115 
116     private void runClasses(int success, int ignored, int failure, Class<?>... classes) throws TestSetFailedException {
117         DefaultReporterFactory reporterFactory = createReporterFactory();
118         HashMap<String, TestSet> classMethodCounts = new HashMap<>();
119         ConcurrentRunListener reporter = new ClassesParallelRunListener(classMethodCounts, reporterFactory);
120         JUnitCoreRunListener runListener = new JUnitCoreRunListener(reporter, classMethodCounts);
121         RunStatistics result = runClasses(reporterFactory, runListener, classes);
122         assertReporter(result, success, ignored, failure, "classes");
123         classMethodCounts.clear();
124 
125         reporterFactory = createReporterFactory();
126         reporter = new MethodsParallelRunListener(classMethodCounts, reporterFactory, true);
127         runListener = new JUnitCoreRunListener(reporter, classMethodCounts);
128         result = runClasses(reporterFactory, runListener, classes);
129         assertReporter(result, success, ignored, failure, "methods");
130     }
131 
132     private RunStatistics runClasses(Class<?>... classes) throws TestSetFailedException {
133         HashMap<String, TestSet> classMethodCounts = new HashMap<>();
134         final DefaultReporterFactory reporterManagerFactory = createReporterFactory();
135         org.junit.runner.notification.RunListener demultiplexingRunListener =
136                 createRunListener(reporterManagerFactory, classMethodCounts);
137 
138         JUnitCore jUnitCore = new JUnitCore();
139 
140         jUnitCore.addListener(demultiplexingRunListener);
141         Computer computer = new Computer();
142 
143         jUnitCore.run(computer, classes);
144         reporterManagerFactory.close();
145         return reporterManagerFactory.getGlobalRunStatistics();
146     }
147 
148     private RunStatistics runClasses(
149             DefaultReporterFactory reporterManagerFactory,
150             org.junit.runner.notification.RunListener demultiplexingRunListener,
151             Class<?>... classes)
152             throws TestSetFailedException {
153 
154         JUnitCore jUnitCore = new JUnitCore();
155 
156         jUnitCore.addListener(demultiplexingRunListener);
157         Computer computer = new Computer();
158 
159         jUnitCore.run(computer, classes);
160         return reporterManagerFactory.getGlobalRunStatistics();
161     }
162 
163     private org.junit.runner.notification.RunListener createRunListener(
164             ReporterFactory reporterFactory, Map<String, TestSet> testSetMap) {
165         ConcurrentRunListener handler = new ClassesParallelRunListener(testSetMap, reporterFactory);
166         return new JUnitCoreRunListener(handler, testSetMap);
167     }
168 
169     /**
170      *
171      */
172     public static class DummyWithOneIgnore {
173         @Test
174         public void testNotMuch() {}
175 
176         @Ignore
177         @Test
178         public void testStub1() {}
179 
180         @Test
181         public void testStub2() {}
182     }
183 
184     /**
185      *
186      */
187     public static class DummyWithFailure {
188         @Test
189         public void testBeforeFail() {}
190 
191         @Test
192         public void testWillFail() {
193             Assert.fail("We will fail");
194         }
195 
196         @Test
197         public void testAfterFail() {}
198     }
199 
200     /**
201      *
202      */
203     public static class DummyAllOk {
204 
205         @Test
206         public void testNotMuchA() {}
207 
208         @Test
209         public void testStub1A() {}
210 
211         @Test
212         public void testStub2A() {}
213     }
214 
215     /**
216      *
217      */
218     public static class Dummy3 {
219 
220         @Test
221         public void testNotMuchA() {
222             System.out.println("tNMA1");
223             System.err.println("tNMA1err");
224         }
225 
226         @Test
227         public void testStub2A() {
228             System.out.println("tS2A");
229             System.err.println("tS2AErr");
230         }
231     }
232 
233     /**
234      *
235      */
236     public static class Junit3Tc1 extends TestCase {
237 
238         public Junit3Tc1() {
239             super("testNotMuchJunit3TC1");
240         }
241 
242         public void testNotMuchJunit3TC1() {
243             System.out.println("Junit3TC1");
244         }
245 
246         public static junit.framework.Test suite() {
247             TestSuite suite = new TestSuite();
248             suite.addTest(new Junit3Tc1());
249             return suite;
250         }
251     }
252 
253     /**
254      *
255      */
256     public static class Junit3Tc2 extends TestCase {
257         public Junit3Tc2(String testMethod) {
258             super(testMethod);
259         }
260 
261         public void testNotMuchJunit3TC2() {
262             System.out.println("Junit3TC2");
263         }
264 
265         public void testStubJ3TC2A() {
266             System.out.println("testStubJ3TC2A");
267         }
268 
269         public static junit.framework.Test suite() {
270             TestSuite suite = new TestSuite();
271             suite.addTest(new Junit3Tc2("testNotMuchJunit3TC2"));
272             suite.addTest(new Junit3Tc2("testStubJ3TC2A"));
273             return suite;
274         }
275     }
276 
277     /**
278      *
279      */
280     public static class Junit3OddTest1 extends TestCase {
281         public static junit.framework.Test suite() {
282             TestSuite suite = new TestSuite();
283 
284             suite.addTest(new Junit3OddTest1("testMe"));
285             suite.addTest(new Junit3OddTest1("testMe2"));
286 
287             return suite;
288         }
289 
290         public Junit3OddTest1(String name) {
291             super(name);
292         }
293 
294         public void testMe() {
295             assertTrue(true);
296         }
297     }
298 
299     /**
300      *
301      */
302     public static class Junit3WithNestedSuite extends TestCase {
303         public static junit.framework.Test suite() {
304             TestSuite suite = new TestSuite();
305 
306             suite.addTest(new Junit3WithNestedSuite("testMe"));
307             suite.addTest(new Junit3WithNestedSuite("testMe2"));
308             suite.addTestSuite(Junit3Tc2.class);
309             return suite;
310         }
311 
312         public Junit3WithNestedSuite(String name) {
313             super(name);
314         }
315 
316         public void testMe2() {
317             assertTrue(true);
318         }
319     }
320 
321     private DefaultReporterFactory createReporterFactory() {
322         return JUnitCoreTester.defaultNoXml();
323     }
324 
325     private void assertReporter(RunStatistics result, int success, int ignored, int failure, String message) {
326         assertEquals(message, success, result.getCompletedCount());
327         assertEquals(message, ignored, result.getSkipped());
328     }
329 }