Update of /cvsroot/junit/junit/org/junit/internal/runners In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv9576/org/junit/internal/runners Modified Files: Tag: saff_r41_runner_refactoring JavaMethod.java JavaTestInterpreter.java TestClassRunner.java JavaClass.java TestClassMethodsRunner.java Log Message: Parameterized works entirely with JavaTestInterpreter now Index: JavaMethod.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaMethod.java,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -d -r1.1.2.3 -r1.1.2.4 --- JavaMethod.java 18 Oct 2006 17:07:05 -0000 1.1.2.3 +++ JavaMethod.java 18 Oct 2006 18:22:14 -0000 1.1.2.4 @@ -15,6 +15,9 @@ import org.junit.Test; import org.junit.Test.None; import org.junit.runner.Description; +import org.junit.runner.notification.RunNotifier; + +// TODO: check names of various "run" methods public class JavaMethod extends JavaModelElement { // TODO: push out @@ -149,24 +152,42 @@ return Before.class; } - public void runWithoutBeforeAndAfter(TestEnvironment environment, Object test) { + public void runWithoutBeforeAndAfter(TestEnvironment environment, + Object test) { try { environment.getInterpreter().executeMethodBody(test, this); if (expectsException()) - environment.addFailure(new AssertionError("Expected exception: " - + expectedException().getName())); + environment + .addFailure(new AssertionError("Expected exception: " + + expectedException().getName())); } catch (InvocationTargetException e) { Throwable actual= e.getTargetException(); if (!expectsException()) environment.addFailure(actual); else if (isUnexpected(actual)) { String message= "Unexpected exception, expected<" - + expectedException().getName() - + "> but was<" + actual.getClass().getName() + ">"; + + expectedException().getName() + "> but was<" + + actual.getClass().getName() + ">"; environment.addFailure(new Exception(message, actual)); } } catch (Throwable e) { environment.addFailure(e); } } + + void invokeTestMethod(RunNotifier notifier, JavaTestInterpreter interpreter) { + Object test; + try { + test= getJavaClass().newInstance(); + } catch (InvocationTargetException e) { + notifier.testAborted(description(), e.getCause()); + return; + } catch (Exception e) { + notifier.testAborted(description(), e); + return; + } + TestEnvironment testEnvironment= new TestEnvironment(interpreter, + new PerTestNotifier(notifier, description()), test); + testEnvironment.run(this); + } } \ No newline at end of file Index: JavaTestInterpreter.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaTestInterpreter.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -d -r1.1.2.2 -r1.1.2.3 --- JavaTestInterpreter.java 18 Oct 2006 17:07:05 -0000 1.1.2.2 +++ JavaTestInterpreter.java 18 Oct 2006 18:22:14 -0000 1.1.2.3 @@ -1,18 +1,24 @@ package org.junit.internal.runners; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.runner.Runner; public class JavaTestInterpreter { - + public JavaTestInterpreter() { - + } // TODO: push out // TODO: be suspicious of everywhere this is constructed - - /* (non-Javadoc) - * @see org.junit.internal.runners.IJavaTestInterpreter#executeMethodBody(java.lang.Object, org.junit.internal.runners.JavaMethod) + + /* + * (non-Javadoc) + * + * @see org.junit.internal.runners.IJavaTestInterpreter#executeMethodBody(java.lang.Object, + * org.junit.internal.runners.JavaMethod) */ public void executeMethodBody(Object test, JavaMethod javaMethod) throws IllegalAccessException, InvocationTargetException { @@ -23,4 +29,18 @@ return new JavaClass(superclass); } + public Runner runnerFor(Class<?> klass) throws InitializationError { + MethodValidator methodValidator= new MethodValidator(klass); + validate(methodValidator); + methodValidator.assertValid(); + return new TestClassMethodsRunner(new JavaClass(klass), this); + } + + protected void validate(MethodValidator methodValidator) { + methodValidator.validateAllMethods(); + } + + protected JavaMethod interpretJavaMethod(final JavaClass klass, Method method) { + return new JavaMethod(klass, method); + } } Index: TestClassRunner.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/TestClassRunner.java,v retrieving revision 1.2.2.2 retrieving revision 1.2.2.3 diff -u -d -r1.2.2.2 -r1.2.2.3 --- TestClassRunner.java 18 Oct 2006 17:07:05 -0000 1.2.2.2 +++ TestClassRunner.java 18 Oct 2006 18:22:14 -0000 1.2.2.3 @@ -14,23 +14,17 @@ private final Class<?> fTestClass; + private JavaTestInterpreter fInterpreter; + public TestClassRunner(Class<?> klass) throws InitializationError { this(klass, new JavaTestInterpreter()); } public TestClassRunner(Class<?> klass, JavaTestInterpreter interpreter) throws InitializationError { - this(klass, new TestClassMethodsRunner(new JavaClass(klass), - interpreter)); - } - - public TestClassRunner(Class<?> klass, Runner runner) - throws InitializationError { fTestClass= klass; - fEnclosedRunner= runner; - MethodValidator methodValidator= new MethodValidator(klass); - validate(methodValidator); - methodValidator.assertValid(); + fEnclosedRunner= interpreter.runnerFor(klass); + fInterpreter= interpreter; } // TODO: this is parallel to passed-in runner @@ -46,9 +40,8 @@ } }; - TestEnvironment environment= new TestEnvironment( - new JavaTestInterpreter(), new PerTestNotifier(notifier, - getDescription()), null); + TestEnvironment environment= new TestEnvironment(fInterpreter, + new PerTestNotifier(notifier, getDescription()), null); environment.runWithBeforeAndAfter(protectThis, new JavaClass( getTestClass())); } Index: JavaClass.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaClass.java,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -d -r1.1.2.3 -r1.1.2.4 --- JavaClass.java 18 Oct 2006 17:07:05 -0000 1.1.2.3 +++ JavaClass.java 18 Oct 2006 18:22:14 -0000 1.1.2.4 @@ -21,23 +21,24 @@ fClass= type; } - public List<JavaClass> getSuperClasses(JavaTestInterpreter interpreter) { + public List<JavaClass> getSuperClasses() { ArrayList<JavaClass> results= new ArrayList<JavaClass>(); results.add(this); // TODO: this will not add parameterized superclasses (need to use // interpreter here?) if (fClass.getSuperclass() != null) - results.addAll(interpreter.interpretJavaClass( - fClass.getSuperclass()).getSuperClasses(interpreter)); + results.addAll(new JavaClass(fClass.getSuperclass()) + .getSuperClasses()); return results; } public JavaMethodList getMethods(MethodAnnotation methodAnnotation, JavaTestInterpreter interpreter) { JavaMethodList results= new JavaMethodList(); - for (JavaClass eachClass : getSuperClasses(interpreter)) { - for (JavaMethod eachMethod : eachClass.getDeclaredMethods()) { + for (JavaClass eachClass : getSuperClasses()) { + for (JavaMethod eachMethod : eachClass + .getDeclaredMethods(interpreter)) { Annotation annotation= eachMethod .getAnnotation(methodAnnotation); if (annotation != null && !eachMethod.isShadowedBy(results)) @@ -49,19 +50,15 @@ return results; } - private List<JavaMethod> getDeclaredMethods() { + private List<JavaMethod> getDeclaredMethods(JavaTestInterpreter interpreter) { Method[] declaredMethods= fClass.getDeclaredMethods(); ArrayList<JavaMethod> javaMethods= new ArrayList<JavaMethod>(); for (Method method : declaredMethods) { - javaMethods.add(makeJavaMethod(method)); + javaMethods.add(interpreter.interpretJavaMethod(this, method)); } return javaMethods; } - protected JavaMethod makeJavaMethod(Method method) { - return new JavaMethod(this, method); - } - public JavaMethodList getMethods(Class<? extends Annotation> type, JavaTestInterpreter interpreter) { return getMethods(new MethodAnnotation(type), interpreter); Index: TestClassMethodsRunner.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/TestClassMethodsRunner.java,v retrieving revision 1.3.2.3 retrieving revision 1.3.2.4 diff -u -d -r1.3.2.3 -r1.3.2.4 --- TestClassMethodsRunner.java 18 Oct 2006 17:07:05 -0000 1.3.2.3 +++ TestClassMethodsRunner.java 18 Oct 2006 18:22:14 -0000 1.3.2.4 @@ -1,6 +1,5 @@ package org.junit.internal.runners; -import java.lang.reflect.InvocationTargetException; import org.junit.Test; import org.junit.runner.Description; @@ -38,7 +37,7 @@ notifier.testAborted(getDescription(), new Exception( "No runnable methods")); for (JavaMethod method : fTestMethods) - invokeTestMethod(method, notifier); + method.invokeTestMethod(notifier, fInterpreter); } @Override @@ -50,22 +49,6 @@ return spec; } - private void invokeTestMethod(JavaMethod method, RunNotifier notifier) { - Object test; - try { - test= method.getJavaClass().newInstance(); - } catch (InvocationTargetException e) { - notifier.testAborted(method.description(), e.getCause()); - return; - } catch (Exception e) { - notifier.testAborted(method.description(), e); - return; - } - TestEnvironment testEnvironment= new TestEnvironment(fInterpreter, - new PerTestNotifier(notifier, method.description()), test); - testEnvironment.run(method); - } - public void filter(Filter filter) throws NoTestsRemainException { fTestMethods.filter(filter); } |