-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ExternalResourceSupport to prepare for rule-annotated methods and other TestRules #433
- Loading branch information
1 parent
8d6032b
commit 184230f
Showing
6 changed files
with
169 additions
and
37 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...e/src/main/java/org/junit/jupiter/engine/vintage/rulesupport/AbstractTestRuleAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.vintage.rulesupport; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.junit.platform.commons.util.ReflectionUtils; | ||
import org.junit.rules.TestRule; | ||
|
||
public abstract class AbstractTestRuleAdapter implements GenericBeforeAndAfterAdvice { | ||
|
||
protected void executeMethod(String name, TestRule externalResource) { | ||
try { | ||
Method method = externalResource.getClass().getDeclaredMethod(name); | ||
method.setAccessible(true); | ||
ReflectionUtils.invokeMethod(method, externalResource); | ||
} | ||
catch (NoSuchMethodException | SecurityException e) { | ||
// TODO: decide whether this should be logged | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...e/src/main/java/org/junit/jupiter/engine/vintage/rulesupport/ExternalResourceAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.vintage.rulesupport; | ||
|
||
import org.junit.rules.ExternalResource; | ||
|
||
public class ExternalResourceAdapter extends AbstractTestRuleAdapter { | ||
|
||
private final ExternalResource target; | ||
|
||
public ExternalResourceAdapter(ExternalResource target) { | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public void before() { | ||
super.executeMethod("before", this.target); | ||
} | ||
|
||
@Override | ||
public void after() { | ||
super.executeMethod("after", this.target); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...c/main/java/org/junit/jupiter/engine/vintage/rulesupport/GenericBeforeAndAfterAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.vintage.rulesupport; | ||
|
||
public interface GenericBeforeAndAfterAdvice { | ||
|
||
default void before() { | ||
} | ||
|
||
default void after() { | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...engine/src/main/java/org/junit/jupiter/engine/vintage/rulesupport/RuleAnnotatedField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.vintage.rulesupport; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import org.junit.jupiter.api.extension.TestExtensionContext; | ||
import org.junit.rules.ExternalResource; | ||
|
||
class RuleAnnotatedField implements RuleAnnotatedMember { | ||
|
||
private ExternalResource testRuleInstance; | ||
|
||
RuleAnnotatedField(TestExtensionContext context, Field testRuleField) { | ||
Object testInstance = context.getTestInstance(); | ||
|
||
try { | ||
this.testRuleInstance = (ExternalResource) testRuleField.get(testInstance); | ||
} | ||
catch (IllegalAccessException e) { | ||
// TODO: decide whether this should be logged | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public ExternalResource getTestRuleInstance() { | ||
return this.testRuleInstance; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ngine/src/main/java/org/junit/jupiter/engine/vintage/rulesupport/RuleAnnotatedMember.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.vintage.rulesupport; | ||
|
||
import org.junit.rules.ExternalResource; | ||
|
||
interface RuleAnnotatedMember { | ||
public ExternalResource getTestRuleInstance(); | ||
} |