Update of /cvsroot/junit/junit/org/junit/internal/runners In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv6170/org/junit/internal/runners Modified Files: Tag: saff_r41_runner_refactoring TestIntrospector.java TestClassMethodsRunner.java Added Files: Tag: saff_r41_runner_refactoring JavaMethod.java JavaClass.java MethodAnnotation.java Log Message: Begin pushing functionality out of operator classes (like TestIntrospector) and into domain classes (like JavaClass) --- NEW FILE: JavaMethod.java --- /** * */ package org.junit.internal.runners; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.List; class JavaMethod { // TODO: push out private final Method fMethod; JavaMethod(Method current) { fMethod= current; } public boolean isShadowedBy(Method previous) { if (!previous.getName().equals(fMethod.getName())) return false; if (previous.getParameterTypes().length != fMethod .getParameterTypes().length) return false; for (int i= 0; i < previous.getParameterTypes().length; i++) { if (!previous.getParameterTypes()[i].equals(fMethod .getParameterTypes()[i])) return false; } return true; } boolean isShadowedBy(List<Method> results) { for (Method each : results) { if (isShadowedBy(each)) return true; } return false; } public Annotation getAnnotation(MethodAnnotation methodAnnotation) { return fMethod.getAnnotation(methodAnnotation.getAnnotationClass()); } public Method getMethod() { return fMethod; } } --- NEW FILE: JavaClass.java --- /** * */ package org.junit.internal.runners; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; class JavaClass { // TODO: push out private final Class<?> fClass; JavaClass(Class<?> type) { fClass= type; } public List<JavaClass> getSuperClasses() { ArrayList<JavaClass> results= new ArrayList<JavaClass>(); Class<?> current= fClass; while (current != null) { results.add(new JavaClass(current)); current= current.getSuperclass(); } return results; } List<Method> getTestMethods(MethodAnnotation methodAnnotation) { List<Method> results= new ArrayList<Method>(); for (JavaClass eachClass : getSuperClasses()) { List<JavaMethod> methods= eachClass.getDeclaredMethods(); for (JavaMethod eachMethod : methods) { Annotation annotation= eachMethod.getAnnotation(methodAnnotation); if (annotation != null && ! eachMethod.isShadowedBy(results)) results.add(eachMethod.getMethod()); } } if (methodAnnotation.runsTopToBottom()) Collections.reverse(results); return results; } private List<JavaMethod> getDeclaredMethods() { Method[] declaredMethods= fClass.getDeclaredMethods(); ArrayList<JavaMethod> javaMethods= new ArrayList<JavaMethod>(); for (Method method : declaredMethods) { javaMethods.add(new JavaMethod(method)); } return javaMethods; } List<Method> getTestMethods(Class<? extends Annotation> type) { return getTestMethods(new MethodAnnotation(type)); } } --- NEW FILE: MethodAnnotation.java --- /** * */ package org.junit.internal.runners; import java.lang.annotation.Annotation; import org.junit.RunSuperclassMethodsFirst; final class MethodAnnotation { // TODO: push out // TODO: package too big? private final Class<? extends Annotation> fAnnotation; MethodAnnotation(Class<? extends Annotation> annotation) { fAnnotation= annotation; } public boolean runsTopToBottom() { return fAnnotation.getAnnotation(RunSuperclassMethodsFirst.class) != null; } public Class<? extends Annotation> getAnnotationClass() { return fAnnotation; } } Index: TestIntrospector.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/TestIntrospector.java,v retrieving revision 1.3 retrieving revision 1.3.2.1 diff -u -d -r1.3 -r1.3.2.1 --- TestIntrospector.java 27 Apr 2006 22:11:51 -0000 1.3 +++ TestIntrospector.java 26 Sep 2006 16:51:39 -0000 1.3.2.1 @@ -2,12 +2,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.Test.None; @@ -21,56 +17,19 @@ } public List<Method> getTestMethods(Class<? extends Annotation> annotationClass) { - List<Method> results= new ArrayList<Method>(); - for (Class eachClass : getSuperClasses(fTestClass)) { - Method[] methods= eachClass.getDeclaredMethods(); - for (Method eachMethod : methods) { - Annotation annotation= eachMethod.getAnnotation(annotationClass); - if (annotation != null && ! isShadowed(eachMethod, results)) - results.add(eachMethod); - } - } - if (runsTopToBottom(annotationClass)) - Collections.reverse(results); - return results; - } - - public boolean isIgnored(Method eachMethod) { - return eachMethod.getAnnotation(Ignore.class) != null; + return getJavaClass().getTestMethods(new MethodAnnotation(annotationClass)); } - private boolean runsTopToBottom(Class< ? extends Annotation> annotation) { - return annotation.equals(Before.class) || annotation.equals(BeforeClass.class); - } - - private boolean isShadowed(Method method, List<Method> results) { - for (Method each : results) { - if (isShadowed(method, each)) - return true; - } - return false; + JavaClass getJavaClass() { + return new JavaClass(getTestClass()); } - private boolean isShadowed(Method current, Method previous) { - if (! previous.getName().equals(current.getName())) - return false; - if (previous.getParameterTypes().length != current.getParameterTypes().length) - return false; - for (int i= 0; i < previous.getParameterTypes().length; i++) { - if (! previous.getParameterTypes()[i].equals(current.getParameterTypes()[i])) - return false; - } - return true; + public Class<?> getTestClass() { + return fTestClass; } - private List<Class> getSuperClasses(Class< ?> testClass) { - ArrayList<Class> results= new ArrayList<Class>(); - Class<?> current= testClass; - while (current != null) { - results.add(current); - current= current.getSuperclass(); - } - return results; + public boolean isIgnored(Method eachMethod) { + return eachMethod.getAnnotation(Ignore.class) != null; } long getTimeout(Method method) { Index: TestClassMethodsRunner.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/TestClassMethodsRunner.java,v retrieving revision 1.3 retrieving revision 1.3.2.1 diff -u -d -r1.3 -r1.3.2.1 --- TestClassMethodsRunner.java 3 Aug 2006 22:03:01 -0000 1.3 +++ TestClassMethodsRunner.java 26 Sep 2006 16:51:39 -0000 1.3.2.1 @@ -25,9 +25,9 @@ // This assumes that some containing runner will perform validation of the test methods public TestClassMethodsRunner(Class<?> klass) { fTestClass= klass; - fTestMethods= new TestIntrospector(getTestClass()).getTestMethods(Test.class); + fTestMethods= new JavaClass(klass).getTestMethods(Test.class); } - + @Override public void run(RunNotifier notifier) { if (fTestMethods.isEmpty()) |