com.tngtech.junit.dataprovider.DataProviders Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-dataprovider-core Show documentation
Show all versions of junit-dataprovider-core Show documentation
The common core for a TestNG like dataprovider runner for JUnit.
package com.tngtech.junit.dataprovider;
import static com.tngtech.junit.dataprovider.Preconditions.checkNotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DataProviders {
/**
* Helper method to create an {@link Object} array containing all the given arguments, e.g.
*
*
*
* Object[] a = $("test", 4);
*
*
*
* @param args which should be contained in the resulting {@link Object} array
* @return an {@link Object} array containing all the given {@code args}
* @see #$$
*/
public static Object[] $(Object... args) { // define it with produces var-args warning on user side for java < 6
return args;
}
/**
* Helper method to create an array of the given {@link Object} arrays, e.g.
*
*
*
* // @formatter:off
* Object[][] b = $$(
* $("", 0),
* $("test", 4),
* $("foo bar", 7),
* );
* // @formatter:on
*
*
*
* @param args which should be contained in the resulting array of {@link Object} array
* @return an array of {@link Object} arrays containing all the given {@code args}
* @see #$
*/
public static Object[][] $$(Object[]... args) {
return args;
}
/**
* Creates a dataprovider test for each argument.
*
* @param args which are wrapped in {@link Object} arrays and combined to {@link Object}{@code [][]}
* @return an array which contains {@link Object} arrays for each single argument
*/
public static Object[][] testForEach(Object... args) {
Object[][] result = new Object[args.length][1];
for (int idx = 0; idx < args.length; idx++) {
result[idx][0] = args[idx];
}
return result;
}
/**
* Creates a dataprovider test for each value in the given {@link Enum} class.
*
* @param the type of the enum type subclass modeled by the given {@code Class}
* @param enumClass for which each value is wrapped into an array of {@link Object} arrays
* @return an array which contains {@link Object} arrays for each single value in the given {@link Enum}
* @throws NullPointerException if and only if given {@code enumClass} is {@code null}
*/
public static > Object[][] testForEach(Class enumClass) {
checkNotNull(enumClass, "'enumClass' must not be null");
return testForEach((Object[]) enumClass.getEnumConstants());
}
/**
* Creates a dataprovider test for each combination of elements of the two provided dataproviders.
*
*
*
* Object[][] r = crossProduct(dataProviderMethod1, dataProviderMethod2);
*
*
*
* @param rows1 of first dataprovider which should be cross producted with the second
* @param rows2 of second dataprovider which should be cross producted with the first
* @return an {@link Object} array array containing the cross product of the given {@code rows}
*/
public static Object[][] crossProduct(Object[][] rows1, Object[][] rows2) {
Object[][] rowsOut = new Object[rows1.length * rows2.length][];
int indexOut = 0;
for (Object[] row1 : rows1) {
for (Object[] row2 : rows2) {
Object[] rowOut = new Object[row1.length + row2.length];
System.arraycopy(row1, 0, rowOut, 0, row1.length);
System.arraycopy(row2, 0, rowOut, row1.length, row2.length);
rowsOut[indexOut++] = rowOut;
}
}
return rowsOut;
}
/**
* Creates a dataprovider test for each combination of elements of the two provided single arg dataproviders.
*
*
*
* Object[][] r = crossProduct(dataProviderMethod1, dataProviderMethod2);
*
*
*
* @param rows1 of first single arg dataprovider which should be cross producted with the second
* @param rows2 of second single arg dataprovider which should be cross producted with the first
* @return an {@link Object} array array containing the cross product of the given {@code rows}
*/
public static Object[][] crossProductSingleArg(Object[] rows1, Object[] rows2) {
Object[][] rowsOut = new Object[rows1.length * rows2.length][];
int indexOut = 0;
for (Object entry1 : rows1) {
for (Object entry2 : rows2) {
rowsOut[indexOut++] = new Object[] { entry1, entry2 };
}
}
return rowsOut;
}
/**
* Creates a dataprovider test for each combination of elements of the two provided {@link Iterable}s of
* {@link Iterable} dataproviders.
*
*
*
* Object[][] r = crossProduct(iterableOfIterable1, iterableOfIterable2);
*
*
*
* @param rows1 of first {@link Iterable} of {@link Iterable} which should be cross producted with the second
* @param rows2 of second {@link Iterable} of {@link Iterable} which should be cross producted with the first
* @param inner type of first {@link Iterable} parameter in order to also support covariance for inner types,
* e.g. {@code List>}
* @param inner type of second {@link Iterable} parameter in order to also support covariance for inner types,
* e.g. {@code List>}
* @return an {@link Object} array array containing the cross product of the given {@code rows}
*/
public static , V extends Iterable>> Object[][] crossProduct(Iterable rows1,
Iterable rows2) {
List> rowsOut = new ArrayList>();
for (Iterable> row1 : rows1) {
for (Iterable> row2 : rows2) {
rowsOut.add(concat(row1, row2));
}
}
return convert(rowsOut);
}
/**
* Creates a dataprovider test for each combination of elements of the two provided single arg {@link Iterable}s
* dataprovider.
*
*
*
* Object[][] r = crossProduct(iterable1, iterable2);
*
*
*
* @param rows1 of first single arg {@link Iterable} dataprovider which should be cross producted with the second
* @param rows2 of second single arg {@link Iterable} dataprovider which should be cross producted with the first
* @return an {@link Object} array array containing the cross product of the given {@code rows}
*/
public static Object[][] crossProductSingleArg(Iterable> rows1, Iterable> rows2) {
List> rowsOut = new ArrayList>();
for (Object row1 : rows1) {
for (Object row2 : rows2) {
rowsOut.add(Arrays.asList(row1, row2));
}
}
return convert(rowsOut);
}
private static List