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 org.apache.maven.surefire.common.junit4.JUnit4RunListener;
23  import org.apache.maven.surefire.common.junit48.JUnit46StackTraceWriter;
24  import org.apache.maven.surefire.report.RunListener;
25  import org.apache.maven.surefire.report.StackTraceWriter;
26  import org.junit.runner.Description;
27  import org.junit.runner.Result;
28  import org.junit.runner.notification.Failure;
29  
30  import java.util.Map;
31  
32  /**
33   * Noteworthy things about JUnit4 listening:
34   * <br>
35   * A class that is annotated with @Ignore will have one invocation of "testSkipped" with source==name
36   * A method that is annotated with @Ignore will have a invocation of testSkipped with source and name distinct
37   * Methods annotated with @Ignore trigger no further events.
38   *
39   * @see org.apache.maven.surefire.junitcore.ConcurrentRunListener for details about parallel running
40   */
41  public class JUnitCoreRunListener
42      extends JUnit4RunListener
43  {
44      private final Map<String, TestSet> classMethodCounts;
45  
46      /**
47       * @param reporter          the report manager to log testing events to
48       * @param classMethodCounts A map of methods
49       */
50      public JUnitCoreRunListener( RunListener reporter, Map<String, TestSet> classMethodCounts )
51      {
52          super( reporter );
53          this.classMethodCounts = classMethodCounts;
54      }
55  
56      /**
57       * Called right before any tests from a specific class are run.
58       *
59       * @see org.junit.runner.notification.RunListener#testRunStarted(org.junit.runner.Description)
60       */
61      @Override
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          try
74          {
75              reporter.testSetCompleted( null );
76          }
77          finally
78          {
79              classMethodCounts.clear();
80          }
81      }
82  
83      private void fillTestCountMap( Description testDesc )
84      {
85          for ( Description child : testDesc.getChildren() )
86          {
87              if ( !asTestLeaf( child ) )
88              {
89                  fillTestCountMap( child );
90              }
91          }
92      }
93  
94      private boolean asTestLeaf( Description description )
95      {
96          if ( description.isTest() )
97          {
98              final String testClassName = extractDescriptionClassName( description );
99              if ( testClassName != null )
100             {
101                 final TestSet testSet;
102                 if ( classMethodCounts.containsKey( testClassName ) )
103                 {
104                     testSet = classMethodCounts.get( testClassName );
105                 }
106                 else
107                 {
108                     testSet = new TestSet( testClassName );
109                     classMethodCounts.put( testClassName, testSet );
110                 }
111                 testSet.incrementTestMethodCount();
112             }
113             return true;
114         }
115         else
116         {
117             return false;
118         }
119     }
120 
121     @Override
122     protected StackTraceWriter createStackTraceWriter( Failure failure )
123     {
124         return new JUnit46StackTraceWriter( failure );
125     }
126 
127     @Override
128     protected String extractDescriptionClassName( Description description )
129     {
130         return description.getClassName();
131     }
132 
133     @Override
134     protected String extractDescriptionMethodName( Description description )
135     {
136         return description.getMethodName();
137     }
138 }