Search in sources :

Example 1 with ParameterContext

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

the class ParameterizedTestParameterResolver method resolveParameter.

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
    Object argument = arguments[parameterContext.getIndex()];
    Parameter parameter = parameterContext.getParameter();
    Optional<ConvertWith> annotation = AnnotationUtils.findAnnotation(parameter, ConvertWith.class);
    // @formatter:off
    ArgumentConverter argumentConverter = annotation.map(ConvertWith::value).map(clazz -> (ArgumentConverter) ReflectionUtils.newInstance(clazz)).map(converter -> AnnotationConsumerInitializer.initialize(parameter, converter)).orElse(DefaultArgumentConverter.INSTANCE);
    // @formatter:on
    try {
        return argumentConverter.convert(argument, parameterContext);
    } catch (Exception ex) {
        throw new ParameterResolutionException("Error resolving parameter at index " + parameterContext.getIndex(), ex);
    }
}
Also used : AnnotationConsumerInitializer(org.junit.jupiter.params.support.AnnotationConsumerInitializer) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) ConvertWith(org.junit.jupiter.params.converter.ConvertWith) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable) Optional(java.util.Optional) AnnotationUtils(org.junit.platform.commons.util.AnnotationUtils) Method(java.lang.reflect.Method) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) ArgumentConverter(org.junit.jupiter.params.converter.ArgumentConverter) DefaultArgumentConverter(org.junit.jupiter.params.converter.DefaultArgumentConverter) ArgumentConverter(org.junit.jupiter.params.converter.ArgumentConverter) DefaultArgumentConverter(org.junit.jupiter.params.converter.DefaultArgumentConverter) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ConvertWith(org.junit.jupiter.params.converter.ConvertWith) Parameter(java.lang.reflect.Parameter) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException)

Example 2 with ParameterContext

use of org.junit.jupiter.api.extension.ParameterContext 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 3 with ParameterContext

use of org.junit.jupiter.api.extension.ParameterContext in project logging-log4j2 by apache.

the class AppenderResolver method resolveParameter.

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
    final LoggerContext loggerContext = getParameterLoggerContext(parameterContext, extensionContext);
    if (loggerContext == null) {
        throw new ParameterResolutionException("No LoggerContext defined");
    }
    final String name = parameterContext.findAnnotation(Named.class).map(Named::value).map(s -> s.isEmpty() ? parameterContext.getParameter().getName() : s).orElseThrow(() -> new ParameterResolutionException("No @Named present after checking earlier"));
    final Appender appender = loggerContext.getConfiguration().getAppender(name);
    if (appender == null) {
        throw new ParameterResolutionException("No appender named " + name);
    }
    return appender;
}
Also used : ParameterContext(org.junit.jupiter.api.extension.ParameterContext) LoggerContextResolver.getParameterLoggerContext(org.apache.logging.log4j.core.test.junit.LoggerContextResolver.getParameterLoggerContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Appender(org.apache.logging.log4j.core.Appender) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) Appender(org.apache.logging.log4j.core.Appender) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) LoggerContextResolver.getParameterLoggerContext(org.apache.logging.log4j.core.test.junit.LoggerContextResolver.getParameterLoggerContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 4 with ParameterContext

use of org.junit.jupiter.api.extension.ParameterContext in project hive by apache.

the class DoNothingTCPServerExtension method resolveParameter.

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) throws ParameterResolutionException {
    List<DoNothingTCPServer> servers = (List<DoNothingTCPServer>) context.getStore(ExtensionContext.Namespace.GLOBAL).getOrComputeIfAbsent(context.getUniqueId(), (id) -> new ArrayList<DoNothingTCPServer>());
    try {
        DoNothingTCPServer server = new DoNothingTCPServer();
        server.start();
        servers.add(server);
        return server;
    } catch (IOException e) {
        throw new ParameterResolutionException("Problem when initialising the server; see cause for details.", e);
    }
}
Also used : ParameterContext(org.junit.jupiter.api.extension.ParameterContext) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) List(java.util.List) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) IOException(java.io.IOException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) ArrayList(java.util.ArrayList) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with ParameterContext

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

the class ExecutableInvoker method resolveParameters.

/**
 * Resolve the array of parameters for the supplied executable, target, and
 * outer instance.
 *
 * @param executable the executable for which to resolve parameters
 * @param target an {@code Optional} containing the target on which the
 * executable will be invoked; never {@code null} but should be empty for
 * static methods and constructors
 * @param outerInstance the outer instance that will be supplied as the
 * first argument to a constructor for an inner class; should be {@code null}
 * for methods and constructors for top-level or static classes
 * @param extensionContext the current {@code ExtensionContext}
 * @param extensionRegistry the {@code ExtensionRegistry} to retrieve
 * {@code ParameterResolvers} from
 * @return the array of Objects to be used as parameters in the executable
 * invocation; never {@code null} though potentially empty
 */
private Object[] resolveParameters(Executable executable, Optional<Object> target, Object outerInstance, ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) {
    Preconditions.notNull(target, "target must not be null");
    Parameter[] parameters = executable.getParameters();
    Object[] values = new Object[parameters.length];
    int start = 0;
    // the executable is a constructor for an inner class.
    if (outerInstance != null) {
        values[0] = outerInstance;
        start = 1;
    }
    // Resolve remaining parameters dynamically
    for (int i = start; i < parameters.length; i++) {
        ParameterContext parameterContext = new DefaultParameterContext(parameters[i], i, target);
        values[i] = resolveParameter(parameterContext, executable, extensionContext, extensionRegistry);
    }
    return values;
}
Also used : Parameter(java.lang.reflect.Parameter) ParameterContext(org.junit.jupiter.api.extension.ParameterContext)

Aggregations

ParameterContext (org.junit.jupiter.api.extension.ParameterContext)7 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)5 ParameterResolver (org.junit.jupiter.api.extension.ParameterResolver)5 Parameter (java.lang.reflect.Parameter)4 ParameterResolutionException (org.junit.jupiter.api.extension.ParameterResolutionException)4 Executable (java.lang.reflect.Executable)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 Constructor (java.lang.reflect.Constructor)2 Optional (java.util.Optional)2 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)2 ReflectionUtils (org.junit.platform.commons.util.ReflectionUtils)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Modifier (java.lang.reflect.Modifier)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toList (java.util.stream.Collectors.toList)1 Appender (org.apache.logging.log4j.core.Appender)1