1 package org.apache.maven.plugin.surefire.booterclient.output;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.report.ReportEntry;
23 import org.apache.maven.surefire.util.NestedRuntimeException;
24
25 import java.io.BufferedWriter;
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30
31
32
33
34
35
36
37
38
39
40
41 public class FileOutputConsumerProxy
42 extends OutputConsumerProxy
43 {
44
45 private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
46
47 private final File reportsDirectory;
48
49 private final StringBuffer outputBuffer = new StringBuffer();
50
51 private volatile PrintWriter printWriter;
52
53
54
55
56
57
58
59 public FileOutputConsumerProxy( OutputConsumer outputConsumer, File reportsDirectory )
60 {
61 super( outputConsumer );
62 this.reportsDirectory = reportsDirectory;
63 }
64
65 public void testSetStarting( ReportEntry reportEntry )
66 {
67 if ( printWriter != null )
68 {
69 throw new IllegalStateException( "testSetStarting called twice" );
70 }
71
72 if ( !reportsDirectory.exists() )
73 {
74
75 reportsDirectory.mkdirs();
76 }
77
78 File file = new File( reportsDirectory, reportEntry.getName() + "-output.txt" );
79 try
80 {
81 this.printWriter = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) );
82 }
83 catch ( IOException e )
84 {
85 throw new NestedRuntimeException( e );
86 }
87 super.testSetStarting( reportEntry );
88 }
89
90 public void testSetCompleted()
91 {
92 if ( printWriter == null )
93 {
94 throw new IllegalStateException( "testSetCompleted called before testSetStarting" );
95 }
96 if ( outputBuffer.length() > 0 )
97 {
98 printWriter.write( outputBuffer.toString() );
99 printWriter.write( LINE_SEPARATOR );
100 outputBuffer.setLength( 0 );
101 }
102 printWriter.close();
103 this.printWriter = null;
104 super.testSetCompleted();
105 }
106
107
108
109
110
111 public void consumeOutputLine( String line )
112 {
113 if ( printWriter == null )
114 {
115 outputBuffer.append( line );
116 outputBuffer.append( LINE_SEPARATOR );
117 return;
118 }
119
120 if ( outputBuffer.length() > 0 )
121 {
122 printWriter.write( outputBuffer.toString() );
123 printWriter.write( LINE_SEPARATOR );
124 outputBuffer.setLength( 0 );
125 }
126 printWriter.write( line );
127 printWriter.write( LINE_SEPARATOR );
128 }
129
130 }