1 | |
package org.apache.maven.surefire.common.junit4; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.surefire.NonAbstractClassFilter; |
23 | |
import org.apache.maven.surefire.common.junit3.JUnit3TestChecker; |
24 | |
import org.apache.maven.surefire.util.ReflectionUtils; |
25 | |
import org.apache.maven.surefire.util.ScannerFilter; |
26 | |
|
27 | |
import java.lang.annotation.Annotation; |
28 | |
import java.lang.reflect.Method; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class JUnit4TestChecker |
34 | |
implements ScannerFilter |
35 | |
{ |
36 | |
private final NonAbstractClassFilter nonAbstractClassFilter; |
37 | |
|
38 | |
private final Class runWith; |
39 | |
|
40 | |
private final JUnit3TestChecker jUnit3TestChecker; |
41 | |
|
42 | |
|
43 | |
public JUnit4TestChecker( ClassLoader testClassLoader ) |
44 | 0 | { |
45 | 0 | this.jUnit3TestChecker = new JUnit3TestChecker( testClassLoader ); |
46 | 0 | this.runWith = getJUnitClass( testClassLoader, org.junit.runner.RunWith.class.getName() ); |
47 | 0 | this.nonAbstractClassFilter = new NonAbstractClassFilter(); |
48 | 0 | } |
49 | |
|
50 | |
public boolean accept( Class testClass ) |
51 | |
{ |
52 | 0 | return jUnit3TestChecker.accept( testClass ) || isValidJUnit4Test( testClass ); |
53 | |
} |
54 | |
|
55 | |
@SuppressWarnings( { "unchecked" } ) |
56 | |
private boolean isValidJUnit4Test( Class testClass ) |
57 | |
{ |
58 | 0 | if ( !nonAbstractClassFilter.accept( testClass ) ) |
59 | |
{ |
60 | 0 | return false; |
61 | |
} |
62 | |
|
63 | 0 | Annotation runWithAnnotation = testClass.getAnnotation( runWith ); |
64 | 0 | if ( runWithAnnotation != null ) |
65 | |
{ |
66 | 0 | return true; |
67 | |
} |
68 | |
|
69 | 0 | Class classToCheck = testClass; |
70 | 0 | while ( classToCheck != null ) |
71 | |
{ |
72 | 0 | if ( checkforTestAnnotatedMethod( classToCheck ) ) |
73 | |
{ |
74 | 0 | return true; |
75 | |
} |
76 | 0 | classToCheck = classToCheck.getSuperclass(); |
77 | |
} |
78 | 0 | return false; |
79 | |
} |
80 | |
|
81 | |
private boolean checkforTestAnnotatedMethod( Class testClass ) |
82 | |
{ |
83 | 0 | for ( Method lMethod : testClass.getDeclaredMethods() ) |
84 | |
{ |
85 | 0 | for ( Annotation lAnnotation : lMethod.getAnnotations() ) |
86 | |
{ |
87 | 0 | if ( org.junit.Test.class.isAssignableFrom( lAnnotation.annotationType() ) ) |
88 | |
{ |
89 | 0 | return true; |
90 | |
} |
91 | |
} |
92 | |
} |
93 | 0 | return false; |
94 | |
} |
95 | |
|
96 | |
private Class getJUnitClass( ClassLoader classLoader, String className ) |
97 | |
{ |
98 | 0 | return ReflectionUtils.tryLoadClass( classLoader, className ); |
99 | |
} |
100 | |
|
101 | |
} |