Skip to content

Commit 0e2bc71

Browse files
committed
Issue #3166: detailed docmentation provided for redirecting standard streams
1 parent 4a81bc9 commit 0e2bc71

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

documentation/src/docs/asciidoc/user-guide/advanced-topics/junit-platform-reporting.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ $ java -jar junit-platform-console-standalone-{platform-version}.jar <OPTIONS> \
164164
--redirect-stderr=bar.txt
165165
----
166166

167+
If both the `--redirect-stdout` and `--redirect-stderr` parameters point to the same
168+
file, the output will be merged into that file.
169+
170+
The default charset is used for writing to the files.
171+
167172
[[junit-platform-reporting-legacy-xml]]
168173
==== Legacy XML format
169174

junit-platform-console/src/main/java/org/junit/platform/console/options/TestConsoleOutputOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@ public void setTheme(Theme theme) {
7575
this.theme = theme;
7676
}
7777

78-
@API(status = INTERNAL, since = "5.12")
78+
@API(status = INTERNAL, since = "1.13")
7979
public Path getStdoutPath() {
8080
return this.stdoutPath;
8181
}
8282

83-
@API(status = INTERNAL, since = "5.12")
83+
@API(status = INTERNAL, since = "1.13")
8484
public void setStdoutPath(Path stdoutPath) {
8585
this.stdoutPath = stdoutPath;
8686
}
8787

88-
@API(status = INTERNAL, since = "5.12")
88+
@API(status = INTERNAL, since = "1.13")
8989
public Path getStderrPath() {
9090
return this.stderrPath;
9191
}
9292

93-
@API(status = INTERNAL, since = "5.12")
93+
@API(status = INTERNAL, since = "1.13")
9494
public void setStderrPath(Path stderrPath) {
9595
this.stderrPath = stderrPath;
9696
}

junit-platform-console/src/main/java/org/junit/platform/console/tasks/StdStreamHandler.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ private boolean isSameFile(Path path1, Path path2) {
3030
return (path1.normalize().toAbsolutePath().equals(path2.normalize().toAbsolutePath()));
3131
}
3232

33+
/**
34+
* Redirects standard output (stdout) and standard error (stderr) to the specified file paths.
35+
* If the paths are the same, both streams are redirected to the same file.
36+
* The default charset is used for writing to the files.
37+
*
38+
* @param stdoutPath The file path for standard output. {@code null} means no redirection.
39+
* @param stderrPath The file path for standard error. {@code null} means no redirection.
40+
*/
3341
public void redirectStdStreams(Path stdoutPath, Path stderrPath) {
3442
if (isSameFile(stdoutPath, stderrPath)) {
3543
try {
@@ -71,11 +79,15 @@ public void redirectStdStreams(Path stdoutPath, Path stderrPath) {
7179

7280
@Override
7381
public void close() {
74-
if (stdout != null) {
75-
stdout.close();
82+
try {
83+
if (stdout != null) {
84+
stdout.close();
85+
}
7686
}
77-
if (stderr != null) {
78-
stderr.close();
87+
finally {
88+
if (stderr != null) {
89+
stderr.close();
90+
}
7991
}
8092
}
8193
}

platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherIntegrationTests.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
import java.io.IOException;
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
22+
import java.util.stream.Stream;
2223

2324
import org.junit.jupiter.api.Test;
2425
import org.junit.jupiter.api.io.TempDir;
2526
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.Arguments;
28+
import org.junit.jupiter.params.provider.MethodSource;
2629
import org.junit.jupiter.params.provider.ValueSource;
30+
import org.junit.platform.console.options.StdStreamTestCase;
2731

2832
/**
2933
* @since 1.0
@@ -98,32 +102,37 @@ void executeScanModules(final String line) {
98102
assertEquals(0, new ConsoleLauncherWrapper().execute(args1).getTestsFoundCount());
99103
}
100104

105+
private static Stream<Arguments> redirectStreamParams() {
106+
return Stream.of(
107+
Arguments.of("--redirect-stdout", StdStreamTestCase.getStdoutOutputFileSize()),
108+
Arguments.of("--redirect-stderr", StdStreamTestCase.getStderrOutputFileSize())
109+
);
110+
}
111+
101112
@ParameterizedTest
102-
@ValueSource(strings = { "--redirect-stdout", "--redirect-stderr" })
103-
void executeWithRedirectedStdStream(String redirectedStream, @TempDir Path tempDir) throws IOException {
113+
@MethodSource("redirectStreamParams")
114+
void executeWithRedirectedStdStream(String redirectedStream, int outputFileSize, @TempDir Path tempDir)
115+
throws IOException {
104116
Path outputFile = tempDir.resolve("output.txt");
105-
var line = String.format(
106-
"execute -e junit-jupiter --select-method org.junit.platform.console.options.StdStreamTestCase#printTest "
107-
+ "%s %s",
108-
redirectedStream, outputFile);
117+
var line = String.format("execute -e junit-jupiter --select-class %s %s %s",
118+
StdStreamTestCase.class.getName(), redirectedStream, outputFile);
109119
var args = line.split(" ");
110120
new ConsoleLauncherWrapper().execute(args);
111121

112122
assertTrue(Files.exists(outputFile), "File does not exist.");
113-
assertEquals(20, Files.size(outputFile), "Invalid file size.");
123+
assertEquals(outputFileSize, Files.size(outputFile), "Invalid file size.");
114124
}
115125

116126
@Test
117127
void executeWithRedirectedStdStreamsToSameFile(@TempDir Path tempDir) throws IOException {
118128
Path outputFile = tempDir.resolve("output.txt");
119129
var line = String.format(
120-
"execute -e junit-jupiter --select-method org.junit.platform.console.options.StdStreamTestCase#printTest "
121-
+ "--redirect-stdout %s --redirect-stderr %s",
122-
outputFile, outputFile);
130+
"execute -e junit-jupiter --select-class %s --redirect-stdout %s --redirect-stderr %s",
131+
StdStreamTestCase.class.getName(), outputFile, outputFile);
123132
var args = line.split(" ");
124133
new ConsoleLauncherWrapper().execute(args);
125134

126135
assertTrue(Files.exists(outputFile), "File does not exist.");
127-
assertEquals(40, Files.size(outputFile), "Invalid file size.");
136+
assertEquals(StdStreamTestCase.getStdoutOutputFileSize() + StdStreamTestCase.getStderrOutputFileSize(), Files.size(outputFile), "Invalid file size.");
128137
}
129138
}

platform-tests/src/test/java/org/junit/platform/console/options/StdStreamTestCase.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@
1414

1515
public class StdStreamTestCase {
1616

17+
private static final String STDOUT_DATA = "Writing to STDOUT...";
18+
private static final String STDERR_DATA = "Writing to STDERR...";
19+
20+
public static int getStdoutOutputFileSize() {
21+
return STDOUT_DATA.length();
22+
}
23+
24+
public static int getStderrOutputFileSize() {
25+
return STDERR_DATA.length();
26+
}
27+
1728
@Test
1829
void printTest() {
19-
System.out.print("Writing to STDOUT...");
20-
System.err.print("Writing to STDERR...");
30+
System.out.print(STDOUT_DATA);
31+
System.err.print(STDERR_DATA);
2132
}
2233
}

0 commit comments

Comments
 (0)