View Javadoc
1   package org.apache.maven.surefire.testng.utils;
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.List;
23  
24  import org.apache.maven.surefire.testset.TestListResolver;
25  import org.testng.IMethodSelector;
26  import org.testng.IMethodSelectorContext;
27  import org.testng.ITestNGMethod;
28  
29  /**
30   * For internal use only
31   *
32   * @author Olivier Lamy
33   * @since 2.7.3
34   */
35  public class MethodSelector
36          implements IMethodSelector
37  {
38      private static volatile TestListResolver testListResolver = null;
39  
40      @Override
41      public void setTestMethods( List arg0 )
42      {
43      }
44  
45      @Override
46      public boolean includeMethod( IMethodSelectorContext context, ITestNGMethod testngMethod, boolean isTestMethod )
47      {
48          return testngMethod.isBeforeClassConfiguration() || testngMethod.isBeforeGroupsConfiguration()
49                  || testngMethod.isBeforeMethodConfiguration() || testngMethod.isBeforeSuiteConfiguration()
50                  || testngMethod.isBeforeTestConfiguration() || testngMethod.isAfterClassConfiguration()
51                  || testngMethod.isAfterGroupsConfiguration() || testngMethod.isAfterMethodConfiguration()
52                  || testngMethod.isAfterSuiteConfiguration() || testngMethod.isAfterTestConfiguration()
53                  || shouldRun( testngMethod );
54      }
55  
56      public static void setTestListResolver( TestListResolver testListResolver )
57      {
58          MethodSelector.testListResolver = testListResolver;
59      }
60  
61      private static boolean shouldRun( ITestNGMethod test )
62      {
63          TestListResolver resolver = testListResolver;
64          boolean hasTestResolver = resolver != null && !resolver.isEmpty();
65          if ( hasTestResolver )
66          {
67              boolean run = false;
68              for ( Class<?> clazz = test.getRealClass(); !run && clazz != Object.class; clazz = clazz.getSuperclass() )
69              {
70                  run = resolver.shouldRun( clazz, test.getMethodName() );
71              }
72              return run;
73          }
74          return false;
75      }
76  }