From 9ddc533d7dd04aafbe9f39a88c50cee7ae2ce6a5 Mon Sep 17 00:00:00 2001 From: Jordon Phillips Date: Thu, 28 Dec 2023 15:38:58 +0100 Subject: [PATCH] Make parameterizedTestSource public This makes the `parameterizedTestSource` method of the test suite class public, allowing people to use a customized suite as a source for JUnit parameterized tests. Previously the only way to use the test suite as a parameterized test source was to use the default method that only lets you customize the class loader by passing in a class to source it from. --- .../testrunner/SmithyTestSuite.java | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestSuite.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestSuite.java index 544cc5409d6..943c4d4e17b 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestSuite.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestSuite.java @@ -66,13 +66,13 @@ public static SmithyTestSuite runner() { * of the given {@code contextClass}, and that model discovery should be * used using the given {@code contextClass}. * - *

Each returns {@code Object[]} contains the filename of the test as + *

Each returned {@code Object[]} contains the filename of the test as * the first argument, followed by a {@code Callable} * as the second argument. All a parameterized test needs to do is call * {@code call} on the provided {@code Callable} to execute the test and * fail if the test case is invalid. * - *

For example, the following can used as a unit test: + *

For example, the following can be used as a unit test: * *

{@code
      * import java.util.concurrent.Callable;
@@ -107,7 +107,49 @@ public static Stream defaultParameterizedTestSource(Class contextCl
                 .parameterizedTestSource();
     }
 
-    private Stream parameterizedTestSource() {
+    /**
+     * Factory method used to create a JUnit 5 {@code ParameterizedTest}
+     * {@code MethodSource}.
+     *
+     * 

Test cases need to be added to the test suite before calling this, + * for example by using {@link #addTestCasesFromDirectory(Path)}. + * + *

Each returned {@code Object[]} contains the filename of the test as + * the first argument, followed by a {@code Callable} + * as the second argument. All a parameterized test needs to do is call + * {@code call} on the provided {@code Callable} to execute the test and + * fail if the test case is invalid. + * + *

For example, the following can be used as a unit test: + * + *

{@code
+     * import java.util.concurrent.Callable;
+     * import java.util.stream.Stream;
+     * import org.junit.jupiter.params.ParameterizedTest;
+     * import org.junit.jupiter.params.provider.MethodSource;
+     * import software.amazon.smithy.model.validation.testrunner.SmithyTestCase;
+     * import software.amazon.smithy.model.validation.testrunner.SmithyTestSuite;
+     *
+     * public class TestRunnerTest {
+     *     \@ParameterizedTest(name = "\{0\}")
+     *     \@MethodSource("source")
+     *     public void testRunner(String filename, Callable<SmithyTestCase.Result> callable) throws Exception {
+     *         callable.call();
+     *     }
+     *
+     *     public static Stream<?> source() {
+     *         ModelAssembler assembler = Model.assembler(TestRunnerTest.class.getClassLoader());
+     *         return SmithyTestSuite.runner()
+     *                 .setModelAssemblerFactory(assembler::copy)
+     *                 .addTestCasesFromUrl(TestRunnerTest.class.getResource("errorfiles"))
+     *                 .parameterizedTestSource();
+     *     }
+     * }
+     * }
+ * + * @return Returns the Stream that should be used as a JUnit 5 {@code MethodSource} return value. + */ + public Stream parameterizedTestSource() { return cases.stream().map(testCase -> { Callable callable = createTestCaseCallable(testCase); Callable wrappedCallable = () -> callable.call().unwrap();