View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.junitcore;
20  
21  import java.util.Map;
22  
23  import org.apache.maven.surefire.api.report.StackTraceWriter;
24  import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
25  import org.apache.maven.surefire.common.junit4.JUnit4StackTraceWriter;
26  import org.junit.runner.Description;
27  import org.junit.runner.Result;
28  import org.junit.runner.notification.Failure;
29  
30  import static org.apache.maven.surefire.api.util.internal.TestClassMethodNameUtils.extractClassName;
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  final class JUnitCoreRunListener extends JUnit4RunListener {
42      private final Map<String, TestSet> classMethodCounts;
43  
44      /**
45       * @param reporter          the report manager to log testing events to
46       * @param classMethodCounts A map of methods
47       */
48      JUnitCoreRunListener(ConcurrentRunListener reporter, Map<String, TestSet> classMethodCounts) {
49          super(reporter);
50          this.classMethodCounts = classMethodCounts;
51      }
52  
53      /**
54       * Called right before any tests from a specific class are run.
55       *
56       * @see org.junit.runner.notification.RunListener#testRunStarted(org.junit.runner.Description)
57       */
58      @Override
59      public void testRunStarted(Description description) throws Exception {
60          fillTestCountMap(description);
61          reporter.testSetStarting(null); // Not entirely meaningful as we can see
62      }
63  
64      @Override
65      public void testRunFinished(Result result) throws Exception {
66          try {
67              reporter.testSetCompleted(null);
68          } finally {
69              classMethodCounts.clear();
70          }
71      }
72  
73      private void fillTestCountMap(Description testDesc) {
74          for (Description child : testDesc.getChildren()) {
75              if (!asTestLeaf(child)) {
76                  fillTestCountMap(child);
77              }
78          }
79      }
80  
81      private boolean asTestLeaf(Description description) {
82          if (description.isTest()) {
83              final String testClassName = extractClassName(description.getDisplayName());
84              if (testClassName != null) {
85                  final TestSet testSet;
86                  if (classMethodCounts.containsKey(testClassName)) {
87                      testSet = classMethodCounts.get(testClassName);
88                  } else {
89                      testSet = new TestSet(testClassName, getRunMode(), classMethodIndexer);
90                      classMethodCounts.put(testClassName, testSet);
91                  }
92                  testSet.incrementTestMethodCount();
93              }
94              return true;
95          } else {
96              return false;
97          }
98      }
99  
100     @Override
101     protected StackTraceWriter createStackTraceWriter(Failure failure) {
102         return new JUnit4StackTraceWriter(failure);
103     }
104 }