Skip to content

Commit

Permalink
Merge pull request #519 from kcooney/undeprecate
Browse files Browse the repository at this point in the history
Remove @deprecated from MethodRule and related methods.
  • Loading branch information
David Saff committed Oct 29, 2012
2 parents 2f879b9 + b1db2bb commit 35505c4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 58 deletions.
49 changes: 25 additions & 24 deletions src/main/java/org/junit/ClassRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import java.lang.annotation.Target;

/**
* Annotates static fields that contain rules or methods that return them. A field must be public,
* Annotates static fields that reference rules or methods that return them. A field must be public,
* static, and a subtype of {@link org.junit.rules.TestRule}. A method must be public static, and return
* a subtype of {@link org.junit.rules.TestRule}
* a subtype of {@link org.junit.rules.TestRule}.<p>
*
* The {@link org.junit.runners.model.Statement} passed
* to the {@link org.junit.rules.TestRule} will run any {@link BeforeClass} methods,
* then the entire body of the test class (all contained methods, if it is
Expand Down Expand Up @@ -36,20 +37,20 @@
* &#064;RunWith(Suite.class)
* &#064;SuiteClasses({A.class, B.class, C.class})
* public class UsesExternalResource {
* public static Server myServer= new Server();
* public static Server myServer= new Server();
*
* &#064;ClassRule
* public static ExternalResource resource= new ExternalResource() {
* &#064;Override
* protected void before() throws Throwable {
* myServer.connect();
* };
* &#064;ClassRule
* public static ExternalResource resource= new ExternalResource() {
* &#064;Override
* protected void before() throws Throwable {
* myServer.connect();
* }
*
* &#064;Override
* protected void after() {
* myServer.disconnect();
* };
* };
* &#064;Override
* protected void after() {
* myServer.disconnect();
* }
* };
* }
* </pre>
*
Expand All @@ -59,19 +60,19 @@
* &#064;RunWith(Suite.class)
* &#064;SuiteClasses({A.class, B.class, C.class})
* public class UsesExternalResource {
* public static Server myServer= new Server();
* public static Server myServer= new Server();
*
* &#064;ClassRule
* public static ExternalResource getResource() {
* return new ExternalResource() {
* &#064;Override
* protected void before() throws Throwable {
* myServer.connect();
* &#064;ClassRule
* public static ExternalResource getResource() {
* return new ExternalResource() {
* &#064;Override
* protected void before() throws Throwable {
* myServer.connect();
* }
*
* &#064;Override
* protected void after() {
* myServer.disconnect();
* &#064;Override
* protected void after() {
* myServer.disconnect();
* }
* };
* }
Expand Down
47 changes: 23 additions & 24 deletions src/main/java/org/junit/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import java.lang.annotation.Target;

/**
* Annotates fields that contain rules or methods that return a rule. A field must be public, not
* static, and a subtype of {@link org.junit.rules.TestRule}. A method must be public, not static
* and must return a subtype of {@link org.junit.rules.TestRule}.
* Annotates fields that reference rules or methods that return a rule. A field must be public, not
* static, and a subtype of {@link org.junit.rules.TestRule} (preferred) or
* {@link org.junit.rules.MethodRule}. A method must be public, not static,
* and must return a subtype of {@link org.junit.rules.TestRule} (preferred) or
* {@link org.junit.rules.MethodRule}.<p>
*
* The {@link org.junit.runners.model.Statement} passed
* to the {@link org.junit.rules.TestRule} will run any {@link Before} methods,
* then the {@link Test} method, and finally any {@link After} methods,
Expand All @@ -17,21 +20,21 @@
* However, if there are mutliple fields (or methods) they will be applied in an order
* that depends on your JVM's implementation of the reflection API, which is
* undefined, in general. Rules defined by fields will always be applied
* before Rules defined by methods.
* before Rules defined by methods.<p>
*
* For example, here is a test class that creates a temporary folder before
* each test method, and deletes it after each:
*
* <pre>
* public static class HasTempFolder {
* &#064;Rule
* public TemporaryFolder folder= new TemporaryFolder();
* &#064;Rule
* public TemporaryFolder folder= new TemporaryFolder();
*
* &#064;Test
* public void testUsingTempFolder() throws IOException {
* File createdFile= folder.newFile(&quot;myfile.txt&quot;);
* File createdFolder= folder.newFolder(&quot;subfolder&quot;);
* // ...
* &#064;Test
* public void testUsingTempFolder() throws IOException {
* File createdFile= folder.newFile(&quot;myfile.txt&quot;);
* File createdFolder= folder.newFolder(&quot;subfolder&quot;);
* // ...
* }
* }
* </pre>
Expand All @@ -40,29 +43,25 @@
*
* <pre>
* public static class HasTempFolder {
* private TemporaryFolder folder= new TemporaryFolder();
* private TemporaryFolder folder= new TemporaryFolder();
*
* &#064;Rule
* public TemporaryFolder getFolder() {
* return folder;
* &#064;Rule
* public TemporaryFolder getFolder() {
* return folder;
* }
*
* &#064;Test
* public void testUsingTempFolder() throws IOException {
* File createdFile= folder.newFile(&quot;myfile.txt&quot;);
* File createdFolder= folder.newFolder(&quot;subfolder&quot;);
* // ...
* &#064;Test
* public void testUsingTempFolder() throws IOException {
* File createdFile= folder.newFile(&quot;myfile.txt&quot;);
* File createdFolder= folder.newFolder(&quot;subfolder&quot;);
* // ...
* }
* }
* </pre>
*
* For more information and more examples, see
* {@link org.junit.rules.TestRule}.
*
* Note: for backwards compatibility, this annotation may also mark
* fields or methods of type {@link org.junit.rules.MethodRule}, which will be honored. However,
* this is a deprecated interface and feature.
*
* @since 4.7
*/
@Retention(RetentionPolicy.RUNTIME)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/junit/rules/MethodRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* <li>{@link Verifier}: fail test if object state ends up incorrect</li>
* </ul>
*
* Note that {@link MethodRule} is now deprecated, you should be using {@link TestRule} instead.
* Note that {@link MethodRule} has been replaced by {@link TestRule},
* which has the added benefit of supporting class rules.
*
* @since 4.7
*/
@Deprecated
public interface MethodRule {
/**
* Modifies the method-running {@link Statement} to implement an additional
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/junit/rules/TestWatchman.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
* </pre>
*
* @since 4.7
* @deprecated {@link MethodRule} is deprecated.
* Use {@link TestWatcher} implements {@link TestRule} instead.
* @deprecated Use {@link TestWatcher} (which implements {@link TestRule}) instead.
*/
@Deprecated
public class TestWatchman implements MethodRule {
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ private Statement withRules(FrameworkMethod method, Object target,
return result;
}

@SuppressWarnings("deprecation")
private Statement withMethodRules(FrameworkMethod method, List<TestRule> testRules,
Object target, Statement result) {
for (org.junit.rules.MethodRule each : getMethodRules(target)) {
Expand All @@ -353,7 +352,6 @@ private Statement withMethodRules(FrameworkMethod method, List<TestRule> testRul
return result;
}

@SuppressWarnings("deprecation")
private List<org.junit.rules.MethodRule> getMethodRules(Object target) {
return rules(target);
}
Expand All @@ -362,11 +360,7 @@ private List<org.junit.rules.MethodRule> getMethodRules(Object target) {
* @param target the test case instance
* @return a list of MethodRules that should be applied when executing this
* test
* @deprecated {@link org.junit.rules.MethodRule} is a deprecated interface. Port to
* {@link TestRule} and
* {@link BlockJUnit4ClassRunner#getTestRules(Object)}
*/
@Deprecated
protected List<org.junit.rules.MethodRule> rules(Object target) {
return getTestClass().getAnnotatedFieldValues(target, Rule.class,
org.junit.rules.MethodRule.class);
Expand Down

0 comments on commit 35505c4

Please sign in to comment.