Skip to content

Commit

Permalink
Add the ability to clear the logs of the *Log rules.
Browse files Browse the repository at this point in the history
This helps you if your test writes to the error/output stream
multiple times and you only want to test what's written by a single
call to your object under test.
  • Loading branch information
stefanbirkner committed Feb 10, 2014
1 parent e789343 commit 462ac60
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.4.1-SNAPSHOT</version>
<version>1.5.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>System Rules</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
* }
* }
* </pre>
*
* You can clear the log if you only want to test parts of the text written to
* the standard error stream.
*
* <pre>
* &#064;Test
* public void captureErrorStream() {
* System.err.print("before");
* log.clear();
* System.err.print("afterwards");
* assertEquals("afterwards", log.getLog());
* }
* </pre>
*/
public class StandardErrorStreamLog extends PrintStreamLog {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
* }
* }
* </pre>
*
* You can clear the log if you only want to test parts of the text written to
* the standard output stream.
*
* <pre>
* &#064;Test
* public void captureOutputStream() {
* System.out.print("before");
* log.clear();
* System.out.print("afterwards");
* assertEquals("afterwards", log.getLog());
* }
* </pre>
*/
public class StandardOutputStreamLog extends PrintStreamLog {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ protected void after() {

protected abstract void setStream(PrintStream wrappedLog);

/**
* Clears the log. The log can be used again.
*/
public void clear() {
log.reset();
}

/**
* Returns the text written to the standard error stream.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class StandardErrorStreamLogTest {

@Test
public void logWriting() throws Throwable {
executeRuleWithStatement();
executeRuleWithStatement(new WriteTextToStandardOutputStream());
assertThat(log.getLog(), is(equalTo(ARBITRARY_TEXT)));
}

@Test
public void restoreSystemErrorStream() throws Throwable {
PrintStream originalStream = err;
executeRuleWithStatement();
executeRuleWithStatement(new WriteTextToStandardOutputStream());
assertThat(originalStream, is(sameInstance(err)));
}

Expand All @@ -38,21 +38,37 @@ public void stillWritesToSystemErrorStream() throws Throwable {
try {
ByteArrayOutputStream captureErrorStream = new ByteArrayOutputStream();
setErr(new PrintStream(captureErrorStream));
executeRuleWithStatement();
executeRuleWithStatement(new WriteTextToStandardOutputStream());
assertThat(captureErrorStream, hasToString(equalTo(ARBITRARY_TEXT)));
} finally {
setErr(originalStream);
}
}

private void executeRuleWithStatement() throws Throwable {
log.apply(new WriteTextToStandardErrorStream(), null).evaluate();
@Test
public void collectsLogAfterClearing() throws Throwable {
executeRuleWithStatement(new ClearLogWhileWritingTextToStandardOutputStream());
assertThat(log.getLog(), is(equalTo(ARBITRARY_TEXT)));
}

private void executeRuleWithStatement(Statement statement) throws Throwable {
log.apply(statement, null).evaluate();
}

private class WriteTextToStandardErrorStream extends Statement {
private class WriteTextToStandardOutputStream extends Statement {
@Override
public void evaluate() throws Throwable {
err.print(ARBITRARY_TEXT);
}
}

private class ClearLogWhileWritingTextToStandardOutputStream extends
Statement {
@Override
public void evaluate() throws Throwable {
err.print(ARBITRARY_TEXT);
log.clear();
err.print(ARBITRARY_TEXT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class StandardOutputStreamLogTest {

@Test
public void logWriting() throws Throwable {
executeRuleWithStatement();
executeRuleWithStatement(new WriteTextToStandardOutputStream());
assertThat(log.getLog(), is(equalTo(ARBITRARY_TEXT)));
}

@Test
public void restoreSystemOutputStream() throws Throwable {
PrintStream originalStream = out;
executeRuleWithStatement();
executeRuleWithStatement(new WriteTextToStandardOutputStream());
assertThat(originalStream, is(sameInstance(out)));
}

Expand All @@ -38,16 +38,22 @@ public void stillWritesToSystemOutputStream() throws Throwable {
try {
ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream();
setOut(new PrintStream(captureOutputStream));
executeRuleWithStatement();
executeRuleWithStatement(new WriteTextToStandardOutputStream());
assertThat(captureOutputStream,
hasToString(equalTo(ARBITRARY_TEXT)));
} finally {
setOut(originalStream);
}
}

private void executeRuleWithStatement() throws Throwable {
log.apply(new WriteTextToStandardOutputStream(), null).evaluate();
@Test
public void collectsLogAfterClearing() throws Throwable {
executeRuleWithStatement(new ClearLogWhileWritingTextToStandardOutputStream());
assertThat(log.getLog(), is(equalTo(ARBITRARY_TEXT)));
}

private void executeRuleWithStatement(Statement statement) throws Throwable {
log.apply(statement, null).evaluate();
}

private class WriteTextToStandardOutputStream extends Statement {
Expand All @@ -56,4 +62,14 @@ public void evaluate() throws Throwable {
out.print(ARBITRARY_TEXT);
}
}

private class ClearLogWhileWritingTextToStandardOutputStream extends
Statement {
@Override
public void evaluate() throws Throwable {
out.print(ARBITRARY_TEXT);
log.clear();
out.print(ARBITRARY_TEXT);
}
}
}

0 comments on commit 462ac60

Please sign in to comment.