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);
}
}
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);
}
}
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;
}
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);
}
}
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;
}
Aggregations