1 package org.apache.maven.surefire.common.junit3;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Method;
23 import org.apache.maven.surefire.NonAbstractClassFilter;
24 import org.apache.maven.surefire.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.util.ReflectionUtils.tryGetMethod;
29 import static org.apache.maven.surefire.util.ReflectionUtils.tryLoadClass;
30
31
32
33
34
35
36
37
38
39 public class JUnit3TestChecker
40 implements ScannerFilter
41 {
42 private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
43
44 private final Class<?> junitClass;
45
46 private final NonAbstractClassFilter nonAbstractClassFilter = new NonAbstractClassFilter();
47
48 public JUnit3TestChecker( ClassLoader testClassLoader )
49 {
50 junitClass = tryLoadClass( testClassLoader, "junit.framework.Test" );
51 }
52
53 @Override
54 public boolean accept( Class testClass )
55 {
56 return nonAbstractClassFilter.accept( testClass ) && isValidJUnit3Test( testClass );
57 }
58
59 private boolean isValidJUnit3Test( Class<?> testClass )
60 {
61 return junitClass != null && ( junitClass.isAssignableFrom( testClass ) || isSuiteOnly( testClass ) );
62 }
63
64 private boolean isSuiteOnly( Class testClass )
65 {
66 final Method suite = tryGetMethod( testClass, "suite", EMPTY_CLASS_ARRAY );
67 if ( suite != null )
68 {
69 final int modifiers = suite.getModifiers();
70 if ( isPublic( modifiers ) && isStatic( modifiers ) )
71 {
72 return junit.framework.Test.class.isAssignableFrom( suite.getReturnType() );
73 }
74 }
75 return false;
76 }
77
78 }