Skip to content

Commit a203454

Browse files
committed
Add methods for tapping err and out simultaneously
The output of command-line applications is usually the output to the standard output stream and the error output stream intermixed. The new methods allow capturing the intermixed output. @test void application_writes_text_to_System_err_and_out( ) throws Exception { String text = tapSystemErrAndOut(() -> { System.err.print("text from err"); System.out.print("text from out"); }); assertEquals("text from errtext from out", text); } @test void application_writes_mutliple_lines_to_System_err_and_out( ) throws Exception { String text = tapSystemErrAndOutNormalized(() -> { System.err.println("text from err"); System.out.println("text from out"); }); assertEquals("text from err\ntext from out\n", text); }
1 parent 8903a1d commit a203454

File tree

5 files changed

+253
-6
lines changed

5 files changed

+253
-6
lines changed

README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ System Lambda is available from
2020
<dependency>
2121
<groupId>com.github.stefanbirkner</groupId>
2222
<artifactId>system-lambda</artifactId>
23-
<version>1.1.1</version>
23+
<version>1.2.0</version>
2424
</dependency>
2525
```
2626

@@ -113,8 +113,9 @@ void execute_code_that_manipulates_system_properties(
113113

114114
Command-line applications usually write to the console. If you write such
115115
applications you need to test the output of these applications. The methods
116-
`tapSystemErr`, `tapSystemErrNormalized`, `tapSystemOut` and
117-
`tapSystemOutNormalized` allow you to tap the text that is written to
116+
`tapSystemErr`, `tapSystemErrNormalized`, `tapSystemOut`,
117+
`tapSystemOutNormalized`, `tapSystemErrAndOut` and
118+
`tapSystemErrAndOutNormalized` allow you to tap the text that is written to
118119
`System.err`/`System.out`. The methods with the suffix `Normalized` normalize
119120
line breaks to `\n` so that you can run tests with the same assertions on
120121
different operating systems.
@@ -157,6 +158,26 @@ void application_writes_mutliple_lines_to_System_out(
157158
});
158159
assertEquals("first line\nsecond line\n", text);
159160
}
161+
162+
@Test
163+
void application_writes_text_to_System_err_and_out(
164+
) throws Exception {
165+
String text = tapSystemErrAndOut(() -> {
166+
System.err.print("text from err");
167+
System.out.print("text from out");
168+
});
169+
assertEquals("text from errtext from out", text);
170+
}
171+
172+
@Test
173+
void application_writes_mutliple_lines_to_System_err_and_out(
174+
) throws Exception {
175+
String text = tapSystemErrAndOutNormalized(() -> {
176+
System.err.println("text from err");
177+
System.out.println("text from out");
178+
});
179+
assertEquals("text from err\ntext from out\n", text);
180+
}
160181
```
161182

162183
You can assert that nothing is written to `System.err`/`System.out` by wrapping

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>system-lambda</artifactId>
12-
<version>1.2.0-SNAPSHOT</version>
12+
<version>1.2.0</version>
1313
<packaging>jar</packaging>
1414

1515
<name>System Lambda</name>

src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java

