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              @Override
67              public void run()
68              {
69                  try
70                  {
71                      jUnit4TestSetReporter.testStarted( testSomething2 );
72                      jUnit4TestSetReporter.testFailure( new Failure( testSomething2, new AssertionError( "Fud" ) ) );
73                      jUnit4TestSetReporter.testFinished( testSomething2 );
74                      countDownLatch.countDown();
75                  }
76                  catch ( Exception e )
77                  {
78                      throw new RuntimeException( e );
79                  }
80              }
81          } ).start();
82  
83          countDownLatch.await();
84          jUnit4TestSetReporter.testFinished( testSomething );
85  
86          Assert.assertEquals( "Failing tests", 1, reporter.getTestFailed() );
87          Assert.assertEquals( "Succeeded tests", 1, reporter.getTestSucceeded() );
88      }
89  
90  
91      public static class STest1
92      {
93          @Test
94          public void testSomething()
95          {
96  
97          }
98      }
99  
100     public static class STest2
101     {
102         @Test
103         public void testSomething2()
104         {
105 
106         }
107     }
108 
109 }