All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.contrib.java.lang.system.PrintStreamLog Maven / Gradle / Ivy

There is a newer version: 1.19.0
Show newest version
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);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy