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