From 99d7f473201c858ebd432849d87621a6b7958992 Mon Sep 17 00:00:00 2001 From: Liam Cark Date: Mon, 10 Apr 2017 17:08:03 +0200 Subject: [PATCH] support Iterables and Arrays in the ParameterizedExtension This also includes support for single parameter tests. --- .../parameterized/ParameterizedExtension.java | 25 +++++------ .../ParameterizedExtensionTests.java | 42 +++++++++++++++---- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java b/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java index b09adbc83a0a..bf1958f2a5b1 100644 --- a/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java +++ b/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java @@ -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); /** @@ -72,7 +73,7 @@ private static boolean validInjectionMix(ExtensionContext context) { @Override public Stream 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()); } @@ -113,7 +114,7 @@ private static Boolean indexRangeComplete(List parameterValues) { } private static Optional> parameters(ExtensionContext context) { - return context.getStore(parameters).getOrComputeIfAbsent("parameterMethod", + return context.getStore(PARAMETERS).getOrComputeIfAbsent("parameterMethod", k -> new ParameterWrapper(callParameters(context)), ParameterWrapper.class).getValue(); } @@ -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 convertParametersMethodReturnType(Object o) { - if (o instanceof Collection) { - return (Collection) o; - } - else { - throw wrongParametersReturnType(); - } + private static Collection 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 { diff --git a/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java b/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java index 9eb0a40ca03a..d7e849e4a20a 100644 --- a/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java +++ b/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java @@ -49,7 +49,7 @@ void parametrizedWithParameterFieldInjection() { } @ExtendWith(ParameterizedExtension.class) - protected static class FibonacciTest { + private static class FibonacciTest { @Parameters public static Collection data() { return Arrays.asList( @@ -88,7 +88,7 @@ void paremeterizedWithConstructorInjection() { } @ExtendWith(ParameterizedExtension.class) - protected static class ParameterizedTestWithConstructor { + private static class ParameterizedTestWithConstructor { @Parameters public static Collection data() { return Arrays.asList( @@ -116,7 +116,7 @@ void unMatchedConstructorArgumentCount() { } @ExtendWith(ParameterizedExtension.class) - protected static class UnMatchedConstructor { + private static class UnMatchedConstructor { @Parameters public static Collection data() { return Arrays.asList(new Object[][] { { 0, 2 } }); @@ -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) @@ -168,7 +168,7 @@ void noInjectionMix() { } @ExtendWith(ParameterizedExtension.class) - protected static class DoubleInjection { + private static class DoubleInjection { @Parameterized.Parameter public int a; @@ -220,7 +220,7 @@ void emptyParametersList() { } @ExtendWith(ParameterizedExtension.class) - protected static class EmptyParameters { + private static class EmptyParameters { public EmptyParameters() { } @@ -243,7 +243,7 @@ void duplicatedParameterFieldIndex() { } @ExtendWith(ParameterizedExtension.class) - protected static class DuplicatedIndex { + private static class DuplicatedIndex { @Parameterized.Parameter public int a; @@ -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) { @@ -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) { @@ -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();