Commit 8b4a87d2 authored by Phillip Webb's avatar Phillip Webb

Use OutputCapture for log tests and disable ANSI

Refactor JavaLoggerSystemTests to make use of OutputCapture and ensure
that captured output never includes ANSI symbols.
parent 148dd2cc
...@@ -24,6 +24,8 @@ import java.io.PrintStream; ...@@ -24,6 +24,8 @@ import java.io.PrintStream;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.junit.runners.model.Statement; 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. * Capture output from System.out and System.err.
...@@ -55,6 +57,7 @@ public class OutputCapture implements TestRule { ...@@ -55,6 +57,7 @@ public class OutputCapture implements TestRule {
} }
protected void captureOutput() { protected void captureOutput() {
AnsiOutput.setEnabled(Enabled.NEVER);
this.copy = new ByteArrayOutputStream(); this.copy = new ByteArrayOutputStream();
this.captureOut = new CaptureOutputStream(System.out, this.copy); this.captureOut = new CaptureOutputStream(System.out, this.copy);
this.captureErr = new CaptureOutputStream(System.err, this.copy); this.captureErr = new CaptureOutputStream(System.err, this.copy);
...@@ -63,6 +66,7 @@ public class OutputCapture implements TestRule { ...@@ -63,6 +66,7 @@ public class OutputCapture implements TestRule {
} }
protected void releaseOutput() { protected void releaseOutput() {
AnsiOutput.setEnabled(Enabled.DETECT);
System.setOut(this.captureOut.getOriginal()); System.setOut(this.captureOut.getOriginal());
System.setErr(this.captureErr.getOriginal()); System.setErr(this.captureErr.getOriginal());
this.copy = null; this.copy = null;
......
...@@ -16,14 +16,14 @@ ...@@ -16,14 +16,14 @@
package org.springframework.boot.logging.java; package org.springframework.boot.logging.java;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import org.apache.commons.logging.impl.Jdk14Logger; import org.apache.commons.logging.impl.Jdk14Logger;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LogLevel;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -42,18 +42,14 @@ public class JavaLoggerSystemTests { ...@@ -42,18 +42,14 @@ public class JavaLoggerSystemTests {
private JavaLoggingSystem loggingSystem = new JavaLoggingSystem(getClass() private JavaLoggingSystem loggingSystem = new JavaLoggingSystem(getClass()
.getClassLoader()); .getClassLoader());
private PrintStream savedOutput; @Rule
public OutputCapture output = new OutputCapture();
private ByteArrayOutputStream output;
private Jdk14Logger logger; private Jdk14Logger logger;
@Before @Before
public void init() throws SecurityException, IOException { public void init() throws SecurityException, IOException {
this.logger = new Jdk14Logger(getClass().getName()); this.logger = new Jdk14Logger(getClass().getName());
this.savedOutput = System.err;
this.output = new ByteArrayOutputStream();
System.setErr(new PrintStream(this.output));
} }
@After @After
...@@ -61,19 +57,13 @@ public class JavaLoggerSystemTests { ...@@ -61,19 +57,13 @@ public class JavaLoggerSystemTests {
System.clearProperty("LOG_FILE"); System.clearProperty("LOG_FILE");
System.clearProperty("LOG_PATH"); System.clearProperty("LOG_PATH");
System.clearProperty("PID"); System.clearProperty("PID");
System.setErr(this.savedOutput);
System.err.println(this.output);
}
private String getOutput() {
return this.output.toString();
} }
@Test @Test
public void testCustomFormatter() throws Exception { public void testCustomFormatter() throws Exception {
this.loggingSystem.initialize(); this.loggingSystem.initialize();
this.logger.info("Hello world"); this.logger.info("Hello world");
String output = getOutput().trim(); String output = this.output.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world")); assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertTrue("Wrong output:\n" + output, output.contains("???? INFO [")); assertTrue("Wrong output:\n" + output, output.contains("???? INFO ["));
} }
...@@ -86,7 +76,7 @@ public class JavaLoggerSystemTests { ...@@ -86,7 +76,7 @@ public class JavaLoggerSystemTests {
"logging.properties")); "logging.properties"));
this.logger.info("Hello world"); this.logger.info("Hello world");
this.logger.info("Hello world"); this.logger.info("Hello world");
String output = getOutput().trim(); String output = this.output.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world")); assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertTrue("Wrong output:\n" + output, output.contains("1234 INFO [")); assertTrue("Wrong output:\n" + output, output.contains("1234 INFO ["));
} }
...@@ -95,7 +85,7 @@ public class JavaLoggerSystemTests { ...@@ -95,7 +85,7 @@ public class JavaLoggerSystemTests {
public void testNonDefaultConfigLocation() throws Exception { public void testNonDefaultConfigLocation() throws Exception {
this.loggingSystem.initialize("classpath:logging-nondefault.properties"); this.loggingSystem.initialize("classpath:logging-nondefault.properties");
this.logger.info("Hello world"); this.logger.info("Hello world");
String output = getOutput().trim(); String output = this.output.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("INFO: Hello")); assertTrue("Wrong output:\n" + output, output.contains("INFO: Hello"));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment