Search in sources :

Example 16 with ExtensionContext

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

the class ClassTestDescriptor method invokeBeforeAllMethods.

private void invokeBeforeAllMethods(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    Object testInstance = extensionContext.getTestInstance().orElse(null);
    for (Method method : this.beforeAllMethods) {
        throwableCollector.execute(() -> executableInvoker.invoke(method, testInstance, extensionContext, registry));
        if (throwableCollector.isNotEmpty()) {
            break;
        }
    }
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Method(java.lang.reflect.Method) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 17 with ExtensionContext

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

the class ExecutableInvoker method resolveParameter.

private Object resolveParameter(ParameterContext parameterContext, Executable executable, ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) {
    try {
        // @formatter:off
        List<ParameterResolver> matchingResolvers = extensionRegistry.stream(ParameterResolver.class).filter(resolver -> resolver.supportsParameter(parameterContext, extensionContext)).collect(toList());
        if (matchingResolvers.isEmpty()) {
            throw new ParameterResolutionException(String.format("No ParameterResolver registered for parameter [%s] in executable [%s].", parameterContext.getParameter(), executable.toGenericString()));
        }
        if (matchingResolvers.size() > 1) {
            // @formatter:off
            String resolverNames = matchingResolvers.stream().map(resolver -> resolver.getClass().getName()).collect(joining(", "));
            // @formatter:on
            throw new ParameterResolutionException(String.format("Discovered multiple competing ParameterResolvers for parameter [%s] in executable [%s]: %s", parameterContext.getParameter(), executable.toGenericString(), resolverNames));
        }
        ParameterResolver resolver = matchingResolvers.get(0);
        Object value = resolver.resolveParameter(parameterContext, extensionContext);
        validateResolvedType(parameterContext.getParameter(), value, executable, resolver);
        logger.trace(() -> String.format("ParameterResolver [%s] resolved a value of type [%s] for parameter [%s] in executable [%s].", resolver.getClass().getName(), (value != null ? value.getClass().getName() : null), parameterContext.getParameter(), executable.toGenericString()));
        return value;
    } catch (ParameterResolutionException ex) {
        throw ex;
    } catch (Throwable ex) {
        throw new ParameterResolutionException(String.format("Failed to resolve parameter [%s] in executable [%s]", parameterContext.getParameter(), executable.toGenericString()), ex);
    }
}
Also used : ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) Preconditions(org.junit.platform.commons.util.Preconditions) ReflectionUtils.isAssignableTo(org.junit.platform.commons.util.ReflectionUtils.isAssignableTo) Logger(org.junit.platform.commons.logging.Logger) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) API(org.apiguardian.api.API) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) Constructor(java.lang.reflect.Constructor) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) Collectors.joining(java.util.stream.Collectors.joining) LoggerFactory(org.junit.platform.commons.logging.LoggerFactory) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable) Optional(java.util.Optional) Method(java.lang.reflect.Method) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException)

Example 18 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext in project component-runtime by Talend.

the class JUnit5HttpApi method beforeAll.

