Skip to content

Commit

Permalink
Rename MethodContext to MethodInvocationContext
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks msg systems ag
(http://www.msg-systems.com) for supporting the JUnit crowdfunding
campaign!
------------------------------------------------------------------------
  • Loading branch information
sbrannen committed Jan 3, 2016
1 parent 52792a1 commit 0fd7630
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 98 deletions.
Expand Up @@ -13,7 +13,7 @@
import java.lang.reflect.Parameter;

import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.api.extension.MethodContext;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.MethodParameterResolver;
import org.junit.gen5.commons.util.ReflectionUtils;

Expand All @@ -23,12 +23,16 @@
public class CustomAnnotationParameterResolver implements MethodParameterResolver {

@Override
public boolean supports(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext) {
public boolean supports(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) {

return parameter.isAnnotationPresent(CustomAnnotation.class);
}

@Override
public Object resolve(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext) {
public Object resolve(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) {

return ReflectionUtils.newInstance(parameter.getType());
}

Expand Down
Expand Up @@ -13,7 +13,7 @@
import java.lang.reflect.Parameter;

import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.api.extension.MethodContext;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.MethodParameterResolver;

/**
Expand All @@ -22,12 +22,16 @@
public class CustomTypeParameterResolver implements MethodParameterResolver {

@Override
public boolean supports(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext) {
public boolean supports(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) {

return parameter.getType().equals(CustomType.class);
}

@Override
public Object resolve(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext) {
public Object resolve(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) {

return new CustomType();
}

Expand Down
Expand Up @@ -13,12 +13,12 @@
import java.lang.reflect.Method;

/**
* {@code MethodContext} encapsulates the <em>context</em> in which
* {@code MethodInvocationContext} encapsulates the <em>context</em> in which
* a method is to be invoked.
*
* @since 5.0
*/
public interface MethodContext {
public interface MethodInvocationContext {

Object getInstance();

Expand Down
Expand Up @@ -29,28 +29,28 @@ public interface MethodParameterResolver extends ExtensionPoint {

/**
* Determine if this resolver supports resolution of the given {@link Parameter}
* for the supplied {@link MethodContext} and {@link ExtensionContext}.
* for the supplied {@link MethodInvocationContext} and {@link ExtensionContext}.
*
* @param parameter parameter to be resolved
* @param methodContext method context the parameter belongs to
* @param extensionContext extension context of the method about to be executed
* @param methodInvocationContext method invocation context for the parameter
* @param extensionContext extension context of the method about to be invoked
* @return {@code true} if this resolver can resolve the parameter
* @see #resolve
*/
boolean supports(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext)
throws ParameterResolutionException;
boolean supports(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) throws ParameterResolutionException;

/**
* Resolve the given {@link Parameter} for the supplied {@link MethodContext}
* Resolve the given {@link Parameter} for the supplied {@link MethodInvocationContext}
* and {@link ExtensionContext}.
*
* @param parameter parameter to be resolved
* @param methodContext method context the parameter belongs to
* @param extensionContext extension context of the method about to be executed
* @param methodInvocationContext method invocation context for the parameter
* @param extensionContext extension context of the method about to be invoked
* @return the resolved parameter object
* @see #supports
*/
Object resolve(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext)
throws ParameterResolutionException;
Object resolve(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) throws ParameterResolutionException;

}
Expand Up @@ -11,7 +11,7 @@
package org.junit.gen5.engine.junit5.descriptor;

import static org.junit.gen5.commons.util.AnnotationUtils.findAnnotatedMethods;
import static org.junit.gen5.engine.junit5.descriptor.MethodContextImpl.methodContext;
import static org.junit.gen5.engine.junit5.descriptor.MethodInvocationContextFactory.methodInvocationContext;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -216,13 +216,13 @@ private void registerAnnotatedMethodsAsExtensions(TestExtensionRegistry extensio

private BeforeAllExtensionPoint synthesizeBeforeAllExtensionPoint(TestExtensionRegistry registry, Method method) {
return (BeforeAllExtensionPoint) extensionContext -> {
new MethodInvoker(extensionContext, registry).invoke(methodContext(null, method));
new MethodInvoker(extensionContext, registry).invoke(methodInvocationContext(null, method));
};
}

private AfterAllExtensionPoint synthesizeAfterAllExtensionPoint(TestExtensionRegistry registry, Method method) {
return (AfterAllExtensionPoint) extensionContext -> {
new MethodInvoker(extensionContext, registry).invoke(methodContext(null, method));
new MethodInvoker(extensionContext, registry).invoke(methodInvocationContext(null, method));
};
}

Expand All @@ -243,7 +243,7 @@ private void runMethodInTestExtensionContext(Method method, TestExtensionContext

// @formatter:off
ReflectionUtils.getOuterInstance(context.getTestInstance(), method.getDeclaringClass())
.ifPresent(instance -> new MethodInvoker(context, registry).invoke(methodContext(instance, method)));
.ifPresent(instance -> new MethodInvoker(context, registry).invoke(methodInvocationContext(instance, method)));
// @formatter:on
}

Expand Down

This file was deleted.

@@ -0,0 +1,55 @@
/*
* 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.gen5.engine.junit5.descriptor;

import java.lang.reflect.Method;

import org.junit.gen5.api.extension.MethodInvocationContext;

/**
* Factory for {@link MethodInvocationContext} instances.
*
* @since 5.0
*/
class MethodInvocationContextFactory {

/**
* Create a new {@link MethodInvocationContext} based on the supplied
* {@code instance} and {@code method}.
*/
static MethodInvocationContext methodInvocationContext(Object instance, Method method) {
return new DefaultMethodInvocationContext(instance, method);
}

private static class DefaultMethodInvocationContext implements MethodInvocationContext {

private final Object instance;

private final Method method;

private DefaultMethodInvocationContext(Object instance, Method method) {
this.instance = instance;
this.method = method;
}

@Override
public Object getInstance() {
return this.instance;
}

@Override
public Method getMethod() {
return this.method;
}

}

}
Expand Up @@ -10,7 +10,7 @@

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

import static org.junit.gen5.engine.junit5.descriptor.MethodContextImpl.methodContext;
import static org.junit.gen5.engine.junit5.descriptor.MethodInvocationContextFactory.methodInvocationContext;

import java.lang.reflect.Method;
import java.util.Set;
Expand All @@ -20,7 +20,7 @@
import org.junit.gen5.api.extension.BeforeEachExtensionPoint;
import org.junit.gen5.api.extension.ConditionEvaluationResult;
import org.junit.gen5.api.extension.InstancePostProcessor;
import org.junit.gen5.api.extension.MethodContext;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.JavaSource;
Expand Down Expand Up @@ -158,9 +158,9 @@ private void invokeTestMethod(TestExtensionContext testExtensionContext,
TestExtensionRegistry testExtensionRegistry, ThrowableCollector throwableCollector) {

throwableCollector.execute(() -> {
MethodContext methodContext = methodContext(testExtensionContext.getTestInstance(),
testExtensionContext.getTestMethod());
new MethodInvoker(testExtensionContext, testExtensionRegistry).invoke(methodContext);
MethodInvocationContext methodInvocationContext = methodInvocationContext(
testExtensionContext.getTestInstance(), testExtensionContext.getTestMethod());
new MethodInvoker(testExtensionContext, testExtensionRegistry).invoke(methodInvocationContext);
});
}

Expand Down
Expand Up @@ -18,15 +18,16 @@
import java.util.List;

import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.api.extension.MethodContext;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.MethodParameterResolver;
import org.junit.gen5.api.extension.ParameterResolutionException;
import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.engine.junit5.execution.TestExtensionRegistry.ApplicationOrder;

/**
* {@code MethodInvoker} encapsulates the invocation of a method, including support for dynamic resolution of method
* parameters via {@link MethodParameterResolver MethodParameterResolvers}.
* {@code MethodInvoker} encapsulates the invocation of a method, including
* support for dynamic resolution of method parameters via
* {@link MethodParameterResolver MethodParameterResolvers}.
*
* @since 5.0
*/
Expand All @@ -41,41 +42,42 @@ public MethodInvoker(ExtensionContext extensionContext, TestExtensionRegistry ex
this.extensionRegistry = extensionRegistry;
}

public Object invoke(MethodContext methodContext) {
return ReflectionUtils.invokeMethod(methodContext.getMethod(), methodContext.getInstance(),
resolveParameters(methodContext));
public Object invoke(MethodInvocationContext methodInvocationContext) {
return ReflectionUtils.invokeMethod(methodInvocationContext.getMethod(), methodInvocationContext.getInstance(),
resolveParameters(methodInvocationContext));
}

/**
* Resolve the array of parameters for the configured method.
*
* @return the array of Objects to be used as parameters in the method invocation; never {@code null} though
* potentially empty
* @return the array of Objects to be used as parameters in the method
* invocation; never {@code null} though potentially empty
*/
private Object[] resolveParameters(MethodContext methodContext) throws ParameterResolutionException {
private Object[] resolveParameters(MethodInvocationContext methodInvocationContext)
throws ParameterResolutionException {
// @formatter:off
return Arrays.stream(methodContext.getMethod().getParameters())
.map(param -> resolveParameter(param, methodContext))
return Arrays.stream(methodInvocationContext.getMethod().getParameters())
.map(param -> resolveParameter(param, methodInvocationContext))
.toArray(Object[]::new);
// @formatter:on
}

private Object resolveParameter(Parameter parameter, MethodContext methodContext)
private Object resolveParameter(Parameter parameter, MethodInvocationContext methodInvocationContext)
throws ParameterResolutionException {

try {
final List<MethodParameterResolver> matchingResolvers = new ArrayList<>();
extensionRegistry.stream(MethodParameterResolver.class, ApplicationOrder.FORWARD).forEach(
registeredExtensionPoint -> {
if (registeredExtensionPoint.getExtensionPoint().supports(parameter, methodContext,
if (registeredExtensionPoint.getExtensionPoint().supports(parameter, methodInvocationContext,
extensionContext))
matchingResolvers.add(registeredExtensionPoint.getExtensionPoint());
});

if (matchingResolvers.size() == 0) {
throw new ParameterResolutionException(
String.format("No MethodParameterResolver registered for parameter [%s] in method [%s].", parameter,
methodContext.getMethod().toGenericString()));
methodInvocationContext.getMethod().toGenericString()));
}
if (matchingResolvers.size() > 1) {
// @formatter:off
Expand All @@ -85,16 +87,16 @@ private Object resolveParameter(Parameter parameter, MethodContext methodContext
// @formatter:on
throw new ParameterResolutionException(String.format(
"Discovered multiple competing MethodParameterResolvers for parameter [%s] in method [%s]: %s",
parameter, methodContext.getMethod().toGenericString(), resolverNames));
parameter, methodInvocationContext.getMethod().toGenericString(), resolverNames));
}
return matchingResolvers.get(0).resolve(parameter, methodContext, extensionContext);
return matchingResolvers.get(0).resolve(parameter, methodInvocationContext, extensionContext);
}
catch (Throwable ex) {
if (ex instanceof ParameterResolutionException) {
throw (ParameterResolutionException) ex;
}
throw new ParameterResolutionException(String.format("Failed to resolve parameter [%s] in method [%s]",
parameter, methodContext.getMethod().toGenericString()), ex);
parameter, methodInvocationContext.getMethod().toGenericString()), ex);
}
}

Expand Down
Expand Up @@ -16,7 +16,7 @@

import org.junit.gen5.api.TestName;
import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.api.extension.MethodContext;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.MethodParameterResolver;

/**
Expand All @@ -29,12 +29,16 @@
public class TestNameParameterResolver implements MethodParameterResolver {

@Override
public boolean supports(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext) {
public boolean supports(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) {

return (parameter.getType() == String.class && isAnnotated(parameter, TestName.class));
}

@Override
public String resolve(Parameter parameter, MethodContext methodContext, ExtensionContext extensionContext) {
public String resolve(Parameter parameter, MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) {

return extensionContext.getDisplayName();
}

Expand Down

0 comments on commit 0fd7630

Please sign in to comment.