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.List;
24  import java.util.Set;
25  
26  import org.junit.runner.Description;
27  import org.junit.runner.manipulation.Filter;
28  import org.junit.runner.notification.Failure;
29  
30  import static org.apache.maven.surefire.util.internal.StringUtils.isBlank;
31  import static org.junit.runner.Description.TEST_MECHANISM;
32  
33  /**
34   *
35   * Utility method used among all JUnit4 providers
36   *
37   * @author Qingzhou Luo
38   *
39   */
40  public final class JUnit4ProviderUtil
41  {
42      private JUnit4ProviderUtil()
43      {
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      {
55          Set<Description> failingTestDescriptions = new HashSet<Description>();
56  
57          for ( Failure failure : allFailures )
58          {
59              Description description = failure.getDescription();
60              if ( description.isTest() && !isFailureInsideJUnitItself( description ) )
61              {
62                  failingTestDescriptions.add( description );
63              }
64          }
65          return failingTestDescriptions;
66      }
67  
68      public static boolean isFailureInsideJUnitItself( Description failure )
69      {
70          return TEST_MECHANISM.equals( failure );
71      }
72  
73      /**
74       * Java Patterns of regex is slower than cutting a substring.
75       * @param description method(class) or method[#](class) or method[#whatever-literals](class)
76       * @return method JUnit test method
77       */
78      public static ClassMethod cutTestClassAndMethod( Description description )
79      {
80          String name = description.getDisplayName();
81          String clazz = null;
82          String method = null;
83          if ( name != null )
84          {
85              // The order is : 1.method and then 2.class
86              // method(class)
87              name = name.trim();
88              if ( name.endsWith( ")" ) )
89              {
90                  int classBracket = name.lastIndexOf( '(' );
91                  if ( classBracket != -1 )
92                  {
93                      clazz = tryBlank( name.substring( classBracket + 1, name.length() - 1 ) );
94                      method = tryBlank( name.substring( 0, classBracket ) );
95                  }
96              }
97          }
98          return new ClassMethod( clazz, method );
99      }
100 
101     private static String tryBlank( String s )
102     {
103         s = s.trim();
104         return isBlank( s ) ? null : s;
105     }
106 
107     public static Filter createMatchAnyDescriptionFilter( Iterable<Description> descriptions )
108     {
109         return new MatchDescriptions( descriptions );
110     }
111 }