View Javadoc

1   package org.apache.maven.surefire.junitcore;
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.ArrayList;
23  import java.util.Map;
24  import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
25  import org.apache.maven.surefire.common.junit48.JUnit46StackTraceWriter;
26  import org.apache.maven.surefire.report.RunListener;
27  import org.apache.maven.surefire.report.StackTraceWriter;
28  
29  import org.junit.runner.Description;
30  import org.junit.runner.Result;
31  import org.junit.runner.notification.Failure;
32  
33  /**
34   * Noteworthy things about JUnit4 listening:
35   * <p/>
36   * A class that is annotated with @Ignore will have one invocation of "testSkipped" with source==name
37   * A method that is annotated with @Ignore will have a invocation of testSkipped with source and name distinct
38   * Methods annotated with @Ignore trigger no further events.
39   *
40   * @see org.apache.maven.surefire.junitcore.ConcurrentRunListener for details about parallel running
41   */
42  public class JUnitCoreRunListener
43      extends JUnit4RunListener
44  {
45      private final Map<String, TestSet> classMethodCounts;
46  
47      /**
48       * @param reporter          the report manager to log testing events to
49       * @param classMethodCounts A map of methods
50       */
51      public JUnitCoreRunListener( RunListener reporter, Map<String, TestSet> classMethodCounts )
52      {
53          super( reporter );
54          this.classMethodCounts = classMethodCounts;
55      }
56  
57      /**
58       * Called right before any tests from a specific class are run.
59       *
60       * @see org.junit.runner.notification.RunListener#testRunStarted(org.junit.runner.Description)
61       */
62      public void testRunStarted( Description description )
63          throws Exception
64      {
65          fillTestCountMap( description );
66          reporter.testSetStarting( null ); // Not entirely meaningful as we can see
67      }
68  
69      @Override
70      public void testRunFinished( Result result )
71          throws Exception
72      {
73          reporter.testSetCompleted( null );
74      }
75  
76      private void fillTestCountMap( Description description )
77      {
78          final ArrayList<Description> children = description.getChildren();
79  
80          TestSet testSet = new TestSet( description );
81          String itemTestClassName = null;
82          for ( Description item : children )
83          {
84              if ( !item.isTest() )
85              {
86                  fillTestCountMap( item );
87              }
88              else
89              {
90                  if ( item.getMethodName() != null )
91                  {
92                      testSet.incrementTestMethodCount();
93                      if ( itemTestClassName == null )
94                      {
95                          itemTestClassName = item.getClassName();
96                      }
97                  }
98                  else
99                  {
100                     classMethodCounts.put( item.getClassName(), new TestSet( item ) );
101                 }
102             }
103         }
104         if ( itemTestClassName != null )
105         {
106             classMethodCounts.put( itemTestClassName, testSet );
107         }
108     }
109 
110     @Override
111     protected StackTraceWriter createStackTraceWriter( Failure failure )
112     {
113         return new JUnit46StackTraceWriter( failure );
114     }
115 }