com.teamscale.config.junit.VarargsAggregator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-config-test-fixtures Show documentation
Show all versions of teamscale-config-test-fixtures Show documentation
Support for the common .teamscale.toml config format
package com.teamscale.config.junit;
import java.lang.reflect.Array;
import java.util.Arrays;
import org.junit.jupiter.api.extension.ParameterContext;
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.converter.DefaultArgumentConverter;
import org.junit.platform.commons.util.Preconditions;
/**
* Allows to aggregate multiple arguments to a test method, e.g., from a
* {@link org.junit.jupiter.params.provider.CsvSource}, as a singe varargs
* argument. Annotate your {@code T...} varargs parameter as follows:
* {@code @AggregateWith(VarargsAggregator.class) T... args}.
*
* @see
* ParameterizedTest with varargs
*/
public class VarargsAggregator implements ArgumentsAggregator {
@Override
public Object aggregateArguments(ArgumentsAccessor accessor, ParameterContext context)
throws ArgumentsAggregationException {
Preconditions.condition(context.getParameter().isVarArgs(),
"VarargsAggregator is meant to be applied to varargs arguments only");
Class> componentType = context.getParameter().getType().getComponentType();
Object[] varargs = accessor.toList().stream().skip(context.getIndex()).toArray();
return Arrays.stream(varargs).map(value -> DefaultArgumentConverter.INSTANCE.convert(value, componentType))
.toArray(size -> (Object[]) Array.newInstance(componentType, size));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy