| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api; |
| 12 | |
| 13 | import static java.util.Spliterator.ORDERED; |
| 14 | import static java.util.Spliterators.spliteratorUnknownSize; |
| 15 | import static org.apiguardian.api.API.Status.MAINTAINED; |
| 16 | |
| 17 | import java.net.URI; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.function.Function; |
| 20 | import java.util.stream.Stream; |
| 21 | import java.util.stream.StreamSupport; |
| 22 | |
| 23 | import org.apiguardian.api.API; |
| 24 | import org.junit.jupiter.api.function.Executable; |
| 25 | import org.junit.jupiter.api.function.ThrowingConsumer; |
| 26 | import org.junit.platform.commons.util.Preconditions; |
| 27 | |
| 28 | /** |
| 29 | * A {@code DynamicTest} is a test case generated at runtime. |
| 30 | * |
| 31 | * <p>It is composed of a {@linkplain DynamicNode#getDisplayName display name} |
| 32 | * and an {@link #getExecutable Executable}. |
| 33 | * |
| 34 | * <p>Instances of {@code DynamicTest} must be generated by factory methods |
| 35 | * annotated with {@link TestFactory @TestFactory}. |
| 36 | * |
| 37 | * <p>Note that dynamic tests are quite different from standard {@link Test @Test} |
| 38 | * cases since callbacks such as {@link BeforeEach @BeforeEach} and |
| 39 | * {@link AfterEach @AfterEach} methods are not executed for dynamic tests. |
| 40 | * |
| 41 | * @since 5.0 |
| 42 | * @see #dynamicTest(String, Executable) |
| 43 | * @see #stream(Iterator, Function, ThrowingConsumer) |
| 44 | * @see Test |
| 45 | * @see TestFactory |
| 46 | * @see DynamicContainer |
| 47 | * @see Executable |
| 48 | */ |
| 49 | @API(status = MAINTAINED, since = "5.3") |
| 50 | public class DynamicTest extends DynamicNode { |
| 51 | |
| 52 | /** |
| 53 | * Factory for creating a new {@code DynamicTest} for the supplied display |
| 54 | * name and executable code block. |
| 55 | * |
| 56 | * @param displayName the display name for the dynamic test; never |
| 57 | * {@code null} or blank |
| 58 | * @param executable the executable code block for the dynamic test; |
| 59 | * never {@code null} |
| 60 | * @see #stream(Iterator, Function, ThrowingConsumer) |
| 61 | */ |
| 62 | public static DynamicTest dynamicTest(String displayName, Executable executable) { |
| 63 | return new DynamicTest(displayName, null, executable); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Factory for creating a new {@code DynamicTest} for the supplied display |
| 68 | * name, custom test source {@link URI}, and executable code block. |
| 69 | * |
| 70 | * @param displayName the display name for the dynamic test; never |
| 71 | * {@code null} or blank |
| 72 | * @param testSourceUri a custom test source URI for the dynamic test; may |
| 73 | * be {@code null} if the framework should generate the test source based on |
| 74 | * the {@code @TestFactory} method |
| 75 | * @param executable the executable code block for the dynamic test; |
| 76 | * never {@code null} |
| 77 | * @since 5.3 |
| 78 | * @see #stream(Iterator, Function, ThrowingConsumer) |
| 79 | */ |
| 80 | public static DynamicTest dynamicTest(String displayName, URI testSourceUri, Executable executable) { |
| 81 | return new DynamicTest(displayName, testSourceUri, executable); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Generate a stream of dynamic tests based on the given generator and test |
| 86 | * executor. |
| 87 | * |
| 88 | * <p>Use this method when the set of dynamic tests is nondeterministic in |
| 89 | * nature or when the input comes from an existing {@link Iterator}. See |
| 90 | * {@link #stream(Stream, Function, ThrowingConsumer)} as an alternative. |
| 91 | * |
| 92 | * <p>The given {@code inputGenerator} is responsible for generating |
| 93 | * input values. A {@link DynamicTest} will be added to the resulting |
| 94 | * stream for each dynamically generated input value, using the given |
| 95 | * {@code displayNameGenerator} and {@code testExecutor}. |
| 96 | * |
| 97 | * @param inputGenerator an {@code Iterator} that serves as a dynamic |
| 98 | * <em>input generator</em>; never {@code null} |
| 99 | * @param displayNameGenerator a function that generates a display name |
| 100 | * based on an input value; never {@code null} |
| 101 | * @param testExecutor a consumer that executes a test based on an input |
| 102 | * value; never {@code null} |
| 103 | * @param <T> the type of <em>input</em> generated by the {@code inputGenerator} |
| 104 | * and used by the {@code displayNameGenerator} and {@code testExecutor} |
| 105 | * @return a stream of dynamic tests based on the given generator and |
| 106 | * executor; never {@code null} |
| 107 | * @see #dynamicTest(String, Executable) |
| 108 | * @see #stream(Stream, Function, ThrowingConsumer) |
| 109 | */ |
| 110 | public static <T> Stream<DynamicTest> stream(Iterator<T> inputGenerator, |
| 111 | Function<? super T, String> displayNameGenerator, ThrowingConsumer<? super T> testExecutor) { |
| 112 | |
| 113 | Preconditions.notNull(inputGenerator, "inputGenerator must not be null"); |
| 114 | |
| 115 | return stream(StreamSupport.stream(spliteratorUnknownSize(inputGenerator, ORDERED), false), |
| 116 | displayNameGenerator, testExecutor); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Generate a stream of dynamic tests based on the given input stream and |
| 121 | * test executor. |
| 122 | * |
| 123 | * <p>Use this method when the set of dynamic tests is nondeterministic in |
| 124 | * nature or when the input comes from an existing {@link Stream}. See |
| 125 | * {@link #stream(Iterator, Function, ThrowingConsumer)} as an alternative. |
| 126 | * |
| 127 | * <p>The given {@code inputStream} is responsible for supplying input values. |
| 128 | * A {@link DynamicTest} will be added to the resulting stream for each |
| 129 | * dynamically supplied input value, using the given {@code displayNameGenerator} |
| 130 | * and {@code testExecutor}. |
| 131 | * |
| 132 | * @param inputStream a {@code Stream} that supplies dynamic input values; |
| 133 | * never {@code null} |
| 134 | * @param displayNameGenerator a function that generates a display name |
| 135 | * based on an input value; never {@code null} |
| 136 | * @param testExecutor a consumer that executes a test based on an input |
| 137 | * value; never {@code null} |
| 138 | * @param <T> the type of <em>input</em> supplied by the {@code inputStream} |
| 139 | * and used by the {@code displayNameGenerator} and {@code testExecutor} |
| 140 | * @return a stream of dynamic tests based on the given generator and |
| 141 | * executor; never {@code null} |
| 142 | * @since 5.7 |
| 143 | * @see #dynamicTest(String, Executable) |
| 144 | * @see #stream(Iterator, Function, ThrowingConsumer) |
| 145 | */ |
| 146 | @API(status = MAINTAINED, since = "5.7") |
| 147 | public static <T> Stream<DynamicTest> stream(Stream<T> inputStream, |
| 148 | Function<? super T, String> displayNameGenerator, ThrowingConsumer<? super T> testExecutor) { |
| 149 | |
| 150 | Preconditions.notNull(inputStream, "inputStream must not be null"); |
| 151 | Preconditions.notNull(displayNameGenerator, "displayNameGenerator must not be null"); |
| 152 | Preconditions.notNull(testExecutor, "testExecutor must not be null"); |
| 153 | |
| 154 | return inputStream // |
| 155 | .map(input -> dynamicTest(displayNameGenerator.apply(input), () -> testExecutor.accept(input))); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Generate a stream of dynamic tests based on the given generator and test |
| 160 | * executor. |
| 161 | * |
| 162 | * <p>Use this method when the set of dynamic tests is nondeterministic in |
| 163 | * nature or when the input comes from an existing {@link Iterator}. See |
| 164 | * {@link #stream(Stream, ThrowingConsumer)} as an alternative. |
| 165 | * |
| 166 | * <p>The given {@code inputGenerator} is responsible for generating |
| 167 | * input values and display names. A {@link DynamicTest} will be added to |
| 168 | * the resulting stream for each dynamically generated input value, |
| 169 | * using the given {@code testExecutor}. |
| 170 | * |
| 171 | * @param inputGenerator an {@code Iterator} with {@code Named} values |
| 172 | * that serves as a dynamic <em>input generator</em>; never {@code null} |
| 173 | * @param testExecutor a consumer that executes a test based on an input |
| 174 | * value; never {@code null} |
| 175 | * @param <T> the type of <em>input</em> generated by the {@code inputGenerator} |
| 176 | * and used by the {@code testExecutor} |
| 177 | * @return a stream of dynamic tests based on the given generator and |
| 178 | * executor; never {@code null} |
| 179 | * @since 5.8 |
| 180 | * |
| 181 | * @see #dynamicTest(String, Executable) |
| 182 | * @see #stream(Stream, ThrowingConsumer) |
| 183 | * @see Named |
| 184 | */ |
| 185 | @API(status = MAINTAINED, since = "5.8") |
| 186 | public static <T> Stream<DynamicTest> stream(Iterator<? extends Named<T>> inputGenerator, |
| 187 | ThrowingConsumer<? super T> testExecutor) { |
| 188 | Preconditions.notNull(inputGenerator, "inputGenerator must not be null"); |
| 189 | |
| 190 | return stream(StreamSupport.stream(spliteratorUnknownSize(inputGenerator, ORDERED), false), testExecutor); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Generate a stream of dynamic tests based on the given input stream and |
| 195 | * test executor. |
| 196 | * |
| 197 | * <p>Use this method when the set of dynamic tests is nondeterministic in |
| 198 | * nature or when the input comes from an existing {@link Stream}. See |
| 199 | * {@link #stream(Iterator, ThrowingConsumer)} as an alternative. |
| 200 | * |
| 201 | * <p>The given {@code inputStream} is responsible for supplying input values |
| 202 | * and display names. A {@link DynamicTest} will be added to the resulting stream for |
| 203 | * each dynamically supplied input value, using the given {@code testExecutor}. |
| 204 | * |
| 205 | * @param inputStream a {@code Stream} that supplies dynamic {@code Named} |
| 206 | * input values; never {@code null} |
| 207 | * @param testExecutor a consumer that executes a test based on an input |
| 208 | * value; never {@code null} |
| 209 | * @param <T> the type of <em>input</em> supplied by the {@code inputStream} |
| 210 | * and used by the {@code displayNameGenerator} and {@code testExecutor} |
| 211 | * @return a stream of dynamic tests based on the given generator and |
| 212 | * executor; never {@code null} |
| 213 | * @since 5.8 |
| 214 | * |
| 215 | * @see #dynamicTest(String, Executable) |
| 216 | * @see #stream(Iterator, ThrowingConsumer) |
| 217 | * @see Named |
| 218 | */ |
| 219 | @API(status = MAINTAINED, since = "5.8") |
| 220 | public static <T> Stream<DynamicTest> stream(Stream<? extends Named<T>> inputStream, |
| 221 | ThrowingConsumer<? super T> testExecutor) { |
| 222 | Preconditions.notNull(inputStream, "inputStream must not be null"); |
| 223 | Preconditions.notNull(testExecutor, "testExecutor must not be null"); |
| 224 | |
| 225 | return inputStream // |
| 226 | .map(input -> dynamicTest(input.getName(), () -> testExecutor.accept(input.getPayload()))); |
| 227 | } |
| 228 | |
| 229 | private final Executable executable; |
| 230 | |
| 231 | private DynamicTest(String displayName, URI testSourceUri, Executable executable) { |
| 232 | super(displayName, testSourceUri); |
| 233 | this.executable = Preconditions.notNull(executable, "executable must not be null"); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get the {@code executable} code block associated with this {@code DynamicTest}. |
| 238 | */ |
| 239 | public Executable getExecutable() { |
| 240 | return this.executable; |
| 241 | } |
| 242 | |
| 243 | } |