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