Total Lines | 33 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package io.github.glytching.junit.extension.util; |
||
5 | public class ExtensionUtil { |
||
6 | |||
7 | // this is a utility class - hide the public ctor |
||
8 | private ExtensionUtil() {} |
||
9 | |||
10 | /** |
||
11 | * Creates a {@link ExtensionContext.Store} for a given {@code extensionContext}. A {@link |
||
12 | * ExtensionContext.Store} is bound to an {@link ExtensionContext} so different test invocations |
||
13 | * do not share the same store. For example a test invocation on {@code ClassA.testMethodA} will |
||
14 | * have a different {@link ExtensionContext.Store} instance to that associated with a test |
||
15 | * invocation on {@code ClassA.testMethodB} or test invocation on {@code ClassC.testMethodC}. |
||
16 | * |
||
17 | * @param extensionContext the <em>context</em> in which the current test or container is being |
||
18 | * executed |
||
19 | * @return a {@link ExtensionContext.Store} for the given {@code extensionContext} |
||
20 | */ |
||
21 | public static ExtensionContext.Store getStore(ExtensionContext extensionContext, Class clazz) { |
||
22 | return extensionContext.getStore(namespace(extensionContext, clazz)); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Creates a {@link ExtensionContext.Namespace} in which extension state is stored on creation for |
||
27 | * post execution destruction. Storing data in a custom namespace prevents accidental cross |
||
28 | * pollination of data between extensions and between different invocations within the lifecycle |
||
29 | * of a single extension. |
||
30 | * |
||
31 | * @param extensionContext the <em>context</em> in which the current test or container is being |
||
32 | * executed |
||
33 | * @return a {@link ExtensionContext.Namespace} describing the scope for an extension |
||
34 | */ |
||
35 | private static ExtensionContext.Namespace namespace( |
||
36 | ExtensionContext extensionContext, Class clazz) { |
||
37 | return ExtensionContext.Namespace.create(clazz, extensionContext); |
||
38 | } |
||
40 |