Skip to content

Commit

Permalink
#62 Removed all usages of get/put/removeAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Jan 19, 2016
1 parent b725c08 commit e103a16
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 68 deletions.
Expand Up @@ -64,42 +64,6 @@ public void fromMethodTestDescriptor() {
);
}

@Test
public void flatAttributeAccess() {
ClassTestDescriptor classTestDescriptor = outerClassDescriptor(null);

ExtensionContext parentContext = new ClassBasedContainerExtensionContext(null, null, classTestDescriptor);

assertNull(parentContext.getAttribute("not set"));

parentContext.putAttribute("attr1", "value1");
assertEquals("value1", parentContext.getAttribute("attr1"));

assertEquals("value1", parentContext.removeAttribute("attr1"));
assertNull(parentContext.getAttribute("attr1"));
}

@Test
public void nestedAttributeAccess() {
MethodTestDescriptor methodTestDescriptor = methodDescriptor();
ClassTestDescriptor classTestDescriptor = outerClassDescriptor(methodTestDescriptor);

ExtensionContext parentContext = new ClassBasedContainerExtensionContext(null, null, classTestDescriptor);

MethodBasedTestExtensionContext childContext = new MethodBasedTestExtensionContext(parentContext, null,
methodTestDescriptor, new OuterClass());

parentContext.putAttribute("attr1", "value1");
assertEquals("value1", childContext.getAttribute("attr1"));

childContext.putAttribute("attr1", "value1 changed");
assertEquals("value1 changed", childContext.getAttribute("attr1"));
assertEquals("value1", parentContext.getAttribute("attr1"));

childContext.removeAttribute("attr1");
assertEquals("value1", childContext.getAttribute("attr1"));
}

@Test
public void reportEntriesArePublishedToExecutionContext() {
ClassTestDescriptor classTestDescriptor = outerClassDescriptor(null);
Expand Down
Expand Up @@ -60,14 +60,6 @@ public interface ExtensionContext {

AnnotatedElement getElement();

// Attributes will be removed when storing methods are done

Object getAttribute(String key);

void putAttribute(String key, Object value);

Object removeAttribute(String key);

default Store getStore() {
return getStore(Namespace.DEFAULT);
}
Expand Down
Expand Up @@ -20,9 +20,6 @@

abstract class AbstractExtensionContext implements ExtensionContext {

private final Map<String, Object> attributes = new HashMap<>();

//Will replace attributes if done
private final ExtensionValuesStore valuesStore;

private final ExtensionContext parent;
Expand All @@ -37,7 +34,7 @@ abstract class AbstractExtensionContext implements ExtensionContext {
this.valuesStore = createStore(parent);
}

private final ExtensionValuesStore createStore(ExtensionContext parent) {
private ExtensionValuesStore createStore(ExtensionContext parent) {
ExtensionValuesStore parentStore = null;
if (parent != null) {
parentStore = ((AbstractExtensionContext) parent).valuesStore;
Expand All @@ -55,24 +52,6 @@ public Optional<ExtensionContext> getParent() {
return Optional.ofNullable(parent);
}

@Override
public Object getAttribute(String key) {
Object value = attributes.get(key);
if (value == null && parent != null)
return parent.getAttribute(key);
return value;
}

@Override
public void putAttribute(String key, Object value) {
attributes.put(key, value);
}

@Override
public Object removeAttribute(String key) {
return attributes.remove(key);
}

protected TestDescriptor getTestDescriptor() {
return testDescriptor;
}
Expand Down
Expand Up @@ -15,6 +15,8 @@
import java.lang.reflect.Parameter;

import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.api.extension.ExtensionContext.Namespace;
import org.junit.gen5.api.extension.ExtensionContext.Store;
import org.junit.gen5.api.extension.InstancePostProcessor;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.MethodParameterResolver;
Expand All @@ -34,6 +36,8 @@
*/
public class MockitoExtension implements InstancePostProcessor, MethodParameterResolver {

private static final Namespace namespace = Namespace.of(MockitoExtension.class);

@Override
public void postProcessTestInstance(TestExtensionContext context) {
MockitoAnnotations.initMocks(context.getTestInstance());
Expand All @@ -49,11 +53,11 @@ public boolean supports(Parameter parameter, MethodInvocationContext methodInvoc
@Override
public Object resolve(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) throws ParameterResolutionException {
ExtensionContext.Store mocks = extensionContext.getStore(ExtensionContext.Namespace.of(getClass()));
Store mocks = extensionContext.getStore(namespace);
return getMock(parameter.getType(), mocks);
}

private Object getMock(Class<?> mockType, ExtensionContext.Store mocks) {
private Object getMock(Class<?> mockType, Store mocks) {
return mocks.getOrComputeIfAbsent(mockType, type -> mock(mockType));
}

Expand Down

0 comments on commit e103a16

Please sign in to comment.