|
| 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.params.provider; |
| 12 | + |
| 13 | +import static org.apiguardian.api.API.Status.EXPERIMENTAL; |
| 14 | + |
| 15 | +import java.lang.annotation.Documented; |
| 16 | +import java.lang.annotation.ElementType; |
| 17 | +import java.lang.annotation.Retention; |
| 18 | +import java.lang.annotation.RetentionPolicy; |
| 19 | +import java.lang.annotation.Target; |
| 20 | + |
| 21 | +import org.apiguardian.api.API; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | + |
| 24 | +/** |
| 25 | + * {@code @FieldSource} is an {@link ArgumentsSource} which provides access to |
| 26 | + * values returned from {@linkplain #value() fields} of the class in which this |
| 27 | + * annotation is declared or from static fields in external classes referenced |
| 28 | + * by <em>fully-qualified field name</em>. |
| 29 | + * |
| 30 | + * <p>Each field must be able to supply a <em>stream</em> of <em>arguments</em>, |
| 31 | + * and each set of "arguments" within the "stream" will be provided as the physical |
| 32 | + * arguments for individual invocations of the annotated |
| 33 | + * {@link ParameterizedTest @ParameterizedTest} method. |
| 34 | + * |
| 35 | + * <p>In this context, a "stream" is anything that JUnit can reliably convert to |
| 36 | + * a {@link java.util.stream.Stream Stream}; however, the actual concrete return |
| 37 | + * type can take on many forms. Generally speaking this translates to a |
| 38 | + * {@link java.util.Collection Collection}, an {@link Iterable}, a |
| 39 | + * {@link java.util.function.Supplier Supplier} of a stream |
| 40 | + * ({@link java.util.stream.Stream Stream}, |
| 41 | + * {@link java.util.stream.DoubleStream DoubleStream}, |
| 42 | + * {@link java.util.stream.LongStream LongStream}, or |
| 43 | + * {@link java.util.stream.IntStream IntStream}), a {@code Supplier} of an |
| 44 | + * {@link java.util.Iterator Iterator}, an array of objects, or an array of |
| 45 | + * primitives. Each set of "arguments" within the "stream" can be supplied as an |
| 46 | + * instance of {@link Arguments}, an array of objects (for example, {@code Object[]}, |
| 47 | + * {@code String[]}, etc.), or a single <em>value</em> if the parameterized test |
| 48 | + * method accepts a single argument. |
| 49 | + * |
| 50 | + * <p>In contrast to the supported return types for {@link MethodSource @MethodSource} |
| 51 | + * factory methods, the value of a {@code @FieldSource} field cannot be an instance of |
| 52 | + * {@link java.util.stream.Stream Stream}, |
| 53 | + * {@link java.util.stream.DoubleStream DoubleStream}, |
| 54 | + * {@link java.util.stream.LongStream LongStream}, |
| 55 | + * {@link java.util.stream.IntStream IntStream}, or |
| 56 | + * {@link java.util.Iterator Iterator}, since the values of such types are |
| 57 | + * <em>consumed</em> the first time they are processed. However, if you wish to |
| 58 | + * use one of these types, you can wrap it in a {@code Supplier} — for |
| 59 | + * example, {@code Supplier<IntStream>}. |
| 60 | + * |
| 61 | + * <p>Please note that a one-dimensional array of objects supplied as a set of |
| 62 | + * "arguments" will be handled differently than other types of arguments. |
| 63 | + * Specifically, all of the elements of a one-dimensional array of objects will |
| 64 | + * be passed as individual physical arguments to the {@code @ParameterizedTest} |
| 65 | + * method. This behavior can be seen in the table below for the |
| 66 | + * {@code Supplier<Stream<Object[]>> objectArrayStreamSupplier} field: the |
| 67 | + * {@code @ParameterizedTest} method accepts individual {@code String} and |
| 68 | + * {@code int} arguments rather than a single {@code Object[]} array. In contrast, |
| 69 | + * any multidimensional array supplied as a set of "arguments" will be passed as |
| 70 | + * a single physical argument to the {@code @ParameterizedTest} method without |
| 71 | + * modification. This behavior can be seen in the table below for the |
| 72 | + * {@code Supplier<Stream<int[][]>> twoDimensionalIntArrayStreamSupplier} and |
| 73 | + * {@code Supplier<Stream<Object[][]>> twoDimensionalObjectArrayStreamSupplier} |
| 74 | + * fields: the {@code @ParameterizedTest} methods for those fields accept individual |
| 75 | + * {@code int[][]} and {@code Object[][]} arguments, respectively. |
| 76 | + * |
| 77 | + * <h2>Examples</h2> |
| 78 | + * |
| 79 | + * <p>The following table displays compatible method signatures for parameterized |
| 80 | + * test methods and their corresponding {@code @FieldSource} fields. |
| 81 | + * |
| 82 | + * <table class="plain"> |
| 83 | + * <caption>Compatible method signatures and field declarations</caption> |
| 84 | + * <tr><th>{@code @ParameterizedTest} method</th><th>{@code @FieldSource} field</th></tr> |
| 85 | + * <tr><td>{@code void test(String)}</td><td>{@code static List<String> listOfStrings}</td></tr> |
| 86 | + * <tr><td>{@code void test(String)}</td><td>{@code static String[] arrayOfStrings}</td></tr> |
| 87 | + * <tr><td>{@code void test(int)}</td><td>{@code static int[] intArray}</td></tr> |
| 88 | + * <tr><td>{@code void test(int[])}</td><td>{@code static int[][] twoDimensionalIntArray}</td></tr> |
| 89 | + * <tr><td>{@code void test(String, String)}</td><td>{@code static String[][] twoDimensionalStringArray}</td></tr> |
| 90 | + * <tr><td>{@code void test(String, int)}</td><td>{@code static Object[][] twoDimensionalObjectArray}</td></tr> |
| 91 | + * <tr><td>{@code void test(int)}</td><td>{@code static Supplier<IntStream> intStreamSupplier}</td></tr> |
| 92 | + * <tr><td>{@code void test(String)}</td><td>{@code static Supplier<Stream<String>> stringStreamSupplier}</td></tr> |
| 93 | + * <tr><td>{@code void test(String, int)}</td><td>{@code static Supplier<Stream<Object[]>> objectArrayStreamSupplier}</td></tr> |
| 94 | + * <tr><td>{@code void test(String, int)}</td><td>{@code static Supplier<Stream<Arguments>> argumentsStreamSupplier}</td></tr> |
| 95 | + * <tr><td>{@code void test(int[])}</td><td>{@code static Supplier<Stream<int[]>> intArrayStreamSupplier}</td></tr> |
| 96 | + * <tr><td>{@code void test(int[][])}</td><td>{@code static Supplier<Stream<int[][]>> twoDimensionalIntArrayStreamSupplier}</td></tr> |
| 97 | + * <tr><td>{@code void test(Object[][])}</td><td>{@code static Supplier<Stream<Object[][]>> twoDimensionalObjectArrayStreamSupplier}</td></tr> |
| 98 | + * </table> |
| 99 | + * |
| 100 | + * <p>Fields within the test class must be {@code static} unless the |
| 101 | + * {@link org.junit.jupiter.api.TestInstance.Lifecycle#PER_CLASS PER_CLASS} |
| 102 | + * test instance lifecycle mode is used; whereas, fields in external classes must |
| 103 | + * always be {@code static}. |
| 104 | + * |
| 105 | + * @since 5.11 |
| 106 | + * @see MethodSource |
| 107 | + * @see Arguments |
| 108 | + * @see ArgumentsSource |
| 109 | + * @see ParameterizedTest |
| 110 | + * @see org.junit.jupiter.api.TestInstance |
| 111 | + */ |
| 112 | +@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) |
| 113 | +@Retention(RetentionPolicy.RUNTIME) |
| 114 | +@Documented |
| 115 | +@API(status = EXPERIMENTAL, since = "5.11") |
| 116 | +@ArgumentsSource(FieldArgumentsProvider.class) |
| 117 | +@SuppressWarnings("exports") |
| 118 | +public @interface FieldSource { |
| 119 | + |
| 120 | + /** |
| 121 | + * The names of fields within the test class or in external classes to use |
| 122 | + * as sources for arguments. |
| 123 | + * |
| 124 | + * <p>Fields in external classes must be referenced by <em>fully-qualified |
| 125 | + * field name</em> — for example, |
| 126 | + * {@code "com.example.WebUtils#httpMethodNames"} or |
| 127 | + * {@code "com.example.TopLevelClass$NestedClass#numbers"} for a field in a |
| 128 | + * static nested class. |
| 129 | + * |
| 130 | + * <p>If no field names are declared, a field within the test class that has |
| 131 | + * the same name as the test method will be used as the field by default. |
| 132 | + * |
| 133 | + * <p>For further information, see the {@linkplain FieldSource class-level Javadoc}. |
| 134 | + */ |
| 135 | + String[] value() default {}; |
| 136 | + |
| 137 | +} |
0 commit comments