diff --git a/README.md b/README.md
index afc642d0..292395ca 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,7 @@ System Rules is available from
If your code under test writes raw binary data to {@code System.err} then + * you can read it by means of {@link #getLogAsBytes()}). + * + *
+ * public class SystemErrTest { + * @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); + * assertEquals(data, systemErrRule.{@link #getLogAsBytes()}); + * } + * } + *+ * *
You don't have to enable logging for every test. It can be enabled for * specific tests only. * @@ -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}. * diff --git a/src/main/java/org/junit/contrib/java/lang/system/SystemOutRule.java b/src/main/java/org/junit/contrib/java/lang/system/SystemOutRule.java index 5e40439b..e1db929c 100644 --- a/src/main/java/org/junit/contrib/java/lang/system/SystemOutRule.java +++ b/src/main/java/org/junit/contrib/java/lang/system/SystemOutRule.java @@ -49,6 +49,23 @@ * } * * + *
If your code under test writes raw binary data to {@code System.out} then + * you can read it by means of {@link #getLogAsBytes()}). + * + *
+ * public class SystemOutTest { + * @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); + * assertEquals(data, systemOutRule.{@link #getLogAsBytes()}); + * } + * } + *+ * *
You don't have to enable logging for every test. It can be enabled for
* specific tests only.
*
@@ -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}.
*
diff --git a/src/main/java/org/junit/contrib/java/lang/system/internal/LogPrintStream.java b/src/main/java/org/junit/contrib/java/lang/system/internal/LogPrintStream.java
index dc408321..09ce70e3 100644
--- a/src/main/java/org/junit/contrib/java/lang/system/internal/LogPrintStream.java
+++ b/src/main/java/org/junit/contrib/java/lang/system/internal/LogPrintStream.java
@@ -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;
}
diff --git a/src/test/java/org/junit/contrib/java/lang/system/SystemErrRuleTest.java b/src/test/java/org/junit/contrib/java/lang/system/SystemErrRuleTest.java
index 330d50b3..3dc445d0 100644
--- a/src/test/java/org/junit/contrib/java/lang/system/SystemErrRuleTest.java
+++ b/src/test/java/org/junit/contrib/java/lang/system/SystemErrRuleTest.java
@@ -373,4 +373,17 @@ public static void verifyResult(Collection