From 8bed6c9278517d9fb13f274bc5a6ade358a39745 Mon Sep 17 00:00:00 2001 From: dsaff Date: Wed, 14 Nov 2007 18:21:30 +0000 Subject: [PATCH] - Moved InitializationError to ParentRunner, since it was only used by subclasses of ParentRunner. - Broke up TestMethod into FrameworkMethod (which makes it more clear that these methods can also be Before, After, etc.), and TestAnnotation (for specific information only available on the @Test annotation). - Created TestMethodElement to encapsulate the relationship between @Test, @Before, and @After. This class may go away again quickly - Updated version in docs to 4.5 - Included docs about junit-dep jar --- README.html | 34 ++++--- .../junit/experimental/theories/Theories.java | 99 +++++++++++-------- .../internal/runners/JUnit4ClassRunner.java | 56 ++++++----- .../junit/internal/runners/ParentRunner.java | 8 ++ .../internal/runners/TestMethodElement.java | 29 ++++++ .../internal/runners/links/InvokeMethod.java | 6 +- .../internal/runners/links/RunAfters.java | 6 +- .../{TestMethod.java => FrameworkMethod.java} | 69 +++---------- .../runners/model/InitializationError.java | 3 + .../runners/model/TestAnnotation.java | 29 ++++++ .../internal/runners/model/TestClass.java | 18 ++-- .../internal/runners/model/TestElement.java | 12 +-- src/org/junit/runner/Runner.java | 8 -- src/org/junit/runners/Parameterized.java | 12 +-- .../extendingwithstubs/StubbedTheories.java | 6 +- .../extension/TestMethodInterfaceTest.java | 9 +- 16 files changed, 225 insertions(+), 179 deletions(-) create mode 100644 src/org/junit/internal/runners/TestMethodElement.java rename src/org/junit/internal/runners/model/{TestMethod.java => FrameworkMethod.java} (51%) create mode 100644 src/org/junit/internal/runners/model/TestAnnotation.java diff --git a/README.html b/README.html index e92372370c7b..3535696ea480 100644 --- a/README.html +++ b/README.html @@ -4,13 +4,13 @@ - JUnit 4.4 + JUnit 4.5

JUnit -4.4

+4.5
Brought to you by Kent Beck, Erich Gamma, and David Saff. @@ -19,7 +19,7 @@


(see also JUnit.org)
-
18 July 2007 +
[old date] 18 July 2007

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

    @@ -42,7 +42,7 @@

-

Summary of Changes in version 4.4

+

Summary of Changes in version 4.5

JUnit is designed to efficiently capture developers' intentions about their code, and quickly check their code matches those intentions. @@ -122,6 +122,9 @@

assertThat

we have decided to include the classes from hamcrest-core, from the
Hamcrest project. This is the first time that third-party classes have been included in JUnit.

