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.common.junit4;
20  
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.maven.surefire.api.util.internal.ClassMethod;
27  import org.junit.runner.Description;
28  import org.junit.runner.manipulation.Filter;
29  import org.junit.runner.notification.Failure;
30  
31  import static org.apache.maven.surefire.api.util.internal.TestClassMethodNameUtils.extractClassName;
32  import static org.apache.maven.surefire.api.util.internal.TestClassMethodNameUtils.extractMethodName;
33  import static org.junit.runner.Description.TEST_MECHANISM;
34  
35  /**
36   *
37   * Utility method used among all JUnit4 providers
38   *
39   * @author Qingzhou Luo
40   *
41   */
42  public final class JUnit4ProviderUtil {
43      private JUnit4ProviderUtil() {
44          throw new IllegalStateException("Cannot instantiate.");
45      }
46  
47      /**
48       * Get all descriptions from a list of Failures
49       *
50       * @param allFailures the list of failures for a given test class
51       * @return the list of descriptions
52       */
53      public static Set<Description> generateFailingTestDescriptions(List<Failure> allFailures) {
54          Set<Description> failingTestDescriptions = new HashSet<>();
55  
56          for (Failure failure : allFailures) {
57              Description description = failure.getDescription();
58              if (description.isTest() && !isFailureInsideJUnitItself(description)) {
59                  failingTestDescriptions.add(description);
60              }
61          }
62          return failingTestDescriptions;
63      }
64  
65      public static boolean isFailureInsideJUnitItself(Description failure) {
66          return TEST_MECHANISM.equals(failure);
67      }
68  
69      /**
70       * Java Patterns of regex is slower than cutting a substring.
71       * @param description method(class) or method[#](class) or method[#whatever-literals](class)
72       * @return method JUnit test method
73       */
74      public static ClassMethod toClassMethod(Description description) {
75          String clazz = extractClassName(description.getDisplayName());
76          if (clazz == null || isInsaneJunitNullString(clazz)) {
77              // This can happen upon early failures (class instantiation error etc)
78              Iterator<Description> it = description.getChildren().iterator();
79              if (it.hasNext()) {
80                  description = it.next();
81                  clazz = extractClassName(description.getDisplayName());
82              }
83              if (clazz == null) {
84                  clazz = "Test Instantiation Error";
85              }
86          }
87          String method = extractMethodName(description.getDisplayName());
88          return new ClassMethod(clazz, method);
89      }
90  
91      private static boolean isInsaneJunitNullString(String value) {
92          return "null".equals(value);
93      }
94  
95      public static Filter createMatchAnyDescriptionFilter(Iterable<Description> descriptions) {
96          return new MatchDescriptions(descriptions);
97      }
98  }