From 1da16986427f387390299cfa1d75632420c08051 Mon Sep 17 00:00:00 2001 From: Andrea Falconi Date: Mon, 10 Apr 2017 12:52:51 +0200 Subject: [PATCH] Fix config file generation in integration tests. Using SpringBoot OutputCapture to capture stdout and write it to file doesn't always work---sometimes I get Spring DEBUG messages in there too! So I'm doing this myself to be on the safe side. --- .../integration/config/ConfigFileTest.java | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/components/server/src/test/java/integration/config/ConfigFileTest.java b/components/server/src/test/java/integration/config/ConfigFileTest.java index 6a188a0..76ceafc 100644 --- a/components/server/src/test/java/integration/config/ConfigFileTest.java +++ b/components/server/src/test/java/integration/config/ConfigFileTest.java @@ -4,14 +4,11 @@ import static util.spring.io.ResourceLocation.classpath; import static util.spring.io.ResourceLocation.filepathFromCwd; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; +import java.io.*; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.springframework.boot.test.OutputCapture; import util.config.ConfigProvider; import util.spring.io.ResourceLocation; @@ -20,10 +17,22 @@ public abstract class ConfigFileTest { - - @Rule - public final OutputCapture generatedConfig = new OutputCapture(); - + + private static void writeStdoutToFile(File f, Runnable outWriter) + throws IOException { + FileOutputStream fileContents = new FileOutputStream(f); + PrintStream ps = new PrintStream(fileContents); + PrintStream currentStdout = System.out; + try { + System.setOut(ps); + outWriter.run(); + System.out.flush(); + } finally { + System.setOut(currentStdout); + } + } + + @Rule public final TemporaryFolder configDirUnderPwd = new TemporaryFolder(new File("./")); @@ -32,23 +41,13 @@ public abstract class ConfigFileTest { protected abstract RunnableApp getFileGenerator(); protected abstract ConfigProvider getFileContents(); - - protected String generateFile() { - getFileGenerator().run(null); - - String fileContents = generatedConfig.toString(); - return fileContents; - } - - protected ResourceLocation writeFile() throws IOException { - String fileContents = generateFile(); - + + private ResourceLocation writeFile() throws IOException { String fileName = getConfigProvider().getConfigFileName(); File configFile = configDirUnderPwd.newFile(fileName); - PrintWriter out = new PrintWriter(configFile); - out.print(fileContents); - out.close(); - + + writeStdoutToFile(configFile, () -> getFileGenerator().run(null)); + String configDirName = configDirUnderPwd.getRoot().getName(); return filepathFromCwd(configDirName, fileName); }