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