44import static util .spring .io .ResourceLocation .classpath ;
55import 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
119import org .junit .Rule ;
1210import org .junit .Test ;
1311import org .junit .rules .TemporaryFolder ;
14- import org .springframework .boot .test .OutputCapture ;
1512
1613import util .config .ConfigProvider ;
1714import util .spring .io .ResourceLocation ;
2017
2118
2219public 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