All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teamscale.config.junit.VarargsAggregator Maven / Gradle / Ivy

There is a newer version: 8.9.19
Show newest version
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