org.junit.contrib.java.lang.system.PrintStreamLog Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of system-rules Show documentation
Show all versions of system-rules Show documentation
A collection of JUnit rules for testing code which uses java.lang.System.
package org.junit.contrib.java.lang.system;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import org.junit.rules.ExternalResource;
abstract class PrintStreamLog extends ExternalResource {
private final ByteArrayOutputStream log = new ByteArrayOutputStream();
private PrintStream originalStream;
@Override
protected void before() throws Throwable {
originalStream = getOriginalStream();
PrintStream wrappedLog = new PrintStream(log);
setStream(wrappedLog);
}
@Override
protected void after() {
setStream(originalStream);
}
abstract PrintStream getOriginalStream();
abstract void setStream(PrintStream wrappedLog);
/**
* Returns the text written to the standard error stream.
*
* @return the text written to the standard error stream.
*/
public String getLog() {
try {
return log.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}