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 | 0 | private static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); |
46 | |
|
47 | |
private final File reportsDirectory; |
48 | |
|
49 | 0 | 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 | 0 | super( outputConsumer ); |
62 | 0 | this.reportsDirectory = reportsDirectory; |
63 | 0 | } |
64 | |
|
65 | |
public void testSetStarting( ReportEntry reportEntry ) |
66 | |
{ |
67 | 0 | if ( printWriter != null ) |
68 | |
{ |
69 | 0 | throw new IllegalStateException( "testSetStarting called twice" ); |
70 | |
} |
71 | |
|
72 | 0 | if ( !reportsDirectory.exists() ) |
73 | |
{ |
74 | |
|
75 | 0 | reportsDirectory.mkdirs(); |
76 | |
} |
77 | |
|
78 | 0 | File file = new File( reportsDirectory, reportEntry.getName() + "-output.txt" ); |
79 | |
try |
80 | |
{ |
81 | 0 | this.printWriter = new PrintWriter( new BufferedWriter( new FileWriter( file ) ) ); |
82 | |
} |
83 | 0 | catch ( IOException e ) |
84 | |
{ |
85 | 0 | throw new NestedRuntimeException( e ); |
86 | 0 | } |
87 | 0 | super.testSetStarting( reportEntry ); |
88 | 0 | } |
89 | |
|
90 | |
public void testSetCompleted() |
91 | |
{ |
92 | 0 | if ( printWriter == null ) |
93 | |
{ |
94 | 0 | throw new IllegalStateException( "testSetCompleted called before testSetStarting" ); |
95 | |
} |
96 | 0 | if ( outputBuffer.length() > 0 ) |
97 | |
{ |
98 | 0 | printWriter.write( outputBuffer.toString() ); |
99 | 0 | printWriter.write( LINE_SEPARATOR ); |
100 | 0 | outputBuffer.setLength( 0 ); |
101 | |
} |
102 | 0 | printWriter.close(); |
103 | 0 | this.printWriter = null; |
104 | 0 | super.testSetCompleted(); |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void consumeOutputLine( String line ) |
112 | |
{ |
113 | 0 | if ( printWriter == null ) |
114 | |
{ |
115 | 0 | outputBuffer.append( line ); |
116 | 0 | outputBuffer.append( LINE_SEPARATOR ); |
117 | 0 | return; |
118 | |
} |
119 | |
|
120 | 0 | if ( outputBuffer.length() > 0 ) |
121 | |
{ |
122 | 0 | printWriter.write( outputBuffer.toString() ); |
123 | 0 | printWriter.write( LINE_SEPARATOR ); |
124 | 0 | outputBuffer.setLength( 0 ); |
125 | |
} |
126 | 0 | printWriter.write( line ); |
127 | 0 | printWriter.write( LINE_SEPARATOR ); |
128 | 0 | } |
129 | |
|
130 | |
} |