junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestTemplateInvocationContextProvider.java
raw symbols SCIP| 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.extension; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import java.util.stream.Stream; |
| 16 | |
| 17 | import org.apiguardian.api.API; |
| 18 | |
| 19 | /** |
| 20 | * {@code TestTemplateInvocationContextProvider} defines the API for |
| 21 | * {@link Extension Extensions} that wish to provide one or multiple contexts |
| 22 | * for the invocation of a |
| 23 | * {@link org.junit.jupiter.api.TestTemplate @TestTemplate} method. |
| 24 | * |
| 25 | * <p>This extension point makes it possible to execute a test template in |
| 26 | * different contexts — for example, with different parameters, by |
| 27 | * preparing the test class instance differently, or multiple times without |
| 28 | * modifying the context. |
| 29 | * |
| 30 | * <p>This interface defines two methods: {@link #supportsTestTemplate} and |
| 31 | * {@link #provideTestTemplateInvocationContexts}. The former is called by the |
| 32 | * framework to determine whether this extension wants to act on a test template |
| 33 | * that is about to be executed. If so, the latter is called and must return a |
| 34 | * {@link Stream} of {@link TestTemplateInvocationContext} instances. Otherwise, |
| 35 | * this provider is ignored for the execution of the current test template. |
| 36 | * |
| 37 | * <p>A provider that has returned {@code true} from its {@link #supportsTestTemplate} |
| 38 | * method is called <em>active</em>. When multiple providers are active for a |
| 39 | * test template method, the {@code Streams} returned by their |
| 40 | * {@link #provideTestTemplateInvocationContexts} methods will be chained, and |
| 41 | * the test template method will be invoked using the contexts of all active |
| 42 | * providers. |
| 43 | * |
| 44 | * <h2>Constructor Requirements</h2> |
| 45 | * |
| 46 | * <p>Consult the documentation in {@link Extension} for details on |
| 47 | * constructor requirements. |
| 48 | * |
| 49 | * @since 5.0 |
| 50 | * @see org.junit.jupiter.api.TestTemplate |
| 51 | * @see TestTemplateInvocationContext |
| 52 | */ |
| 53 | @API(status = STABLE, since = "5.0") |
| 54 | public interface TestTemplateInvocationContextProvider extends Extension { |
| 55 | |
| 56 | /** |
| 57 | * Determine if this provider supports providing invocation contexts for the |
| 58 | * test template method represented by the supplied {@code context}. |
| 59 | * |
| 60 | * @param context the extension context for the test template method about |
| 61 | * to be invoked; never {@code null} |
| 62 | * @return {@code true} if this provider can provide invocation contexts |
| 63 | * @see #provideTestTemplateInvocationContexts |
| 64 | * @see ExtensionContext |
| 65 | */ |
| 66 | boolean supportsTestTemplate(ExtensionContext context); |
| 67 | |
| 68 | /** |
| 69 | * Provide {@linkplain TestTemplateInvocationContext invocation contexts} |
| 70 | * for the test template method represented by the supplied {@code context}. |
| 71 | * |
| 72 | * <p>This method is only called by the framework if {@link #supportsTestTemplate} |
| 73 | * previously returned {@code true} for the same {@link ExtensionContext}; |
| 74 | * this method is allowed to return an empty {@code Stream} but not {@code null}. |
| 75 | * |
| 76 | * <p>The returned {@code Stream} will be properly closed by calling |
| 77 | * {@link Stream#close()}, making it safe to use a resource such as |
| 78 | * {@link java.nio.file.Files#lines(java.nio.file.Path) Files.lines()}. |
| 79 | * |
| 80 | * @param context the extension context for the test template method about |
| 81 | * to be invoked; never {@code null} |
| 82 | * @return a {@code Stream} of {@code TestTemplateInvocationContext} |
| 83 | * instances for the invocation of the test template method; never {@code null} |
| 84 | * @see #supportsTestTemplate |
| 85 | * @see ExtensionContext |
| 86 | */ |
| 87 | Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context); |
| 88 | |
| 89 | } |