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