• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.internal.runners;
2 
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.Method;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.List;
9 
10 import org.junit.AfterClass;
11 import org.junit.Before;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.junit.internal.MethodSorter;
15 import org.junit.runners.BlockJUnit4ClassRunner;
16 
17 /**
18  * @deprecated Included for backwards compatibility with JUnit 4.4. Will be
19  *             removed in the next major release. Please use
20  *             {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
21  */
22 @Deprecated
23 public class TestClass {
24     private final Class<?> klass;
25 
TestClass(Class<?> klass)26     public TestClass(Class<?> klass) {
27         this.klass = klass;
28     }
29 
getTestMethods()30     public List<Method> getTestMethods() {
31         return getAnnotatedMethods(Test.class);
32     }
33 
getBefores()34     List<Method> getBefores() {
35         return getAnnotatedMethods(BeforeClass.class);
36     }
37 
getAfters()38     List<Method> getAfters() {
39         return getAnnotatedMethods(AfterClass.class);
40     }
41 
getAnnotatedMethods(Class<? extends Annotation> annotationClass)42     public List<Method> getAnnotatedMethods(Class<? extends Annotation> annotationClass) {
43         List<Method> results = new ArrayList<Method>();
44         for (Class<?> eachClass : getSuperClasses(klass)) {
45             Method[] methods = MethodSorter.getDeclaredMethods(eachClass);
46             for (Method eachMethod : methods) {
47                 Annotation annotation = eachMethod.getAnnotation(annotationClass);
48                 if (annotation != null && !isShadowed(eachMethod, results)) {
49                     results.add(eachMethod);
50                 }
51             }
52         }
53         if (runsTopToBottom(annotationClass)) {
54             Collections.reverse(results);
55         }
56         return results;
57     }
58 
runsTopToBottom(Class<? extends Annotation> annotation)59     private boolean runsTopToBottom(Class<? extends Annotation> annotation) {
60         return annotation.equals(Before.class) || annotation.equals(BeforeClass.class);
61     }
62 
isShadowed(Method method, List<Method> results)63     private boolean isShadowed(Method method, List<Method> results) {
64         for (Method each : results) {
65             if (isShadowed(method, each)) {
66                 return true;
67             }
68         }
69         return false;
70     }
71 
isShadowed(Method current, Method previous)72     private boolean isShadowed(Method current, Method previous) {
73         if (!previous.getName().equals(current.getName())) {
74             return false;
75         }
76         if (previous.getParameterTypes().length != current.getParameterTypes().length) {
77             return false;
78         }
79         for (int i = 0; i < previous.getParameterTypes().length; i++) {
80             if (!previous.getParameterTypes()[i].equals(current.getParameterTypes()[i])) {
81                 return false;
82             }
83         }
84         return true;
85     }
86 
getSuperClasses(Class<?> testClass)87     private List<Class<?>> getSuperClasses(Class<?> testClass) {
88         List<Class<?>> results = new ArrayList<Class<?>>();
89         Class<?> current = testClass;
90         while (current != null) {
91             results.add(current);
92             current = current.getSuperclass();
93         }
94         return results;
95     }
96 
getConstructor()97     public Constructor<?> getConstructor() throws SecurityException, NoSuchMethodException {
98         return klass.getConstructor();
99     }
100 
getJavaClass()101     public Class<?> getJavaClass() {
102         return klass;
103     }
104 
getName()105     public String getName() {
106         return klass.getName();
107     }
108 
109 }
110