Commit 4038b394 authored by Phillip Webb's avatar Phillip Webb

Add OutputCapture test class

parent 17442788
...@@ -34,6 +34,14 @@ ...@@ -34,6 +34,14 @@
<artifactId>groovy-templates</artifactId> <artifactId>groovy-templates</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- Test -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-boot</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.javassist</groupId> <groupId>org.javassist</groupId>
<artifactId>javassist</artifactId> <artifactId>javassist</artifactId>
......
...@@ -16,8 +16,6 @@ ...@@ -16,8 +16,6 @@
package org.springframework.boot.cli; package org.springframework.boot.cli;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL; import java.net.URL;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
...@@ -26,9 +24,10 @@ import java.util.concurrent.TimeUnit; ...@@ -26,9 +24,10 @@ import java.util.concurrent.TimeUnit;
import org.apache.ivy.util.FileUtil; import org.apache.ivy.util.FileUtil;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.cli.command.RunCommand; import org.springframework.boot.cli.command.RunCommand;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
...@@ -43,37 +42,14 @@ public class SampleIntegrationTests { ...@@ -43,37 +42,14 @@ public class SampleIntegrationTests {
@BeforeClass @BeforeClass
public static void cleanGrapes() throws Exception { public static void cleanGrapes() throws Exception {
GrapesCleaner.cleanIfNecessary(); // GrapesCleaner.cleanIfNecessary();
// System.setProperty("ivy.message.logger.level", "3"); // System.setProperty("ivy.message.logger.level", "3");
} }
private RunCommand command; @Rule
public OutputCapture outputCapture = new OutputCapture();
private PrintStream savedOutput;
private ByteArrayOutputStream output;
private PrintStream savedErr;
@Before
public void init() {
this.savedOutput = System.out;
this.savedErr = System.err;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
System.setErr(new PrintStream(this.output));
}
@After private RunCommand command;
public void clear() {
System.setOut(this.savedOutput);
System.setErr(this.savedErr);
System.out.println(getOutput());
}
private String getOutput() {
return this.output.toString();
}
private void start(final String... sample) throws Exception { private void start(final String... sample) throws Exception {
Future<RunCommand> future = Executors.newSingleThreadExecutor().submit( Future<RunCommand> future = Executors.newSingleThreadExecutor().submit(
...@@ -98,21 +74,22 @@ public class SampleIntegrationTests { ...@@ -98,21 +74,22 @@ public class SampleIntegrationTests {
@Test @Test
public void appSample() throws Exception { public void appSample() throws Exception {
start("samples/app.groovy"); start("samples/app.groovy");
String output = getOutput(); String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello World")); assertTrue("Wrong output: " + output, output.contains("Hello World"));
} }
@Test @Test
public void templateSample() throws Exception { public void templateSample() throws Exception {
start("samples/template.groovy"); start("samples/template.groovy");
String output = getOutput(); String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello World!")); assertTrue("Wrong output: " + output, output.contains("Hello World!"));
} }
@Test @Test
public void jobSample() throws Exception { public void jobSample() throws Exception {
start("samples/job.groovy", "foo=bar"); start("samples/job.groovy", "foo=bar");
String output = getOutput(); String output = this.outputCapture.getOutputAndRelease();
System.out.println(output);
assertTrue("Wrong output: " + output, assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters")); output.contains("completed with the following parameters"));
} }
...@@ -120,11 +97,11 @@ public class SampleIntegrationTests { ...@@ -120,11 +97,11 @@ public class SampleIntegrationTests {
@Test @Test
public void reactorSample() throws Exception { public void reactorSample() throws Exception {
start("samples/reactor.groovy", "Phil"); start("samples/reactor.groovy", "Phil");
String output = getOutput(); String output = this.outputCapture.getOutputAndRelease();
int count = 0; int count = 0;
while (!output.contains("Hello Phil") && count++ < 5) { while (!output.contains("Hello Phil") && count++ < 5) {
Thread.sleep(200); Thread.sleep(200);
output = getOutput(); output = this.outputCapture.getOutputAndRelease();
} }
assertTrue("Wrong output: " + output, output.contains("Hello Phil")); assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
} }
...@@ -132,7 +109,7 @@ public class SampleIntegrationTests { ...@@ -132,7 +109,7 @@ public class SampleIntegrationTests {
@Test @Test
public void jobWebSample() throws Exception { public void jobWebSample() throws Exception {
start("samples/job.groovy", "samples/web.groovy", "foo=bar"); start("samples/job.groovy", "samples/web.groovy", "foo=bar");
String output = getOutput(); String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters")); output.contains("completed with the following parameters"));
String result = FileUtil.readEntirely(new URL("http://localhost:8080") String result = FileUtil.readEntirely(new URL("http://localhost:8080")
...@@ -170,14 +147,14 @@ public class SampleIntegrationTests { ...@@ -170,14 +147,14 @@ public class SampleIntegrationTests {
@Test @Test
public void integrationSample() throws Exception { public void integrationSample() throws Exception {
start("samples/integration.groovy"); start("samples/integration.groovy");
String output = getOutput(); String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello, World")); assertTrue("Wrong output: " + output, output.contains("Hello, World"));
} }
@Test @Test
public void xmlSample() throws Exception { public void xmlSample() throws Exception {
start("samples/app.xml", "samples/runner.groovy"); start("samples/app.xml", "samples/runner.groovy");
String output = getOutput(); String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello World")); assertTrue("Wrong output: " + output, output.contains("Hello World"));
} }
......
...@@ -16,48 +16,24 @@ ...@@ -16,48 +16,24 @@
package org.springframework.boot.sample.batch; package org.springframework.boot.sample.batch;
import java.io.ByteArrayOutputStream; import org.junit.Rule;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.sample.batch.SampleBatchApplication;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class SampleBatchApplicationTests { public class SampleBatchApplicationTests {
private PrintStream savedOutput; @Rule
private PrintStream savedErr; public OutputCapture outputCapture = new OutputCapture();
private ByteArrayOutputStream output;
@Before
public void init() {
this.savedOutput = System.out;
this.savedErr = System.err;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
System.setErr(new PrintStream(this.output));
}
@After
public void after() {
System.setOut(this.savedOutput);
System.setErr(this.savedErr);
}
private String getOutput() {
return this.output.toString();
}
@Test @Test
public void testDefaultSettings() throws Exception { public void testDefaultSettings() throws Exception {
assertEquals(0, SpringApplication.exit(SpringApplication assertEquals(0, SpringApplication.exit(SpringApplication
.run(SampleBatchApplication.class))); .run(SampleBatchApplication.class)));
String output = getOutput(); String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters")); output.contains("completed with the following parameters"));
} }
......
...@@ -16,30 +16,20 @@ ...@@ -16,30 +16,20 @@
package org.springframework.boot.sample.profile; package org.springframework.boot.sample.profile;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.sample.profile.SampleProfileApplication; import org.springframework.boot.OutputCapture;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class SampleProfileApplicationTests { public class SampleProfileApplicationTests {
private static PrintStream savedOutput; @Rule
private static ByteArrayOutputStream output; public OutputCapture outputCapture = new OutputCapture();
private String profiles;
@BeforeClass private String profiles;
public static void init() {
savedOutput = System.out;
output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
}
@Before @Before
public void before() { public void before() {
...@@ -56,19 +46,10 @@ public class SampleProfileApplicationTests { ...@@ -56,19 +46,10 @@ public class SampleProfileApplicationTests {
} }
} }
@AfterClass
public static void clear() {
System.setOut(savedOutput);
}
private static String getOutput() {
return output.toString();
}
@Test @Test
public void testDefaultProfile() throws Exception { public void testDefaultProfile() throws Exception {
SampleProfileApplication.main(new String[0]); SampleProfileApplication.main(new String[0]);
String output = getOutput(); String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello Phil")); assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
} }
...@@ -76,7 +57,7 @@ public class SampleProfileApplicationTests { ...@@ -76,7 +57,7 @@ public class SampleProfileApplicationTests {
public void testGoodbyeProfile() throws Exception { public void testGoodbyeProfile() throws Exception {
System.setProperty("spring.profiles.active", "goodbye"); System.setProperty("spring.profiles.active", "goodbye");
SampleProfileApplication.main(new String[0]); SampleProfileApplication.main(new String[0]);
String output = getOutput(); String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Goodbye Everyone")); assertTrue("Wrong output: " + output, output.contains("Goodbye Everyone"));
} }
...@@ -84,7 +65,7 @@ public class SampleProfileApplicationTests { ...@@ -84,7 +65,7 @@ public class SampleProfileApplicationTests {
public void testGoodbyeProfileFromCommandline() throws Exception { public void testGoodbyeProfileFromCommandline() throws Exception {
SampleProfileApplication SampleProfileApplication
.main(new String[] { "--spring.profiles.active=goodbye" }); .main(new String[] { "--spring.profiles.active=goodbye" });
String output = getOutput(); String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Goodbye Everyone")); assertTrue("Wrong output: " + output, output.contains("Goodbye Everyone"));
} }
......
...@@ -16,13 +16,11 @@ ...@@ -16,13 +16,11 @@
package org.springframework.boot.sample.simple; package org.springframework.boot.sample.simple;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
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.sample.simple.SampleSimpleApplication; import org.springframework.boot.OutputCapture;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -34,17 +32,13 @@ import static org.junit.Assert.assertTrue; ...@@ -34,17 +32,13 @@ import static org.junit.Assert.assertTrue;
*/ */
public class SampleSimpleApplicationTests { public class SampleSimpleApplicationTests {
private PrintStream savedOutput; @Rule
public OutputCapture outputCapture = new OutputCapture();
private ByteArrayOutputStream output;
private String profiles; private String profiles;
@Before @Before
public void init() { public void init() {
this.savedOutput = System.out;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
this.profiles = System.getProperty("spring.profiles.active"); this.profiles = System.getProperty("spring.profiles.active");
} }
...@@ -56,24 +50,19 @@ public class SampleSimpleApplicationTests { ...@@ -56,24 +50,19 @@ public class SampleSimpleApplicationTests {
else { else {
System.clearProperty("spring.profiles.active"); System.clearProperty("spring.profiles.active");
} }
System.setOut(this.savedOutput);
}
private String getOutput() {
return this.output.toString();
} }
@Test @Test
public void testDefaultSettings() throws Exception { public void testDefaultSettings() throws Exception {
SampleSimpleApplication.main(new String[0]); SampleSimpleApplication.main(new String[0]);
String output = getOutput(); String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello Phil")); assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
} }
@Test @Test
public void testCommandLineOverrides() throws Exception { public void testCommandLineOverrides() throws Exception {
SampleSimpleApplication.main(new String[] { "--name=Gordon" }); SampleSimpleApplication.main(new String[] { "--name=Gordon" });
String output = getOutput(); String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello Gordon")); assertTrue("Wrong output: " + output, output.contains("Hello Gordon"));
} }
......
...@@ -16,41 +16,21 @@ ...@@ -16,41 +16,21 @@
package org.springframework.boot.sample.xml; package org.springframework.boot.sample.xml;
import java.io.ByteArrayOutputStream; import org.junit.Rule;
import java.io.PrintStream;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.sample.xml.SampleSpringXmlApplication; import org.springframework.boot.OutputCapture;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class SampleSpringXmlApplicationTests { public class SampleSpringXmlApplicationTests {
private static PrintStream savedOutput; @Rule
private static ByteArrayOutputStream output; public OutputCapture outputCapture = new OutputCapture();
@BeforeClass
public static void init() {
savedOutput = System.out;
output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
}
@AfterClass
public static void clear() {
System.setOut(savedOutput);
}
private static String getOutput() {
return output.toString();
}
@Test @Test
public void testDefaultSettings() throws Exception { public void testDefaultSettings() throws Exception {
SampleSpringXmlApplication.main(new String[0]); SampleSpringXmlApplication.main(new String[0]);
String output = getOutput(); String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello World")); assertTrue("Wrong output: " + output, output.contains("Hello World"));
} }
......
/*
* 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;
/**
* 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 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;
}
}
}
...@@ -16,14 +16,11 @@ ...@@ -16,14 +16,11 @@
package org.springframework.boot; package org.springframework.boot;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.After; import org.junit.Rule;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
...@@ -39,22 +36,10 @@ import static org.junit.Assert.assertTrue; ...@@ -39,22 +36,10 @@ import static org.junit.Assert.assertTrue;
@Configuration @Configuration
public class SimpleMainTests { public class SimpleMainTests {
private static final String SPRING_STARTUP = "root of context hierarchy"; @Rule
private PrintStream savedOutput; public OutputCapture outputCapture = new OutputCapture();
private ByteArrayOutputStream output;
@Before
public void open() {
this.savedOutput = System.out;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
}
@After private static final String SPRING_STARTUP = "root of context hierarchy";
public void after() {
System.setOut(this.savedOutput);
System.out.println(getOutput());
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void emptyApplicationContext() throws Exception { public void emptyApplicationContext() throws Exception {
...@@ -99,7 +84,7 @@ public class SimpleMainTests { ...@@ -99,7 +84,7 @@ public class SimpleMainTests {
} }
private String getOutput() { private String getOutput() {
return this.output.toString(); return this.outputCapture.toString();
} }
} }
...@@ -16,9 +16,7 @@ ...@@ -16,9 +16,7 @@
package org.springframework.boot.context.initializer; package org.springframework.boot.context.initializer;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.LogManager; import java.util.logging.LogManager;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
...@@ -29,8 +27,8 @@ import org.junit.Before; ...@@ -29,8 +27,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.initializer.LoggingApplicationContextInitializer;
import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.logging.java.JavaLoggingSystem;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
...@@ -48,19 +46,15 @@ public class LoggingApplicationContextInitializerTests { ...@@ -48,19 +46,15 @@ public class LoggingApplicationContextInitializerTests {
@Rule @Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder(); public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public OutputCapture outputCapture = new OutputCapture();
private LoggingApplicationContextInitializer initializer = new LoggingApplicationContextInitializer(); private LoggingApplicationContextInitializer initializer = new LoggingApplicationContextInitializer();
private Log logger = new SLF4JLogFactory().getInstance(getClass()); private Log logger = new SLF4JLogFactory().getInstance(getClass());
private PrintStream savedOutput;
private ByteArrayOutputStream output;
@Before @Before
public void init() throws SecurityException, IOException { public void init() throws SecurityException, IOException {
this.savedOutput = System.err;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
LogManager.getLogManager().readConfiguration( LogManager.getLogManager().readConfiguration(
JavaLoggingSystem.class.getResourceAsStream("logging.properties")); JavaLoggingSystem.class.getResourceAsStream("logging.properties"));
this.initializer.initialize(new SpringApplication()); this.initializer.initialize(new SpringApplication());
...@@ -71,12 +65,6 @@ public class LoggingApplicationContextInitializerTests { ...@@ -71,12 +65,6 @@ public class LoggingApplicationContextInitializerTests {
System.clearProperty("LOG_FILE"); System.clearProperty("LOG_FILE");
System.clearProperty("LOG_PATH"); System.clearProperty("LOG_PATH");
System.clearProperty("PID"); System.clearProperty("PID");
System.setOut(this.savedOutput);
System.out.println(getOutput());
}
private String getOutput() {
return this.output.toString();
} }
@Test @Test
...@@ -84,7 +72,7 @@ public class LoggingApplicationContextInitializerTests { ...@@ -84,7 +72,7 @@ public class LoggingApplicationContextInitializerTests {
GenericApplicationContext context = new GenericApplicationContext(); GenericApplicationContext context = new GenericApplicationContext();
this.initializer.initialize(context); this.initializer.initialize(context);
this.logger.info("Hello world"); this.logger.info("Hello world");
String output = getOutput().trim(); String output = this.outputCapture.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world")); assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertFalse("Wrong output:\n" + output, output.contains("???")); assertFalse("Wrong output:\n" + output, output.contains("???"));
} }
...@@ -104,7 +92,7 @@ public class LoggingApplicationContextInitializerTests { ...@@ -104,7 +92,7 @@ public class LoggingApplicationContextInitializerTests {
}); });
this.initializer.initialize(context); this.initializer.initialize(context);
this.logger.info("Hello world"); this.logger.info("Hello world");
String output = getOutput().trim(); String output = this.outputCapture.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world")); assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertFalse("Wrong output:\n" + output, output.contains("???")); assertFalse("Wrong output:\n" + output, output.contains("???"));
assertTrue("Wrong output:\n" + output, output.startsWith("/tmp/spring.log")); assertTrue("Wrong output:\n" + output, output.startsWith("/tmp/spring.log"));
...@@ -146,7 +134,7 @@ public class LoggingApplicationContextInitializerTests { ...@@ -146,7 +134,7 @@ public class LoggingApplicationContextInitializerTests {
this.initializer.initialize(context); this.initializer.initialize(context);
Log logger = LogFactory.getLog(LoggingApplicationContextInitializerTests.class); Log logger = LogFactory.getLog(LoggingApplicationContextInitializerTests.class);
logger.info("Hello world"); logger.info("Hello world");
String output = getOutput().trim(); String output = this.outputCapture.toString().trim();
assertTrue("Wrong output:\n" + output, output.startsWith("foo.log")); assertTrue("Wrong output:\n" + output, output.startsWith("foo.log"));
} }
...@@ -169,7 +157,7 @@ public class LoggingApplicationContextInitializerTests { ...@@ -169,7 +157,7 @@ public class LoggingApplicationContextInitializerTests {
this.initializer.initialize(context); this.initializer.initialize(context);
Log logger = LogFactory.getLog(LoggingApplicationContextInitializerTests.class); Log logger = LogFactory.getLog(LoggingApplicationContextInitializerTests.class);
logger.info("Hello world"); logger.info("Hello world");
String output = getOutput().trim(); String output = this.outputCapture.toString().trim();
assertTrue("Wrong output:\n" + output, output.startsWith("foo/spring.log")); assertTrue("Wrong output:\n" + output, output.startsWith("foo/spring.log"));
} }
......
...@@ -16,15 +16,12 @@ ...@@ -16,15 +16,12 @@
package org.springframework.boot.logging.logback; package org.springframework.boot.logging.logback;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.logging.logback.LogbackLoggingSystem; import org.springframework.boot.OutputCapture;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -35,30 +32,17 @@ import static org.junit.Assert.assertTrue; ...@@ -35,30 +32,17 @@ import static org.junit.Assert.assertTrue;
*/ */
public class LogbackLoggingSystemTests { public class LogbackLoggingSystemTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
private LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem(getClass() private LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem(getClass()
.getClassLoader()); .getClassLoader());
private PrintStream savedOutput;
private ByteArrayOutputStream output;
@Before
public void init() {
this.savedOutput = System.out;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
}
@After @After
public void clear() { public void clear() {
System.clearProperty("LOG_FILE"); System.clearProperty("LOG_FILE");
System.clearProperty("LOG_PATH"); System.clearProperty("LOG_PATH");
System.clearProperty("PID"); System.clearProperty("PID");
System.setOut(this.savedOutput);
}
private String getOutput() {
return this.output.toString();
} }
@Test @Test
...@@ -66,7 +50,7 @@ public class LogbackLoggingSystemTests { ...@@ -66,7 +50,7 @@ public class LogbackLoggingSystemTests {
this.loggingSystem.initialize("classpath:logback-nondefault.xml"); this.loggingSystem.initialize("classpath:logback-nondefault.xml");
Log logger = LogFactory.getLog(LogbackLoggingSystemTests.class); Log logger = LogFactory.getLog(LogbackLoggingSystemTests.class);
logger.info("Hello world"); logger.info("Hello world");
String output = getOutput().trim(); String output = this.outputCapture.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.startsWith("/tmp/spring.log")); assertTrue("Wrong output:\n" + output, output.startsWith("/tmp/spring.log"));
} }
......
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