@Override
public void beforeAll(final ExtensionContext extensionContext) {
    final HttpApi config = extensionContext.getElement().map(c -> c.getAnnotation(HttpApi.class)).orElse(null);
    if (config != null) {
        setGlobalProxyConfiguration(config.globalProxyConfiguration());
        setLogLevel(config.logLevel());
        setPort(config.port());
        newInstance(config.responseLocator(), ResponseLocator.class).ifPresent(this::setResponseLocator);
        newInstance(config.headerFilter(), Predicate.class).ifPresent(this::setHeaderFilter);
        newInstance(config.executor(), Executor.class).ifPresent(this::setExecutor);
        newInstance(config.sslContext(), Supplier.class).map(s -> SSLContext.class.cast(s.get())).ifPresent(this::setSslContext);
        setSkipProxyHeaders(config.skipProxyHeaders());
        if (config.useSsl()) {
            activeSsl();
        }
    }
    extensionContext.getStore(NAMESPACE).put(HttpApiHandler.class.getName(), this);
    final HandlerImpl<JUnit5HttpApi> handler = new HandlerImpl<>(this, null, null);
    extensionContext.getStore(NAMESPACE).put(HandlerImpl.class.getName(), handler);
    handler.start();
}
Also used : SSLContext(javax.net.ssl.SSLContext) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ResponseLocator(org.talend.sdk.component.junit.http.api.ResponseLocator) HttpApiInject(org.talend.sdk.component.junit.http.junit5.HttpApiInject) Supplier(java.util.function.Supplier) HandlerImpl(org.talend.sdk.component.junit.http.internal.impl.HandlerImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpApiHandler(org.talend.sdk.component.junit.http.api.HttpApiHandler) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) JUnit5InjectionSupport(org.talend.sdk.component.junit.base.junit5.JUnit5InjectionSupport) Handlers(org.talend.sdk.component.junit.http.internal.impl.Handlers) Annotation(java.lang.annotation.Annotation) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) Optional(java.util.Optional) DefaultResponseLocator(org.talend.sdk.component.junit.http.internal.impl.DefaultResponseLocator) HttpApi(org.talend.sdk.component.junit.http.junit5.HttpApi) Executor(java.util.concurrent.Executor) ResponseLocator(org.talend.sdk.component.junit.http.api.ResponseLocator) DefaultResponseLocator(org.talend.sdk.component.junit.http.internal.impl.DefaultResponseLocator) HttpApiHandler(org.talend.sdk.component.junit.http.api.HttpApiHandler) HttpApi(org.talend.sdk.component.junit.http.junit5.HttpApi) HandlerImpl(org.talend.sdk.component.junit.http.internal.impl.HandlerImpl) Predicate(java.util.function.Predicate)

Example 19 with ExtensionContext

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

the class TestMethodTestDescriptor method prepare.

// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception {
    ExtensionRegistry registry = populateNewExtensionRegistry(context);
    Object testInstance = context.getTestInstanceProvider().getTestInstance(Optional.of(registry));
    ThrowableCollector throwableCollector = new ThrowableCollector();
    ExtensionContext extensionContext = new MethodExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, context.getConfigurationParameters(), testInstance, throwableCollector);
    // @formatter:off
    return context.extend().withExtensionRegistry(registry).withExtensionContext(extensionContext).withThrowableCollector(throwableCollector).build();
// @formatter:on
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 20 with ExtensionContext

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

the class TestMethodTestDescriptor method invokeBeforeMethodsOrCallbacksUntilExceptionOccurs.

private <T extends Extension> void invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(JupiterEngineExecutionContext context, BiFunction<ExtensionContext, T, Executable> generator, Class<T> type) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    for (T callback : registry.getExtensions(type)) {
        Executable executable = generator.apply(extensionContext, callback);
        throwableCollector.execute(executable);
        if (throwableCollector.isNotEmpty()) {
            break;
        }
    }
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Executable(org.junit.jupiter.api.function.Executable) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Aggregations

ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)37 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)13 Method (java.lang.reflect.Method)11 ThrowableCollector (org.junit.jupiter.engine.execution.ThrowableCollector)11 Test (org.junit.jupiter.api.Test)10 Optional (java.util.Optional)7 UniqueId (org.junit.platform.engine.UniqueId)5 List (java.util.List)4 Stream (java.util.stream.Stream)4 API (org.apiguardian.api.API)4 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)4 Executable (org.junit.jupiter.api.function.Executable)4 JupiterEngineExecutionContext (org.junit.jupiter.engine.execution.JupiterEngineExecutionContext)4 Preconditions (org.junit.platform.commons.util.Preconditions)4 Arrays (java.util.Arrays)3 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)3 BeforeEachCallback (org.junit.jupiter.api.extension.BeforeEachCallback)3 TestTemplateInvocationContext (org.junit.jupiter.api.extension.TestTemplateInvocationContext)3 ClassExtensionContext (org.junit.jupiter.engine.descriptor.ClassExtensionContext)3 JUnitException (org.junit.platform.commons.JUnitException)3