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

org.junitpioneer.jupiter.cartesian.CartesianValueArgumentsProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2023 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
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package org.junitpioneer.jupiter.cartesian;

import static java.util.stream.Collectors.toUnmodifiableList;

import java.lang.reflect.Array;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.support.AnnotationConsumer;
import org.junit.platform.commons.PreconditionViolationException;

/**
 * This is a slightly modified copy of Jupiter's {@code ValueArgumentsProvider},
 * except it does NOT support {@code @ParameterizedTest} and implements {@link CartesianArgumentsProvider}
 * for use with {@code @CartesianTest}.
 */
class CartesianValueArgumentsProvider
		implements CartesianParameterArgumentsProvider, AnnotationConsumer {

	private Object[] arguments;

	@Override
	public void accept(CartesianTest.Values source) {
		// @formatter:off
		List arrays =
				// Declaration of  is necessary due to a bug in Eclipse Photon.
				Stream. of(
						source.shorts(),
						source.bytes(),
						source.ints(),
						source.longs(),
						source.floats(),
						source.doubles(),
						source.chars(),
						source.booleans(),
						source.strings(),
						source.classes()
				)
				.filter(array -> Array.getLength(array) > 0)
				.collect(toUnmodifiableList());
		// @formatter:on

		if (arrays.size() != 1)
			throw new PreconditionViolationException("Exactly one type of input must be provided in the @"
					+ CartesianTest.Values.class.getSimpleName() + " annotation, but there were " + arrays.size());

		Object originalArray = arrays.get(0);
		arguments = IntStream
				.range(0, Array.getLength(originalArray)) //
				.mapToObj(index -> Array.get(originalArray, index)) //
				.toArray();
	}

	@Override
	public Stream provideArguments(ExtensionContext context, Parameter parameter) {
		return Arrays.stream(arguments);
	}

}