Skip to content

Commit

Permalink
Raw bytes of System.out and System.err are logged
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
marcvinyals and stefanbirkner committed Nov 14, 2018
1 parent 5342d5a commit f4782eb
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ System Rules is available from
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.18.0</version>
<version>1.19.0</version>
</dependency>

Please don't forget to add the scope `test` if you're using System
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>system-rules</artifactId>
<version>1.19.0-SNAPSHOT</version>
<version>1.19.0</version>
<packaging>jar</packaging>

<name>System Rules</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@
* }
* </pre>
*
* <p>If your code under test writes raw binary data to {@code System.err} then
* you can read it by means of {@link #getLogAsBytes()}).
*
* <pre>
* public class SystemErrTest {
* &#064;Rule
* public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();
*
* &#064;Test
* public void test() {
* byte[] data = { 1, 2, 3, 4, 5 };
* System.err.write(data, 0, data.length);
* assertEquals(data, systemErrRule.{@link #getLogAsBytes()});
* }
* }
* </pre>
*
* <p>You don't have to enable logging for every test. It can be enabled for
* specific tests only.
*
Expand Down Expand Up @@ -221,6 +238,17 @@ public String getLogWithNormalizedLineSeparator() {
return logPrintStream.getLogWithNormalizedLineSeparator();
}

/**
* Returns the raw bytes that are written to {@code System.err} since
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
*
* @return the raw bytes that are written to {@code System.err} since
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
*/
public byte[] getLogAsBytes() {
return logPrintStream.getLogAsBytes();
}

/**
* Start logging of everything that is written to {@code System.err}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@
* }
* </pre>
*
* <p>If your code under test writes raw binary data to {@code System.out} then
* you can read it by means of {@link #getLogAsBytes()}).
*
* <pre>
* public class SystemOutTest {
* &#064;Rule
* public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
*
* &#064;Test
* public void test() {
* byte[] data = { 1, 2, 3, 4, 5 };
* System.out.write(data, 0, data.length);
* assertEquals(data, systemOutRule.{@link #getLogAsBytes()});
* }
* }
* </pre>
*
* <p>You don't have to enable logging for every test. It can be enabled for
* specific tests only.
*
Expand Down Expand Up @@ -221,6 +238,17 @@ public String getLogWithNormalizedLineSeparator() {
return logPrintStream.getLogWithNormalizedLineSeparator();
}

/**
* Returns the raw bytes that are written to {@code System.out} since
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
*
* @return the raw bytes that are written to {@code System.out} since
* {@link #enableLog()} (respectively {@link #clearLog()} has been called.
*/
public byte[] getLogAsBytes() {
return logPrintStream.getLogAsBytes();
}

/**
* Start logging of everything that is written to {@code System.out}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public String getLogWithNormalizedLineSeparator() {
return getLog().replace(lineSeparator, "\n");
}

public byte[] getLogAsBytes() {
return muteableLogStream.log.toByteArray();
}

public void mute() {
muteableLogStream.originalStreamMuted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,17 @@ public static void verifyResult(Collection<Failure> failures) {
assertThat(failures).isEmpty();
}
}

public static class raw_bytes_of_output_are_available_when_logging_is_enabled {
@Rule
public final SystemErrRule systemErrRule = new SystemErrRule()
.enableLog();

@Test
public void test() {
byte[] data = { 1, 2, 3, 4, 5 };
System.err.write(data, 0, data.length);
assertThat(systemErrRule.getLogAsBytes()).isEqualTo(data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,17 @@ public static void verifyResult(Collection<Failure> failures) {
assertThat(failures).isEmpty();
}
}

public static class raw_bytes_of_output_are_available_when_logging_is_enabled {
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule()
.enableLog();

@Test
public void test() {
byte[] data = { 1, 2, 3, 4, 5 };
System.out.write(data, 0, data.length);
assertThat(systemOutRule.getLogAsBytes()).isEqualTo(data);
}
}
}

0 comments on commit f4782eb

Please sign in to comment.