1 package org.apache.maven.surefire.booter.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 public class FileOutputConsumerProxy
39 extends OutputConsumerProxy
40 {
41
42 private static final String USER_DIR = System.getProperty( "user.dir" );
43
44 private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
45
46 private File reportsDirectory;
47
48 private PrintWriter printWriter;
49
50 private StringBuffer outputBuffer = new StringBuffer();
51
52
53
54
55
56 public FileOutputConsumerProxy( OutputConsumer outputConsumer )
57 {
58 this( outputConsumer, new File( USER_DIR ) );
59 }
60
61
62
63
64
65
66 public FileOutputConsumerProxy( OutputConsumer outputConsumer, File reportsDirectory )
67 {
68 super( outputConsumer );
69 this.setReportsDirectory( reportsDirectory );
70 }
71
72
73
74
75
76
77 public void setReportsDirectory( File reportsDirectory )
78 {
79 this.reportsDirectory = reportsDirectory;
80 }
81
82
83
84
85 public File getReportsDirectory()
86 {
87 return reportsDirectory;
88 }
89
90
91
92
93
94
95 public void setPrintWriter( PrintWriter writer )
96 {
97 this.printWriter = writer;
98 }
99
100
101
102
103 public PrintWriter getPrintWriter()
104 {
105 return printWriter;
106 }
107
108 public void testSetStarting( ReportEntry reportEntry )
109 {
110 if ( getPrintWriter() != null )
111 {
112 throw new IllegalStateException( "testSetStarting called twice" );
113 }
114 File file = new File( getReportsDirectory(), reportEntry.getName() + "-output.txt" );
115 try
116 {
117 setPrintWriter( new PrintWriter( new BufferedWriter( new FileWriter( file ) ) ) );
118 }
119 catch ( IOException e )
120 {
121 throw new NestedRuntimeException( e );
122 }
123 super.testSetStarting( reportEntry );
124 }
125
126 public void testSetCompleted()
127 {
128 if ( getPrintWriter() == null )
129 {
130 throw new IllegalStateException( "testSetCompleted called before testSetStarting" );
131 }
132 if ( outputBuffer.length() > 0 )
133 {
134 getPrintWriter().write( outputBuffer.toString() );
135 getPrintWriter().write( LINE_SEPARATOR );
136 outputBuffer.setLength( 0 );
137 }
138 getPrintWriter().close();
139 setPrintWriter( null );
140 super.testSetCompleted();
141 }
142
143
144
145
146 public void consumeOutputLine( String line )
147 {
148 if ( getPrintWriter() == null )
149 {
150 outputBuffer.append( line );
151 outputBuffer.append( LINE_SEPARATOR );
152 return;
153 }
154
155 if ( outputBuffer.length() > 0 )
156 {
157 getPrintWriter().write( outputBuffer.toString() );
158 getPrintWriter().write( LINE_SEPARATOR );
159 outputBuffer.setLength( 0 );
160 }
161 getPrintWriter().write( line );
162 getPrintWriter().write( LINE_SEPARATOR );
163 }
164
165 }