View Javadoc
1   package org.apache.maven.surefire.common.junit48;
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 org.apache.maven.shared.utils.io.SelectorUtils;
23  import org.junit.runner.Description;
24  import org.junit.runner.manipulation.Filter;
25  
26  import java.util.Map;
27  import java.util.Set;
28  
29  /**
30   * Only run test methods in the given input map, indexed by test class
31   */
32  final class FailingMethodFilter
33      extends Filter
34  {
35      // Map from Class -> List of method names. Are the method names hashed to include the signature?
36      private final Map<Class<?>, Set<String>> failingClassMethodPatterns;
37  
38      public FailingMethodFilter( Map<Class<?>, Set<String>> failingClassMethodPatterns )
39      {
40          this.failingClassMethodPatterns = failingClassMethodPatterns;
41      }
42  
43      @Override
44      public boolean shouldRun( Description description )
45      {
46          return isDescriptionMatch( description );
47      }
48  
49      private boolean isDescriptionMatch( Description description )
50      {
51          if ( description.getTestClass() == null || description.getMethodName() == null )
52          {
53              for ( Description childrenDescription : description.getChildren() )
54              {
55                  if ( isDescriptionMatch( childrenDescription ) )
56                  {
57                      return true;
58                  }
59              }
60              return false;
61          }
62  
63          Set<String> testMethodPatterns = failingClassMethodPatterns.get( description.getTestClass() );
64          String testMethod = description.getMethodName();
65          return testMethodPatterns != null && matchMethod( testMethodPatterns, testMethod );
66      }
67  
68      private static boolean matchMethod( Set<String> patterns, String methodName )
69      {
70          for ( String pattern : patterns )
71          {
72              if ( SelectorUtils.match( pattern, methodName ) )
73              {
74                  return true;
75              }
76          }
77          return false;
78      }
79  
80      @Override
81      public String describe()
82      {
83          return "By failing class method";
84      }
85  }