| 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 org.apiguardian.api.API.Status.MAINTAINED; |
| 14 | |
| 15 | import java.net.URI; |
| 16 | import java.util.stream.Stream; |
| 17 | import java.util.stream.StreamSupport; |
| 18 | |
| 19 | import org.apiguardian.api.API; |
| 20 | import org.junit.platform.commons.util.Preconditions; |
| 21 | |
| 22 | /** |
| 23 | * A {@code DynamicContainer} is a container generated at runtime. |
| 24 | * |
| 25 | * <p>It is composed of a {@linkplain DynamicNode#getDisplayName display name} |
| 26 | * and an {@link Iterable} or {@link Stream} of {@link DynamicNode DynamicNodes}. |
| 27 | * |
| 28 | * <p>Instances of {@code DynamicContainer} must be generated by factory methods |
| 29 | * annotated with {@link TestFactory @TestFactory}. |
| 30 | * |
| 31 | * @since 5.0 |
| 32 | * @see #dynamicContainer(String, Iterable) |
| 33 | * @see #dynamicContainer(String, Stream) |
| 34 | * @see TestFactory |
| 35 | * @see DynamicTest |
| 36 | */ |
| 37 | @API(status = MAINTAINED, since = "5.3") |
| 38 | public class DynamicContainer extends DynamicNode { |
| 39 | |
| 40 | /** |
| 41 | * Factory for creating a new {@code DynamicContainer} for the supplied display |
| 42 | * name and collection of dynamic nodes. |
| 43 | * |
| 44 | * <p>The collection of dynamic nodes must not contain {@code null} elements. |
| 45 | * |
| 46 | * @param displayName the display name for the dynamic container; never |
| 47 | * {@code null} or blank |
| 48 | * @param dynamicNodes collection of dynamic nodes to execute; |
| 49 | * never {@code null} |
| 50 | * @see #dynamicContainer(String, Stream) |
| 51 | */ |
| 52 | public static DynamicContainer dynamicContainer(String displayName, Iterable<? extends DynamicNode> dynamicNodes) { |
| 53 | return dynamicContainer(displayName, null, StreamSupport.stream(dynamicNodes.spliterator(), false)); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Factory for creating a new {@code DynamicContainer} for the supplied display |
| 58 | * name and stream of dynamic nodes. |
| 59 | * |
| 60 | * <p>The stream of dynamic nodes must not contain {@code null} elements. |
| 61 | * |
| 62 | * @param displayName the display name for the dynamic container; never |
| 63 | * {@code null} or blank |
| 64 | * @param dynamicNodes stream of dynamic nodes to execute; |
| 65 | * never {@code null} |
| 66 | * @see #dynamicContainer(String, Iterable) |
| 67 | */ |
| 68 | public static DynamicContainer dynamicContainer(String displayName, Stream<? extends DynamicNode> dynamicNodes) { |
| 69 | return dynamicContainer(displayName, null, dynamicNodes); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Factory for creating a new {@code DynamicContainer} for the supplied display |
| 74 | * name, custom test source {@link URI}, and stream of dynamic nodes. |
| 75 | * |
| 76 | * <p>The stream of dynamic nodes must not contain {@code null} elements. |
| 77 | * |
| 78 | * @param displayName the display name for the dynamic container; never |
| 79 | * {@code null} or blank |
| 80 | * @param testSourceUri a custom test source URI for the dynamic container; |
| 81 | * may be {@code null} if the framework should generate the test source based |
| 82 | * on the {@code @TestFactory} method |
| 83 | * @param dynamicNodes stream of dynamic nodes to execute; never {@code null} |
| 84 | * @since 5.3 |
| 85 | * @see #dynamicContainer(String, Iterable) |
| 86 | */ |
| 87 | public static DynamicContainer dynamicContainer(String displayName, URI testSourceUri, |
| 88 | Stream<? extends DynamicNode> dynamicNodes) { |
| 89 | |
| 90 | return new DynamicContainer(displayName, testSourceUri, dynamicNodes); |
| 91 | } |
| 92 | |
| 93 | private final Stream<? extends DynamicNode> children; |
| 94 | |
| 95 | private DynamicContainer(String displayName, URI testSourceUri, Stream<? extends DynamicNode> children) { |
| 96 | super(displayName, testSourceUri); |
| 97 | Preconditions.notNull(children, "children must not be null"); |
| 98 | this.children = children; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the {@link Stream} of {@link DynamicNode DynamicNodes} associated |
| 103 | * with this {@code DynamicContainer}. |
| 104 | */ |
| 105 | public Stream<? extends DynamicNode> getChildren() { |
| 106 | return children; |
| 107 | } |
| 108 | |
| 109 | } |