diff --git a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java index c36d29d0..ab59a0f1 100644 --- a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java +++ b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java @@ -3,7 +3,9 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.function.UnaryOperator; import org.junit.jupiter.params.ParameterizedTest; @@ -55,4 +57,16 @@ void testIsNumber(Number number) { // Expect: assertThat(number).isInstanceOf(Number.class); } + + @DataProvider + public static List>> listOfListOfUnaryOperator() { + return Collections.singletonList(Collections.singletonList((string) -> "merged-" + string)); + } + + @ParameterizedTest + @UseDataProvider("listOfListOfUnaryOperator") + public void testListOfListOfUnaryOperator(UnaryOperator operator) { + // Expect: + assertThat(operator.apply("test")).isEqualTo("merged-test"); + } } diff --git a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java index 1d1b95fb..fdd4605e 100644 --- a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java +++ b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java @@ -3,9 +3,11 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.UnaryOperator; import org.junit.jupiter.params.ParameterizedTest; @@ -60,4 +62,16 @@ void testSingleArgIterableOfString(String string) { // Expected: assertThat(string).isNotEqualTo("1234"); } -} \ No newline at end of file + + @DataProvider + public static List> listOfUnaryOperator() { + return Collections.singletonList((string) -> "merged" + string); + } + + @ParameterizedTest + @UseDataProvider("listOfUnaryOperator") + public void testListOfUnaryOperator(UnaryOperator operator) { + // Expected: + assertThat(operator.apply("test")).isEqualTo("mergedtest"); + } +} diff --git a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java index 768c5c05..ae8c948e 100644 --- a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java +++ b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java @@ -3,7 +3,9 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.function.UnaryOperator; import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.extension.ExtendWith; @@ -58,4 +60,16 @@ void testIsNumber(Number number) { // Expect: assertThat(number).isInstanceOf(Number.class); } + + @DataProvider + public static List>> listOfListOfUnaryOperator() { + return Collections.singletonList(Collections.singletonList((string) -> "merged-" + string)); + } + + @TestTemplate + @UseDataProvider("listOfListOfUnaryOperator") + public void testListOfListOfUnaryOperator(UnaryOperator operator) { + // Expect: + assertThat(operator.apply("test")).isEqualTo("merged-test"); + } } diff --git a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java index aeea06c9..458c9055 100644 --- a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java +++ b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java @@ -2,10 +2,13 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.UnaryOperator; import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.extension.ExtendWith; @@ -63,4 +66,16 @@ void testSingleArgIterableOfString(String string) { // Expected: assertThat(string).isNotEqualTo("1234"); } -} \ No newline at end of file + + @DataProvider + public static List> listOfUnaryOperator() { + return Collections.singletonList((string) -> "merged" + string); + } + + @TestTemplate + @UseDataProvider("listOfUnaryOperator") + public void testListOfUnaryOperator(UnaryOperator operator) { + // Expected: + assertThat(operator.apply("test")).isEqualTo("mergedtest"); + } +} diff --git a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderListArgAcceptanceTest.java b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderListArgAcceptanceTest.java index 6286c92e..af909ac1 100644 --- a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderListArgAcceptanceTest.java +++ b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderListArgAcceptanceTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.junit.Test; @@ -44,4 +45,24 @@ public void test(List strings, String expectedValue) { // Expected: assertThat(strings).contains(expectedValue); } -} \ No newline at end of file + + public interface UnaryOperator { + T apply(T arg); + } + + @DataProvider + public static List>> listOfListOfUnaryOperator() { + return Collections.singletonList(Collections.>singletonList(new UnaryOperator() { + @Override + public String apply(String arg) { + return "merged-" + arg; + } + })); + } + + @Test + @UseDataProvider("listOfListOfUnaryOperator") + public void testListOfListOfUnaryOperator(UnaryOperator operator) { + assertThat(operator.apply("test")).isEqualTo("merged-test"); + } +} diff --git a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderSingleArgAcceptanceTest.java b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderSingleArgAcceptanceTest.java index 53bd62c9..b6b2598d 100644 --- a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderSingleArgAcceptanceTest.java +++ b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderSingleArgAcceptanceTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -63,4 +64,25 @@ public void testSingleArgIterableOfString(String string) { // Expected: assertThat(string).isNotEqualTo("1234"); } -} \ No newline at end of file + + public interface UnaryOperator { + T apply(T arg); + } + + @DataProvider + public static List> dataProviderListOfUnaryOperator() { + return Collections.>singletonList(new UnaryOperator() { + @Override + public String apply(String arg) { + return "merged" + arg; + } + }); + } + + @Test + @UseDataProvider + public void testListOfUnaryOperator(UnaryOperator operator) { + // Expected: + assertThat(operator.apply("test")).isEqualTo("mergedtest"); + } +} diff --git a/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/DataConverter.java b/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/DataConverter.java index 47bce6ed..9aa9cb56 100644 --- a/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/DataConverter.java +++ b/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/DataConverter.java @@ -57,7 +57,7 @@ public DataConverter() { * * * Please note, that {@link Iterable} can be replaced by any valid subtype (checked via {@link Class#isAssignableFrom(Class)}). As well - * as an arbitrary inner type is also accepted. Only rawtypes are not supported currently. + * as an arbitrary inner type is also accepted. * * @param type to be checked for convertibility (use either {@link Method#getGenericReturnType()}, {@link Method#getReturnType()}, or * simple {@link Class} if possible) @@ -70,11 +70,7 @@ public boolean canConvert(Type type) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; - - Type rawType = parameterizedType.getRawType(); - if (Iterable.class.isAssignableFrom((Class) rawType)) { - return canConvertIterableOf(parameterizedType); - } + return Iterable.class.isAssignableFrom((Class) parameterizedType.getRawType()); } return false; } @@ -103,18 +99,6 @@ public List convert(Object data, boolean isVarargs, Class[] paramet return super.convert(data, isVarargs, parameterTypes, context); } - private boolean canConvertIterableOf(ParameterizedType parameterizedType) { - if (parameterizedType.getActualTypeArguments().length == 1) { - Type innerType = parameterizedType.getActualTypeArguments()[0]; - if (parameterizedType.getActualTypeArguments()[0] instanceof ParameterizedType) { - ParameterizedType innerType2 = (ParameterizedType) innerType; - return Iterable.class.isAssignableFrom((Class) innerType2.getRawType()); - } - return true; - } - return false; - } - public void setObjectArrayConverter(ObjectArrayConverter objectArrayConverter) { this.objectArrayConverter = objectArrayConverter; } diff --git a/junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/DataConverterTest.java b/junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/DataConverterTest.java index bd57d57a..2dd48b0b 100644 --- a/junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/DataConverterTest.java +++ b/junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/DataConverterTest.java @@ -112,6 +112,30 @@ public void testCanConvertShouldReturnTrueIfTypeIsListOfObject() { assertThat(result).isTrue(); } + @Test + public void testCanConvertShouldReturnTrueIfTypeIsListOfNonIterableParameterizedType() { + // Given: + Type type = getMethod("methodReturningListOfNonIterableParameterizedType").getGenericReturnType(); + + // When: + boolean result = underTest.canConvert(type); + + // Then: + assertThat(result).isTrue(); + } + + @Test + public void testCanConvertShouldReturnTrueIfTypeIsListOfRawType() { + // Given: + Type type = getMethod("methodReturningListOfRawType").getGenericReturnType(); + + // When: + boolean result = underTest.canConvert(type); + + // Then: + assertThat(result).isTrue(); + } + @Test public void testCanConvertShouldReturnTrueIfTypeIsIterableOfWildcard() { // Given: @@ -169,7 +193,7 @@ public void testCanConvertShouldReturnFalseIfTypeIsTwoArgList() { boolean result = underTest.canConvert(type); // Then: - assertThat(result).isFalse(); + assertThat(result).isTrue(); } @Test @@ -533,6 +557,15 @@ public static List methodReturningListOfObject() { return null; } + @SuppressWarnings("rawtypes") + public static List methodReturningListOfRawType() { + return null; + } + + public static List> methodReturningListOfNonIterableParameterizedType() { + return null; + } + public static List methodReturningIterableOfWildcard() { return null; }