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.common.junit4;
20  
21  import java.util.concurrent.CountDownLatch;
22  
23  import junit.framework.Assert;
24  import junit.framework.TestCase;
25  import org.apache.maven.surefire.api.util.internal.DaemonThreadFactory;
26  import org.junit.Test;
27  import org.junit.runner.Description;
28  import org.junit.runner.Request;
29  import org.junit.runner.Runner;
30  import org.junit.runner.notification.Failure;
31  import org.junit.runner.notification.RunListener;
32  import org.junit.runner.notification.RunNotifier;
33  
34  /**
35   * @author Kristian Rosenvold
36   */
37  public class JUnit4RunListenerTest extends TestCase {
38      public void testTestStarted() {
39          RunListener jUnit4TestSetReporter = new JUnit4RunListener(new MockReporter());
40          Runner junitTestRunner =
41                  Request.classes("abc", STest1.class, STest2.class).getRunner();
42          RunNotifier runNotifier = new RunNotifier();
43          runNotifier.addListener(jUnit4TestSetReporter);
44          junitTestRunner.run(runNotifier);
45      }
46  
47      public void testParallelInvocations() throws Exception {
48          final MockReporter reporter = new MockReporter();
49          final RunListener jUnit4TestSetReporter = new JUnit4RunListener(reporter);
50          final CountDownLatch countDownLatch = new CountDownLatch(1);
51          final Description testSomething = Description.createTestDescription(STest1.class, "testSomething");
52          final Description testSomething2 = Description.createTestDescription(STest2.class, "testSomething2");
53  
54          jUnit4TestSetReporter.testStarted(testSomething);
55  
56          DaemonThreadFactory.newDaemonThread(new Runnable() {
57                      @Override
58                      public void run() {
59                          try {
60                              jUnit4TestSetReporter.testStarted(testSomething2);
61                              jUnit4TestSetReporter.testFailure(new Failure(testSomething2, new AssertionError("Fud")));
62                              jUnit4TestSetReporter.testFinished(testSomething2);
63                              countDownLatch.countDown();
64                          } catch (Exception e) {
65                              throw new RuntimeException(e);
66                          }
67                      }
68                  })
69                  .start();
70  
71          countDownLatch.await();
72          jUnit4TestSetReporter.testFinished(testSomething);
73  
74          Assert.assertEquals("Failing tests", 1, reporter.getTestFailed());
75          Assert.assertEquals("Succeeded tests", 1, reporter.getTestSucceeded());
76      }
77  
78      /**
79       *
80       */
81      public static class STest1 {
82          @Test
83          public void testSomething() {}
84      }
85  
86      /**
87       *
88       */
89      public static class STest2 {
90          @Test
91          public void testSomething2() {}
92      }
93  }