View Javadoc

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