Skip to content

Commit

Permalink
Cleanup and simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Merdes authored and marcphilipp committed Nov 25, 2016
1 parent 77e0955 commit aeb2045
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public abstract class AbstractTestRuleAdapter implements GenericBeforeAndAfterAd

protected final TestRule target;

public AbstractTestRuleAdapter(TestRule target) {
this.target = target;
public AbstractTestRuleAdapter(RuleAnnotatedMember annotatedMember) {
this.target = annotatedMember.getTestRuleInstance();
}

protected void executeMethod(String name, TestRule externalResource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
abstract class AbstractTestRuleSupport implements BeforeEachCallback, AfterEachCallback {

private final Class<Rule> annotationType = Rule.class;

private final Class<? extends TestRule> ruleType;
private final Class<? extends TestRule> ruleType = TestRule.class;
private final Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator;

protected AbstractTestRuleSupport(Class<? extends TestRule> ruleType,
Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator) {
this.ruleType = ruleType;
protected AbstractTestRuleSupport(Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator) {
this.adapterGenerator = adapterGenerator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

package org.junit.jupiter.engine.vintage.rulesupport;

import org.junit.rules.TestRule;

public class ExternalResourceAdapter extends AbstractTestRuleAdapter {

public ExternalResourceAdapter(TestRule target) {
super(target);
public ExternalResourceAdapter(RuleAnnotatedMember annotatedMember) {
super(annotatedMember);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.rules.TestRule;

public class ExternalResourceSupport implements BeforeEachCallback, AfterEachCallback {

private final Class<? extends TestRule> ruleType = TestRule.class;
private final Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator = annotatedMember -> new ExternalResourceAdapter(
annotatedMember.getTestRuleInstance());
private final Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator = ExternalResourceAdapter::new;

private AbstractTestRuleSupport fieldSupport = new TestRuleFieldSupport(ruleType, adapterGenerator);
private AbstractTestRuleSupport methodSupport = new TestRuleMethodSupport(ruleType, adapterGenerator);
private final AbstractTestRuleSupport fieldSupport = new TestRuleFieldSupport(this.adapterGenerator);
private final AbstractTestRuleSupport methodSupport = new TestRuleMethodSupport(this.adapterGenerator);

@Override
public void beforeEach(TestExtensionContext context) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@

import java.lang.reflect.Field;

import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.rules.TestRule;

class RuleAnnotatedField extends AbstractRuleAnnotatedMember {

RuleAnnotatedField(TestExtensionContext context, Field testRuleField) {
Object testInstance = context.getTestInstance();

RuleAnnotatedField(Object testInstance, Field testRuleField) {
try {
this.testRuleInstance = (TestRule) testRuleField.get(testInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@

import java.lang.reflect.Method;

import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.rules.TestRule;

class RuleAnnotatedMethod extends AbstractRuleAnnotatedMember {

RuleAnnotatedMethod(TestExtensionContext context, Method member) {
Object testInstance = context.getTestInstance();

RuleAnnotatedMethod(Object testInstance, Method member) {
//no args
this.testRuleInstance = (TestRule) ReflectionUtils.invokeMethod(member, testInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@
import java.util.stream.Stream;

import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.rules.TestRule;

class TestRuleFieldSupport extends AbstractTestRuleSupport {

TestRuleFieldSupport(Class<? extends TestRule> ruleType,
Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator) {
super(ruleType, adapterGenerator);
TestRuleFieldSupport(Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator) {
super(adapterGenerator);
}

@Override
protected RuleAnnotatedMember createRuleAnnotatedMember(TestExtensionContext context, Member member) {
return new RuleAnnotatedField(context, (Field) member);
return new RuleAnnotatedField(context.getTestInstance(), (Field) member);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@

import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.platform.commons.util.AnnotationUtils;
import org.junit.rules.TestRule;

class TestRuleMethodSupport extends AbstractTestRuleSupport {

TestRuleMethodSupport(Class<? extends TestRule> ruleType,
Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator) {
super(ruleType, adapterGenerator);
TestRuleMethodSupport(Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator) {
super(adapterGenerator);
}

@Override
protected RuleAnnotatedMember createRuleAnnotatedMember(TestExtensionContext context, Member member) {
return new RuleAnnotatedMethod(context, (Method) member);
return new RuleAnnotatedMethod(context.getTestInstance(), (Method) member);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

package org.junit.jupiter.engine.vintage.rulesupport;

import org.junit.rules.TestRule;

public class VerifierAdapter extends AbstractTestRuleAdapter {

public VerifierAdapter(TestRule target) {
super(target);
public VerifierAdapter(RuleAnnotatedMember annotatedMember) {
super(annotatedMember);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.rules.TestRule;

public class VerifierSupport implements AfterEachCallback {

private final Class<? extends TestRule> ruleType = TestRule.class;
private final Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator = annotatedMember -> new VerifierAdapter(
annotatedMember.getTestRuleInstance());
private final Function<RuleAnnotatedMember, AbstractTestRuleAdapter> adapterGenerator = VerifierAdapter::new;

private AbstractTestRuleSupport fieldSupport = new TestRuleFieldSupport(ruleType, adapterGenerator);
private AbstractTestRuleSupport methodSupport = new TestRuleMethodSupport(ruleType, adapterGenerator);
private AbstractTestRuleSupport fieldSupport = new TestRuleFieldSupport(this.adapterGenerator);
private AbstractTestRuleSupport methodSupport = new TestRuleMethodSupport(this.adapterGenerator);

@Override
public void afterEach(TestExtensionContext context) throws Exception {
Expand Down

0 comments on commit aeb2045

Please sign in to comment.