Skip to content

Commit

Permalink
Introduce ParameterInfo support for extensions
Browse files Browse the repository at this point in the history
`Extensions` can get it from `ExtensionContext.Store` and access all
indexed parameter declarations as well as the arguments for the current
invocation.

Resolves #1139.
  • Loading branch information
marcphilipp committed Mar 5, 2025
1 parent 1e1f8d5 commit 1cc2b09
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void formatTestNames(Blackhole blackhole) throws Exception {
var method = TestCase.class.getDeclaredMethod("parameterizedTest", int.class);
var formatter = new ParameterizedInvocationNameFormatter(
DISPLAY_NAME_PLACEHOLDER + " " + DEFAULT_DISPLAY_NAME + " ({0})", "displayName",
new ParameterizedTestContext(method, method.getAnnotation(ParameterizedTest.class)), 512);
new ParameterizedTestContext(TestCase.class, method, method.getAnnotation(ParameterizedTest.class)), 512);
for (int i = 0; i < argumentsList.size(); i++) {
Arguments arguments = argumentsList.get(i);
blackhole.consume(formatter.format(i, EvaluatedArgumentSet.allOf(arguments)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2015-2025 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.jupiter.params;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.support.ParameterDeclarations;
import org.junit.jupiter.params.support.ParameterInfo;

/**
* @since 5.13
*/
class DefaultParameterInfo implements ParameterInfo {

private final ParameterDeclarations declarations;
private final ArgumentsAccessor arguments;

DefaultParameterInfo(ParameterDeclarations declarations, ArgumentsAccessor arguments) {
this.declarations = declarations;
this.arguments = arguments;
}

@Override
public ParameterDeclarations getDeclarations() {
return this.declarations;
}

@Override
public ArgumentsAccessor getArguments() {
return this.arguments;
}

void store(ExtensionContext context) {
context.getStore(NAMESPACE).put(KEY, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,36 @@

class ParameterizedClassContext implements ParameterizedDeclarationContext<ContainerTemplateInvocationContext> {

private final Class<?> clazz;
private final Class<?> testClass;
private final ParameterizedClass annotation;
private final TestInstance.Lifecycle testInstanceLifecycle;
private final ResolverFacade resolverFacade;
private final InjectionType injectionType;
private final List<ArgumentSetLifecycleMethod> beforeMethods;
private final List<ArgumentSetLifecycleMethod> afterMethods;

ParameterizedClassContext(Class<?> clazz, ParameterizedClass annotation,
ParameterizedClassContext(Class<?> testClass, ParameterizedClass annotation,
TestInstance.Lifecycle testInstanceLifecycle) {
this.clazz = clazz;
this.testClass = testClass;
this.annotation = annotation;
this.testInstanceLifecycle = testInstanceLifecycle;

List<Field> fields = findParameterAnnotatedFields(clazz);
List<Field> fields = findParameterAnnotatedFields(testClass);
if (fields.isEmpty()) {
this.resolverFacade = ResolverFacade.create(ReflectionUtils.getDeclaredConstructor(clazz), annotation);
this.resolverFacade = ResolverFacade.create(ReflectionUtils.getDeclaredConstructor(testClass), annotation);
this.injectionType = InjectionType.CONSTRUCTOR;
}
else {
this.resolverFacade = ResolverFacade.create(clazz, fields);
this.resolverFacade = ResolverFacade.create(testClass, fields);
this.injectionType = InjectionType.FIELDS;
}

this.beforeMethods = findLifecycleMethodsAndAssertStaticAndNonPrivate(clazz, testInstanceLifecycle, TOP_DOWN,
BeforeArgumentSet.class, BeforeArgumentSet::injectArguments, this.resolverFacade);
this.beforeMethods = findLifecycleMethodsAndAssertStaticAndNonPrivate(testClass, testInstanceLifecycle,
TOP_DOWN, BeforeArgumentSet.class, BeforeArgumentSet::injectArguments, this.resolverFacade);

// Make a local copy since findAnnotatedMethods() returns an immutable list.
this.afterMethods = new ArrayList<>(
findLifecycleMethodsAndAssertStaticAndNonPrivate(clazz, testInstanceLifecycle, BOTTOM_UP,
findLifecycleMethodsAndAssertStaticAndNonPrivate(testClass, testInstanceLifecycle, BOTTOM_UP,
AfterArgumentSet.class, AfterArgumentSet::injectArguments, this.resolverFacade));

// Since the bottom-up ordering of afterMethods will later be reversed when the
Expand All @@ -86,14 +86,19 @@ private static List<Field> findParameterAnnotatedFields(Class<?> clazz) {
return findFields(clazz, it -> isAnnotated(it, Parameter.class), BOTTOM_UP);
}

@Override
public Class<?> getTestClass() {
return this.testClass;
}

@Override
public ParameterizedClass getAnnotation() {
return this.annotation;
}

@Override
public Class<?> getAnnotatedElement() {
return this.clazz;
return this.testClass;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
interface ParameterizedDeclarationContext<C> {

Class<?> getTestClass();

Annotation getAnnotation();

AnnotatedElement getAnnotatedElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@

package org.junit.jupiter.params;

import static org.junit.platform.commons.util.ClassLoaderUtils.getClassLoader;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.aggregator.DefaultArgumentsAccessor;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.support.ParameterDeclarations;

class ParameterizedInvocationContext<T extends ParameterizedDeclarationContext<?>> {

Expand Down Expand Up @@ -44,7 +49,8 @@ public void prepareInvocation(ExtensionContext context) {
if (this.declarationContext.isAutoClosingArguments()) {
registerAutoCloseableArgumentsInStoreForClosing(context);
}
new ArgumentCountValidator(this.declarationContext, this.arguments).validate(context);
validateArgumentCount(context);
storeParameterInfo(context);
}

private void registerAutoCloseableArgumentsInStoreForClosing(ExtensionContext context) {
Expand All @@ -58,6 +64,18 @@ private void registerAutoCloseableArgumentsInStoreForClosing(ExtensionContext co
.forEach(closeable -> store.put(argumentIndex.incrementAndGet(), closeable));
}

private void validateArgumentCount(ExtensionContext context) {
new ArgumentCountValidator(this.declarationContext, this.arguments).validate(context);
}

private void storeParameterInfo(ExtensionContext context) {
ParameterDeclarations declarations = this.declarationContext.getResolverFacade().getIndexedParameterDeclarations();
ClassLoader classLoader = getClassLoader(this.declarationContext.getTestClass());
Object[] arguments = this.arguments.getConsumedPayloads();
ArgumentsAccessor accessor = DefaultArgumentsAccessor.create(invocationIndex, classLoader, arguments);
new DefaultParameterInfo(declarations, accessor).store(context);
}

private static class CloseableArgument implements ExtensionContext.Store.CloseableResource {

private final AutoCloseable autoCloseable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@
*/
class ParameterizedTestContext implements ParameterizedDeclarationContext<TestTemplateInvocationContext> {

private final Class<?> testClass;
private final Method method;
private final ParameterizedTest annotation;
private final ResolverFacade resolverFacade;

ParameterizedTestContext(Method method, ParameterizedTest annotation) {
ParameterizedTestContext(Class<?> testClass, Method method, ParameterizedTest annotation) {
this.testClass = testClass;
this.method = Preconditions.notNull(method, "method must not be null");
this.annotation = Preconditions.notNull(annotation, "annotation must not be null");
this.resolverFacade = ResolverFacade.create(method, annotation);
}

@Override
public Class<?> getTestClass() {
return this.testClass;
}

@Override
public ParameterizedTest getAnnotation() {
return this.annotation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public boolean supportsTestTemplate(ExtensionContext context) {
return false;
}

ParameterizedTestContext methodContext = new ParameterizedTestContext(context.getRequiredTestMethod(),
annotation.get());
ParameterizedTestContext methodContext = new ParameterizedTestContext(context.getRequiredTestClass(),
context.getRequiredTestMethod(), annotation.get());

getStore(context).put(DECLARATION_CONTEXT_KEY, methodContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.aggregator.ArgumentsAggregationException;
import org.junit.jupiter.params.aggregator.ArgumentsAggregator;
import org.junit.jupiter.params.aggregator.DefaultArgumentsAccessor;
import org.junit.jupiter.params.aggregator.SimpleArgumentsAggregator;
import org.junit.jupiter.params.converter.ArgumentConverter;
import org.junit.jupiter.params.converter.ConvertWith;
Expand All @@ -56,6 +55,7 @@
import org.junit.jupiter.params.support.FieldContext;
import org.junit.jupiter.params.support.ParameterDeclaration;
import org.junit.jupiter.params.support.ParameterDeclarations;
import org.junit.jupiter.params.support.ParameterInfo;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.function.Try;
Expand Down Expand Up @@ -457,10 +457,11 @@ private static ParameterResolutionException parameterResolutionException(String

private interface Resolver {

Object resolve(ParameterContext parameterContext, int parameterIndex, EvaluatedArgumentSet arguments,
int invocationIndex);
Object resolve(ParameterContext parameterContext, int parameterIndex, ExtensionContext extensionContext,
EvaluatedArgumentSet arguments, int invocationIndex);

Object resolve(FieldContext fieldContext, EvaluatedArgumentSet arguments, int invocationIndex);
Object resolve(FieldContext fieldContext, ExtensionContext extensionContext, EvaluatedArgumentSet arguments,
int invocationIndex);

}

Expand All @@ -475,8 +476,8 @@ private static class Converter implements Resolver {
}

@Override
public Object resolve(ParameterContext parameterContext, int parameterIndex, EvaluatedArgumentSet arguments,
int invocationIndex) {
public Object resolve(ParameterContext parameterContext, int parameterIndex, ExtensionContext extensionContext,
EvaluatedArgumentSet arguments, int invocationIndex) {
Object argument = arguments.getConsumedPayload(parameterIndex);
try {
return this.argumentConverter.convert(argument, parameterContext);
Expand All @@ -487,7 +488,8 @@ public Object resolve(ParameterContext parameterContext, int parameterIndex, Eva
}

@Override
public Object resolve(FieldContext fieldContext, EvaluatedArgumentSet arguments, int invocationIndex) {
public Object resolve(FieldContext fieldContext, ExtensionContext extensionContext,
EvaluatedArgumentSet arguments, int invocationIndex) {
Object argument = arguments.getConsumedPayload(fieldContext.getParameterIndex());
try {
return this.argumentConverter.convert(argument, fieldContext);
Expand Down Expand Up @@ -515,10 +517,9 @@ protected Object aggregateArguments(ArgumentsAccessor accessor, Class<?> targetT
}

@Override
public Object resolve(ParameterContext parameterContext, int parameterIndex, EvaluatedArgumentSet arguments,
int invocationIndex) {
ArgumentsAccessor accessor = DefaultArgumentsAccessor.create(parameterContext, invocationIndex,
arguments.getConsumedPayloads());
public Object resolve(ParameterContext parameterContext, int parameterIndex, ExtensionContext extensionContext,
EvaluatedArgumentSet arguments, int invocationIndex) {
ArgumentsAccessor accessor = ParameterInfo.get(extensionContext).getArguments();
try {
return this.argumentsAggregator.aggregateArguments(accessor, parameterContext);
}
Expand All @@ -529,9 +530,9 @@ public Object resolve(ParameterContext parameterContext, int parameterIndex, Eva
}

@Override
public Object resolve(FieldContext fieldContext, EvaluatedArgumentSet arguments, int invocationIndex) {
ArgumentsAccessor accessor = DefaultArgumentsAccessor.create(fieldContext, invocationIndex,
arguments.getConsumedPayloads());
public Object resolve(FieldContext fieldContext, ExtensionContext extensionContext,
EvaluatedArgumentSet arguments, int invocationIndex) {
ArgumentsAccessor accessor = ParameterInfo.get(extensionContext).getArguments();
try {
return this.argumentsAggregator.aggregateArguments(accessor, fieldContext);
}
Expand Down Expand Up @@ -650,7 +651,7 @@ public Optional<String> getParameterName() {
@Override
public Object resolve(Resolver resolver, ExtensionContext extensionContext, EvaluatedArgumentSet arguments,
int invocationIndex, Optional<ParameterContext> originalParameterContext) {
return resolver.resolve(this, arguments, invocationIndex);
return resolver.resolve(this, extensionContext, arguments, invocationIndex);
}
}

Expand Down Expand Up @@ -692,7 +693,8 @@ public Object resolve(Resolver resolver, ExtensionContext extensionContext, Eval
ParameterContext parameterContext = originalParameterContext //
.filter(it -> it.getParameter().equals(this.parameter)) //
.orElseGet(() -> toParameterContext(extensionContext, originalParameterContext));
return resolver.resolve(parameterContext, getParameterIndex(), arguments, invocationIndex);
return resolver.resolve(parameterContext, getParameterIndex(), extensionContext, arguments,
invocationIndex);
}

private ParameterContext toParameterContext(ExtensionContext extensionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import java.util.function.BiFunction;

import org.apiguardian.api.API;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.converter.DefaultArgumentConverter;
import org.junit.jupiter.params.support.FieldContext;
import org.junit.platform.commons.util.ClassUtils;
import org.junit.platform.commons.util.Preconditions;

Expand All @@ -42,20 +40,11 @@ public class DefaultArgumentsAccessor implements ArgumentsAccessor {
private final Object[] arguments;
private final BiFunction<Object, Class<?>, Object> converter;

public static DefaultArgumentsAccessor create(ParameterContext parameterContext, int invocationIndex,
Object... arguments) {

Preconditions.notNull(parameterContext, "ParameterContext must not be null");
BiFunction<Object, Class<?>, Object> converter = (source, targetType) -> DefaultArgumentConverter.INSTANCE //
.convert(source, targetType, parameterContext);
return new DefaultArgumentsAccessor(converter, invocationIndex, arguments);
}

public static DefaultArgumentsAccessor create(FieldContext fieldContext, int invocationIndex, Object... arguments) {
public static DefaultArgumentsAccessor create(int invocationIndex, ClassLoader classLoader, Object[] arguments) {
Preconditions.notNull(classLoader, "ClassLoader must not be null");

Preconditions.notNull(fieldContext, "FieldContext must not be null");
BiFunction<Object, Class<?>, Object> converter = (source, targetType) -> DefaultArgumentConverter.INSTANCE //
.convert(source, targetType, fieldContext);
.convert(source, targetType, classLoader);
return new DefaultArgumentsAccessor(converter, invocationIndex, arguments);
}

Expand Down
Loading

0 comments on commit 1cc2b09

Please sign in to comment.