Created
June 26, 2010 22:11
-
-
Save dgageot/454387 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.algodeal.test.rules; | |
import static com.algodeal.test.rules.ConsoleRecorder.ConsoleType.*; | |
import java.io.*; | |
import org.junit.rules.ExternalResource; | |
/** | |
* JUnit Rule to record System.out or System.err output. | |
*/ | |
public class ConsoleRecorder extends ExternalResource { | |
private final ByteArrayOutputStream recordedContent = new ByteArrayOutputStream(); | |
private final PrintStream recordingOutput = new PrintStream(recordedContent); | |
private final ConsoleType console; | |
private ConsoleRecorder(ConsoleType recorderType) { | |
console = recorderType; | |
} | |
public static ConsoleRecorder recordSystemOut() { | |
return new ConsoleRecorder(OUT); | |
} | |
public static ConsoleRecorder recordSystemErr() { | |
return new ConsoleRecorder(ERR); | |
} | |
@Override | |
protected void before() throws Throwable { | |
console.redirectTo(recordingOutput); | |
} | |
@Override | |
protected void after() { | |
console.restore(); | |
} | |
public String getOutput() { | |
recordingOutput.flush(); | |
return recordedContent.toString(); | |
} | |
static enum ConsoleType { | |
OUT { | |
@Override | |
void redirectTo(PrintStream stream) { | |
System.setOut(stream); | |
} | |
@Override | |
void restore() { | |
System.setOut(System.out); | |
} | |
}, | |
ERR { | |
@Override | |
void redirectTo(PrintStream stream) { | |
System.setErr(stream); | |
} | |
@Override | |
void restore() { | |
System.setErr(System.err); | |
} | |
}; | |
abstract void redirectTo(PrintStream stream); | |
abstract void restore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment