Skip to content

Commit 1da1698

Browse files
committed
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.
1 parent 0da0dd2 commit 1da1698

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

components/server/src/test/java/integration/config/ConfigFileTest.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
import static util.spring.io.ResourceLocation.classpath;
55
import static util.spring.io.ResourceLocation.filepathFromCwd;
66

7-
import java.io.File;
8-
import java.io.IOException;
9-
import java.io.PrintWriter;
7+
import java.io.*;
108

119
import org.junit.Rule;
1210
import org.junit.Test;
1311
import org.junit.rules.TemporaryFolder;
14-
import org.springframework.boot.test.OutputCapture;
1512

1613
import util.config.ConfigProvider;
1714
import util.spring.io.ResourceLocation;
@@ -20,10 +17,22 @@
2017

2118

2219
public abstract class ConfigFileTest<T> {
23-
24-
@Rule
25-
public final OutputCapture generatedConfig = new OutputCapture();
26-
20+
21+
private static void writeStdoutToFile(File f, Runnable outWriter)
22+
throws IOException {
23+
FileOutputStream fileContents = new FileOutputStream(f);
24+
PrintStream ps = new PrintStream(fileContents);
25+
PrintStream currentStdout = System.out;
26+
try {
27+
System.setOut(ps);
28+
outWriter.run();
29+
System.out.flush();
30+
} finally {
31+
System.setOut(currentStdout);
32+
}
33+
}
34+
35+
2736
@Rule
2837
public final TemporaryFolder configDirUnderPwd = new TemporaryFolder(new File("./"));
2938

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

3443
protected abstract ConfigProvider<T> getFileContents();
35-
36-
protected String generateFile() {
37-
getFileGenerator().run(null);
38-
39-
String fileContents = generatedConfig.toString();
40-
return fileContents;
41-
}
42-
43-
protected ResourceLocation writeFile() throws IOException {
44-
String fileContents = generateFile();
45-
44+
45+
private ResourceLocation writeFile() throws IOException {
4646
String fileName = getConfigProvider().getConfigFileName();
4747
File configFile = configDirUnderPwd.newFile(fileName);
48-
PrintWriter out = new PrintWriter(configFile);
49-
out.print(fileContents);
50-
out.close();
51-
48+
49+
writeStdoutToFile(configFile, () -> getFileGenerator().run(null));
50+
5251
String configDirName = configDirUnderPwd.getRoot().getName();
5352
return filepathFromCwd(configDirName, fileName);
5453
}

0 commit comments

Comments
 (0)