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.junit3;
20  
21  import java.lang.reflect.Method;
22  
23  import org.apache.maven.surefire.api.filter.NonAbstractClassFilter;
24  import org.apache.maven.surefire.api.util.ScannerFilter;
25  
26  import static java.lang.reflect.Modifier.isPublic;
27  import static java.lang.reflect.Modifier.isStatic;
28  import static org.apache.maven.surefire.api.util.ReflectionUtils.tryGetMethod;
29  import static org.apache.maven.surefire.api.util.ReflectionUtils.tryLoadClass;
30  
31  /**
32   * Missing tests ? This class is basically a subset of the JUnit4TestChecker, which is tested
33   * to boredom and back. Unfortunately we don't have any common module between these providers,
34   * so this stuff is duplicated. We should probably make some modules and just shade the content
35   * into the providers.
36   *
37   * @author Kristian Rosenvold
38   */
39  public class JUnit3TestChecker implements ScannerFilter {
40      private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
41  
42      private final Class<?> junitClass;
43  
44      private final NonAbstractClassFilter nonAbstractClassFilter = new NonAbstractClassFilter();
45  
46      public JUnit3TestChecker(ClassLoader testClassLoader) {
47          junitClass = tryLoadClass(testClassLoader, "junit.framework.Test");
48      }
49  
50      @Override
51      public boolean accept(Class testClass) {
52          return nonAbstractClassFilter.accept(testClass) && isValidJUnit3Test(testClass);
53      }
54  
55      private boolean isValidJUnit3Test(Class<?> testClass) {
56          return junitClass != null && (junitClass.isAssignableFrom(testClass) || isSuiteOnly(testClass));
57      }
58  
59      private boolean isSuiteOnly(Class testClass) {
60          final Method suite = tryGetMethod(testClass, "suite", EMPTY_CLASS_ARRAY);
61          if (suite != null) {
62              final int modifiers = suite.getModifiers();
63              if (isPublic(modifiers) && isStatic(modifiers)) {
64                  return junit.framework.Test.class.isAssignableFrom(suite.getReturnType());
65              }
66          }
67          return false;
68      }
69  }