Skip to content

Commit 92768de

Browse files
authored
Document that Streams returned from argument sources closed automatically
... in Javadoc and User Guide
1 parent dbfc4fd commit 92768de

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

Diff for: documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

+8
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,10 @@ an array of primitives. The "arguments" within the stream can be supplied as an
19541954
of `Arguments`, an array of objects (e.g., `Object[]`), or a single value if the
19551955
parameterized class or test method accepts a single argument.
19561956

1957+
If the return type is `Stream` or one of the primitive streams,
1958+
JUnit will properly close it by calling `BaseStream.close()`,
1959+
making it safe to use a resource such as `Files.lines()`.
1960+
19571961
If you only need a single parameter, you can return a `Stream` of instances of the
19581962
parameter type as demonstrated in the following example.
19591963

@@ -2049,6 +2053,10 @@ are _consumed_ the first time they are processed. However, if you wish to use on
20492053
these types, you can wrap it in a `Supplier` — for example, `Supplier<IntStream>`.
20502054
====
20512055

2056+
If the `Supplier` return type is `Stream` or one of the primitive streams,
2057+
JUnit will properly close it by calling `BaseStream.close()`,
2058+
making it safe to use a resource such as `Files.lines()`.
2059+
20522060
Please note that a one-dimensional array of objects supplied as a set of "arguments" will
20532061
be handled differently than other types of arguments. Specifically, all the elements of a
20542062
one-dimensional array of objects will be passed as individual physical arguments to the

Diff for: junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/AnnotationBasedArgumentsProvider.java

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ protected Stream<? extends Arguments> provideArguments(ExtensionContext context,
7676
getClass().getName()));
7777
}
7878

79+
/**
80+
* The returned {@code Stream} will be {@link Stream#close() properly closed}
81+
* by the default implementation of
82+
* {@link #provideArguments(ParameterDeclarations, ExtensionContext)},
83+
* making it safe to use a resource such as
84+
* {@link java.nio.file.Files#lines(java.nio.file.Path) Files.lines()}.
85+
*/
7986
protected Stream<? extends Arguments> provideArguments(ParameterDeclarations parameters, ExtensionContext context,
8087
A annotation) {
8188
return provideArguments(context, annotation);

Diff for: junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/FieldSource.java

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
* use one of these types, you can wrap it in a {@code Supplier} &mdash; for
6262
* example, {@code Supplier<IntStream>}.
6363
*
64+
* <p>If the {@code Supplier} return type is {@code Stream} or
65+
* one of the primitive streams, JUnit will properly close it by calling
66+
* {@link java.util.stream.BaseStream#close() BaseStream.close()},
67+
* making it safe to use a resource such as
68+
* {@link java.nio.file.Files#lines(java.nio.file.Path) Files.lines()}.
69+
*
6470
* <p>Please note that a one-dimensional array of objects supplied as a set of
6571
* "arguments" will be handled differently than other types of arguments.
6672
* Specifically, all of the elements of a one-dimensional array of objects will

Diff for: junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodSource.java

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
* {@code String[]}, etc.), or a single <em>value</em> if the parameterized test
5151
* method accepts a single argument.
5252
*
53+
* <p>If the return type is {@code Stream} or
54+
* one of the primitive streams, JUnit will properly close it by calling
55+
* {@link java.util.stream.BaseStream#close() BaseStream.close()},
56+
* making it safe to use a resource such as
57+
* {@link java.nio.file.Files#lines(java.nio.file.Path) Files.lines()}.
58+
*
5359
* <p>Please note that a one-dimensional array of objects supplied as a set of
5460
* "arguments" will be handled differently than other types of arguments.
5561
* Specifically, all of the elements of a one-dimensional array of objects will

0 commit comments

Comments
 (0)