From 58592c80ea69ffd8930a8e0a8b7217fc7a9d2a16 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 9 Nov 2020 14:39:05 +0100 Subject: [PATCH] Fixing the OutputCapture issue --- .../cloud/cli/command/url/OutputCapture.java | 127 ++++++++++++++++++ .../cli/command/url/UrlDecodeCommandTest.java | 1 - .../cli/command/url/UrlEncodeCommandTest.java | 1 - 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/OutputCapture.java diff --git a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/OutputCapture.java b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/OutputCapture.java new file mode 100644 index 0000000..fcb141e --- /dev/null +++ b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/OutputCapture.java @@ -0,0 +1,127 @@ +/* + * Copyright 2012-2014 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 + * + * https://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.cloud.cli.command.url; + +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; + +/** + * 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() { + 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() { + System.setOut(this.captureOut.getOriginal()); + System.setErr(this.captureErr.getOriginal()); + this.copy = null; + } + + public void flush() { + try { + this.captureOut.flush(); + this.captureErr.flush(); + } + catch (IOException ex) { + // ignore + } + } + + @Override + public String toString() { + flush(); + return this.copy.toString(); + } + + private static class CaptureOutputStream extends OutputStream { + + private final PrintStream original; + + private final OutputStream copy; + + 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); + } + + public PrintStream getOriginal() { + return this.original; + } + + @Override + public void flush() throws IOException { + this.copy.flush(); + this.original.flush(); + } + } + +} diff --git a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java index a65f3a8..cc970ed 100644 --- a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java +++ b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlDecodeCommandTest.java @@ -19,7 +19,6 @@ import org.junit.Rule; import org.junit.Test; import org.springframework.boot.cli.command.Command; import org.springframework.boot.cli.command.status.ExitStatus; -import org.springframework.boot.test.rule.OutputCapture; import static org.junit.Assert.*; diff --git a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java index b866797..8a59ce6 100644 --- a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java +++ b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/url/UrlEncodeCommandTest.java @@ -19,7 +19,6 @@ import org.junit.Rule; import org.junit.Test; import org.springframework.boot.cli.command.Command; import org.springframework.boot.cli.command.status.ExitStatus; -import org.springframework.boot.test.rule.OutputCapture; import static org.junit.Assert.*;