Update of /cvsroot/junit/junit/org/junit/internal/runners In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv7311/org/junit/internal/runners Modified Files: Tag: saff_r41_runner_refactoring JavaMethod.java TestClassMethodsRunner.java ErrorReportingRunner.java MethodAnnotation.java PerTestNotifier.java TestMethodRunner.java JavaTestInterpreter.java JavaModelElement.java MethodValidator.java JavaClass.java TestClassRunner.java Added Files: Tag: saff_r41_runner_refactoring JavaMethodList.java TestEnvironment.java FailedBefore.java Removed Files: Tag: saff_r41_runner_refactoring BeforeAndAfterRunner.java Log Message: BeforeAndAfterRunner is no more --- NEW FILE: JavaMethodList.java --- package org.junit.internal.runners; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import org.junit.runner.manipulation.Filter; import org.junit.runner.manipulation.NoTestsRemainException; import org.junit.runner.manipulation.Sorter; public class JavaMethodList extends ArrayList<JavaMethod> { private static final long serialVersionUID= 1L; void filter(Filter filter) throws NoTestsRemainException { for (Iterator<JavaMethod> iter= iterator(); iter.hasNext();) { JavaMethod method= iter.next(); if (!filter.shouldRun(method.description())) iter.remove(); } if (isEmpty()) throw new NoTestsRemainException(); } void filter(final Sorter sorter) { Collections.sort(this, new Comparator<JavaMethod>() { public int compare(JavaMethod o1, JavaMethod o2) { return sorter.compare(o1.description(), o2.description()); } }); } } --- NEW FILE: TestEnvironment.java --- /** * */ package org.junit.internal.runners; import java.lang.annotation.Annotation; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class TestEnvironment { private final JavaTestInterpreter fInterpreter; private final PerTestNotifier fNotifier; private final Object fTest; public TestEnvironment(JavaTestInterpreter interpreter, PerTestNotifier notifier, Object test) { fInterpreter= interpreter; fNotifier= notifier; fTest= test; } void runAfters(Class<? extends Annotation> afterAnnotation, JavaClass javaClass, Object test) { List<JavaMethod> afters= javaClass.getMethods(afterAnnotation, getInterpreter()); for (JavaMethod after : afters) { try { after.invoke(test); } catch (Throwable e) { addFailure(e); } } } public void runWithBeforeAndAfter(Runnable protectThis, JavaModelElement element) { // TODO: is this envious now? Yes try { runBefores(element, fTest); protectThis.run(); } catch (FailedBefore e) { } finally { runAfters(element.getAfterAnnotation(), element.getJavaClass(), fTest); } } private void runBefores(JavaModelElement element, Object test) throws FailedBefore { // TODO: envious of environment? try { List<JavaMethod> befores= element.getJavaClass().getMethods( element.getBeforeAnnotation(), getInterpreter()); // TODO: no auto-correct if wrong type on left side of new-style // for? for (JavaMethod before : befores) before.invoke(test); } catch (Throwable e) { addFailure(e); throw new FailedBefore(); } } void runWithoutTimeout(final JavaMethod javaMethod) { runWithBeforeAndAfter(new Runnable() { public void run() { javaMethod.runWithoutBeforeAndAfter(TestEnvironment.this, fTest); } }, javaMethod); } void runWithTimeout(long timeout, final JavaMethod method) { ExecutorService service= Executors.newSingleThreadExecutor(); Callable<Object> callable= new Callable<Object>() { public Object call() throws Exception { runWithoutTimeout(method); return null; } }; Future<Object> result= service.submit(callable); service.shutdown(); try { boolean terminated= service.awaitTermination(timeout, TimeUnit.MILLISECONDS); if (!terminated) service.shutdownNow(); result.get(timeout, TimeUnit.MILLISECONDS); // throws the exception // if one occurred // during the invocation } catch (TimeoutException e) { addFailure(new Exception(String.format( "test timed out after %d milliseconds", timeout))); } catch (Exception e) { addFailure(e); } } void addFailure(Throwable e) { fNotifier.addFailure(e); } void run(JavaMethod javaMethod) { if (javaMethod.isIgnored()) { fNotifier.fireTestIgnored(); return; } fNotifier.fireTestStarted(); try { long timeout= javaMethod.getTimeout(); if (timeout > 0) runWithTimeout(timeout, javaMethod); else runWithoutTimeout(javaMethod); } finally { fNotifier.fireTestFinished(); } } JavaTestInterpreter getInterpreter() { return fInterpreter; } } --- NEW FILE: FailedBefore.java --- package org.junit.internal.runners; public class FailedBefore extends Exception { private static final long serialVersionUID= 1L; } Index: JavaMethod.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaMethod.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 --- JavaMethod.java 17 Oct 2006 19:06:09 -0000 1.1.2.2 +++ JavaMethod.java 18 Oct 2006 17:07:05 -0000 1.1.2.3 @@ -9,15 +9,21 @@ import java.lang.reflect.Modifier; import java.util.List; +import org.junit.After; +import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.Test.None; +import org.junit.runner.Description; public class JavaMethod extends JavaModelElement { // TODO: push out private final Method fMethod; - public JavaMethod(Method current) { + private final JavaClass fJavaClass; + + public JavaMethod(JavaClass javaClass, Method current) { + fJavaClass= javaClass; fMethod= current; } @@ -25,8 +31,7 @@ Method previous= previousJavaMethod.fMethod; if (!previous.getName().equals(fMethod.getName())) return false; - if (previous.getParameterTypes().length != fMethod - .getParameterTypes().length) + if (previous.getParameterTypes().length != fMethod.getParameterTypes().length) return false; for (int i= 0; i < previous.getParameterTypes().length; i++) { if (!previous.getParameterTypes()[i].equals(fMethod @@ -69,16 +74,16 @@ } boolean isUnexpected(Throwable exception) { - return ! expectedException().isAssignableFrom(exception.getClass()); + return !expectedException().isAssignableFrom(exception.getClass()); } boolean expectsException() { return expectedException() != null; } - void invoke(Object object) throws IllegalAccessException, + public Object invoke(Object object) throws IllegalAccessException, InvocationTargetException { - fMethod.invoke(object); + return fMethod.invoke(object); } @Override @@ -90,12 +95,14 @@ Method each= fMethod; if (Modifier.isStatic(each.getModifiers()) != isStatic) { String state= isStatic ? "should" : "should not"; - errors.add(new Exception("Method " + each.getName() + "() " - + state + " be static")); + errors.add(new Exception("Method " + each.getName() + "() " + state + + " be static")); } if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) - errors.add(new Exception("Class " + each.getDeclaringClass().getName() - + " should be public")); + errors + .add(new Exception("Class " + + each.getDeclaringClass().getName() + + " should be public")); if (!Modifier.isPublic(each.getModifiers())) errors.add(new Exception("Method " + each.getName() + " should be public")); @@ -107,25 +114,59 @@ + " should have no parameters")); } - void runUnprotected(JavaTestInterpreter javaTestInterpreter, Object test, PerTestNotifier perTestNotifier) { + // TODO: push out + public Description description() { + return Description.createTestDescription(fJavaClass.getTestClass(), + getName()); + } + + public boolean isStatic() { + return Modifier.isStatic(getModifiers()); + } + + // TODO: sort methods + private int getModifiers() { + return fMethod.getModifiers(); + } + + public boolean isPublic() { + return Modifier.isPublic(getModifiers()); + } + + // TODO: push out + @Override + public JavaClass getJavaClass() { + return fJavaClass; + } + + @Override + public Class<? extends Annotation> getAfterAnnotation() { + return After.class; + } + + @Override + public Class<? extends Annotation> getBeforeAnnotation() { + return Before.class; + } + + public void runWithoutBeforeAndAfter(TestEnvironment environment, Object test) { try { - javaTestInterpreter.executeMethodBody(test, this); + environment.getInterpreter().executeMethodBody(test, this); if (expectsException()) - perTestNotifier.addFailure(new AssertionError( - "Expected exception: " - + expectedException().getName())); + environment.addFailure(new AssertionError("Expected exception: " + + expectedException().getName())); } catch (InvocationTargetException e) { Throwable actual= e.getTargetException(); if (!expectsException()) - perTestNotifier.addFailure(actual); + environment.addFailure(actual); else if (isUnexpected(actual)) { String message= "Unexpected exception, expected<" + expectedException().getName() + "> but was<" + actual.getClass().getName() + ">"; - perTestNotifier.addFailure(new Exception(message, actual)); + environment.addFailure(new Exception(message, actual)); } } catch (Throwable e) { - perTestNotifier.addFailure(e); + environment.addFailure(e); } } } \ No newline at end of file Index: TestClassMethodsRunner.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/TestClassMethodsRunner.java,v retrieving revision 1.3.2.2 retrieving revision 1.3.2.3 diff -u -d -r1.3.2.2 -r1.3.2.3 --- TestClassMethodsRunner.java 17 Oct 2006 19:07:09 -0000 1.3.2.2 +++ TestClassMethodsRunner.java 18 Oct 2006 17:07:05 -0000 1.3.2.3 @@ -1,11 +1,6 @@ package org.junit.internal.runners; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; import org.junit.Test; import org.junit.runner.Description; @@ -15,110 +10,67 @@ import org.junit.runner.manipulation.NoTestsRemainException; import org.junit.runner.manipulation.Sortable; import org.junit.runner.manipulation.Sorter; -import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; -public class TestClassMethodsRunner extends Runner implements Filterable, Sortable { - private final List<JavaMethod> fTestMethods; +public class TestClassMethodsRunner extends Runner implements Filterable, + Sortable { + private final JavaMethodList fTestMethods; + private final JavaClass fTestClass; + private final JavaTestInterpreter fInterpreter; - // This assumes that some containing runner will perform validation of the test methods - public TestClassMethodsRunner(JavaClass klass, JavaTestInterpreter javaTestInterpreter) { - + // This assumes that some containing runner will perform validation of the + // test methods + public TestClassMethodsRunner(JavaClass klass, + JavaTestInterpreter javaTestInterpreter) { // TODO: DUP? fTestClass= klass; - fTestMethods= klass.getTestMethods(Test.class); + fTestMethods= klass.getMethods(Test.class, javaTestInterpreter); fInterpreter= javaTestInterpreter; } // TODO: rename method to element? - + @Override public void run(RunNotifier notifier) { if (fTestMethods.isEmpty()) - testAborted(notifier, getDescription(), new Exception("No runnable methods")); + notifier.testAborted(getDescription(), new Exception( + "No runnable methods")); for (JavaMethod method : fTestMethods) invokeTestMethod(method, notifier); } - private void testAborted(RunNotifier notifier, Description description, Throwable cause) { - // TODO: duped! - // TODO: envious - notifier.fireTestStarted(description); - notifier.fireTestFailure(new Failure(description, cause)); - notifier.fireTestFinished(description); - } - @Override public Description getDescription() { - Description spec= Description.createSuiteDescription(getName()); + Description spec= Description.createSuiteDescription(fTestClass + .getName()); for (JavaMethod method : fTestMethods) - spec.addChild(methodDescription(method)); + spec.addChild(method.description()); return spec; } - protected Description methodDescription(Method method) { - return methodDescription(new JavaMethod(method)); - } - - protected String getName() { - return getTestClass().getName(); - } - - protected Object createTest() throws Exception { - return getTestClass().newTestObject(); - } - private void invokeTestMethod(JavaMethod method, RunNotifier notifier) { Object test; try { - test= createTest(); + test= method.getJavaClass().newInstance(); } catch (InvocationTargetException e) { - testAborted(notifier, methodDescription(method), e.getCause()); - return; + notifier.testAborted(method.description(), e.getCause()); + return; } catch (Exception e) { - testAborted(notifier, methodDescription(method), e); + notifier.testAborted(method.description(), e); return; } - createMethodRunner(test, method, notifier).run(); - } - - private TestMethodRunner createMethodRunner(Object test, JavaMethod method, RunNotifier notifier) { - return new TestMethodRunner(test, method, notifier, methodDescription(method), fInterpreter); - } - - protected String testName(JavaModelElement method) { - return method.getName(); - } - - private Description methodDescription(JavaMethod method) { - return describe(method, getTestClass()); - } - - private Description describe(JavaMethod method, JavaClass testClass) { - return Description.createTestDescription(testClass.getTestClass(), testName(method)); + TestEnvironment testEnvironment= new TestEnvironment(fInterpreter, + new PerTestNotifier(notifier, method.description()), test); + testEnvironment.run(method); } public void filter(Filter filter) throws NoTestsRemainException { - for (Iterator<JavaMethod> iter= fTestMethods.iterator(); iter.hasNext();) { - JavaMethod method= iter.next(); - if (!filter.shouldRun(methodDescription(method))) - iter.remove(); - } - if (fTestMethods.isEmpty()) - throw new NoTestsRemainException(); + fTestMethods.filter(filter); } public void sort(final Sorter sorter) { - Collections.sort(fTestMethods, new Comparator<JavaMethod>() { - public int compare(JavaMethod o1, JavaMethod o2) { - return sorter.compare(methodDescription(o1), methodDescription(o2)); - } - }); - } - - protected JavaClass getTestClass() { - return fTestClass; + fTestMethods.filter(sorter); } } \ No newline at end of file Index: ErrorReportingRunner.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/ErrorReportingRunner.java,v retrieving revision 1.3 retrieving revision 1.3.2.1 diff -u -d -r1.3 -r1.3.2.1 --- ErrorReportingRunner.java 25 Aug 2006 14:01:36 -0000 1.3 +++ ErrorReportingRunner.java 18 Oct 2006 17:07:05 -0000 1.3.2.1 @@ -3,7 +3,6 @@ import org.junit.runner.Description; import org.junit.runner.Runner; import org.junit.runner.notification.RunNotifier; -import org.junit.runner.notification.Failure; public class ErrorReportingRunner extends Runner { private final Description fDescription; @@ -20,11 +19,8 @@ return fDescription; } - // TODO: this is duplicated in TestClassMethodsRunner @Override public void run(RunNotifier notifier) { - notifier.fireTestStarted(fDescription); - notifier.fireTestFailure(new Failure(fDescription, fCause)); - notifier.fireTestFinished(fDescription); + notifier.testAborted(fDescription, fCause); } } \ No newline at end of file Index: MethodAnnotation.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/MethodAnnotation.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- MethodAnnotation.java 26 Sep 2006 16:51:39 -0000 1.1.2.1 +++ MethodAnnotation.java 18 Oct 2006 17:07:05 -0000 1.1.2.2 @@ -7,12 +7,12 @@ import org.junit.RunSuperclassMethodsFirst; -final class MethodAnnotation { +public class MethodAnnotation { // TODO: push out // TODO: package too big? private final Class<? extends Annotation> fAnnotation; - MethodAnnotation(Class<? extends Annotation> annotation) { + public MethodAnnotation(Class<? extends Annotation> annotation) { fAnnotation= annotation; } Index: PerTestNotifier.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/PerTestNotifier.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- PerTestNotifier.java 17 Oct 2006 19:03:52 -0000 1.1.2.1 +++ PerTestNotifier.java 18 Oct 2006 17:07:05 -0000 1.1.2.2 @@ -1,12 +1,14 @@ package org.junit.internal.runners; +import java.lang.reflect.InvocationTargetException; + import org.junit.runner.Description; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; public class PerTestNotifier { // TODO: push out - + private final RunNotifier fNotifier; private final Description fDescription; @@ -17,6 +19,11 @@ } public void addFailure(Throwable targetException) { + if (targetException instanceof InvocationTargetException) { + InvocationTargetException i= (InvocationTargetException) targetException; + addFailure(i.getTargetException()); + return; + } fNotifier.fireTestFailure(new Failure(fDescription, targetException)); } Index: TestMethodRunner.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/TestMethodRunner.java,v retrieving revision 1.2.2.1 retrieving revision 1.2.2.2 diff -u -d -r1.2.2.1 -r1.2.2.2 --- TestMethodRunner.java 17 Oct 2006 19:08:09 -0000 1.2.2.1 +++ TestMethodRunner.java 18 Oct 2006 17:07:05 -0000 1.2.2.2 @@ -1,83 +1,18 @@ package org.junit.internal.runners; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import org.junit.After; -import org.junit.Before; -import org.junit.runner.Description; -import org.junit.runner.notification.RunNotifier; - -public class TestMethodRunner extends BeforeAndAfterRunner { - private final Object fTest; +public class TestMethodRunner { private final JavaMethod fJavaMethod; - private final JavaTestInterpreter fInterpreter; + private final TestEnvironment fEnvironment; - public TestMethodRunner(Object test, JavaMethod method, - RunNotifier notifier, Description description, - JavaTestInterpreter interpreter) { - super(test.getClass(), Before.class, After.class, test, - new PerTestNotifier(notifier, description)); - fTest= test; + public TestMethodRunner(JavaMethod method, TestEnvironment environment) { + fEnvironment= environment; fJavaMethod= method; - fInterpreter= interpreter; } public void run() { - if (fJavaMethod.isIgnored()) { - fPerTestNotifier.fireTestIgnored(); - return; - } - fPerTestNotifier.fireTestStarted(); - try { - long timeout= fJavaMethod.getTimeout(); - if (timeout > 0) - runWithTimeout(timeout); - else - runMethod(); - } finally { - fPerTestNotifier.fireTestFinished(); - } - } - - private void runWithTimeout(long timeout) { - ExecutorService service= Executors.newSingleThreadExecutor(); - Callable<Object> callable= new Callable<Object>() { - public Object call() throws Exception { - runMethod(); - return null; - } - }; - Future<Object> result= service.submit(callable); - service.shutdown(); - try { - boolean terminated= service.awaitTermination(timeout, - TimeUnit.MILLISECONDS); - if (!terminated) - service.shutdownNow(); - result.get(timeout, TimeUnit.MILLISECONDS); // throws the exception - // if one occurred - // during the invocation - } catch (TimeoutException e) { - fPerTestNotifier.addFailure(new Exception(String.format( - "test timed out after %d milliseconds", timeout))); - } catch (Exception e) { - fPerTestNotifier.addFailure(e); - } - } - - private void runMethod() { - runProtected(); - } - - @Override - protected void runUnprotected() { - fJavaMethod.runUnprotected(fInterpreter, fTest, fPerTestNotifier); + fEnvironment.run(fJavaMethod); } } Index: JavaTestInterpreter.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaTestInterpreter.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- JavaTestInterpreter.java 17 Oct 2006 19:00:57 -0000 1.1.2.1 +++ JavaTestInterpreter.java 18 Oct 2006 17:07:05 -0000 1.1.2.2 @@ -3,8 +3,13 @@ import java.lang.reflect.InvocationTargetException; 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) @@ -14,4 +19,8 @@ javaMethod.invoke(test); } + public JavaClass interpretJavaClass(Class<?> superclass) { + return new JavaClass(superclass); + } + } Index: JavaModelElement.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaModelElement.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- JavaModelElement.java 17 Oct 2006 19:06:09 -0000 1.1.2.1 +++ JavaModelElement.java 18 Oct 2006 17:07:05 -0000 1.1.2.2 @@ -1,5 +1,13 @@ package org.junit.internal.runners; +import java.lang.annotation.Annotation; + public abstract class JavaModelElement { public abstract String getName(); + + public abstract Class<? extends Annotation> getBeforeAnnotation(); + + public abstract Class<? extends Annotation> getAfterAnnotation(); + + public abstract JavaClass getJavaClass(); } Index: MethodValidator.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/MethodValidator.java,v retrieving revision 1.3.2.1 retrieving revision 1.3.2.2 diff -u -d -r1.3.2.1 -r1.3.2.2 --- MethodValidator.java 17 Oct 2006 19:06:32 -0000 1.3.2.1 +++ MethodValidator.java 18 Oct 2006 17:07:05 -0000 1.3.2.2 @@ -49,7 +49,7 @@ private void validateTestMethods(Class<? extends Annotation> annotation, boolean isStatic) { - List<JavaMethod> methods= fJavaClass.getTestMethods(annotation); + List<JavaMethod> methods= fJavaClass.getMethods(annotation, new JavaTestInterpreter()); for (JavaMethod eachMethod : methods) { eachMethod.validateAsTestMethod(isStatic, fErrors); } Index: JavaClass.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/Attic/JavaClass.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 --- JavaClass.java 17 Oct 2006 19:06:09 -0000 1.1.2.2 +++ JavaClass.java 18 Oct 2006 17:07:05 -0000 1.1.2.3 @@ -10,6 +10,9 @@ import java.util.Collections; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; + public class JavaClass extends JavaModelElement { // TODO: push out private final Class<?> fClass; @@ -18,22 +21,26 @@ fClass= type; } - public List<JavaClass> getSuperClasses() { + public List<JavaClass> getSuperClasses(JavaTestInterpreter interpreter) { ArrayList<JavaClass> results= new ArrayList<JavaClass>(); - Class<?> current= fClass; - while (current != null) { - results.add(new JavaClass(current)); - current= current.getSuperclass(); - } + 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)); return results; } - List<JavaMethod> getTestMethods(MethodAnnotation methodAnnotation) { - List<JavaMethod> results= new ArrayList<JavaMethod>(); - for (JavaClass eachClass : getSuperClasses()) { + public JavaMethodList getMethods(MethodAnnotation methodAnnotation, + JavaTestInterpreter interpreter) { + JavaMethodList results= new JavaMethodList(); + for (JavaClass eachClass : getSuperClasses(interpreter)) { for (JavaMethod eachMethod : eachClass.getDeclaredMethods()) { - Annotation annotation= eachMethod.getAnnotation(methodAnnotation); - if (annotation != null && ! eachMethod.isShadowedBy(results)) + Annotation annotation= eachMethod + .getAnnotation(methodAnnotation); + if (annotation != null && !eachMethod.isShadowedBy(results)) results.add(eachMethod); } } @@ -46,13 +53,18 @@ Method[] declaredMethods= fClass.getDeclaredMethods(); ArrayList<JavaMethod> javaMethods= new ArrayList<JavaMethod>(); for (Method method : declaredMethods) { - javaMethods.add(new JavaMethod(method)); + javaMethods.add(makeJavaMethod(method)); } return javaMethods; } - List<JavaMethod> getTestMethods(Class<? extends Annotation> type) { - return getTestMethods(new MethodAnnotation(type)); + 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); } public Class getTestClass() { @@ -63,7 +75,9 @@ try { getTestClass().getConstructor(); } catch (Exception e) { - errors.add(new Exception("Test class should have public zero-argument constructor", e)); + errors.add(new Exception( + "Test class should have public zero-argument constructor", + e)); } } @@ -72,9 +86,24 @@ return fClass.getName(); } - Object newTestObject() - throws InstantiationException, IllegalAccessException, - InvocationTargetException, NoSuchMethodException { + protected Object newInstance() throws InstantiationException, + IllegalAccessException, InvocationTargetException, + NoSuchMethodException { return getTestClass().getConstructor().newInstance(); } + + @Override + public Class<? extends Annotation> getAfterAnnotation() { + return AfterClass.class; + } + + @Override + public Class<? extends Annotation> getBeforeAnnotation() { + return BeforeClass.class; + } + + @Override + public JavaClass getJavaClass() { + return this; + } } \ No newline at end of file Index: TestClassRunner.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/internal/runners/TestClassRunner.java,v retrieving revision 1.2.2.1 retrieving revision 1.2.2.2 diff -u -d -r1.2.2.1 -r1.2.2.2 --- TestClassRunner.java 17 Oct 2006 19:07:40 -0000 1.2.2.1 +++ TestClassRunner.java 18 Oct 2006 17:07:05 -0000 1.2.2.2 @@ -1,7 +1,5 @@ package org.junit.internal.runners; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.runner.Description; import org.junit.runner.Runner; import org.junit.runner.manipulation.Filter; @@ -15,13 +13,15 @@ protected final Runner fEnclosedRunner; private final Class<?> fTestClass; - + 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, JavaTestInterpreter interpreter) + throws InitializationError { + this(klass, new TestClassMethodsRunner(new JavaClass(klass), + interpreter)); } public TestClassRunner(Class<?> klass, Runner runner) @@ -40,16 +40,17 @@ @Override public void run(final RunNotifier notifier) { - BeforeAndAfterRunner runner= new BeforeAndAfterRunner(getTestClass(), - BeforeClass.class, AfterClass.class, null, new PerTestNotifier(notifier, - getDescription())) { - @Override - protected void runUnprotected() { + Runnable protectThis= new Runnable() { + public void run() { fEnclosedRunner.run(notifier); } }; - runner.runProtected(); + TestEnvironment environment= new TestEnvironment( + new JavaTestInterpreter(), new PerTestNotifier(notifier, + getDescription()), null); + environment.runWithBeforeAndAfter(protectThis, new JavaClass( + getTestClass())); } @Override --- BeforeAndAfterRunner.java DELETED --- |