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

org.junit.contrib.java.lang.system.internal.PrintStreamHandler Maven / Gradle / Ivy

There is a newer version: 1.19.0
Show newest version
package org.junit.contrib.java.lang.system.internal;

import static java.lang.System.err;
import static java.lang.System.out;
import static java.lang.System.setErr;
import static java.lang.System.setOut;

import java.io.PrintStream;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public enum PrintStreamHandler {
	SYSTEM_OUT {
		@Override
		PrintStream getStream() {
			return out;
		}

		@Override
		void replaceCurrentStreamWithStream(PrintStream stream) {
			setOut(stream);
		}
	},
	SYSTEM_ERR {
		@Override
		PrintStream getStream() {
			return err;
		}

		@Override
		void replaceCurrentStreamWithStream(PrintStream stream) {
			setErr(stream);
		}
	};

	Statement createRestoreStatement(final Statement base) {
		return new Statement() {
			@Override
			public void evaluate() throws Throwable {
				PrintStream originalStream = getStream();
				try {
					base.evaluate();
				} finally {
					replaceCurrentStreamWithStream(originalStream);
				}
			}
		};
	}

	abstract PrintStream getStream();

	abstract void replaceCurrentStreamWithStream(PrintStream stream);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy