Skip to content

Commit

Permalink
Revert "Make TestRule an abstract class. Specify target of the rule a…
Browse files Browse the repository at this point in the history
…nnotations."

I accidentically based this branch off of the run-leaf branch. This is the
first step to fix this.

This reverts commit 8ff0b44.
  • Loading branch information
kcooney committed Jan 8, 2011
1 parent bed58a5 commit 7f6173a
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 63 deletions.
3 changes: 0 additions & 3 deletions src/main/java/org/junit/ClassRule.java
@@ -1,9 +1,7 @@
package org.junit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.rules.TestRule;

Expand Down Expand Up @@ -54,6 +52,5 @@
* For more information and more examples, see {@link TestRule}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ClassRule {
}
3 changes: 0 additions & 3 deletions src/main/java/org/junit/Rule.java
@@ -1,9 +1,7 @@
package org.junit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.rules.MethodRule;

Expand Down Expand Up @@ -42,7 +40,6 @@
*/
@SuppressWarnings("deprecation")
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Rule {

}
5 changes: 2 additions & 3 deletions src/main/java/org/junit/rules/ExpectedException.java
Expand Up @@ -41,7 +41,7 @@
* }
* </pre>
*/
public class ExpectedException extends TestRule {
public class ExpectedException implements TestRule {
/**
* @return a Rule that expects no exception to be thrown
* (identical to behavior without this Rule)
Expand All @@ -56,8 +56,7 @@ private ExpectedException() {

}

@Override
protected Statement apply(Statement base,
public Statement apply(Statement base,
org.junit.runner.Description description) {
return new ExpectedExceptionStatement(base);
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/junit/rules/ExternalResource.java
Expand Up @@ -32,9 +32,8 @@
* }
* </pre>
*/
public abstract class ExternalResource extends TestRule {
@Override
protected Statement apply(Statement base, Description description) {
public abstract class ExternalResource implements TestRule {
public Statement apply(Statement base, Description description) {
return statement(base);
}

Expand Down
31 changes: 8 additions & 23 deletions src/main/java/org/junit/rules/TestRule.java
Expand Up @@ -17,7 +17,7 @@
* The default JUnit test runners for suites and
* individual test cases recognize {@link TestRule}s introduced in two different
* ways. {@link Rule} annotates method-level {@link TestRule}s, and {@link ClassRule}
* annotates class-level {@link TestRule}s. See the Javadoc for those annotations
* annotates class-level {@link TestRule}s. See javadoc for those annotations
* for more information.
*
* Multiple {@link TestRule}s can be applied to a test or suite execution. The
Expand All @@ -33,38 +33,23 @@
* <li>{@link ExternalResource}: start and stop a server, for example</li>
* <li>{@link TemporaryFolder}: create fresh files, and delete after test</li>
* <li>{@link TestName}: remember the test name for use during the method</li>
* <li>{@link TestWatcher}: add logic at events during method execution</li>
* <li>{@link TestWatchman}: add logic at events during method execution</li>
* <li>{@link Timeout}: cause test to fail after a set time</li>
* <li>{@link Verifier}: fail test if object state ends up incorrect</li>
* </ul>
*
* [[MethodRule has been deprecated, and all uses will be changed to TestRule,
* so this javadoc will become more truthy with time.]]
*/
public abstract class TestRule {
public interface TestRule {
/**
* Modifies the method-running {@link Statement} to implement this
* Modifies the method-running {@link Statement} to implement an additional
* test-running rule.
*
* @param base The {@link Statement} to be modified
* @param description A {@link Description} of the test implemented in {@code base}
* @return a new statement, which may be the same as {@code base},
* a wrapper around {@code base}, or a completely new Statement.
*/
protected abstract Statement apply(Statement base, Description description);

/**
* Modifies the method-running {@link Statement} to implement the additional
* test-running rules.
*
* @param rules The {@link TestRule rules} to apply
* @param base The {@link Statement} to be modified
* @param description A {@link Description} of the test implemented in {@code base}
* @return a new statement, which may be the same as {@code base},
* a wrapper around {@code base}, or a completely new Statement.
*/
public static Statement applyAll(Iterable<TestRule> rules, Statement base,
Description description) {
Statement result = base;
for (TestRule each : rules)
result= each.apply(result, description);
return result;
}
Statement apply(Statement base, Description description);
}
5 changes: 2 additions & 3 deletions src/main/java/org/junit/rules/TestWatcher.java
Expand Up @@ -36,9 +36,8 @@
* }
* </pre>
*/
public class TestWatcher extends TestRule {
@Override
protected Statement apply(final Statement base, final Description description) {
public class TestWatcher implements TestRule {
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/junit/rules/Timeout.java
Expand Up @@ -33,7 +33,7 @@
* }
* </pre>
*/
public class Timeout extends TestRule {
public class Timeout implements TestRule {
private final int fMillis;

/**
Expand All @@ -43,8 +43,7 @@ public Timeout(int millis) {
fMillis= millis;
}

@Override
protected Statement apply(Statement base, Description description) {
public Statement apply(Statement base, Description description) {
return new FailOnTimeout(base, fMillis);
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/junit/rules/Verifier.java
Expand Up @@ -25,9 +25,8 @@
* }
* </pre>
*/
public class Verifier extends TestRule {
@Override
protected Statement apply(final Statement base, Description description) {
public class Verifier implements TestRule {
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java
Expand Up @@ -362,7 +362,9 @@ private List<org.junit.rules.MethodRule> getMethodRules(Object target) {

private Statement withTestRules(FrameworkMethod method, Object target,
Statement result) {
return TestRule.applyAll(getTestRules(target), result, describeChild(method));
for (TestRule each : getTestRules(target))
result= each.apply(result, describeChild(method));
return result;
}

private List<TestRule> getTestRules(Object target) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/junit/runners/ParentRunner.java
Expand Up @@ -192,7 +192,11 @@ private Statement withClassRules(Statement statement) {
if (classRules.isEmpty()) {
return statement;
}
return TestRule.applyAll(classRules, statement, getDescription());
Statement next = statement;
for (final TestRule classRule : classRules) {
next = classRule.apply(next, getDescription());
}
return next;
}

/**
Expand Down
Expand Up @@ -61,11 +61,10 @@ public void ruleIsIntroducedAndEvaluatedOnSubclass() {
assertEquals(1, ExampleTestWithClassRule.counter.count);
}

public static class CustomCounter extends TestRule {
public static class CustomCounter implements TestRule {
public int count = 0;

@Override
protected Statement apply(final Statement base, Description description) {
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Expand Down
Expand Up @@ -248,11 +248,10 @@ public static class PrivateRule {
hasSingleFailureContaining("must be public"));
}

public static class CustomTestName extends TestRule {
public static class CustomTestName implements TestRule {
public String name = null;

@Override
protected Statement apply(final Statement base, final Description description) {
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Expand Down
18 changes: 7 additions & 11 deletions src/test/java/org/junit/tests/experimental/rules/TestRuleTest.java
Expand Up @@ -28,8 +28,7 @@ public class TestRuleTest {
public static class ExampleTest {
@Rule
public TestRule example= new TestRule() {
@Override
protected Statement apply(final Statement base, Description description) {
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Expand All @@ -54,7 +53,7 @@ public void ruleIsIntroducedAndEvaluated() {
}

@SuppressWarnings("deprecation")
public static class BothKindsOfRule extends TestRule implements org.junit.rules.MethodRule {
public static class BothKindsOfRule implements TestRule, org.junit.rules.MethodRule {
public int applications = 0;

public Statement apply(Statement base, FrameworkMethod method,
Expand All @@ -63,8 +62,7 @@ public Statement apply(Statement base, FrameworkMethod method,
return base;
}

@Override
protected Statement apply(Statement base, Description description) {
public Statement apply(Statement base, Description description) {
applications++;
return base;
}
Expand Down Expand Up @@ -98,9 +96,8 @@ public void ruleIsIntroducedAndEvaluatedOnSubclass() {
private static int runCount;

public static class MultipleRuleTest {
private static class Increment extends TestRule {
@Override
protected Statement apply(final Statement base, Description description) {
private static class Increment implements TestRule {
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Expand Down Expand Up @@ -277,11 +274,10 @@ public static class PrivateRule {
hasSingleFailureContaining("must be public"));
}

public static class CustomTestName extends TestRule {
public static class CustomTestName implements TestRule {
public String name = null;

@Override
protected Statement apply(final Statement base, final Description description) {
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Expand Down

0 comments on commit 7f6173a

Please sign in to comment.