diff options
author | Paul Duffin <paulduffin@google.com> | 2016-12-14 11:13:37 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2016-12-15 18:53:12 +0000 |
commit | 4dd042caba6f0ee54f604a409df7152b3e8205bb (patch) | |
tree | fd9aa7f11b8c8f927c9e98fd828e60086895ca5e /src/main/java/org/junit/internal | |
parent | 50db5f5810104e1dd0b0294145e9d3e602bb2627 (diff) | |
download | junit-4dd042caba6f0ee54f604a409df7152b3e8205bb.tar.gz |
Moved source to match upstream file structure
Will make it simpler to update JUnit source.
Bug: 33613916
Test: make checkbuild
Change-Id: I76984a6defd3e40f34eea995e6ed865d32d53da3
Diffstat (limited to 'src/main/java/org/junit/internal')
48 files changed, 2238 insertions, 0 deletions
diff --git a/src/main/java/org/junit/internal/ArrayComparisonFailure.java b/src/main/java/org/junit/internal/ArrayComparisonFailure.java new file mode 100644 index 0000000..08851de --- /dev/null +++ b/src/main/java/org/junit/internal/ArrayComparisonFailure.java @@ -0,0 +1,59 @@ +package org.junit.internal; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; + +/** + * Thrown when two array elements differ + * @see Assert#assertArrayEquals(String, Object[], Object[]) + */ +public class ArrayComparisonFailure extends AssertionError { + + private static final long serialVersionUID= 1L; + + private List<Integer> fIndices= new ArrayList<Integer>(); + private final String fMessage; + private final AssertionError fCause; + + /** + * Construct a new <code>ArrayComparisonFailure</code> with an error text and the array's + * dimension that was not equal + * @param cause the exception that caused the array's content to fail the assertion test + * @param index the array position of the objects that are not equal. + * @see Assert#assertArrayEquals(String, Object[], Object[]) + */ + public ArrayComparisonFailure(String message, AssertionError cause, int index) { + fMessage= message; + fCause= cause; + addDimension(index); + } + + public void addDimension(int index) { + fIndices.add(0, index); + } + + @Override + public String getMessage() { + StringBuilder builder= new StringBuilder(); + if (fMessage != null) + builder.append(fMessage); + builder.append("arrays first differed at element "); + for (int each : fIndices) { + builder.append("["); + builder.append(each); + builder.append("]"); + } + builder.append("; "); + builder.append(fCause.getMessage()); + return builder.toString(); + } + + /** + * {@inheritDoc} + */ + @Override public String toString() { + return getMessage(); + } +} diff --git a/src/main/java/org/junit/internal/AssumptionViolatedException.java b/src/main/java/org/junit/internal/AssumptionViolatedException.java new file mode 100644 index 0000000..8e11268 --- /dev/null +++ b/src/main/java/org/junit/internal/AssumptionViolatedException.java @@ -0,0 +1,40 @@ +package org.junit.internal; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.SelfDescribing; +import org.hamcrest.StringDescription; + +public class AssumptionViolatedException extends RuntimeException implements SelfDescribing { + private static final long serialVersionUID= 1L; + + private final Object fValue; + + private final Matcher<?> fMatcher; + + public AssumptionViolatedException(Object value, Matcher<?> matcher) { + super(value instanceof Throwable ? (Throwable) value : null); + fValue= value; + fMatcher= matcher; + } + + public AssumptionViolatedException(String assumption) { + this(assumption, null); + } + + @Override + public String getMessage() { + return StringDescription.asString(this); + } + + public void describeTo(Description description) { + if (fMatcher != null) { + description.appendText("got: "); + description.appendValue(fValue); + description.appendText(", expected: "); + description.appendDescriptionOf(fMatcher); + } else { + description.appendText("failed assumption: " + fValue); + } + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/ComparisonCriteria.java b/src/main/java/org/junit/internal/ComparisonCriteria.java new file mode 100644 index 0000000..e97011d --- /dev/null +++ b/src/main/java/org/junit/internal/ComparisonCriteria.java @@ -0,0 +1,76 @@ +package org.junit.internal; + +import java.lang.reflect.Array; + +import org.junit.Assert; + +/** + * Defines criteria for finding two items "equal enough". Concrete subclasses + * may demand exact equality, or, for example, equality within a given delta. + */ +public abstract class ComparisonCriteria { + /** + * Asserts that two arrays are equal, according to the criteria defined by + * the concrete subclass. If they are not, an {@link AssertionError} is + * thrown with the given message. If <code>expecteds</code> and + * <code>actuals</code> are <code>null</code>, they are considered equal. + * + * @param message + * the identifying message for the {@link AssertionError} ( + * <code>null</code> okay) + * @param expecteds + * Object array or array of arrays (multi-dimensional array) with + * expected values. + * @param actuals + * Object array or array of arrays (multi-dimensional array) with + * actual values + */ + public void arrayEquals(String message, Object expecteds, Object actuals) + throws ArrayComparisonFailure { + if (expecteds == actuals) + return; + String header= message == null ? "" : message + ": "; + + int expectedsLength= assertArraysAreSameLength(expecteds, + actuals, header); + + for (int i= 0; i < expectedsLength; i++) { + Object expected= Array.get(expecteds, i); + Object actual= Array.get(actuals, i); + + if (isArray(expected) && isArray(actual)) { + try { + arrayEquals(message, expected, actual); + } catch (ArrayComparisonFailure e) { + e.addDimension(i); + throw e; + } + } else + try { + assertElementsEqual(expected, actual); + } catch (AssertionError e) { + throw new ArrayComparisonFailure(header, e, i); + } + } + } + + private boolean isArray(Object expected) { + return expected != null && expected.getClass().isArray(); + } + + private int assertArraysAreSameLength(Object expecteds, + Object actuals, String header) { + if (expecteds == null) + Assert.fail(header + "expected array was null"); + if (actuals == null) + Assert.fail(header + "actual array was null"); + int actualsLength= Array.getLength(actuals); + int expectedsLength= Array.getLength(expecteds); + if (actualsLength != expectedsLength) + Assert.fail(header + "array lengths differed, expected.length=" + + expectedsLength + " actual.length=" + actualsLength); + return expectedsLength; + } + + protected abstract void assertElementsEqual(Object expected, Object actual); +} diff --git a/src/main/java/org/junit/internal/ExactComparisonCriteria.java b/src/main/java/org/junit/internal/ExactComparisonCriteria.java new file mode 100644 index 0000000..0a632ff --- /dev/null +++ b/src/main/java/org/junit/internal/ExactComparisonCriteria.java @@ -0,0 +1,10 @@ +package org.junit.internal; + +import org.junit.Assert; + +public class ExactComparisonCriteria extends ComparisonCriteria { + @Override + protected void assertElementsEqual(Object expected, Object actual) { + Assert.assertEquals(expected, actual); + } +} diff --git a/src/main/java/org/junit/internal/InexactComparisonCriteria.java b/src/main/java/org/junit/internal/InexactComparisonCriteria.java new file mode 100644 index 0000000..ef3d7ff --- /dev/null +++ b/src/main/java/org/junit/internal/InexactComparisonCriteria.java @@ -0,0 +1,19 @@ +package org.junit.internal; + +import org.junit.Assert; + +public class InexactComparisonCriteria extends ComparisonCriteria { + public double fDelta; + + public InexactComparisonCriteria(double delta) { + fDelta= delta; + } + + @Override + protected void assertElementsEqual(Object expected, Object actual) { + if (expected instanceof Double) + Assert.assertEquals((Double)expected, (Double)actual, fDelta); + else + Assert.assertEquals((Float)expected, (Float)actual, fDelta); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/JUnitSystem.java b/src/main/java/org/junit/internal/JUnitSystem.java new file mode 100644 index 0000000..6d9c242 --- /dev/null +++ b/src/main/java/org/junit/internal/JUnitSystem.java @@ -0,0 +1,8 @@ +package org.junit.internal; + +import java.io.PrintStream; + +public interface JUnitSystem { + void exit(int i); + PrintStream out(); +} diff --git a/src/main/java/org/junit/internal/RealSystem.java b/src/main/java/org/junit/internal/RealSystem.java new file mode 100644 index 0000000..1067c6d --- /dev/null +++ b/src/main/java/org/junit/internal/RealSystem.java @@ -0,0 +1,15 @@ +package org.junit.internal; + +import java.io.PrintStream; + +public class RealSystem implements JUnitSystem { + + public void exit(int code) { + System.exit(code); + } + + public PrintStream out() { + return System.out; + } + +} diff --git a/src/main/java/org/junit/internal/TextListener.java b/src/main/java/org/junit/internal/TextListener.java new file mode 100644 index 0000000..2b1c679 --- /dev/null +++ b/src/main/java/org/junit/internal/TextListener.java @@ -0,0 +1,98 @@ +package org.junit.internal; + +import java.io.PrintStream; +import java.text.NumberFormat; +import java.util.List; + +import org.junit.runner.Description; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunListener; + +public class TextListener extends RunListener { + + private final PrintStream fWriter; + + public TextListener(JUnitSystem system) { + this(system.out()); + } + + public TextListener(PrintStream writer) { + this.fWriter= writer; + } + + @Override + public void testRunFinished(Result result) { + printHeader(result.getRunTime()); + printFailures(result); + printFooter(result); + } + + @Override + public void testStarted(Description description) { + fWriter.append('.'); + } + + @Override + public void testFailure(Failure failure) { + fWriter.append('E'); + } + + @Override + public void testIgnored(Description description) { + fWriter.append('I'); + } + + /* + * Internal methods + */ + + private PrintStream getWriter() { + return fWriter; + } + + protected void printHeader(long runTime) { + getWriter().println(); + getWriter().println("Time: " + elapsedTimeAsString(runTime)); + } + + protected void printFailures(Result result) { + List<Failure> failures= result.getFailures(); + if (failures.size() == 0) + return; + if (failures.size() == 1) + getWriter().println("There was " + failures.size() + " failure:"); + else + getWriter().println("There were " + failures.size() + " failures:"); + int i= 1; + for (Failure each : failures) + printFailure(each, "" + i++); + } + + protected void printFailure(Failure each, String prefix) { + getWriter().println(prefix + ") " + each.getTestHeader()); + getWriter().print(each.getTrace()); + } + + protected void printFooter(Result result) { + if (result.wasSuccessful()) { + getWriter().println(); + getWriter().print("OK"); + getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")"); + + } else { + getWriter().println(); + getWriter().println("FAILURES!!!"); + getWriter().println("Tests run: " + result.getRunCount() + ", Failures: " + result.getFailureCount()); + } + getWriter().println(); + } + + /** + * Returns the formatted string of the elapsed time. Duplicated from + * BaseTestRunner. Fix it. + */ + protected String elapsedTimeAsString(long runTime) { + return NumberFormat.getInstance().format((double) runTime / 1000); + } +} diff --git a/src/main/java/org/junit/internal/builders/AllDefaultPossibilitiesBuilder.java b/src/main/java/org/junit/internal/builders/AllDefaultPossibilitiesBuilder.java new file mode 100644 index 0000000..d3bd50a --- /dev/null +++ b/src/main/java/org/junit/internal/builders/AllDefaultPossibilitiesBuilder.java @@ -0,0 +1,57 @@ +/** + * + */ +package org.junit.internal.builders; + +import java.util.Arrays; +import java.util.List; + +import org.junit.runner.Runner; +import org.junit.runners.model.RunnerBuilder; + +public class AllDefaultPossibilitiesBuilder extends RunnerBuilder { + private final boolean fCanUseSuiteMethod; + + public AllDefaultPossibilitiesBuilder(boolean canUseSuiteMethod) { + fCanUseSuiteMethod= canUseSuiteMethod; + } + + @Override + public Runner runnerForClass(Class<?> testClass) throws Throwable { + List<RunnerBuilder> builders= Arrays.asList( + ignoredBuilder(), + annotatedBuilder(), + suiteMethodBuilder(), + junit3Builder(), + junit4Builder()); + + for (RunnerBuilder each : builders) { + Runner runner= each.safeRunnerForClass(testClass); + if (runner != null) + return runner; + } + return null; + } + + protected JUnit4Builder junit4Builder() { + return new JUnit4Builder(); + } + + protected JUnit3Builder junit3Builder() { + return new JUnit3Builder(); + } + + protected AnnotatedBuilder annotatedBuilder() { + return new AnnotatedBuilder(this); + } + + protected IgnoredBuilder ignoredBuilder() { + return new IgnoredBuilder(); + } + + protected RunnerBuilder suiteMethodBuilder() { + if (fCanUseSuiteMethod) + return new SuiteMethodBuilder(); + return new NullBuilder(); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/builders/AnnotatedBuilder.java b/src/main/java/org/junit/internal/builders/AnnotatedBuilder.java new file mode 100644 index 0000000..8ed9ca7 --- /dev/null +++ b/src/main/java/org/junit/internal/builders/AnnotatedBuilder.java @@ -0,0 +1,45 @@ +/** + * + */ +package org.junit.internal.builders; + +import org.junit.runner.RunWith; +import org.junit.runner.Runner; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.RunnerBuilder; + +public class AnnotatedBuilder extends RunnerBuilder { + private static final String CONSTRUCTOR_ERROR_FORMAT= "Custom runner class %s should have a public constructor with signature %s(Class testClass)"; + + private RunnerBuilder fSuiteBuilder; + + public AnnotatedBuilder(RunnerBuilder suiteBuilder) { + fSuiteBuilder= suiteBuilder; + } + + @Override + public Runner runnerForClass(Class<?> testClass) throws Exception { + RunWith annotation= testClass.getAnnotation(RunWith.class); + if (annotation != null) + return buildRunner(annotation.value(), testClass); + return null; + } + + public Runner buildRunner(Class<? extends Runner> runnerClass, + Class<?> testClass) throws Exception { + try { + return runnerClass.getConstructor(Class.class).newInstance( + new Object[] { testClass }); + } catch (NoSuchMethodException e) { + try { + return runnerClass.getConstructor(Class.class, + RunnerBuilder.class).newInstance( + new Object[] { testClass, fSuiteBuilder }); + } catch (NoSuchMethodException e2) { + String simpleName= runnerClass.getSimpleName(); + throw new InitializationError(String.format( + CONSTRUCTOR_ERROR_FORMAT, simpleName, simpleName)); + } + } + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/builders/IgnoredBuilder.java b/src/main/java/org/junit/internal/builders/IgnoredBuilder.java new file mode 100644 index 0000000..6be342c --- /dev/null +++ b/src/main/java/org/junit/internal/builders/IgnoredBuilder.java @@ -0,0 +1,17 @@ +/** + * + */ +package org.junit.internal.builders; + +import org.junit.Ignore; +import org.junit.runner.Runner; +import org.junit.runners.model.RunnerBuilder; + +public class IgnoredBuilder extends RunnerBuilder { + @Override + public Runner runnerForClass(Class<?> testClass) { + if (testClass.getAnnotation(Ignore.class) != null) + return new IgnoredClassRunner(testClass); + return null; + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/builders/IgnoredClassRunner.java b/src/main/java/org/junit/internal/builders/IgnoredClassRunner.java new file mode 100644 index 0000000..b4200a8 --- /dev/null +++ b/src/main/java/org/junit/internal/builders/IgnoredClassRunner.java @@ -0,0 +1,26 @@ +/** + * + */ +package org.junit.internal.builders; + +import org.junit.runner.Description; +import org.junit.runner.Runner; +import org.junit.runner.notification.RunNotifier; + +public class IgnoredClassRunner extends Runner { + private final Class<?> fTestClass; + + public IgnoredClassRunner(Class<?> testClass) { + fTestClass= testClass; + } + + @Override + public void run(RunNotifier notifier) { + notifier.fireTestIgnored(getDescription()); + } + + @Override + public Description getDescription() { + return Description.createSuiteDescription(fTestClass); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/builders/JUnit3Builder.java b/src/main/java/org/junit/internal/builders/JUnit3Builder.java new file mode 100644 index 0000000..ddb070b --- /dev/null +++ b/src/main/java/org/junit/internal/builders/JUnit3Builder.java @@ -0,0 +1,21 @@ +/** + * + */ +package org.junit.internal.builders; + +import org.junit.internal.runners.JUnit38ClassRunner; +import org.junit.runner.Runner; +import org.junit.runners.model.RunnerBuilder; + +public class JUnit3Builder extends RunnerBuilder { + @Override + public Runner runnerForClass(Class<?> testClass) throws Throwable { + if (isPre4Test(testClass)) + return new JUnit38ClassRunner(testClass); + return null; + } + + boolean isPre4Test(Class<?> testClass) { + return junit.framework.TestCase.class.isAssignableFrom(testClass); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/builders/JUnit4Builder.java b/src/main/java/org/junit/internal/builders/JUnit4Builder.java new file mode 100644 index 0000000..4380db7 --- /dev/null +++ b/src/main/java/org/junit/internal/builders/JUnit4Builder.java @@ -0,0 +1,15 @@ +/** + * + */ +package org.junit.internal.builders; + +import org.junit.runner.Runner; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.RunnerBuilder; + +public class JUnit4Builder extends RunnerBuilder { + @Override + public Runner runnerForClass(Class<?> testClass) throws Throwable { + return new BlockJUnit4ClassRunner(testClass); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/builders/NullBuilder.java b/src/main/java/org/junit/internal/builders/NullBuilder.java new file mode 100644 index 0000000..9d43d69 --- /dev/null +++ b/src/main/java/org/junit/internal/builders/NullBuilder.java @@ -0,0 +1,14 @@ +/** + * + */ +package org.junit.internal.builders; + +import org.junit.runner.Runner; +import org.junit.runners.model.RunnerBuilder; + +public class NullBuilder extends RunnerBuilder { + @Override + public Runner runnerForClass(Class<?> each) throws Throwable { + return null; + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/builders/SuiteMethodBuilder.java b/src/main/java/org/junit/internal/builders/SuiteMethodBuilder.java new file mode 100644 index 0000000..659bf31 --- /dev/null +++ b/src/main/java/org/junit/internal/builders/SuiteMethodBuilder.java @@ -0,0 +1,26 @@ +/** + * + */ +package org.junit.internal.builders; + +import org.junit.internal.runners.SuiteMethod; +import org.junit.runner.Runner; +import org.junit.runners.model.RunnerBuilder; + +public class SuiteMethodBuilder extends RunnerBuilder { + @Override + public Runner runnerForClass(Class<?> each) throws Throwable { + if (hasSuiteMethod(each)) + return new SuiteMethod(each); + return null; + } + + public boolean hasSuiteMethod(Class<?> testClass) { + try { + testClass.getMethod("suite"); + } catch (NoSuchMethodException e) { + return false; + } + return true; + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/matchers/CombinableMatcher.java b/src/main/java/org/junit/internal/matchers/CombinableMatcher.java new file mode 100644 index 0000000..e9e6947 --- /dev/null +++ b/src/main/java/org/junit/internal/matchers/CombinableMatcher.java @@ -0,0 +1,34 @@ +package org.junit.internal.matchers; + +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.anyOf; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +public class CombinableMatcher<T> extends BaseMatcher<T> { + + private final Matcher<? extends T> fMatcher; + + public CombinableMatcher(Matcher<? extends T> matcher) { + fMatcher= matcher; + } + + public boolean matches(Object item) { + return fMatcher.matches(item); + } + + public void describeTo(Description description) { + description.appendDescriptionOf(fMatcher); + } + + @SuppressWarnings("unchecked") + public CombinableMatcher<T> and(Matcher<? extends T> matcher) { + return new CombinableMatcher<T>(allOf(matcher, fMatcher)); + } + + @SuppressWarnings("unchecked") + public CombinableMatcher<T> or(Matcher<? extends T> matcher) { + return new CombinableMatcher<T>(anyOf(matcher, fMatcher)); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/matchers/Each.java b/src/main/java/org/junit/internal/matchers/Each.java new file mode 100644 index 0000000..527db3b --- /dev/null +++ b/src/main/java/org/junit/internal/matchers/Each.java @@ -0,0 +1,24 @@ +package org.junit.internal.matchers; + +import static org.hamcrest.CoreMatchers.not; +import static org.junit.internal.matchers.IsCollectionContaining.hasItem; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +public class Each { + public static <T> Matcher<Iterable<T>> each(final Matcher<T> individual) { + final Matcher<Iterable<T>> allItemsAre = not(hasItem(not(individual))); + + return new BaseMatcher<Iterable<T>>() { + public boolean matches(Object item) { + return allItemsAre.matches(item); + } + + public void describeTo(Description description) { + description.appendText("each "); + individual.describeTo(description); + } + }; + } +} diff --git a/src/main/java/org/junit/internal/matchers/IsCollectionContaining.java b/src/main/java/org/junit/internal/matchers/IsCollectionContaining.java new file mode 100644 index 0000000..4436a83 --- /dev/null +++ b/src/main/java/org/junit/internal/matchers/IsCollectionContaining.java @@ -0,0 +1,67 @@ +package org.junit.internal.matchers; + +import static org.hamcrest.core.AllOf.allOf; +import static org.hamcrest.core.IsEqual.equalTo; + +import java.util.ArrayList; +import java.util.Collection; + +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; + +// Copied (hopefully temporarily) from hamcrest-library +public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> { + private final Matcher<? extends T> elementMatcher; + + public IsCollectionContaining(Matcher<? extends T> elementMatcher) { + this.elementMatcher = elementMatcher; + } + + @Override + public boolean matchesSafely(Iterable<T> collection) { + for (T item : collection) { + if (elementMatcher.matches(item)){ + return true; + } + } + return false; + } + + public void describeTo(Description description) { + description + .appendText("a collection containing ") + .appendDescriptionOf(elementMatcher); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher) { + return new IsCollectionContaining<T>(elementMatcher); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItem(T element) { + return hasItem(equalTo(element)); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItems(Matcher<? extends T>... elementMatchers) { + Collection<Matcher<? extends Iterable<T>>> all + = new ArrayList<Matcher<? extends Iterable<T>>>(elementMatchers.length); + for (Matcher<? extends T> elementMatcher : elementMatchers) { + all.add(hasItem(elementMatcher)); + } + return allOf(all); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItems(T... elements) { + Collection<Matcher<? extends Iterable<T>>> all + = new ArrayList<Matcher<? extends Iterable<T>>>(elements.length); + for (T element : elements) { + all.add(hasItem(element)); + } + return allOf(all); + } + +} diff --git a/src/main/java/org/junit/internal/matchers/StringContains.java b/src/main/java/org/junit/internal/matchers/StringContains.java new file mode 100644 index 0000000..e5f5334 --- /dev/null +++ b/src/main/java/org/junit/internal/matchers/StringContains.java @@ -0,0 +1,31 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.junit.internal.matchers; + +import org.hamcrest.Factory; +import org.hamcrest.Matcher; + +/** + * Tests if the argument is a string that contains a substring. + */ +public class StringContains extends SubstringMatcher { + public StringContains(String substring) { + super(substring); + } + + @Override + protected boolean evalSubstringOf(String s) { + return s.indexOf(substring) >= 0; + } + + @Override + protected String relationship() { + return "containing"; + } + + @Factory + public static Matcher<String> containsString(String substring) { + return new StringContains(substring); + } + +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/matchers/SubstringMatcher.java b/src/main/java/org/junit/internal/matchers/SubstringMatcher.java new file mode 100644 index 0000000..1c65240 --- /dev/null +++ b/src/main/java/org/junit/internal/matchers/SubstringMatcher.java @@ -0,0 +1,28 @@ +package org.junit.internal.matchers; + +import org.hamcrest.Description; + +public abstract class SubstringMatcher extends TypeSafeMatcher<String> { + + protected final String substring; + + protected SubstringMatcher(final String substring) { + this.substring = substring; + } + + @Override + public boolean matchesSafely(String item) { + return evalSubstringOf(item); + } + + public void describeTo(Description description) { + description.appendText("a string ") + .appendText(relationship()) + .appendText(" ") + .appendValue(substring); + } + + protected abstract boolean evalSubstringOf(String string); + + protected abstract String relationship(); +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/matchers/TypeSafeMatcher.java b/src/main/java/org/junit/internal/matchers/TypeSafeMatcher.java new file mode 100644 index 0000000..794a174 --- /dev/null +++ b/src/main/java/org/junit/internal/matchers/TypeSafeMatcher.java @@ -0,0 +1,60 @@ +package org.junit.internal.matchers; + +import java.lang.reflect.Method; + +import org.hamcrest.BaseMatcher; + +/** + * Convenient base class for Matchers that require a non-null value of a specific type. + * This simply implements the null check, checks the type and then casts. + * + * @author Joe Walnes + */ +public abstract class TypeSafeMatcher<T> extends BaseMatcher<T> { + + private Class<?> expectedType; + + /** + * Subclasses should implement this. The item will already have been checked for + * the specific type and will never be null. + */ + public abstract boolean matchesSafely(T item); + + protected TypeSafeMatcher() { + expectedType = findExpectedType(getClass()); + } + + private static Class<?> findExpectedType(Class<?> fromClass) { + for (Class<?> c = fromClass; c != Object.class; c = c.getSuperclass()) { + for (Method method : c.getDeclaredMethods()) { + if (isMatchesSafelyMethod(method)) { + return method.getParameterTypes()[0]; + } + } + } + + throw new Error("Cannot determine correct type for matchesSafely() method."); + } + + private static boolean isMatchesSafelyMethod(Method method) { + return method.getName().equals("matchesSafely") + && method.getParameterTypes().length == 1 + && !method.isSynthetic(); + } + + protected TypeSafeMatcher(Class<T> expectedType) { + this.expectedType = expectedType; + } + + /** + * Method made final to prevent accidental override. + * If you need to override this, there's no point on extending TypeSafeMatcher. + * Instead, extend the {@link BaseMatcher}. + */ + @SuppressWarnings({"unchecked"}) + public final boolean matches(Object item) { + return item != null + && expectedType.isInstance(item) + && matchesSafely((T) item); + } +} diff --git a/src/main/java/org/junit/internal/requests/ClassRequest.java b/src/main/java/org/junit/internal/requests/ClassRequest.java new file mode 100644 index 0000000..53bf520 --- /dev/null +++ b/src/main/java/org/junit/internal/requests/ClassRequest.java @@ -0,0 +1,26 @@ +package org.junit.internal.requests; + + +import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; +import org.junit.runner.Request; +import org.junit.runner.Runner; + +public class ClassRequest extends Request { + private final Class<?> fTestClass; + + private boolean fCanUseSuiteMethod; + + public ClassRequest(Class<?> testClass, boolean canUseSuiteMethod) { + fTestClass= testClass; + fCanUseSuiteMethod= canUseSuiteMethod; + } + + public ClassRequest(Class<?> testClass) { + this(testClass, true); + } + + @Override + public Runner getRunner() { + return new AllDefaultPossibilitiesBuilder(fCanUseSuiteMethod).safeRunnerForClass(fTestClass); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/requests/FilterRequest.java b/src/main/java/org/junit/internal/requests/FilterRequest.java new file mode 100644 index 0000000..e5d98d1 --- /dev/null +++ b/src/main/java/org/junit/internal/requests/FilterRequest.java @@ -0,0 +1,42 @@ +/** + * + */ +package org.junit.internal.requests; + +import org.junit.internal.runners.ErrorReportingRunner; +import org.junit.runner.Request; +import org.junit.runner.Runner; +import org.junit.runner.manipulation.Filter; +import org.junit.runner.manipulation.NoTestsRemainException; + +/** + * A filtered {@link Request}. + */ +public final class FilterRequest extends Request { + private final Request fRequest; + private final Filter fFilter; + + /** + * Creates a filtered Request + * @param classRequest a {@link Request} describing your Tests + * @param filter {@link Filter} to apply to the Tests described in + * <code>classRequest</code> + */ + public FilterRequest(Request classRequest, Filter filter) { + fRequest= classRequest; + fFilter= filter; + } + + @Override + public Runner getRunner() { + try { + Runner runner= fRequest.getRunner(); + fFilter.apply(runner); + return runner; + } catch (NoTestsRemainException e) { + return new ErrorReportingRunner(Filter.class, new Exception(String + .format("No tests found matching %s from %s", fFilter + .describe(), fRequest.toString()))); + } + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/requests/SortingRequest.java b/src/main/java/org/junit/internal/requests/SortingRequest.java new file mode 100644 index 0000000..3c6f4f5 --- /dev/null +++ b/src/main/java/org/junit/internal/requests/SortingRequest.java @@ -0,0 +1,25 @@ +package org.junit.internal.requests; + +import java.util.Comparator; + +import org.junit.runner.Description; +import org.junit.runner.Request; +import org.junit.runner.Runner; +import org.junit.runner.manipulation.Sorter; + +public class SortingRequest extends Request { + private final Request fRequest; + private final Comparator<Description> fComparator; + + public SortingRequest(Request request, Comparator<Description> comparator) { + fRequest= request; + fComparator= comparator; + } + + @Override + public Runner getRunner() { + Runner runner= fRequest.getRunner(); + new Sorter(fComparator).apply(runner); + return runner; + } +} diff --git a/src/main/java/org/junit/internal/requests/package-info.java b/src/main/java/org/junit/internal/requests/package-info.java new file mode 100644 index 0000000..66d2928 --- /dev/null +++ b/src/main/java/org/junit/internal/requests/package-info.java @@ -0,0 +1,6 @@ +/** + * Provides implementations of {@link org.junit.runner.Request}. + * + * @since 4.0 + */ +package org.junit.internal.requests;
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/ClassRoadie.java b/src/main/java/org/junit/internal/runners/ClassRoadie.java new file mode 100644 index 0000000..1f77d37 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/ClassRoadie.java @@ -0,0 +1,79 @@ +package org.junit.internal.runners; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; + +import org.junit.runner.Description; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be + * removed in the next release. Please use + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. + */ +@Deprecated +public +class ClassRoadie { + private RunNotifier fNotifier; + private TestClass fTestClass; + private Description fDescription; + private final Runnable fRunnable; + + public ClassRoadie(RunNotifier notifier, TestClass testClass, + Description description, Runnable runnable) { + fNotifier= notifier; + fTestClass= testClass; + fDescription= description; + fRunnable= runnable; + } + + protected void runUnprotected() { + fRunnable.run(); + }; + + protected void addFailure(Throwable targetException) { + fNotifier.fireTestFailure(new Failure(fDescription, targetException)); + } + + public void runProtected() { + try { + runBefores(); + runUnprotected(); + } catch (FailedBefore e) { + } finally { + runAfters(); + } + } + + private void runBefores() throws FailedBefore { + try { + try { + List<Method> befores= fTestClass.getBefores(); + for (Method before : befores) + before.invoke(null); + } catch (InvocationTargetException e) { + throw e.getTargetException(); + } + } catch (org.junit.internal.AssumptionViolatedException e) { + throw new FailedBefore(); + } catch (Throwable e) { + addFailure(e); + throw new FailedBefore(); + } + } + + private void runAfters() { + List<Method> afters= fTestClass.getAfters(); + for (Method after : afters) + try { + after.invoke(null); + } catch (InvocationTargetException e) { + addFailure(e.getTargetException()); + } catch (Throwable e) { + addFailure(e); // Untested, but seems impossible + } + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/ErrorReportingRunner.java b/src/main/java/org/junit/internal/runners/ErrorReportingRunner.java new file mode 100644 index 0000000..200b6f0 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/ErrorReportingRunner.java @@ -0,0 +1,60 @@ +package org.junit.internal.runners; + +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.List; + +import org.junit.runner.Description; +import org.junit.runner.Runner; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.model.InitializationError; + +public class ErrorReportingRunner extends Runner { + private final List<Throwable> fCauses; + + private final Class<?> fTestClass; + + public ErrorReportingRunner(Class<?> testClass, Throwable cause) { + fTestClass= testClass; + fCauses= getCauses(cause); + } + + @Override + public Description getDescription() { + Description description= Description.createSuiteDescription(fTestClass); + for (Throwable each : fCauses) + description.addChild(describeCause(each)); + return description; + } + + @Override + public void run(RunNotifier notifier) { + for (Throwable each : fCauses) + runCause(each, notifier); + } + + @SuppressWarnings("deprecation") + private List<Throwable> getCauses(Throwable cause) { + if (cause instanceof InvocationTargetException) + return getCauses(cause.getCause()); + if (cause instanceof InitializationError) + return ((InitializationError) cause).getCauses(); + if (cause instanceof org.junit.internal.runners.InitializationError) + return ((org.junit.internal.runners.InitializationError) cause) + .getCauses(); + return Arrays.asList(cause); + } + + private Description describeCause(Throwable child) { + return Description.createTestDescription(fTestClass, + "initializationError"); + } + + private void runCause(Throwable child, RunNotifier notifier) { + Description description= describeCause(child); + notifier.fireTestStarted(description); + notifier.fireTestFailure(new Failure(description, child)); + notifier.fireTestFinished(description); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/FailedBefore.java b/src/main/java/org/junit/internal/runners/FailedBefore.java new file mode 100644 index 0000000..29dcba4 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/FailedBefore.java @@ -0,0 +1,14 @@ +package org.junit.internal.runners; + +import org.junit.runners.BlockJUnit4ClassRunner; + + +/** + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be + * removed in the next release. Please use + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. + */ +@Deprecated +class FailedBefore extends Exception { + private static final long serialVersionUID= 1L; +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/InitializationError.java b/src/main/java/org/junit/internal/runners/InitializationError.java new file mode 100644 index 0000000..5715ec5 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/InitializationError.java @@ -0,0 +1,30 @@ +package org.junit.internal.runners; + +import java.util.Arrays; +import java.util.List; + +@Deprecated +/** + * Use the published version: {@link org.junit.runners.InitializationError} + * This may disappear as soon as 1 April 2009 + */ +public class InitializationError extends Exception { + private static final long serialVersionUID= 1L; + private final List<Throwable> fErrors; + + public InitializationError(List<Throwable> errors) { + fErrors= errors; + } + + public InitializationError(Throwable... errors) { + this(Arrays.asList(errors)); + } + + public InitializationError(String string) { + this(new Exception(string)); + } + + public List<Throwable> getCauses() { + return fErrors; + } +} diff --git a/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java b/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java new file mode 100644 index 0000000..0028d0c --- /dev/null +++ b/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java @@ -0,0 +1,158 @@ +package org.junit.internal.runners; + +import junit.extensions.TestDecorator; +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestListener; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import org.junit.runner.Describable; +import org.junit.runner.Description; +import org.junit.runner.Runner; +import org.junit.runner.manipulation.Filter; +import org.junit.runner.manipulation.Filterable; +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 JUnit38ClassRunner extends Runner implements Filterable, Sortable { + private final class OldTestClassAdaptingListener implements + TestListener { + private final RunNotifier fNotifier; + + private OldTestClassAdaptingListener(RunNotifier notifier) { + fNotifier= notifier; + } + + public void endTest(Test test) { + fNotifier.fireTestFinished(asDescription(test)); + } + + public void startTest(Test test) { + fNotifier.fireTestStarted(asDescription(test)); + } + + // Implement junit.framework.TestListener + public void addError(Test test, Throwable t) { + Failure failure= new Failure(asDescription(test), t); + fNotifier.fireTestFailure(failure); + } + + private Description asDescription(Test test) { + if (test instanceof Describable) { + Describable facade= (Describable) test; + return facade.getDescription(); + } + return Description.createTestDescription(getEffectiveClass(test), getName(test)); + } + + private Class<? extends Test> getEffectiveClass(Test test) { + return test.getClass(); + } + + private String getName(Test test) { + if (test instanceof TestCase) + return ((TestCase) test).getName(); + else + return test.toString(); + } + + public void addFailure(Test test, AssertionFailedError t) { + addError(test, t); + } + } + + private Test fTest; + + public JUnit38ClassRunner(Class<?> klass) { + this(new TestSuite(klass.asSubclass(TestCase.class))); + } + + public JUnit38ClassRunner(Test test) { + super(); + setTest(test); + } + + @Override + public void run(RunNotifier notifier) { + TestResult result= new TestResult(); + result.addListener(createAdaptingListener(notifier)); + getTest().run(result); + } + + public TestListener createAdaptingListener(final RunNotifier notifier) { + return new OldTestClassAdaptingListener(notifier); + } + + @Override + public Description getDescription() { + return makeDescription(getTest()); + } + + private static Description makeDescription(Test test) { + if (test instanceof TestCase) { + TestCase tc= (TestCase) test; + return Description.createTestDescription(tc.getClass(), tc.getName()); + } else if (test instanceof TestSuite) { + TestSuite ts= (TestSuite) test; + String name= ts.getName() == null ? createSuiteDescription(ts) : ts.getName(); + Description description= Description.createSuiteDescription(name); + int n= ts.testCount(); + for (int i= 0; i < n; i++) { + Description made= makeDescription(ts.testAt(i)); + description.addChild(made); + } + return description; + } else if (test instanceof Describable) { + Describable adapter= (Describable) test; + return adapter.getDescription(); + } else if (test instanceof TestDecorator) { + TestDecorator decorator= (TestDecorator) test; + return makeDescription(decorator.getTest()); + } else { + // This is the best we can do in this case + return Description.createSuiteDescription(test.getClass()); + } + } + + private static String createSuiteDescription(TestSuite ts) { + int count= ts.countTestCases(); + String example = count == 0 ? "" : String.format(" [example: %s]", ts.testAt(0)); + return String.format("TestSuite with %s tests%s", count, example); + } + + public void filter(Filter filter) throws NoTestsRemainException { + if (getTest() instanceof Filterable) { + Filterable adapter= (Filterable) getTest(); + adapter.filter(filter); + } else if (getTest() instanceof TestSuite) { + TestSuite suite= (TestSuite) getTest(); + TestSuite filtered= new TestSuite(suite.getName()); + int n= suite.testCount(); + for (int i= 0; i < n; i++) { + Test test= suite.testAt(i); + if (filter.shouldRun(makeDescription(test))) + filtered.addTest(test); + } + setTest(filtered); + } + } + + public void sort(Sorter sorter) { + if (getTest() instanceof Sortable) { + Sortable adapter= (Sortable) getTest(); + adapter.sort(sorter); + } + } + + private void setTest(Test test) { + fTest = test; + } + + private Test getTest() { + return fTest; + } +} diff --git a/src/main/java/org/junit/internal/runners/JUnit4ClassRunner.java b/src/main/java/org/junit/internal/runners/JUnit4ClassRunner.java new file mode 100644 index 0000000..d732880 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/JUnit4ClassRunner.java @@ -0,0 +1,145 @@ +package org.junit.internal.runners; + +import java.lang.annotation.Annotation; +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.runner.Description; +import org.junit.runner.Runner; +import org.junit.runner.manipulation.Filter; +import org.junit.runner.manipulation.Filterable; +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; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be + * removed in the next release. Please use + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. + * + * This may disappear as soon as 1 April 2009 + */ +@Deprecated +public class JUnit4ClassRunner extends Runner implements Filterable, Sortable { + private final List<Method> fTestMethods; + private TestClass fTestClass; + + public JUnit4ClassRunner(Class<?> klass) throws InitializationError { + fTestClass= new TestClass(klass); + fTestMethods= getTestMethods(); + validate(); + } + + protected List<Method> getTestMethods() { + return fTestClass.getTestMethods(); + } + + protected void validate() throws InitializationError { + MethodValidator methodValidator= new MethodValidator(fTestClass); + methodValidator.validateMethodsForDefaultRunner(); + methodValidator.assertValid(); + } + + @Override + public void run(final RunNotifier notifier) { + new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() { + public void run() { + runMethods(notifier); + } + }).runProtected(); + } + + protected void runMethods(final RunNotifier notifier) { + for (Method method : fTestMethods) + invokeTestMethod(method, notifier); + } + + @Override + public Description getDescription() { + Description spec= Description.createSuiteDescription(getName(), classAnnotations()); + List<Method> testMethods= fTestMethods; + for (Method method : testMethods) + spec.addChild(methodDescription(method)); + return spec; + } + + protected Annotation[] classAnnotations() { + return fTestClass.getJavaClass().getAnnotations(); + } + + protected String getName() { + return getTestClass().getName(); + } + + protected Object createTest() throws Exception { + return getTestClass().getConstructor().newInstance(); + } + + protected void invokeTestMethod(Method method, RunNotifier notifier) { + Description description= methodDescription(method); + Object test; + try { + test= createTest(); + } catch (InvocationTargetException e) { + testAborted(notifier, description, e.getCause()); + return; + } catch (Exception e) { + testAborted(notifier, description, e); + return; + } + TestMethod testMethod= wrapMethod(method); + new MethodRoadie(test, testMethod, notifier, description).run(); + } + + private void testAborted(RunNotifier notifier, Description description, + Throwable e) { + notifier.fireTestStarted(description); + notifier.fireTestFailure(new Failure(description, e)); + notifier.fireTestFinished(description); + } + + protected TestMethod wrapMethod(Method method) { + return new TestMethod(method, fTestClass); + } + + protected String testName(Method method) { + return method.getName(); + } + + protected Description methodDescription(Method method) { + return Description.createTestDescription(getTestClass().getJavaClass(), testName(method), testAnnotations(method)); + } + + protected Annotation[] testAnnotations(Method method) { + return method.getAnnotations(); + } + + public void filter(Filter filter) throws NoTestsRemainException { + for (Iterator<Method> iter= fTestMethods.iterator(); iter.hasNext();) { + Method method= iter.next(); + if (!filter.shouldRun(methodDescription(method))) + iter.remove(); + } + if (fTestMethods.isEmpty()) + throw new NoTestsRemainException(); + } + + public void sort(final Sorter sorter) { + Collections.sort(fTestMethods, new Comparator<Method>() { + public int compare(Method o1, Method o2) { + return sorter.compare(methodDescription(o1), methodDescription(o2)); + } + }); + } + + protected TestClass getTestClass() { + return fTestClass; + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/MethodRoadie.java b/src/main/java/org/junit/internal/runners/MethodRoadie.java new file mode 100644 index 0000000..4407821 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/MethodRoadie.java @@ -0,0 +1,157 @@ +package org.junit.internal.runners; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +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; + +import org.junit.internal.AssumptionViolatedException; +import org.junit.runner.Description; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be + * removed in the next release. Please use + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. + */ +@Deprecated +public class MethodRoadie { + private final Object fTest; + private final RunNotifier fNotifier; + private final Description fDescription; + private TestMethod fTestMethod; + + public MethodRoadie(Object test, TestMethod method, RunNotifier notifier, Description description) { + fTest= test; + fNotifier= notifier; + fDescription= description; + fTestMethod= method; + } + + public void run() { + if (fTestMethod.isIgnored()) { + fNotifier.fireTestIgnored(fDescription); + return; + } + fNotifier.fireTestStarted(fDescription); + try { + long timeout= fTestMethod.getTimeout(); + if (timeout > 0) + runWithTimeout(timeout); + else + runTest(); + } finally { + fNotifier.fireTestFinished(fDescription); + } + } + + private void runWithTimeout(final long timeout) { + runBeforesThenTestThenAfters(new Runnable() { + + public void run() { + ExecutorService service= Executors.newSingleThreadExecutor(); + Callable<Object> callable= new Callable<Object>() { + public Object call() throws Exception { + runTestMethod(); + return null; + } + }; + Future<Object> result= service.submit(callable); + service.shutdown(); + try { + boolean terminated= service.awaitTermination(timeout, + TimeUnit.MILLISECONDS); + if (!terminated) + service.shutdownNow(); + result.get(0, 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); + } + } + }); + } + + public void runTest() { + runBeforesThenTestThenAfters(new Runnable() { + public void run() { + runTestMethod(); + } + }); + } + + public void runBeforesThenTestThenAfters(Runnable test) { + try { + runBefores(); + test.run(); + } catch (FailedBefore e) { + } catch (Exception e) { + throw new RuntimeException("test should never throw an exception to this level"); + } finally { + runAfters(); + } + } + + protected void runTestMethod() { + try { + fTestMethod.invoke(fTest); + if (fTestMethod.expectsException()) + addFailure(new AssertionError("Expected exception: " + fTestMethod.getExpectedException().getName())); + } catch (InvocationTargetException e) { + Throwable actual= e.getTargetException(); + if (actual instanceof AssumptionViolatedException) + return; + else if (!fTestMethod.expectsException()) + addFailure(actual); + else if (fTestMethod.isUnexpected(actual)) { + String message= "Unexpected exception, expected<" + fTestMethod.getExpectedException().getName() + "> but was<" + + actual.getClass().getName() + ">"; + addFailure(new Exception(message, actual)); + } + } catch (Throwable e) { + addFailure(e); + } + } + + private void runBefores() throws FailedBefore { + try { + try { + List<Method> befores= fTestMethod.getBefores(); + for (Method before : befores) + before.invoke(fTest); + } catch (InvocationTargetException e) { + throw e.getTargetException(); + } + } catch (AssumptionViolatedException e) { + throw new FailedBefore(); + } catch (Throwable e) { + addFailure(e); + throw new FailedBefore(); + } + } + + private void runAfters() { + List<Method> afters= fTestMethod.getAfters(); + for (Method after : afters) + try { + after.invoke(fTest); + } catch (InvocationTargetException e) { + addFailure(e.getTargetException()); + } catch (Throwable e) { + addFailure(e); // Untested, but seems impossible + } + } + + protected void addFailure(Throwable e) { + fNotifier.fireTestFailure(new Failure(fDescription, e)); + } +} + diff --git a/src/main/java/org/junit/internal/runners/MethodValidator.java b/src/main/java/org/junit/internal/runners/MethodValidator.java new file mode 100644 index 0000000..cadc93f --- /dev/null +++ b/src/main/java/org/junit/internal/runners/MethodValidator.java @@ -0,0 +1,91 @@ +package org.junit.internal.runners; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be + * removed in the next release. Please use + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. + */ +@Deprecated +public class MethodValidator { + + private final List<Throwable> fErrors= new ArrayList<Throwable>(); + + private TestClass fTestClass; + + public MethodValidator(TestClass testClass) { + fTestClass = testClass; + } + + public void validateInstanceMethods() { + validateTestMethods(After.class, false); + validateTestMethods(Before.class, false); + validateTestMethods(Test.class, false); + + List<Method> methods= fTestClass.getAnnotatedMethods(Test.class); + if (methods.size() == 0) + fErrors.add(new Exception("No runnable methods")); + } + + public void validateStaticMethods() { + validateTestMethods(BeforeClass.class, true); + validateTestMethods(AfterClass.class, true); + } + + public List<Throwable> validateMethodsForDefaultRunner() { + validateNoArgConstructor(); + validateStaticMethods(); + validateInstanceMethods(); + return fErrors; + } + + public void assertValid() throws InitializationError { + if (!fErrors.isEmpty()) + throw new InitializationError(fErrors); + } + + public void validateNoArgConstructor() { + try { + fTestClass.getConstructor(); + } catch (Exception e) { + fErrors.add(new Exception("Test class should have public zero-argument constructor", e)); + } + } + + private void validateTestMethods(Class<? extends Annotation> annotation, + boolean isStatic) { + List<Method> methods= fTestClass.getAnnotatedMethods(annotation); + + for (Method each : methods) { + if (Modifier.isStatic(each.getModifiers()) != isStatic) { + String state= isStatic ? "should" : "should not"; + fErrors.add(new Exception("Method " + each.getName() + "() " + + state + " be static")); + } + if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) + fErrors.add(new Exception("Class " + each.getDeclaringClass().getName() + + " should be public")); + if (!Modifier.isPublic(each.getModifiers())) + fErrors.add(new Exception("Method " + each.getName() + + " should be public")); + if (each.getReturnType() != Void.TYPE) + fErrors.add(new Exception("Method " + each.getName() + + " should be void")); + if (each.getParameterTypes().length != 0) + fErrors.add(new Exception("Method " + each.getName() + + " should have no parameters")); + } + } +} diff --git a/src/main/java/org/junit/internal/runners/SuiteMethod.java b/src/main/java/org/junit/internal/runners/SuiteMethod.java new file mode 100644 index 0000000..4e8bebc --- /dev/null +++ b/src/main/java/org/junit/internal/runners/SuiteMethod.java @@ -0,0 +1,40 @@ +package org.junit.internal.runners; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import junit.framework.Test; + +/** Runner for use with JUnit 3.8.x-style AllTests classes + * (those that only implement a static <code>suite()</code> + * method). For example: + * <pre> + * @RunWith(AllTests.class) + * public class ProductTests { + * public static junit.framework.Test suite() { + * ... + * } + * } + * </pre> + */ +public class SuiteMethod extends JUnit38ClassRunner { + public SuiteMethod(Class<?> klass) throws Throwable { + super(testFromSuiteMethod(klass)); + } + + public static Test testFromSuiteMethod(Class<?> klass) throws Throwable { + Method suiteMethod= null; + Test suite= null; + try { + suiteMethod= klass.getMethod("suite"); + if (! Modifier.isStatic(suiteMethod.getModifiers())) { + throw new Exception(klass.getName() + ".suite() must be static"); + } + suite= (Test) suiteMethod.invoke(null); // static method + } catch (InvocationTargetException e) { + throw e.getCause(); + } + return suite; + } +} diff --git a/src/main/java/org/junit/internal/runners/TestClass.java b/src/main/java/org/junit/internal/runners/TestClass.java new file mode 100644 index 0000000..1ca2b9d --- /dev/null +++ b/src/main/java/org/junit/internal/runners/TestClass.java @@ -0,0 +1,102 @@ +package org.junit.internal.runners; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be + * removed in the next release. Please use + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. + */ +@Deprecated +public class TestClass { + private final Class<?> fClass; + + public TestClass(Class<?> klass) { + fClass= klass; + } + + public List<Method> getTestMethods() { + return getAnnotatedMethods(Test.class); + } + + List<Method> getBefores() { + return getAnnotatedMethods(BeforeClass.class); + } + + List<Method> getAfters() { + return getAnnotatedMethods(AfterClass.class); + } + + public List<Method> getAnnotatedMethods(Class<? extends Annotation> annotationClass) { + List<Method> results= new ArrayList<Method>(); + for (Class<?> eachClass : getSuperClasses(fClass)) { + 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; + } + + 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; + } + + 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; + } + + 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 Constructor<?> getConstructor() throws SecurityException, NoSuchMethodException { + return fClass.getConstructor(); + } + + public Class<?> getJavaClass() { + return fClass; + } + + public String getName() { + return fClass.getName(); + } + +} diff --git a/src/main/java/org/junit/internal/runners/TestMethod.java b/src/main/java/org/junit/internal/runners/TestMethod.java new file mode 100644 index 0000000..a06213c --- /dev/null +++ b/src/main/java/org/junit/internal/runners/TestMethod.java @@ -0,0 +1,69 @@ +package org.junit.internal.runners; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +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.runners.BlockJUnit4ClassRunner; + +/** + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be + * removed in the next release. Please use + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. + */ +@Deprecated +public class TestMethod { + private final Method fMethod; + private TestClass fTestClass; + + public TestMethod(Method method, TestClass testClass) { + fMethod= method; + fTestClass= testClass; + } + + public boolean isIgnored() { + return fMethod.getAnnotation(Ignore.class) != null; + } + + public long getTimeout() { + Test annotation= fMethod.getAnnotation(Test.class); + if (annotation == null) + return 0; + long timeout= annotation.timeout(); + return timeout; + } + + protected Class<? extends Throwable> getExpectedException() { + Test annotation= fMethod.getAnnotation(Test.class); + if (annotation == null || annotation.expected() == None.class) + return null; + else + return annotation.expected(); + } + + boolean isUnexpected(Throwable exception) { + return ! getExpectedException().isAssignableFrom(exception.getClass()); + } + + boolean expectsException() { + return getExpectedException() != null; + } + + List<Method> getBefores() { + return fTestClass.getAnnotatedMethods(Before.class); + } + + List<Method> getAfters() { + return fTestClass.getAnnotatedMethods(After.class); + } + + public void invoke(Object test) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { + fMethod.invoke(test); + } + +} diff --git a/src/main/java/org/junit/internal/runners/model/EachTestNotifier.java b/src/main/java/org/junit/internal/runners/model/EachTestNotifier.java new file mode 100644 index 0000000..a7d534c --- /dev/null +++ b/src/main/java/org/junit/internal/runners/model/EachTestNotifier.java @@ -0,0 +1,51 @@ +/** + * + */ +package org.junit.internal.runners.model; + +import org.junit.internal.AssumptionViolatedException; +import org.junit.runner.Description; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.model.MultipleFailureException; + +public class EachTestNotifier { + private final RunNotifier fNotifier; + + private final Description fDescription; + + public EachTestNotifier(RunNotifier notifier, Description description) { + fNotifier= notifier; + fDescription= description; + } + + public void addFailure(Throwable targetException) { + if (targetException instanceof MultipleFailureException) { + addMultipleFailureException((MultipleFailureException) targetException); + } else { + fNotifier + .fireTestFailure(new Failure(fDescription, targetException)); + } + } + + private void addMultipleFailureException(MultipleFailureException mfe) { + for (Throwable each : mfe.getFailures()) + addFailure(each); + } + + public void addFailedAssumption(AssumptionViolatedException e) { + fNotifier.fireTestAssumptionFailed(new Failure(fDescription, e)); + } + + public void fireTestFinished() { + fNotifier.fireTestFinished(fDescription); + } + + public void fireTestStarted() { + fNotifier.fireTestStarted(fDescription); + } + + public void fireTestIgnored() { + fNotifier.fireTestIgnored(fDescription); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/model/MultipleFailureException.java b/src/main/java/org/junit/internal/runners/model/MultipleFailureException.java new file mode 100644 index 0000000..3316806 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/model/MultipleFailureException.java @@ -0,0 +1,12 @@ +package org.junit.internal.runners.model; + +import java.util.List; + +@Deprecated +public class MultipleFailureException extends org.junit.runners.model.MultipleFailureException { + private static final long serialVersionUID= 1L; + + public MultipleFailureException(List<Throwable> errors) { + super(errors); + } +} diff --git a/src/main/java/org/junit/internal/runners/model/ReflectiveCallable.java b/src/main/java/org/junit/internal/runners/model/ReflectiveCallable.java new file mode 100644 index 0000000..9150d90 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/model/ReflectiveCallable.java @@ -0,0 +1,22 @@ +/** + * + */ +package org.junit.internal.runners.model; + +import java.lang.reflect.InvocationTargetException; + +/** + * When invoked, throws the exception from the reflected method, rather than + * wrapping it in an InvocationTargetException. + */ +public abstract class ReflectiveCallable { + public Object run() throws Throwable { + try { + return runReflectiveCall(); + } catch (InvocationTargetException e) { + throw e.getTargetException(); + } + } + + protected abstract Object runReflectiveCall() throws Throwable; +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/package-info.java b/src/main/java/org/junit/internal/runners/package-info.java new file mode 100644 index 0000000..5ab7e7b --- /dev/null +++ b/src/main/java/org/junit/internal/runners/package-info.java @@ -0,0 +1,6 @@ +/** + * Provides implementations of {@link org.junit.runner.Runner} + * + * @since 4.0 + */ +package org.junit.internal.runners;
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/rules/RuleFieldValidator.java b/src/main/java/org/junit/internal/runners/rules/RuleFieldValidator.java new file mode 100644 index 0000000..e7df8bf --- /dev/null +++ b/src/main/java/org/junit/internal/runners/rules/RuleFieldValidator.java @@ -0,0 +1,92 @@ +package org.junit.internal.runners.rules; + +import java.lang.annotation.Annotation; +import java.util.List; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.rules.TestRule; +import org.junit.runners.model.FrameworkField; +import org.junit.runners.model.TestClass; + +/** + * A RuleFieldValidator validates the rule fields of a + * {@link org.junit.runners.model.TestClass}. All reasons for rejecting the + * {@code TestClass} are written to a list of errors. + * + * There are two slightly different validators. The {@link #CLASS_RULE_VALIDATOR} + * validates fields with a {@link ClassRule} annotation and the + * {@link #RULE_VALIDATOR} validates fields with a {@link Rule} annotation. + */ +public enum RuleFieldValidator { + /** + * Validates fields with a {@link ClassRule} annotation. + */ + CLASS_RULE_VALIDATOR(ClassRule.class, true), + /** + * Validates fields with a {@link Rule} annotation. + */ + RULE_VALIDATOR(Rule.class, false); + + private final Class<? extends Annotation> fAnnotation; + + private final boolean fOnlyStaticFields; + + private RuleFieldValidator(Class<? extends Annotation> annotation, + boolean onlyStaticFields) { + this.fAnnotation= annotation; + this.fOnlyStaticFields= onlyStaticFields; + } + + /** + * Validate the {@link org.junit.runners.model.TestClass} and adds reasons + * for rejecting the class to a list of errors. + * @param target the {@code TestClass} to validate. + * @param errors the list of errors. + */ + public void validate(TestClass target, List<Throwable> errors) { + List<FrameworkField> fields= target.getAnnotatedFields(fAnnotation); + for (FrameworkField each : fields) + validateField(each, errors); + } + + private void validateField(FrameworkField field, List<Throwable> errors) { + optionallyValidateStatic(field, errors); + validatePublic(field, errors); + validateTestRuleOrMethodRule(field, errors); + } + + private void optionallyValidateStatic(FrameworkField field, + List<Throwable> errors) { + if (fOnlyStaticFields && !field.isStatic()) + addError(errors, field, "must be static."); + } + + private void validatePublic(FrameworkField field, List<Throwable> errors) { + if (!field.isPublic()) + addError(errors, field, "must be public."); + } + + private void validateTestRuleOrMethodRule(FrameworkField field, + List<Throwable> errors) { + if (!isMethodRule(field) && !isTestRule(field)) + addError(errors, field, "must implement MethodRule or TestRule."); + } + + private boolean isTestRule(FrameworkField target) { + return TestRule.class.isAssignableFrom(target.getType()); + } + + @SuppressWarnings("deprecation") + private boolean isMethodRule(FrameworkField target) { + return org.junit.rules.MethodRule.class.isAssignableFrom(target + .getType()); + } + + private void addError(List<Throwable> errors, FrameworkField field, + String suffix) { + String message= "The @" + fAnnotation.getSimpleName() + " '" + + field.getName() + "' " + suffix; + errors.add(new Exception(message)); + } +} diff --git a/src/main/java/org/junit/internal/runners/statements/ExpectException.java b/src/main/java/org/junit/internal/runners/statements/ExpectException.java new file mode 100644 index 0000000..ddfef07 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/statements/ExpectException.java @@ -0,0 +1,38 @@ +/** + * + */ +package org.junit.internal.runners.statements; + +import org.junit.internal.AssumptionViolatedException; +import org.junit.runners.model.Statement; + +public class ExpectException extends Statement { + private Statement fNext; + private final Class<? extends Throwable> fExpected; + + public ExpectException(Statement next, Class<? extends Throwable> expected) { + fNext= next; + fExpected= expected; + } + + @Override + public void evaluate() throws Exception { + boolean complete = false; + try { + fNext.evaluate(); + complete = true; + } catch (AssumptionViolatedException e) { + throw e; + } catch (Throwable e) { + if (!fExpected.isAssignableFrom(e.getClass())) { + String message= "Unexpected exception, expected<" + + fExpected.getName() + "> but was<" + + e.getClass().getName() + ">"; + throw new Exception(message, e); + } + } + if (complete) + throw new AssertionError("Expected exception: " + + fExpected.getName()); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/statements/Fail.java b/src/main/java/org/junit/internal/runners/statements/Fail.java new file mode 100644 index 0000000..e7d0d5c --- /dev/null +++ b/src/main/java/org/junit/internal/runners/statements/Fail.java @@ -0,0 +1,17 @@ +package org.junit.internal.runners.statements; + +import org.junit.runners.model.Statement; + + +public class Fail extends Statement { + private final Throwable fError; + + public Fail(Throwable e) { + fError= e; + } + + @Override + public void evaluate() throws Throwable { + throw fError; + } +} diff --git a/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java b/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java new file mode 100644 index 0000000..bff7c72 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java @@ -0,0 +1,71 @@ +/** + * + */ +package org.junit.internal.runners.statements; + +import org.junit.runners.model.Statement; + +public class FailOnTimeout extends Statement { + private final Statement fOriginalStatement; + + private final long fTimeout; + + public FailOnTimeout(Statement originalStatement, long timeout) { + fOriginalStatement= originalStatement; + fTimeout= timeout; + } + + @Override + public void evaluate() throws Throwable { + StatementThread thread= evaluateStatement(); + if (!thread.fFinished) + throwExceptionForUnfinishedThread(thread); + } + + private StatementThread evaluateStatement() throws InterruptedException { + StatementThread thread= new StatementThread(fOriginalStatement); + thread.start(); + thread.join(fTimeout); + thread.interrupt(); + return thread; + } + + private void throwExceptionForUnfinishedThread(StatementThread thread) + throws Throwable { + if (thread.fExceptionThrownByOriginalStatement != null) + throw thread.fExceptionThrownByOriginalStatement; + else + throwTimeoutException(thread); + } + + private void throwTimeoutException(StatementThread thread) throws Exception { + Exception exception= new Exception(String.format( + "test timed out after %d milliseconds", fTimeout)); + exception.setStackTrace(thread.getStackTrace()); + throw exception; + } + + private static class StatementThread extends Thread { + private final Statement fStatement; + + private boolean fFinished= false; + + private Throwable fExceptionThrownByOriginalStatement= null; + + public StatementThread(Statement statement) { + fStatement= statement; + } + + @Override + public void run() { + try { + fStatement.evaluate(); + fFinished= true; + } catch (InterruptedException e) { + //don't log the InterruptedException + } catch (Throwable e) { + fExceptionThrownByOriginalStatement= e; + } + } + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/statements/InvokeMethod.java b/src/main/java/org/junit/internal/runners/statements/InvokeMethod.java new file mode 100644 index 0000000..e2e81e1 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/statements/InvokeMethod.java @@ -0,0 +1,22 @@ +/** + * + */ +package org.junit.internal.runners.statements; + +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; + +public class InvokeMethod extends Statement { + private final FrameworkMethod fTestMethod; + private Object fTarget; + + public InvokeMethod(FrameworkMethod testMethod, Object target) { + fTestMethod= testMethod; + fTarget= target; + } + + @Override + public void evaluate() throws Throwable { + fTestMethod.invokeExplosively(fTarget); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/statements/RunAfters.java b/src/main/java/org/junit/internal/runners/statements/RunAfters.java new file mode 100644 index 0000000..475ec72 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/statements/RunAfters.java @@ -0,0 +1,43 @@ +/** + * + */ +package org.junit.internal.runners.statements; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.MultipleFailureException; +import org.junit.runners.model.Statement; + +public class RunAfters extends Statement { + private final Statement fNext; + + private final Object fTarget; + + private final List<FrameworkMethod> fAfters; + + public RunAfters(Statement next, List<FrameworkMethod> afters, Object target) { + fNext= next; + fAfters= afters; + fTarget= target; + } + + @Override + public void evaluate() throws Throwable { + List<Throwable> errors = new ArrayList<Throwable>(); + try { + fNext.evaluate(); + } catch (Throwable e) { + errors.add(e); + } finally { + for (FrameworkMethod each : fAfters) + try { + each.invokeExplosively(fTarget); + } catch (Throwable e) { + errors.add(e); + } + } + MultipleFailureException.assertEmpty(errors); + } +}
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/statements/RunBefores.java b/src/main/java/org/junit/internal/runners/statements/RunBefores.java new file mode 100644 index 0000000..66a34e1 --- /dev/null +++ b/src/main/java/org/junit/internal/runners/statements/RunBefores.java @@ -0,0 +1,30 @@ +/** + * + */ +package org.junit.internal.runners.statements; + +import java.util.List; + +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; + +public class RunBefores extends Statement { + private final Statement fNext; + + private final Object fTarget; + + private final List<FrameworkMethod> fBefores; + + public RunBefores(Statement next, List<FrameworkMethod> befores, Object target) { + fNext= next; + fBefores= befores; + fTarget= target; + } + + @Override + public void evaluate() throws Throwable { + for (FrameworkMethod before : fBefores) + before.invokeExplosively(fTarget); + fNext.evaluate(); + } +}
\ No newline at end of file |