From f4782ebfd72c08bd3f00aa04ad20fe499c5463b4 Mon Sep 17 00:00:00 2001 From: Marc Vinyals Date: Wed, 11 Jul 2018 16:50:54 +0530 Subject: [PATCH] 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 --- README.md | 2 +- pom.xml | 2 +- .../java/lang/system/SystemErrRule.java | 28 +++++++++++++++++++ .../java/lang/system/SystemOutRule.java | 28 +++++++++++++++++++ .../lang/system/internal/LogPrintStream.java | 4 +++ .../java/lang/system/SystemErrRuleTest.java | 13 +++++++++ .../java/lang/system/SystemOutRuleTest.java | 13 +++++++++ 7 files changed, 88 insertions(+), 2 deletions(-) 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 com.github.stefanbirkner system-rules - 1.18.0 + 1.19.0 Please don't forget to add the scope `test` if you're using System diff --git a/pom.xml b/pom.xml index 6b69bb78..e764bd91 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ system-rules - 1.19.0-SNAPSHOT + 1.19.0 jar System Rules diff --git a/src/main/java/org/junit/contrib/java/lang/system/SystemErrRule.java b/src/main/java/org/junit/contrib/java/lang/system/SystemErrRule.java index d8f81299..e61a3feb 100644 --- a/src/main/java/org/junit/contrib/java/lang/system/SystemErrRule.java +++ b/src/main/java/org/junit/contrib/java/lang/system/SystemErrRule.java @@ -49,6 +49,23 @@ * } * * + *

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 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); + } + } } diff --git a/src/test/java/org/junit/contrib/java/lang/system/SystemOutRuleTest.java b/src/test/java/org/junit/contrib/java/lang/system/SystemOutRuleTest.java index 87e842db..6fe567ec 100644 --- a/src/test/java/org/junit/contrib/java/lang/system/SystemOutRuleTest.java +++ b/src/test/java/org/junit/contrib/java/lang/system/SystemOutRuleTest.java @@ -372,4 +372,17 @@ public static void verifyResult(Collection 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); + } + } }