+107-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@
8787
* applications you need to test the output of these applications. The methods
8888
* {@link #tapSystemErr(Statement) tapSystemErr},
8989
* {@link #tapSystemErrNormalized(Statement) tapSystemErrNormalized},
90-
* {@link #tapSystemOut(Statement) tapSystemOut} and
91-
* {@link #tapSystemOutNormalized(Statement) tapSystemOutNormalized} allow you
90+
* {@link #tapSystemOut(Statement) tapSystemOut},
91+
* {@link #tapSystemOutNormalized(Statement) tapSystemOutNormalized},
92+
* {@link #tapSystemErrAndOut(Statement) tapSystemErrAndOut} and
93+
* {@link #tapSystemErrAndOutNormalized(Statement) tapSystemErrAndOutNormalized}
94+
* allow you
9295
* to tap the text that is written to {@code System.err}/{@code System.out}. The
9396
* methods with the suffix {@code Normalized} normalize line breaks to
9497
* {@code \n} so that you can run tests with the same assertions on different
@@ -131,6 +134,26 @@
131134
* System.out.println("second line");
132135
* });
133136
* assertEquals(text, "first line\nsecond line\n");
137+
* }
138+
*
139+
* &#064;Test
140+
* void application_writes_text_to_System_err_and_out(
141+
* ) throws Exception {
142+
* String text = tapSystemErrAndOut((){@literal ->} {
143+
* System.err.print("text from err");
144+
* System.out.print("text from out");
145+
* });
146+
* assertEquals("text from errtext from out", text);
147+
* }
148+
*
149+
* &#064;Test
150+
* void application_writes_mutliple_lines_to_System_err_and_out(
151+
* ) throws Exception {
152+
* String text = tapSystemErrAndOutNormalized((){@literal ->} {
153+
* System.err.println("text from err");
154+
* System.out.println("text from out");
155+
* });
156+
* assertEquals("text from err\ntext from out\n", text);
134157
* }</pre>
135158
*
136159
* <p>You can assert that nothing is written to
@@ -509,6 +532,8 @@ public static void restoreSystemProperties(
509532
* @return text that is written to {@code System.err} by the statement.
510533
* @throws Exception any exception thrown by the statement.
511534
* @see #tapSystemOut(Statement)
535+
* @see #tapSystemErrAndOut(Statement)
536+
* @see #tapSystemErrAndOutNormalized(Statement)
512537
* @since 1.0.0
513538
*/
514539
public static String tapSystemErr(
@@ -541,6 +566,8 @@ public static String tapSystemErr(
541566
* @return text that is written to {@code System.err} by the statement.
542567
* @throws Exception any exception thrown by the statement.
543568
* @see #tapSystemOut(Statement)
569+
* @see #tapSystemErrAndOut(Statement)
570+
* @see #tapSystemErrAndOutNormalized(Statement)
544571
* @since 1.0.0
545572
*/
546573
public static String tapSystemErrNormalized(
@@ -550,6 +577,80 @@ public static String tapSystemErrNormalized(
550577
.replace(lineSeparator(), "\n");
551578
}
552579

580+
/**
581+
* Executes the statement and returns the text that was written to
582+
* {@code System.err} and {@code System.out} by the statement.
583+
* <pre>
584+
* &#064;Test
585+
* void application_writes_text_to_System_err_and_out(
586+
* ) throws Exception {
587+
* String text = tapSystemErrAndOut((){@literal ->} {
588+
* System.err.print("text from err");
589+
* System.out.print("text from out");
590+
* });
591+
* assertEquals("text from errtext from out", text);
592+
* }
593+
* </pre>
594+
*
595+
* @param statement an arbitrary piece of code.
596+
* @return text that is written to {@code System.err} and {@code System.out}
597+
* by the statement.
598+
* @throws Exception any exception thrown by the statement.
599+
* @see #tapSystemErrAndOutNormalized(Statement)
600+
* @see #tapSystemErr(Statement)
601+
* @see #tapSystemErrNormalized(Statement)
602+
* @see #tapSystemOut(Statement)
603+
* @see #tapSystemOutNormalized(Statement)
604+
* @since 1.2.0
605+
*/
606+
public static String tapSystemErrAndOut(
607+
Statement statement
608+
) throws Exception {
609+
TapStream tapStream = new TapStream();
610+
executeWithSystemErrReplacement(
611+
tapStream,
612+
() -> executeWithSystemOutReplacement(
613+
tapStream,
614+
statement
615+
)
616+
);
617+
return tapStream.textThatWasWritten();
618+
}
619+
620+
/**
621+
* Executes the statement and returns the text that was written to
622+
* {@code System.err} and {@code System.out} by the statement. New line
623+
* characters are replaced with a single {@code \n}.
624+
* <pre>
625+
* &#064;Test
626+
* void application_writes_mutliple_lines_to_System_err_and_out(
627+
* ) throws Exception {
628+
* String text = tapSystemErrAndOutNormalized((){@literal ->} {
629+
* System.err.println("text from err");
630+
* System.out.println("text from out");
631+
* });
632+
* assertEquals("text from err\ntext from out\n", text);
633+
* }
634+
* </pre>
635+
*
636+
* @param statement an arbitrary piece of code.
637+
* @return text that is written to {@code System.err} and {@code System.out}
638+
* by the statement.
639+
* @throws Exception any exception thrown by the statement.
640+
* @see #tapSystemErrAndOut(Statement)
641+
* @see #tapSystemErr(Statement)
642+
* @see #tapSystemErrNormalized(Statement)
643+
* @see #tapSystemOut(Statement)
644+
* @see #tapSystemOutNormalized(Statement)
645+
* @since 1.2.0
646+
*/
647+
public static String tapSystemErrAndOutNormalized(
648+
Statement statement
649+
) throws Exception {
650+
return tapSystemErrAndOut(statement)
651+
.replace(lineSeparator(), "\n");
652+
}
653+
553654
/**
554655
* Executes the statement and returns the text that was written to
555656
* {@code System.out} by the statement.
@@ -568,6 +669,8 @@ public static String tapSystemErrNormalized(
568669
* @return text that is written to {@code System.out} by the statement.
569670
* @throws Exception any exception thrown by the statement.
570671
* @see #tapSystemErr(Statement)
672+
* @see #tapSystemErrAndOut(Statement)
673+
* @see #tapSystemErrAndOutNormalized(Statement)
571674
* @since 1.0.0
572675
*/
573676
public static String tapSystemOut(
@@ -600,6 +703,8 @@ public static String tapSystemOut(
600703
* @return text that is written to {@code System.out} by the statement.
601704
* @throws Exception any exception thrown by the statement.
602705
* @see #tapSystemErr(Statement)
706+
* @see #tapSystemErrAndOut(Statement)
707+
* @see #tapSystemErrAndOutNormalized(Statement)
603708
* @since 1.0.0
604709
*/
605710
public static String tapSystemOutNormalized(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.stefanbirkner.systemlambda;
2+
3+
import org.junit.jupiter.api.DisplayNameGeneration;
4+
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
5+
import org.junit.jupiter.api.Nested;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static com.github.stefanbirkner.systemlambda.SystemLambda.*;
9+
import static java.lang.System.err;
10+
import static java.lang.System.out;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
@DisplayNameGeneration(ReplaceUnderscores.class)
14+
class TapSystemErrAndOutNormalizedTest {
15+
16+
@Test
17+
void taps_text_that_is_written_to_System_err_and_out_by_statement_has_only_slash_n_for_new_line(
18+
) throws Exception {
19+
String textWrittenToSystemErr = tapSystemErrAndOutNormalized(
20+
() -> {
21+
err.println("line 1");
22+
out.println("line 2");
23+
err.println("line 3");
24+
out.println("line 4");
25+
}
26+
);
27+
28+
assertThat(textWrittenToSystemErr)
29+
.isEqualTo("line 1\nline 2\nline 3\nline 4\n");
30+
}
31+
32+
@Test
33+
void tapped_text_is_empty_when_statement_does_not_write_to_System_err_nor_out(
34+
) throws Exception {
35+
String textWrittenToSystemErr = tapSystemErrAndOutNormalized(
36+
() -> {}
37+
);
38+
39+
assertThat(textWrittenToSystemErr)
40+
.isEqualTo("");
41+
}
42+
43+
@Nested
44+
class System_err_is_same_as_before
45+
extends RestoreSystemErrChecks
46+
{
47+
System_err_is_same_as_before() {
48+
super(SystemLambda::tapSystemErrAndOutNormalized);
49+
}
50+
}
51+
52+
@Nested
53+
class System_out_is_same_as_before
54+
extends RestoreSystemOutChecks
55+
{
56+
System_out_is_same_as_before() {
57+
super(SystemLambda::tapSystemErrAndOutNormalized);
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.stefanbirkner.systemlambda;
2+
3+
import org.junit.jupiter.api.DisplayNameGeneration;
4+
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
5+
import org.junit.jupiter.api.Nested;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr;
9+
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErrAndOut;
10+
import static java.lang.System.err;
11+
import static java.lang.System.out;
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
@DisplayNameGeneration(ReplaceUnderscores.class)
15+
class TapSystemErrAndOutTest {
16+
17+
@Test
18+
void taps_text_that_is_written_to_System_err_and_out_by_statement(
19+
) throws Exception {
20+
String textWrittenToSystemErrAndOut = tapSystemErrAndOut(
21+
() -> {
22+
err.print("word1 ");
23+
out.print("word2 ");
24+
err.print("word3 ");
25+
out.print("word4 ");
26+
}
27+
);
28+
29+
assertThat(textWrittenToSystemErrAndOut)
30+
.isEqualTo("word1 word2 word3 word4 ");
31+
}
32+
33+
@Test
34+
void tapped_text_is_empty_when_statement_does_not_write_to_System_err_nor_out(
35+
) throws Exception {
36+
String textWrittenToSystemErrAndOut = tapSystemErrAndOut(
37+
() -> {}
38+
);
39+
40+
assertThat(textWrittenToSystemErrAndOut)
41+
.isEqualTo("");
42+
}
43+
44+
@Nested
45+
class System_err_is_same_as_before
46+
extends RestoreSystemErrChecks
47+
{
48+
System_err_is_same_as_before() {
49+
super(SystemLambda::tapSystemErrAndOut);
50+
}
51+
}
52+
53+
@Nested
54+
class System_out_is_same_as_before
55+
extends RestoreSystemOutChecks
56+
{
57+
System_out_is_same_as_before() {
58+
super(SystemLambda::tapSystemErrAndOut);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)