+
  • To allow developers to maintain full control of the classpath contents, the JUnit distribution also provides an unbundled junit-dep jar, +ie without hamcrest-core classes included. This is intended for situations when using other libraries that also depend on hamcrest-core, to +avoid classloading conflicts or issues. Developers using junit-dep should ensure a compatible version of hamcrest-core jar (ie 1.1+) is present in the classpath.

  • JUnit currently ships with a few matchers, defined in org.hamcrest.CoreMatchers and org.junit.matchers.JUnitMatchers.
    To use many, many more, consider downloading the full hamcrest package.

  • @@ -360,13 +363,20 @@

    -junit-4.4.jar +junit-4.5.jar -a jar file with the JUnit framework +a jar file with the JUnit framework, bundled with the hamcrest-core-1.1 dependency. -junit-4.4-src.jar +junit-dep-4.5.jar + +a jar file with the JUnit framework, unbundled from any external dependencies. +Choosing to use this jar developers will need to also provide in the classpath a compatible version of external dependencies (ie hamcrest-core-1.1+) + + + +junit-4.5-src.jar a jar file with the source code of the JUnit framework @@ -407,20 +417,20 @@

    Below are the installation steps for installing JUnit:
    1. -unzip the junit4.4.zip file
    2. +unzip the junit4.5.zip file
    3. -add junit-4.4.jar to the CLASSPATH. For example: - set classpath=%classpath%;INSTALL_DIR\junit-4.4.jar;INSTALL_DIR
    4. +add junit-4.5.jar to the CLASSPATH. For example: + set classpath=%classpath%;INSTALL_DIR\junit-4.5.jar;INSTALL_DIR
    5. test the installation by running java org.junit.runner.JUnitCore org.junit.tests.AllTests.

    6. Notice: that the tests are not -contained in the junit-4.4.jar but in the installation directory directly. +contained in the junit-4.5.jar but in the installation directory directly. Therefore make sure that the installation directory is on the class path
    -Important: don't install junit-4.4.jar +Important: don't install junit-4.5.jar into the extension directory of your JDK installation. If you do so the test class on the files system will not be found.

    diff --git a/src/org/junit/experimental/theories/Theories.java b/src/org/junit/experimental/theories/Theories.java index 421de03d1571..b970c599880d 100644 --- a/src/org/junit/experimental/theories/Theories.java +++ b/src/org/junit/experimental/theories/Theories.java @@ -15,7 +15,7 @@ import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.internal.runners.links.Statement; import org.junit.internal.runners.model.InitializationError; -import org.junit.internal.runners.model.TestMethod; +import org.junit.internal.runners.model.FrameworkMethod; @SuppressWarnings("restriction") public class Theories extends JUnit4ClassRunner { @@ -28,36 +28,35 @@ protected void collectInitializationErrors(List errors) { } @Override - protected List computeTestMethods() { + protected List computeTestMethods() { // TODO: (Jul 20, 2007 2:02:44 PM) Only get methods once, even if they // have both @Test and @Theory - List testMethods= super.computeTestMethods(); + List testMethods= super.computeTestMethods(); testMethods.addAll(getTestClass().getAnnotatedMethods(Theory.class)); return testMethods; } @Override - public Statement childBlock(final TestMethod method) { + public Statement childBlock(final FrameworkMethod method) { return new TheoryAnchor(method); } public class TheoryAnchor extends Statement { private int successes= 0; - private TestMethod fTestMethod; + private FrameworkMethod fTestMethod; private List fInvalidParameters= new ArrayList(); - public TheoryAnchor(TestMethod method) { + public TheoryAnchor(FrameworkMethod method) { fTestMethod= method; } @Override public void evaluate() throws Throwable { runWithAssignment(Assignments.allUnassigned( - fTestMethod.getMethod(), fTestMethod.getTestClass() - .getJavaClass())); + fTestMethod.getMethod(), fTestClass.getJavaClass())); if (successes == 0) Assert @@ -65,7 +64,8 @@ public void evaluate() throws Throwable { + fInvalidParameters); } - protected void runWithAssignment(Assignments parameterAssignment) throws Throwable { + protected void runWithAssignment(Assignments parameterAssignment) + throws Throwable { if (!parameterAssignment.isComplete()) { runWithIncompleteAssignment(parameterAssignment); } else { @@ -73,17 +73,18 @@ protected void runWithAssignment(Assignments parameterAssignment) throws Throwab } } - protected void runWithIncompleteAssignment(Assignments incomplete) throws InstantiationException, - IllegalAccessException, Throwable { + protected void runWithIncompleteAssignment(Assignments incomplete) + throws InstantiationException, IllegalAccessException, + Throwable { for (PotentialAssignment source : incomplete .potentialsForNextUnassigned()) { runWithAssignment(incomplete.assignNext(source)); } } - protected void runWithCompleteAssignment(final Assignments complete) throws InstantiationException, - IllegalAccessException, InvocationTargetException, - NoSuchMethodException, Throwable { + protected void runWithCompleteAssignment(final Assignments complete) + throws InstantiationException, IllegalAccessException, + InvocationTargetException, NoSuchMethodException, Throwable { try { new JUnit4ClassRunner(getTestClass().getJavaClass()) { @Override @@ -92,65 +93,75 @@ protected void collectInitializationErrors( // TODO: (Oct 12, 2007 12:08:03 PM) DUP // do nothing } - + @Override - public Statement childBlock(TestMethod method) { + public Statement childBlock(FrameworkMethod method) { // TODO: (Oct 12, 2007 2:00:52 PM) Name this Link final Statement link= super.childBlock(method); return new Statement() { - + @Override public void evaluate() throws Throwable { try { link.evaluate(); successes++; } catch (AssumptionViolatedException e) { - // TODO: (Oct 12, 2007 2:07:01 PM) DUP? even correct? + // TODO: (Oct 12, 2007 2:07:01 PM) DUP? even + // correct? // do nothing } catch (Throwable e) { - // TODO: (Oct 12, 2007 2:04:01 PM) nullsOk as argument to Assignments constructor + // TODO: (Oct 12, 2007 2:04:01 PM) nullsOk + // as argument to Assignments constructor - reportParameterizedError(e, complete.getAllArguments(nullsOk())); - } + reportParameterizedError(e, complete + .getAllArguments(nullsOk())); + } } - + }; } - + @Override - protected Statement invoke(TestMethod method, Object test) { + protected Statement invoke(FrameworkMethod method, Object test) { // TODO: (Oct 12, 2007 12:07:28 PM) push method in return methodCompletesWithParameters(complete, test); } - + @Override - protected Statement ignoreViolatedAssumptions(final Statement next) { + protected Statement ignoreViolatedAssumptions( + final Statement next) { // TODO: (Oct 12, 2007 2:15:02 PM) name this return new Statement() { - + @Override public void evaluate() throws Throwable { try { - next.evaluate(); + next.evaluate(); } catch (AssumptionViolatedException e) { - // TODO: (Oct 12, 2007 2:19:52 PM) This feels hacky + // TODO: (Oct 12, 2007 2:19:52 PM) This + // feels hacky successes--; handleAssumptionViolation(e); - // TODO: (Oct 12, 2007 2:15:44 PM) Can I remove other calls? + // TODO: (Oct 12, 2007 2:15:44 PM) Can I + // remove other calls? } } }; } - + @Override public Object createTest() throws Exception { // TODO: (Oct 12, 2007 12:31:12 PM) DUP - // TODO: (Oct 12, 2007 12:40:33 PM) honor assumption violations in JUnit4ClassRunner constructor invocations + // TODO: (Oct 12, 2007 12:40:33 PM) honor assumption + // violations in JUnit4ClassRunner constructor + // invocations - return getTestClass().getJavaClass().getConstructors()[0].newInstance(complete.getConstructorArguments(nullsOk())); + return getTestClass().getJavaClass().getConstructors()[0] + .newInstance(complete + .getConstructorArguments(nullsOk())); } }.childBlock(fTestMethod).evaluate(); } catch (AssumptionViolatedException e) { @@ -160,16 +171,17 @@ public Object createTest() throws Exception { } } - private Statement methodCompletesWithParameters(final Assignments complete, - final Object freshInstance) { + private Statement methodCompletesWithParameters( + final Assignments complete, final Object freshInstance) { return new Statement() { @Override public void evaluate() throws Throwable { try { invokeWithActualParameters(freshInstance, complete); } catch (CouldNotGenerateValueException e) { - // ignore - // TODO: (Oct 12, 2007 9:58:11 AM) Do I ignore this elsewhere? + // ignore + // TODO: (Oct 12, 2007 9:58:11 AM) Do I ignore this + // elsewhere? } } }; @@ -177,12 +189,13 @@ public void evaluate() throws Throwable { private void invokeWithActualParameters(Object target, Assignments complete) throws Throwable { - final Object[] values= complete.getMethodArguments(nullsOk(), target); -// try { - fTestMethod.invokeExplosively(target, values); -// } catch (AssumptionViolatedException e) { -// handleAssumptionViolation(e); -// } + final Object[] values= complete.getMethodArguments(nullsOk(), + target); + // try { + fTestMethod.invokeExplosively(target, values); + // } catch (AssumptionViolatedException e) { + // handleAssumptionViolation(e); + // } } protected void handleAssumptionViolation(AssumptionViolatedException e) { diff --git a/src/org/junit/internal/runners/JUnit4ClassRunner.java b/src/org/junit/internal/runners/JUnit4ClassRunner.java index 59491e347507..8f4167e47b89 100644 --- a/src/org/junit/internal/runners/JUnit4ClassRunner.java +++ b/src/org/junit/internal/runners/JUnit4ClassRunner.java @@ -20,7 +20,8 @@ import org.junit.internal.runners.model.EachTestNotifier; import org.junit.internal.runners.model.InitializationError; import org.junit.internal.runners.model.ReflectiveCallable; -import org.junit.internal.runners.model.TestMethod; +import org.junit.internal.runners.model.TestAnnotation; +import org.junit.internal.runners.model.FrameworkMethod; import org.junit.runner.Description; import org.junit.runner.manipulation.Filter; import org.junit.runner.manipulation.Filterable; @@ -29,8 +30,8 @@ import org.junit.runner.manipulation.Sorter; import org.junit.runner.notification.RunNotifier; -public class JUnit4ClassRunner extends ParentRunner implements Filterable, Sortable { - protected final List fTestMethods; +public class JUnit4ClassRunner extends ParentRunner implements Filterable, Sortable { + protected final List fTestMethods; public JUnit4ClassRunner(Class klass) throws InitializationError { super(klass); @@ -38,7 +39,7 @@ public JUnit4ClassRunner(Class klass) throws InitializationError { validate(); } - protected List computeTestMethods() { + protected List computeTestMethods() { return fTestClass.getTestMethods(); } @@ -57,13 +58,13 @@ protected Statement classBlock(final RunNotifier notifier) { return new Statement() { @Override public void evaluate() { - for (TestMethod method : getChildren()) + for (FrameworkMethod method : getChildren()) runChild(method, notifier); } }; } - protected void runChild(TestMethod method, RunNotifier notifier) { + protected void runChild(FrameworkMethod method, RunNotifier notifier) { Description description= describeChild(method); EachTestNotifier eachNotifier= new EachTestNotifier(notifier, description); @@ -75,16 +76,16 @@ public Object createTest() throws Exception { } @Override - protected Description describeChild(TestMethod method) { + protected Description describeChild(FrameworkMethod method) { return Description.createTestDescription(fTestClass.getJavaClass(), testName(method), method.getMethod().getAnnotations()); } - protected String testName(TestMethod method) { + protected String testName(FrameworkMethod method) { return method.getName(); } - public Statement childBlock(TestMethod method) { + public Statement childBlock(FrameworkMethod method) { Object test; try { // TODO: (Oct 12, 2007 11:49:18 AM) Can I ditch reflective callable? @@ -99,16 +100,18 @@ protected Object runReflectiveCall() throws Throwable { return new Fail(e); } + TestAnnotation annotation= new TestAnnotation(method); + Statement link= invoke(method, test); - link= possiblyExpectingExceptions(method, link); - link= withPotentialTimeout(method, link); + link= possiblyExpectingExceptions(annotation, link); + link= withPotentialTimeout(annotation, link); link= withBefores(method, test, link); link= ignoreViolatedAssumptions(link); link= withAfters(method, test, link); return link; } - protected Statement invoke(TestMethod method, Object test) { + protected Statement invoke(FrameworkMethod method, Object test) { return new InvokeMethod(method, test); } @@ -116,37 +119,38 @@ protected Statement ignoreViolatedAssumptions(Statement next) { return new IgnoreViolatedAssumptions(next); } - protected Statement possiblyExpectingExceptions(TestMethod method, + private Statement possiblyExpectingExceptions(TestAnnotation annotation, Statement next) { - return method.expectsException() ? new ExpectException(next, method + return annotation.expectsException() ? new ExpectException(next, annotation .getExpectedException()) : next; } - protected Statement withPotentialTimeout(TestMethod method, Statement next) { - long timeout= method.getTimeout(); + protected Statement withPotentialTimeout(TestAnnotation annotation, + Statement next) { + long timeout= annotation.getTimeout(); return timeout > 0 ? new FailOnTimeout(next, timeout) : next; } - protected Statement withAfters(TestMethod method, Object target, + protected Statement withAfters(FrameworkMethod method, Object target, Statement link) { // TODO: (Oct 12, 2007 10:23:59 AM) Check for DUP in callers - return new RunAfters(link, method, target); + return new RunAfters(link, new TestMethodElement(getTestClass()), target); } - protected Statement withBefores(TestMethod method, Object target, + protected Statement withBefores(FrameworkMethod method, Object target, Statement link) { - return new RunBefores(link, method, target); + return new RunBefores(link, new TestMethodElement(getTestClass()), target); } - protected Notifier notifying(TestMethod method, Statement link) { + protected Notifier notifying(FrameworkMethod method, Statement link) { return method.isIgnored() ? new IgnoreTestNotifier() : new RunTestNotifier(link); } public void filter(Filter filter) throws NoTestsRemainException { - for (Iterator iter= fTestMethods.iterator(); iter.hasNext();) { - TestMethod method= iter.next(); + for (Iterator iter= fTestMethods.iterator(); iter.hasNext();) { + FrameworkMethod method= iter.next(); if (!filter.shouldRun(describeChild(method))) iter.remove(); } @@ -155,8 +159,8 @@ public void filter(Filter filter) throws NoTestsRemainException { } public void sort(final Sorter sorter) { - Collections.sort(fTestMethods, new Comparator() { - public int compare(TestMethod o1, TestMethod o2) { + Collections.sort(fTestMethods, new Comparator() { + public int compare(FrameworkMethod o1, FrameworkMethod o2) { return sorter.compare(describeChild(o1), describeChild(o2)); } @@ -164,7 +168,7 @@ public int compare(TestMethod o1, TestMethod o2) { } @Override - protected List getChildren() { + protected List getChildren() { return fTestMethods; } } diff --git a/src/org/junit/internal/runners/ParentRunner.java b/src/org/junit/internal/runners/ParentRunner.java index 19728848dbef..54a38bb0435e 100644 --- a/src/org/junit/internal/runners/ParentRunner.java +++ b/src/org/junit/internal/runners/ParentRunner.java @@ -8,6 +8,7 @@ import org.junit.internal.runners.links.RunBefores; import org.junit.internal.runners.links.Statement; import org.junit.internal.runners.model.EachTestNotifier; +import org.junit.internal.runners.model.InitializationError; import org.junit.internal.runners.model.TestClass; import org.junit.runner.Description; import org.junit.runner.Runner; @@ -62,4 +63,11 @@ private Annotation[] classAnnotations() { protected String getName() { return fTestClass.getName(); } + + // TODO: (Nov 14, 2007 11:04:54 AM) sort methods + + protected void assertValid(List errors) throws InitializationError { + if (!errors.isEmpty()) + throw new InitializationError(errors); + } } \ No newline at end of file diff --git a/src/org/junit/internal/runners/TestMethodElement.java b/src/org/junit/internal/runners/TestMethodElement.java new file mode 100644 index 000000000000..42247e4f0e47 --- /dev/null +++ b/src/org/junit/internal/runners/TestMethodElement.java @@ -0,0 +1,29 @@ +package org.junit.internal.runners; + +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.internal.runners.model.TestClass; +import org.junit.internal.runners.model.TestElement; +import org.junit.internal.runners.model.FrameworkMethod; + +// TODO: (Nov 14, 2007 11:49:52 AM) Does this belong in this package? + +public class TestMethodElement extends TestElement { + private TestClass fTestClass; + + public TestMethodElement(TestClass testClass) { + fTestClass = testClass; + } + + @Override + public List getBefores() { + return fTestClass.getAnnotatedMethods(Before.class); + } + + @Override + public List getAfters() { + return fTestClass.getAnnotatedMethods(After.class); + } +} diff --git a/src/org/junit/internal/runners/links/InvokeMethod.java b/src/org/junit/internal/runners/links/InvokeMethod.java index efd26ec4136a..c53831bb8e42 100644 --- a/src/org/junit/internal/runners/links/InvokeMethod.java +++ b/src/org/junit/internal/runners/links/InvokeMethod.java @@ -3,13 +3,13 @@ */ package org.junit.internal.runners.links; -import org.junit.internal.runners.model.TestMethod; +import org.junit.internal.runners.model.FrameworkMethod; public class InvokeMethod extends Statement { - private final TestMethod fTestMethod; + private final FrameworkMethod fTestMethod; private Object fTarget; - public InvokeMethod(TestMethod testMethod, Object target) { + public InvokeMethod(FrameworkMethod testMethod, Object target) { fTestMethod= testMethod; fTarget= target; } diff --git a/src/org/junit/internal/runners/links/RunAfters.java b/src/org/junit/internal/runners/links/RunAfters.java index d10f292ac662..a1c39b4133ae 100644 --- a/src/org/junit/internal/runners/links/RunAfters.java +++ b/src/org/junit/internal/runners/links/RunAfters.java @@ -7,7 +7,7 @@ import org.junit.internal.runners.model.MultipleFailureException; import org.junit.internal.runners.model.TestElement; -import org.junit.internal.runners.model.TestMethod; +import org.junit.internal.runners.model.FrameworkMethod; public class RunAfters extends Statement { private final Statement fNext; @@ -30,8 +30,8 @@ public void evaluate() throws Throwable { } catch (Throwable e) { errors.add(e); } finally { - List afters= fElement.getAfters(); - for (TestMethod each : afters) + List afters= fElement.getAfters(); + for (FrameworkMethod each : afters) try { each.invokeExplosively(fTarget); } catch (Throwable e) { diff --git a/src/org/junit/internal/runners/model/TestMethod.java b/src/org/junit/internal/runners/model/FrameworkMethod.java similarity index 51% rename from src/org/junit/internal/runners/model/TestMethod.java rename to src/org/junit/internal/runners/model/FrameworkMethod.java index d9a9e6ccc646..d0bfc78bd71f 100644 --- a/src/org/junit/internal/runners/model/TestMethod.java +++ b/src/org/junit/internal/runners/model/FrameworkMethod.java @@ -4,64 +4,26 @@ 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; -// TODO: (Oct 8, 2007 1:58:24 PM) extract InvokedMethod (which can be Befores, Afters, Tests, etc.) -// This should be just @Test methods (and @Theory) - -public class TestMethod extends TestElement { - private final TestClass fTestClass; +public class FrameworkMethod { private final Method fMethod; - public TestMethod(Method method, TestClass testClass) { - fMethod = method; - fTestClass= testClass; - } - - public Class getExpectedException() { - Test annotation= fMethod.getAnnotation(Test.class); - if (annotation == null || annotation.expected() == None.class) - return null; - else - return annotation.expected(); - } - - public boolean expectsException() { - return getExpectedException() != null; - } - - public long getTimeout() { - Test annotation= fMethod.getAnnotation(Test.class); - if (annotation == null) - return 0; - long timeout= annotation.timeout(); - return timeout; + public FrameworkMethod(Method method) { + fMethod= method; } public boolean isIgnored() { return fMethod.getAnnotation(Ignore.class) != null; } - - @Override - public List getBefores() { - return fTestClass.getAnnotatedMethods(Before.class); - } - - @Override - public List getAfters() { - return fTestClass.getAnnotatedMethods(After.class); - } - public Method getMethod() { + public Method getMethod() { return fMethod; } - public Object invokeExplosively(final Object target, final Object... params) throws Throwable { - return new ReflectiveCallable() { + public Object invokeExplosively(final Object target, final Object... params) + throws Throwable { + return new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return fMethod.invoke(target, params); @@ -84,7 +46,8 @@ public void validate(boolean isStatic, List errors) { + state + " be static")); } if (!Modifier.isPublic(fMethod.getDeclaringClass().getModifiers())) - errors.add(new Exception("Class " + fMethod.getDeclaringClass().getName() + errors.add(new Exception("Class " + + fMethod.getDeclaringClass().getName() + " should be public")); if (!Modifier.isPublic(fMethod.getModifiers())) errors.add(new Exception("Method " + fMethod.getName() @@ -97,26 +60,22 @@ public void validate(boolean isStatic, List errors) { + " should have no parameters")); } - public boolean isShadowedBy(TestMethod each) { - if (! each.getName().equals(getName())) + public boolean isShadowedBy(FrameworkMethod each) { + if (!each.getName().equals(getName())) return false; if (each.getParameterTypes().length != getParameterTypes().length) return false; for (int i= 0; i < each.getParameterTypes().length; i++) { - if (! each.getParameterTypes()[i].equals(getParameterTypes()[i])) + if (!each.getParameterTypes()[i].equals(getParameterTypes()[i])) return false; } return true; } - boolean isShadowedBy(List results) { - for (TestMethod each : results) + boolean isShadowedBy(List results) { + for (FrameworkMethod each : results) if (isShadowedBy(each)) return true; return false; } - - public TestClass getTestClass() { - return fTestClass; - } } \ No newline at end of file diff --git a/src/org/junit/internal/runners/model/InitializationError.java b/src/org/junit/internal/runners/model/InitializationError.java index 599c33fc617d..456919421e4a 100644 --- a/src/org/junit/internal/runners/model/InitializationError.java +++ b/src/org/junit/internal/runners/model/InitializationError.java @@ -3,6 +3,9 @@ import java.util.Arrays; import java.util.List; +// TODO: (Nov 14, 2007 12:03:52 PM) Does this belong here? + + public class InitializationError extends Exception { private static final long serialVersionUID= 1L; private final List fErrors; diff --git a/src/org/junit/internal/runners/model/TestAnnotation.java b/src/org/junit/internal/runners/model/TestAnnotation.java new file mode 100644 index 000000000000..9c2654474626 --- /dev/null +++ b/src/org/junit/internal/runners/model/TestAnnotation.java @@ -0,0 +1,29 @@ +package org.junit.internal.runners.model; + +import org.junit.Test; +import org.junit.Test.None; + +public class TestAnnotation { + public Test fAnnotation; + + public TestAnnotation(FrameworkMethod method) { + fAnnotation= method.getMethod().getAnnotation(Test.class); + } + + public Class getExpectedException() { + if (fAnnotation == null || fAnnotation.expected() == None.class) + return null; + else + return fAnnotation.expected(); + } + + public boolean expectsException() { + return getExpectedException() != null; + } + + public long getTimeout() { + if (fAnnotation == null) + return 0; + return fAnnotation.timeout(); + } +} \ No newline at end of file diff --git a/src/org/junit/internal/runners/model/TestClass.java b/src/org/junit/internal/runners/model/TestClass.java index e381c0293892..f6a509fc8e1d 100644 --- a/src/org/junit/internal/runners/model/TestClass.java +++ b/src/org/junit/internal/runners/model/TestClass.java @@ -20,29 +20,29 @@ public TestClass(Class klass) { fClass= klass; } - public List getTestMethods() { + public List getTestMethods() { return getAnnotatedMethods(Test.class); } @Override - public List getBefores() { + public List getBefores() { return getAnnotatedMethods(BeforeClass.class); } @Override - public List getAfters() { + public List getAfters() { return getAnnotatedMethods(AfterClass.class); } - public List getAnnotatedMethods( + public List getAnnotatedMethods( Class annotationClass) { - List results= new ArrayList(); + List results= new ArrayList(); for (Class eachClass : getSuperClasses(fClass)) { Method[] methods= eachClass.getDeclaredMethods(); for (Method eachMethod : methods) { Annotation annotation= eachMethod .getAnnotation(annotationClass); - TestMethod testMethod= new TestMethod(eachMethod, this); + FrameworkMethod testMethod= new FrameworkMethod(eachMethod); if (annotation != null && !testMethod.isShadowedBy(results)) results.add(testMethod); } @@ -82,9 +82,9 @@ public String getName() { public void validateMethods(Class annotation, boolean isStatic, List errors) { - List methods= getAnnotatedMethods(annotation); + List methods= getAnnotatedMethods(annotation); - for (TestMethod eachTestMethod : methods) { + for (FrameworkMethod eachTestMethod : methods) { eachTestMethod.validate(isStatic, errors); } } @@ -109,7 +109,7 @@ public void validateInstanceMethods(List errors) { validateMethods(Before.class, false, errors); validateMethods(Test.class, false, errors); - List methods= getAnnotatedMethods(Test.class); + List methods= getAnnotatedMethods(Test.class); if (methods.size() == 0) errors.add(new Exception("No runnable methods")); } diff --git a/src/org/junit/internal/runners/model/TestElement.java b/src/org/junit/internal/runners/model/TestElement.java index 8092acb42ad2..4a63c662186e 100644 --- a/src/org/junit/internal/runners/model/TestElement.java +++ b/src/org/junit/internal/runners/model/TestElement.java @@ -3,21 +3,21 @@ import java.util.List; public abstract class TestElement { - public abstract List getAfters(); + public abstract List getAfters(); - public abstract List getBefores(); + public abstract List getBefores(); public void runBefores(Object target) throws Throwable { - List befores= getBefores(); - for (TestMethod before : befores) + List befores= getBefores(); + for (FrameworkMethod before : befores) before.invokeExplosively(target); } public void runAfters(Object target) throws Throwable { MultipleFailureException errors= new MultipleFailureException(); - List afters= getAfters(); - for (TestMethod after : afters) + List afters= getAfters(); + for (FrameworkMethod after : afters) try { after.invokeExplosively(target); } catch (Throwable e) { diff --git a/src/org/junit/runner/Runner.java b/src/org/junit/runner/Runner.java index c84aea823434..39e424f83ac7 100644 --- a/src/org/junit/runner/Runner.java +++ b/src/org/junit/runner/Runner.java @@ -1,8 +1,5 @@ package org.junit.runner; -import java.util.List; - -import org.junit.internal.runners.model.InitializationError; import org.junit.runner.notification.RunNotifier; /** @@ -40,9 +37,4 @@ public abstract class Runner implements Describable { public int testCount() { return getDescription().testCount(); } - - protected void assertValid(List errors) throws InitializationError { - if (!errors.isEmpty()) - throw new InitializationError(errors); - } } \ No newline at end of file diff --git a/src/org/junit/runners/Parameterized.java b/src/org/junit/runners/Parameterized.java index 29f6c5d98a5a..f6497fce8060 100644 --- a/src/org/junit/runners/Parameterized.java +++ b/src/org/junit/runners/Parameterized.java @@ -14,7 +14,7 @@ import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.internal.runners.model.InitializationError; import org.junit.internal.runners.model.TestClass; -import org.junit.internal.runners.model.TestMethod; +import org.junit.internal.runners.model.FrameworkMethod; import org.junit.runner.manipulation.Filterable; import org.junit.runner.notification.RunNotifier; @@ -79,7 +79,7 @@ protected String getName() { } @Override - protected String testName(final TestMethod method) { + protected String testName(final FrameworkMethod method) { return String.format("%s[%s]", method.getName(), fParameterSetNumber); } @@ -93,7 +93,7 @@ protected void collectInitializationErrors(List errors) { public void run(RunNotifier notifier) { // TODO: (Nov 5, 2007 9:57:48 AM) use blocks - for (TestMethod method : fTestMethods) + for (FrameworkMethod method : fTestMethods) runChild(method, notifier); } } @@ -139,10 +139,10 @@ private List getParametersList() throws Throwable { null); } - private TestMethod getParametersMethod() throws Exception { - List methods= fTestClass + private FrameworkMethod getParametersMethod() throws Exception { + List methods= fTestClass .getAnnotatedMethods(Parameters.class); - for (TestMethod each : methods) { + for (FrameworkMethod each : methods) { int modifiers= each.getMethod().getModifiers(); if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) return each; diff --git a/src/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java b/src/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java index b535f3391ac2..1481b821ea4d 100644 --- a/src/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java +++ b/src/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java @@ -10,7 +10,7 @@ import org.junit.experimental.theories.internal.Assignments; import org.junit.internal.runners.links.Statement; import org.junit.internal.runners.model.InitializationError; -import org.junit.internal.runners.model.TestMethod; +import org.junit.internal.runners.model.FrameworkMethod; public class StubbedTheories extends Theories { public StubbedTheories(Class klass) throws InitializationError { @@ -18,12 +18,12 @@ public StubbedTheories(Class klass) throws InitializationError { } @Override - public Statement childBlock(TestMethod method) { + public Statement childBlock(FrameworkMethod method) { return new StubbedTheoryAnchor(method); } public class StubbedTheoryAnchor extends TheoryAnchor { - public StubbedTheoryAnchor(TestMethod method) { + public StubbedTheoryAnchor(FrameworkMethod method) { super(method); } diff --git a/src/org/junit/tests/extension/TestMethodInterfaceTest.java b/src/org/junit/tests/extension/TestMethodInterfaceTest.java index aa8916d58b41..2d44c68805ae 100644 --- a/src/org/junit/tests/extension/TestMethodInterfaceTest.java +++ b/src/org/junit/tests/extension/TestMethodInterfaceTest.java @@ -5,8 +5,8 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.internal.runners.TestMethodElement; import org.junit.internal.runners.model.TestClass; -import org.junit.internal.runners.model.TestMethod; public class TestMethodInterfaceTest { public static class BeforesAndAfters { @@ -24,10 +24,9 @@ public void test() { } @Test - public void getBeforesIsPublic() throws SecurityException, - NoSuchMethodException { - TestMethod testMethod= new TestMethod(BeforesAndAfters.class - .getMethod("test"), new TestClass(BeforesAndAfters.class)); + public void getBeforesIsPublic() throws SecurityException { + TestMethodElement testMethod= new TestMethodElement(new TestClass( + BeforesAndAfters.class)); assertThat(testMethod.getBefores().size(), is(1)); assertThat(testMethod.getAfters().size(), is(1)); }