Skip to content

Commit

Permalink
Fix config file generation in integration tests.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
c0c0n3 committed Apr 10, 2017
1 parent 0da0dd2 commit 1da1698
Showing 1 changed file with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,10 +17,22 @@


public abstract class ConfigFileTest<T> {

@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("./"));

Expand All @@ -32,23 +41,13 @@ public abstract class ConfigFileTest<T> {
protected abstract RunnableApp getFileGenerator();

protected abstract ConfigProvider<T> 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);
}
Expand Down

0 comments on commit 1da1698

Please sign in to comment.