Skip to content

Commit

Permalink
support Iterables and Arrays in the ParameterizedExtension
Browse files Browse the repository at this point in the history
This also includes support for single parameter tests.
  • Loading branch information
LiamClark committed Feb 13, 2018
1 parent c24740d commit 99d7f47
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.junit.platform.commons.meta.API;
import org.junit.platform.commons.util.CollectionUtils;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.runners.Parameterized;

@API(Experimental)
public class ParameterizedExtension implements TestTemplateInvocationContextProvider {
private static ExtensionContext.Namespace parameters = ExtensionContext.Namespace.create(
private final static ExtensionContext.Namespace PARAMETERS = ExtensionContext.Namespace.create(
ParameterizedExtension.class);

/**
Expand Down Expand Up @@ -72,7 +73,7 @@ private static boolean validInjectionMix(ExtensionContext context) {

@Override
public Stream<TestTemplateInvocationContext> provide(ContainerExtensionContext context) {
// grabbing the parent ensures the parameters are stored in the same store across multiple TestTemplates.
// grabbing the parent ensures the PARAMETERS are stored in the same store across multiple TestTemplates.
return context.getParent().flatMap(ParameterizedExtension::parameters).map(
o -> testTemplateContextsFromParameters(o, context)).orElse(Stream.empty());
}
Expand Down Expand Up @@ -113,7 +114,7 @@ private static Boolean indexRangeComplete(List<Integer> parameterValues) {
}

private static Optional<Collection<Object[]>> parameters(ExtensionContext context) {
return context.getStore(parameters).getOrComputeIfAbsent("parameterMethod",
return context.getStore(PARAMETERS).getOrComputeIfAbsent("parameterMethod",
k -> new ParameterWrapper(callParameters(context)), ParameterWrapper.class).getValue();

}
Expand Down Expand Up @@ -243,18 +244,14 @@ private static ParameterResolutionException unMatchedAmountOfParametersException
"The amount of parametersFields in the constructor doesn't match those in the provided parametersFields");
}

private static ParameterResolutionException wrongParametersReturnType() {
return new ParameterResolutionException("The @Parameters returns the wrong type");
}

@SuppressWarnings("unchecked")
private static Collection<Object[]> convertParametersMethodReturnType(Object o) {
if (o instanceof Collection) {
return (Collection<Object[]>) o;
}
else {
throw wrongParametersReturnType();
}
private static Collection<Object[]> convertParametersMethodReturnType(Object obj) {
return CollectionUtils.toStream(obj).map(o -> {
if (o instanceof Object[]) {
return (Object[]) o;
}
return new Object[] { o };
}).collect(toList());
}

private static class ParameterWrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void parametrizedWithParameterFieldInjection() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class FibonacciTest {
private static class FibonacciTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
Expand Down Expand Up @@ -88,7 +88,7 @@ void paremeterizedWithConstructorInjection() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class ParameterizedTestWithConstructor {
private static class ParameterizedTestWithConstructor {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
Expand Down Expand Up @@ -116,7 +116,7 @@ void unMatchedConstructorArgumentCount() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class UnMatchedConstructor {
private static class UnMatchedConstructor {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { 0, 2 } });
Expand All @@ -140,7 +140,7 @@ void unMatchedParameterFieldsCount() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class WrongParameters {
private static class WrongParameters {
@Parameterized.Parameter
public int a;
@Parameterized.Parameter(1)
Expand Down Expand Up @@ -168,7 +168,7 @@ void noInjectionMix() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class DoubleInjection {
private static class DoubleInjection {
@Parameterized.Parameter
public int a;

Expand Down Expand Up @@ -220,7 +220,7 @@ void emptyParametersList() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class EmptyParameters {
private static class EmptyParameters {

public EmptyParameters() {
}
Expand All @@ -243,7 +243,7 @@ void duplicatedParameterFieldIndex() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class DuplicatedIndex {
private static class DuplicatedIndex {
@Parameterized.Parameter
public int a;

Expand All @@ -268,7 +268,7 @@ void parametersAreOnlyCalledOnce() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class ParametersCalledOnce {
private static class ParametersCalledOnce {
private static int invocationCount = 0;

public ParametersCalledOnce(int a) {
Expand All @@ -294,7 +294,7 @@ void multipleTestTemplatesShouldBeRun() {
}

@ExtendWith(ParameterizedExtension.class)
static class MultipleTestTemplates {
private static class MultipleTestTemplates {
private static int invocationCount = 0;

public MultipleTestTemplates(int a) {
Expand All @@ -319,6 +319,30 @@ void secondTemplate() {
}
}

@Test
void singleParameterIterable() {
ExecutionEventRecorder eventRecorder = executeTestsForClass(IterableTest.class);
assertThat(eventRecorder.getTestSuccessfulCount()).isEqualTo(2);
}

@ExtendWith(ParameterizedExtension.class)
private static class IterableTest {

public IterableTest(Object o) {

}

@Parameters
public static Object[] data() {
return new Object[] { new Object(), new Object() };
}

@TestTemplate
void dummy() {

}
}

private ExecutionEventRecorder executeTestsForClass(Class<?> testClass) {
LauncherDiscoveryRequest request = request().selectors(selectClass(testClass)).build();
JupiterTestEngine engine = new JupiterTestEngine();
Expand Down

0 comments on commit 99d7f47

Please sign in to comment.