Skip to content

Commit

Permalink
Use Builder pattern to extend JUnit5Context
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Dec 9, 2015
1 parent 3cbb596 commit efb8674
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 30 deletions.
Expand Up @@ -10,56 +10,103 @@


package org.junit.gen5.engine.junit5; package org.junit.gen5.engine.junit5;


import java.util.HashMap;
import java.util.Map;

import org.junit.gen5.engine.Context; import org.junit.gen5.engine.Context;
import org.junit.gen5.engine.junit5.execution.TestExtensionRegistry; import org.junit.gen5.engine.junit5.execution.TestExtensionRegistry;


public class JUnit5Context implements Context { public class JUnit5Context implements Context {


private final Map<String, Object> map; private final State state;


public JUnit5Context() { public JUnit5Context() {
this(new HashMap<>()); this(new State());
} }


private JUnit5Context(Map<String, Object> map) { private JUnit5Context(State state) {
this.map = map; this.state = state;
} }


public JUnit5Context withTestInstanceProvider(TestInstanceProvider testInstanceProvider) { public TestInstanceProvider getTestInstanceProvider() {
return with(TestInstanceProvider.class.getName(), testInstanceProvider); return state.testInstanceProvider;
} }


public TestInstanceProvider getTestInstanceProvider() { public BeforeEachCallback getBeforeEachCallback() {
return get(TestInstanceProvider.class.getName(), TestInstanceProvider.class); return state.beforeEachCallback;
} }


public JUnit5Context withBeforeEachCallback(BeforeEachCallback beforeEachCallback) { public TestExtensionRegistry getTestExtensionRegistry() {
return with("beforeEachCallback", beforeEachCallback); return state.testExtensionRegistry;
} }


public BeforeEachCallback getBeforeEachCallback() { public Builder extend() {
return get("beforeEachCallback", BeforeEachCallback.class); return builder(this);
} }


public JUnit5Context withTestExtensionRegistry(TestExtensionRegistry testExtensionRegistry) { public static Builder builder() {
return with("testExtensionRegistry", testExtensionRegistry); return new Builder(null, new State());
} }


public TestExtensionRegistry getTestExtensionRegistry() { public static Builder builder(JUnit5Context context) {
return get("testExtensionRegistry", TestExtensionRegistry.class); return new Builder(context.state, null);
} }


private JUnit5Context with(String key, Object value) { private static final class State implements Cloneable {
Map<String, Object> newMap = new HashMap<>(map);
newMap.put(key, value); TestInstanceProvider testInstanceProvider;
return new JUnit5Context(newMap); BeforeEachCallback beforeEachCallback;
TestExtensionRegistry testExtensionRegistry;

@Override
public State clone() {
try {
return (State) super.clone();
}
catch (CloneNotSupportedException e) {
throw new RuntimeException("State could not be cloned", e);
}
}

} }


private <T> T get(String key, Class<T> clazz) { public static class Builder {
return clazz.cast(map.get(key));
private State originalState;
private State newState;

private Builder(State originalState, State state) {
this.originalState = originalState;
this.newState = state;
}

public Builder withTestInstanceProvider(TestInstanceProvider testInstanceProvider) {
newState().testInstanceProvider = testInstanceProvider;
return this;
}

public Builder withBeforeEachCallback(BeforeEachCallback beforeEachCallback) {
newState().beforeEachCallback = beforeEachCallback;
return this;
}

public Builder withTestExtensionRegistry(TestExtensionRegistry testExtensionRegistry) {
newState().testExtensionRegistry = testExtensionRegistry;
return this;
}

public JUnit5Context build() {
if (newState != null) {
originalState = newState;
newState = null;
}
return new JUnit5Context(originalState);
}

private State newState() {
if (newState == null) {
this.newState = originalState.clone();
}
return newState;
}

} }


} }
Expand Up @@ -79,10 +79,11 @@ public boolean isContainer() {
@Override @Override
public JUnit5Context beforeAll(JUnit5Context context) { public JUnit5Context beforeAll(JUnit5Context context) {
// @formatter:off // @formatter:off
return context return context.extend()
.withTestInstanceProvider(testInstanceProvider(context)) .withTestInstanceProvider(testInstanceProvider(context))
.withBeforeEachCallback(beforeEachCallback(context)) .withBeforeEachCallback(beforeEachCallback(context))
.withTestExtensionRegistry(populateNewTestExtensionRegistryFromExtendWith(testClass, context.getTestExtensionRegistry())); .withTestExtensionRegistry(populateNewTestExtensionRegistryFromExtendWith(testClass, context.getTestExtensionRegistry()))
.build();
// @formatter:on // @formatter:on
} }


Expand Down
Expand Up @@ -24,7 +24,7 @@ public JUnit5EngineDescriptor(TestEngine engine) {


@Override @Override
public JUnit5Context beforeAll(JUnit5Context context) { public JUnit5Context beforeAll(JUnit5Context context) {
return context.withTestExtensionRegistry(new TestExtensionRegistry()); return context.extend().withTestExtensionRegistry(new TestExtensionRegistry()).build();
} }


} }
Expand Up @@ -87,8 +87,8 @@ public boolean isContainer() {


@Override @Override
public JUnit5Context execute(JUnit5Context context) throws Throwable { public JUnit5Context execute(JUnit5Context context) throws Throwable {
JUnit5Context myContext = context.withTestExtensionRegistry( JUnit5Context myContext = context.extend().withTestExtensionRegistry(
populateNewTestExtensionRegistryFromExtendWith(testMethod, context.getTestExtensionRegistry())); populateNewTestExtensionRegistryFromExtendWith(testMethod, context.getTestExtensionRegistry())).build();


TestInstanceProvider provider = context.getTestInstanceProvider(); TestInstanceProvider provider = context.getTestInstanceProvider();
Object testInstance = provider.getTestInstance(); Object testInstance = provider.getTestInstance();
Expand Down

0 comments on commit efb8674

Please sign in to comment.