/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled;

/**
 * Capture output from System.out and System.err.
 * 
 * @author Phillip Webb
 */
public class OutputCapture implements TestRule {

	private CaptureOutputStream captureOut;

	private CaptureOutputStream captureErr;

	private ByteArrayOutputStream copy;

	@Override
	public Statement apply(final Statement base, Description description) {
		return new Statement() {
			@Override
			public void evaluate() throws Throwable {
				captureOutput();
				try {
					base.evaluate();
				}
				finally {
					releaseOutput();
				}
			}
		};
	}

	protected void captureOutput() {
		AnsiOutput.setEnabled(Enabled.NEVER);
		this.copy = new ByteArrayOutputStream();
		this.captureOut = new CaptureOutputStream(System.out, this.copy);
		this.captureErr = new CaptureOutputStream(System.err, this.copy);
		System.setOut(new PrintStream(this.captureOut));
		System.setErr(new PrintStream(this.captureErr));
	}

	protected void releaseOutput() {
		AnsiOutput.setEnabled(Enabled.DETECT);
		System.setOut(this.captureOut.getOriginal());
		System.setErr(this.captureErr.getOriginal());
		this.copy = null;
	}

	public String getOutputAndRelease() {
		try {
			return toString();
		}
		finally {
			releaseOutput();
		}
	}

	@Override
	public String toString() {
		return this.copy.toString();
	}

	private static class CaptureOutputStream extends OutputStream {

		private final PrintStream original;

		private final OutputStream copy;

		public CaptureOutputStream(PrintStream original, OutputStream copy) {
			this.original = original;
			this.copy = copy;
		}

		@Override
		public void write(int b) throws IOException {
			this.copy.write(b);
			this.original.write(b);
			this.original.flush();
		}

		@Override
		public void write(byte[] b) throws IOException {
			write(b, 0, b.length);
		}

		@Override
		public void write(byte[] b, int off, int len) throws IOException {
			this.copy.write(b, off, len);
			this.original.write(b, off, len);
			this.original.flush();
		}

		public PrintStream getOriginal() {
			return this.original;
		}
	}

}

@@ -1585,33 +1585,40 @@
 tput
-.setEnabled(Enabled.NEVER
+Control.get().disableAnsiOutput(
 );%0A%09
@@ -1951,34 +1951,40 @@
 tput
-.setEnabled(Enabled.DETECT
+Control.get().enabledAnsiOutput(
 );%0A%09
@@ -3036,18 +3036,18 @@
 iginal;%0A
-
 %09%09%7D%0A%09%7D%0A%0A
 %7D%0A
@@ -3044,10 +3044,717 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Allow AnsiOutput to not be on the test classpath.%0A%09 */%0A%09private static class AnsiOutputControl %7B%0A%0A%09%09public void disableAnsiOutput() %7B%0A%09%09%7D%0A%0A%09%09public void enabledAnsiOutput() %7B%0A%09%09%7D%0A%0A%09%09public static AnsiOutputControl get() %7B%0A%09%09%09try %7B%0A%09%09%09%09Class.forName(%22org.springframework.boot.ansi.AnsiOutput%22);%0A%09%09%09%09return new AnsiPresentOutputControl();%0A%09%09%09%7D%0A%09%09%09catch (ClassNotFoundException e) %7B%0A%09%09%09%09return new AnsiOutputControl();%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09private static class AnsiPresentOutputControl extends AnsiOutputControl %7B%0A%0A%09%09@Override%0A%09%09public void disableAnsiOutput() %7B%0A%09%09%09AnsiOutput.setEnabled(Enabled.NEVER);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void enabledAnsiOutput() %7B%0A%09%09%09AnsiOutput.setEnabled(Enabled.DETECT);%0A%09%09%7D%0A%0A%09%7D%0A%0A
 %7D%0A

Fix NoClassDefFoundError with OutputCapture