Use OutputCapture for log tests and disable ANSI

Refactor JavaLoggerSystemTests to make use of OutputCapture and ensure
that captured output never includes ANSI symbols.
This commit is contained in:
Phillip Webb
2013-08-24 13:24:19 -07:00
parent f15eed6f02
commit 8682d7a829
2 changed files with 11 additions and 17 deletions

View File

@@ -24,6 +24,8 @@ 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.
@@ -55,6 +57,7 @@ public class OutputCapture implements TestRule {
}
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);
@@ -63,6 +66,7 @@ public class OutputCapture implements TestRule {
}
protected void releaseOutput() {
AnsiOutput.setEnabled(Enabled.DETECT);
System.setOut(this.captureOut.getOriginal());
System.setErr(this.captureErr.getOriginal());
this.copy = null;

View File

@@ -16,14 +16,14 @@
package org.springframework.boot.logging.java;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.apache.commons.logging.impl.Jdk14Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.logging.LogLevel;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@@ -42,18 +42,14 @@ public class JavaLoggerSystemTests {
private JavaLoggingSystem loggingSystem = new JavaLoggingSystem(getClass()
.getClassLoader());
private PrintStream savedOutput;
private ByteArrayOutputStream output;
@Rule
public OutputCapture output = new OutputCapture();
private Jdk14Logger logger;
@Before
public void init() throws SecurityException, IOException {
this.logger = new Jdk14Logger(getClass().getName());
this.savedOutput = System.err;
this.output = new ByteArrayOutputStream();
System.setErr(new PrintStream(this.output));
}
@After
@@ -61,19 +57,13 @@ public class JavaLoggerSystemTests {
System.clearProperty("LOG_FILE");
System.clearProperty("LOG_PATH");
System.clearProperty("PID");
System.setErr(this.savedOutput);
System.err.println(this.output);
}
private String getOutput() {
return this.output.toString();
}
@Test
public void testCustomFormatter() throws Exception {
this.loggingSystem.initialize();
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("???? INFO ["));
}
@@ -86,7 +76,7 @@ public class JavaLoggerSystemTests {
"logging.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("Hello world"));
assertTrue("Wrong output:\n" + output, output.contains("1234 INFO ["));
}
@@ -95,7 +85,7 @@ public class JavaLoggerSystemTests {
public void testNonDefaultConfigLocation() throws Exception {
this.loggingSystem.initialize("classpath:logging-nondefault.properties");
this.logger.info("Hello world");
String output = getOutput().trim();
String output = this.output.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("INFO: Hello"));
}