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