Skip to content

Commit f4782eb

Browse files
Raw bytes of System.out and System.err are logged
SystemErrRule and SystemOutRule have a new method "getLogAsBytes" which returns the raw bytes that have been written to System.err/System.out. This method can be used for testing applications that write raw binary data. Fixes #66. Co-authored-by: Stefan Birkner <mail@stefan-birkner.de>
1 parent 5342d5a commit f4782eb

File tree

7 files changed

+88
-2
lines changed

7 files changed

+88
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ System Rules is available from
1414
<dependency>
1515
<groupId>com.github.stefanbirkner</groupId>
1616
<artifactId>system-rules</artifactId>
17-
<version>1.18.0</version>
17+
<version>1.19.0</version>
1818
</dependency>
1919

2020
Please don't forget to add the scope `test` if you're using System

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>system-rules</artifactId>
12-
<version>1.19.0-SNAPSHOT</version>
12+
<version>1.19.0</version>
1313
<packaging>jar</packaging>
1414

1515
<name>System Rules</name>

src/main/java/org/junit/contrib/java/lang/system/SystemErrRule.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@
4949
* }
5050
* </pre>
5151
*
52+
* <p>If your code under test writes raw binary data to {@code System.err} then
53+
* you can read it by means of {@link #getLogAsBytes()}).
54+
*
55+
* <pre>
56+
* public class SystemErrTest {
57+
* &#064;Rule
58+
* public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();
59+
*
60+
* &#064;Test
61+
* public void test() {
62+
* byte[] data = { 1, 2, 3, 4, 5 };
63+
* System.err.write(data, 0, data.length);
64+
* assertEquals(data, systemErrRule.{@link #getLogAsBytes()});
65+
* }
66+
* }
67+
* </pre>
68+
*
5269
* <p>You don't have to enable logging for every test. It can be enabled for
5370
* specific tests only.
5471
*
@@ -221,6 +238,17 @@ public String getLogWithNormalizedLineSeparator() {
221238
return logPrintStream.getLogWithNormalizedLineSeparator();
222239
}
223240

241+
/**
242+
* Returns the raw bytes that are written to {@code System.err} since
243+
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
244+
*
245+
* @return the raw bytes that are written to {@code System.err} since
246+
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
247+
*/
248+
public byte[] getLogAsBytes() {
249+
return logPrintStream.getLogAsBytes();
250+
}
251+
224252
/**
225253
* Start logging of everything that is written to {@code System.err}.
226254
*

src/main/java/org/junit/contrib/java/lang/system/SystemOutRule.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@
4949
* }
5050
* </pre>
5151
*
52+
* <p>If your code under test writes raw binary data to {@code System.out} then
53+
* you can read it by means of {@link #getLogAsBytes()}).
54+
*
55+
* <pre>
56+
* public class SystemOutTest {
57+
* &#064;Rule
58+
* public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
59+
*
60+
* &#064;Test
61+
* public void test() {
62+
* byte[] data = { 1, 2, 3, 4, 5 };
63+
* System.out.write(data, 0, data.length);
64+
* assertEquals(data, systemOutRule.{@link #getLogAsBytes()});
65+
* }
66+
* }
67+
* </pre>
68+
*
5269
* <p>You don't have to enable logging for every test. It can be enabled for
5370
* specific tests only.
5471
*
@@ -221,6 +238,17 @@ public String getLogWithNormalizedLineSeparator() {
221238
return logPrintStream.getLogWithNormalizedLineSeparator();
222239
}
223240

241+
/**
242+
* Returns the raw bytes that are written to {@code System.out} since
243+
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
244+
*
245+
* @return the raw bytes that are written to {@code System.out} since
246+
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
247+
*/
248+
public byte[] getLogAsBytes() {
249+
return logPrintStream.getLogAsBytes();
250+
}
251+
224252
/**
225253
* Start logging of everything that is written to {@code System.out}.
226254
*

src/main/java/org/junit/contrib/java/lang/system/internal/LogPrintStream.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public String getLogWithNormalizedLineSeparator() {
6666
return getLog().replace(lineSeparator, "\n");
6767
}
6868

69+
public byte[] getLogAsBytes() {
70+
return muteableLogStream.log.toByteArray();
71+
}
72+
6973
public void mute() {
7074
muteableLogStream.originalStreamMuted = true;
7175
}

src/test/java/org/junit/contrib/java/lang/system/SystemErrRuleTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,17 @@ public static void verifyResult(Collection<Failure> failures) {
373373
assertThat(failures).isEmpty();
374374
}
375375
}
376+
377+
public static class raw_bytes_of_output_are_available_when_logging_is_enabled {
378+
@Rule
379+
public final SystemErrRule systemErrRule = new SystemErrRule()
380+
.enableLog();
381+
382+
@Test
383+
public void test() {
384+
byte[] data = { 1, 2, 3, 4, 5 };
385+
System.err.write(data, 0, data.length);
386+
assertThat(systemErrRule.getLogAsBytes()).isEqualTo(data);
387+
}
388+
}
376389
}

src/test/java/org/junit/contrib/java/lang/system/SystemOutRuleTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,17 @@ public static void verifyResult(Collection<Failure> failures) {
372372
assertThat(failures).isEmpty();
373373
}
374374
}
375+
376+
public static class raw_bytes_of_output_are_available_when_logging_is_enabled {
377+
@Rule
378+
public final SystemOutRule systemOutRule = new SystemOutRule()
379+
.enableLog();
380+
381+
@Test
382+
public void test() {
383+
byte[] data = { 1, 2, 3, 4, 5 };
384+
System.out.write(data, 0, data.length);
385+
assertThat(systemOutRule.getLogAsBytes()).isEqualTo(data);
386+
}
387+
}
375388
}

0 commit comments

Comments
 (0)