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.lang.annotation.Annotation;
22  import java.lang.reflect.Method;
23  
24  import org.apache.maven.surefire.api.filter.NonAbstractClassFilter;
25  import org.apache.maven.surefire.api.util.ScannerFilter;
26  import org.apache.maven.surefire.common.junit3.JUnit3TestChecker;
27  
28  import static org.apache.maven.surefire.api.util.ReflectionUtils.tryLoadClass;
29  
30  /**
31   * @author Kristian Rosenvold
32   */
33  public class JUnit4TestChecker implements ScannerFilter {
34      private final NonAbstractClassFilter nonAbstractClassFilter;
35  
36      private final Class runWith;
37  
38      private final JUnit3TestChecker jUnit3TestChecker;
39  
40      public JUnit4TestChecker(ClassLoader testClassLoader) {
41          jUnit3TestChecker = new JUnit3TestChecker(testClassLoader);
42          runWith = tryLoadClass(testClassLoader, org.junit.runner.RunWith.class.getName());
43          nonAbstractClassFilter = new NonAbstractClassFilter();
44      }
45  
46      @Override
47      public boolean accept(Class testClass) {
48          return jUnit3TestChecker.accept(testClass) || isValidJUnit4Test(testClass);
49      }
50  
51      @SuppressWarnings({"unchecked"})
52      private boolean isValidJUnit4Test(Class testClass) {
53          if (!nonAbstractClassFilter.accept(testClass)) {
54              return false;
55          }
56  
57          if (isRunWithPresentInClassLoader()) {
58              Annotation runWithAnnotation = testClass.getAnnotation(runWith);
59              if (runWithAnnotation != null) {
60                  return true;
61              }
62          }
63  
64          return lookForTestAnnotatedMethods(testClass);
65      }
66  
67      private boolean lookForTestAnnotatedMethods(Class testClass) {
68          Class classToCheck = testClass;
69          while (classToCheck != null) {
70              if (checkforTestAnnotatedMethod(classToCheck)) {
71                  return true;
72              }
73              classToCheck = classToCheck.getSuperclass();
74          }
75          return false;
76      }
77  
78      public boolean checkforTestAnnotatedMethod(Class testClass) {
79          for (Method lMethod : testClass.getDeclaredMethods()) {
80              for (Annotation lAnnotation : lMethod.getAnnotations()) {
81                  if (org.junit.Test.class.isAssignableFrom(lAnnotation.annotationType())) {
82                      return true;
83                  }
84              }
85          }
86          return false;
87      }
88  
89      public boolean isRunWithPresentInClassLoader() {
90          return runWith != null;
91      }
92  }