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.report.RunListener;
24  
25  import java.util.ArrayList;
26  import java.util.Map;
27  
28  import org.junit.runner.Description;
29  import org.junit.runner.Result;
30  
31  public class JUnitCoreRunListener
32      extends JUnit4RunListener
33  {
34      private final Map<String, TestSet> classMethodCounts;
35  
36      /**
37       * @param reporter          the report manager to log testing events to
38       * @param classMethodCounts A map of methods
39       */
40      public JUnitCoreRunListener( RunListener reporter, Map<String, TestSet> classMethodCounts )
41      {
42          super( reporter );
43          this.classMethodCounts = classMethodCounts;
44      }
45  
46      /**
47       * Called right before any tests from a specific class are run.
48       *
49       * @see org.junit.runner.notification.RunListener#testRunStarted(org.junit.runner.Description)
50       */
51      public void testRunStarted( Description description )
52          throws Exception
53      {
54          fillTestCountMap( description );
55          reporter.testSetStarting( null ); // Not entirely meaningful as we can see
56      }
57  
58      @Override
59      public void testRunFinished( Result result )
60          throws Exception
61      {
62          reporter.testSetCompleted( null );
63      }
64  
65      private void fillTestCountMap( Description description )
66      {
67          final ArrayList<Description> children = description.getChildren();
68  
69          TestSet testSet = new TestSet( description );
70          Class<?> itemTestClass = null;
71          for ( Description item : children )
72          {
73              if ( item.isTest() )
74              {
75                  testSet.incrementTestMethodCount();
76                  if ( itemTestClass == null )
77                  {
78                      itemTestClass = item.getTestClass();
79                  }
80              }
81              else if ( item.getChildren().size() > 0 )
82              {
83                  fillTestCountMap( item );
84              }
85          }
86          if ( itemTestClass != null )
87          {
88              classMethodCounts.put( itemTestClass.getName(), testSet );
89          }
90      }
91  
92  }