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.Collections;
22  import java.util.Map;
23  
24  import org.apache.maven.surefire.api.report.SimpleReportEntry;
25  import org.apache.maven.surefire.api.report.TestOutputReportEntry;
26  import org.apache.maven.surefire.api.report.TestReportListener;
27  import org.apache.maven.surefire.api.report.TestSetReportEntry;
28  import org.apache.maven.surefire.api.util.internal.ClassMethod;
29  import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
30  import org.junit.runner.Description;
31  import org.junit.runner.Result;
32  import org.junit.runner.notification.Failure;
33  
34  import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps;
35  import static org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil.toClassMethod;
36  
37  /**
38   * A class to be used when there is no JUnit parallelism (methods or/and class). This allows to workaround JUnit
39   * limitation a la Junit4 provider. Specifically, we can redirect properly the output even if we don't have class
40   * demarcation in JUnit. It works when if there is a JVM instance per test run, i.e. with reuseForks=false and
41   * forkCount greater than one.
42   */
43  @Deprecated // remove this class after StatelessXmlReporter is capable of parallel test sets processing
44  class NonConcurrentRunListener extends JUnit4RunListener {
45      private Description currentTestSetDescription;
46  
47      private Description lastFinishedDescription;
48  
49      NonConcurrentRunListener(TestReportListener<TestOutputReportEntry> reporter) {
50          super(reporter);
51      }
52  
53      private TestSetReportEntry createReportEntryForTestSet(Description description, Map<String, String> systemProps) {
54          ClassMethod classMethod = toClassMethod(description);
55          String className = classMethod.getClazz();
56          String methodName = classMethod.getMethod();
57          long testRunId = classMethodIndexer.indexClassMethod(className, methodName);
58          return new SimpleReportEntry(getRunMode(), testRunId, className, null, null, null, systemProps);
59      }
60  
61      private TestSetReportEntry createTestSetReportEntryStarted(Description description) {
62          return createReportEntryForTestSet(description, Collections.<String, String>emptyMap());
63      }
64  
65      private TestSetReportEntry createTestSetReportEntryFinished(Description description) {
66          return createReportEntryForTestSet(description, systemProps());
67      }
68  
69      @Override
70      public void testStarted(Description description) throws Exception {
71          finishLastTestSetIfNecessary(description);
72          super.testStarted(description);
73      }
74  
75      private void finishLastTestSetIfNecessary(Description description) {
76          if (describesNewTestSet(description)) {
77              currentTestSetDescription = description;
78              if (lastFinishedDescription != null) {
79                  reporter.testSetCompleted(createTestSetReportEntryFinished(lastFinishedDescription));
80                  lastFinishedDescription = null;
81              }
82              reporter.testSetStarting(createTestSetReportEntryStarted(description));
83          }
84      }
85  
86      private boolean describesNewTestSet(Description description) {
87          if (currentTestSetDescription != null) {
88              if (null != description.getTestClass()) {
89                  return !description.getTestClass().equals(currentTestSetDescription.getTestClass());
90              } else if (description.isSuite()) {
91                  return description.getChildren().equals(currentTestSetDescription.getChildren());
92              }
93  
94              return false;
95          }
96  
97          return true;
98      }
99  
100     @Override
101     public void testFinished(Description description) throws Exception {
102         super.testFinished(description);
103         lastFinishedDescription = description;
104     }
105 
106     @Override
107     public void testIgnored(Description description) throws Exception {
108         finishLastTestSetIfNecessary(description);
109 
110         super.testIgnored(description);
111         lastFinishedDescription = description;
112     }
113 
114     @Override
115     public void testFailure(Failure failure) throws Exception {
116         finishLastTestSetIfNecessary(failure.getDescription());
117 
118         super.testFailure(failure);
119         lastFinishedDescription = failure.getDescription();
120     }
121 
122     @Override
123     public void testAssumptionFailure(Failure failure) {
124         finishLastTestSetIfNecessary(failure.getDescription());
125 
126         super.testAssumptionFailure(failure);
127         lastFinishedDescription = failure.getDescription();
128     }
129 
130     @Override
131     public void testRunStarted(Description description) throws Exception {}
132 
133     @Override
134     public void testRunFinished(Result result) throws Exception {
135         if (lastFinishedDescription != null) {
136             reporter.testSetCompleted(createTestSetReportEntryFinished(lastFinishedDescription));
137             lastFinishedDescription = null;
138         }
139     }
140 }