Search in sources :

Example 1 with InvocationInterceptor

use of org.junit.jupiter.api.extension.InvocationInterceptor in project junit-dataprovider by TNG.

the class UseDataProviderInvocationContextProvider method invokeDataProviderMethodToRetrieveData.

/**
 * Retrieves the test data from given dataprovider method.
 *
 * @param dataProviderMethod the dataprovider method that gives the parameters; never {@code null}
 * @param cacheDataProviderResult determines if the dataprovider result should be cached using
 *            {@code dataProviderMethod} as key
 * @param context the execution context to use to create a {@link TestInfo} if required; never {@code null}
 *
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 * @throws NullPointerException if and only if one of the given arguments is {@code null}
 */
protected Object invokeDataProviderMethodToRetrieveData(Method dataProviderMethod, boolean cacheDataProviderResult, ExtensionContext context) {
    checkNotNull(dataProviderMethod, "'dataProviderMethod' must not be null");
    checkNotNull(context, "'context' must not be null");
    Store store = context.getRoot().getStore(NAMESPACE_USE_DATAPROVIDER);
    Object cached = store.get(dataProviderMethod);
    if (cached != null) {
        return cached;
    }
    try {
        // TODO how to not require junit-jupiter-engine dependency and reuse already existing ExtensionRegistry?
        ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(new DefaultJupiterConfiguration(emptyConfigurationParameters()));
        Object data = executableInvoker.invoke(dataProviderMethod, context.getTestInstance().orElse(null), context, extensionRegistry, InvocationInterceptor::interceptTestFactoryMethod);
        if (cacheDataProviderResult) {
            store.put(dataProviderMethod, data);
        }
        return data;
    } catch (Exception e) {
        throw new ParameterResolutionException(String.format("Exception while invoking dataprovider method '%s': %s", dataProviderMethod.getName(), e.getMessage()), e);
    }
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) DefaultJupiterConfiguration(org.junit.jupiter.engine.config.DefaultJupiterConfiguration) InvocationInterceptor(org.junit.jupiter.api.extension.InvocationInterceptor) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 2 with InvocationInterceptor

use of org.junit.jupiter.api.extension.InvocationInterceptor in project junit-dataprovider by TNG.

the class AbstractUseDataProviderArgumentProvider method invokeDataProviderMethodToRetrieveData.

/**
 * Retrieves the test data from given dataprovider method.
 *
 * @param dataProviderMethod the dataprovider method that gives the parameters; never {@code null}
 * @param cacheDataProviderResult determines if the dataprovider result should be cached using
 *            {@code dataProviderMethod} as key
 * @param context the execution context to use to create a {@link TestInfo} if required; never {@code null}
 *
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 * @throws NullPointerException if and only if one of the given arguments is {@code null}
 */
protected Object invokeDataProviderMethodToRetrieveData(Method dataProviderMethod, boolean cacheDataProviderResult, ExtensionContext context) {
    checkNotNull(dataProviderMethod, "'dataProviderMethod' must not be null");
    checkNotNull(context, "'context' must not be null");
    Store store = context.getRoot().getStore(NAMESPACE_USE_DATAPROVIDER);
    Object cached = store.get(dataProviderMethod);
    if (cached != null) {
        return cached;
    }
    try {
        // TODO how to not require junit-jupiter-engine dependency and reuse already existing ExtensionRegistry?
        ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(new DefaultJupiterConfiguration(emptyConfigurationParameters()));
        Object data = executableInvoker.invoke(dataProviderMethod, context.getTestInstance().orElse(null), context, extensionRegistry, InvocationInterceptor::interceptTestFactoryMethod);
        if (cacheDataProviderResult) {
            store.put(dataProviderMethod, data);
        }
        return data;
    } catch (Exception e) {
        throw new ParameterResolutionException(String.format("Exception while invoking dataprovider method '%s': %s", dataProviderMethod.getName(), e.getMessage()), e);
    }
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) DefaultJupiterConfiguration(org.junit.jupiter.engine.config.DefaultJupiterConfiguration) InvocationInterceptor(org.junit.jupiter.api.extension.InvocationInterceptor) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 3 with InvocationInterceptor

use of org.junit.jupiter.api.extension.InvocationInterceptor in project junit5 by junit-team.

the class InvocationInterceptorChain method chainInterceptors.

private <T> Invocation<T> chainInterceptors(Invocation<T> invocation, InterceptorCall<T> call, List<InvocationInterceptor> interceptors) {
    Invocation<T> result = invocation;
    ListIterator<InvocationInterceptor> iterator = interceptors.listIterator(interceptors.size());
    while (iterator.hasPrevious()) {
        InvocationInterceptor interceptor = iterator.previous();
        result = new InterceptedInvocation<>(result, call, interceptor);
    }
    return result;
}
Also used : InvocationInterceptor(org.junit.jupiter.api.extension.InvocationInterceptor)

Example 4 with InvocationInterceptor

use of org.junit.jupiter.api.extension.InvocationInterceptor in project junit5 by junit-team.

the class ClassBasedTestDescriptor method invokeAfterAllMethods.

private void invokeAfterAllMethods(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    Object testInstance = extensionContext.getTestInstance().orElse(null);
    this.afterAllMethods.forEach(method -> throwableCollector.execute(() -> {
        try {
            executableInvoker.invoke(method, testInstance, extensionContext, registry, ReflectiveInterceptorCall.ofVoidMethod(InvocationInterceptor::interceptAfterAllMethod));
        } catch (Throwable throwable) {
            invokeAfterAllMethodExecutionExceptionHandlers(registry, extensionContext, throwable);
        }
    }));
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) InvocationInterceptor(org.junit.jupiter.api.extension.InvocationInterceptor) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) MutableExtensionRegistry(org.junit.jupiter.engine.extension.MutableExtensionRegistry) ThrowableCollector(org.junit.platform.engine.support.hierarchical.ThrowableCollector) JupiterThrowableCollectorFactory.createThrowableCollector(org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector)

Aggregations

InvocationInterceptor (org.junit.jupiter.api.extension.InvocationInterceptor)4 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)3 Store (org.junit.jupiter.api.extension.ExtensionContext.Store)2 ParameterResolutionException (org.junit.jupiter.api.extension.ParameterResolutionException)2 DefaultJupiterConfiguration (org.junit.jupiter.engine.config.DefaultJupiterConfiguration)2 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 MutableExtensionRegistry (org.junit.jupiter.engine.extension.MutableExtensionRegistry)1 JupiterThrowableCollectorFactory.createThrowableCollector (org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector)1 ThrowableCollector (org.junit.platform.engine.support.hierarchical.ThrowableCollector)1