1 package org.apache.maven.plugin.surefire.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedOutputStream;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27
28 import org.apache.maven.surefire.report.ReportEntry;
29
30 import static org.apache.maven.plugin.surefire.report.FileReporter.getReportFile;
31
32
33
34
35
36
37
38
39
40 public class ConsoleOutputFileReporter
41 implements TestcycleConsoleOutputReceiver
42 {
43 private final File reportsDirectory;
44
45 private final String reportNameSuffix;
46
47 private String reportEntryName;
48
49 private OutputStream fileOutputStream;
50
51 public ConsoleOutputFileReporter( File reportsDirectory, String reportNameSuffix )
52 {
53 this.reportsDirectory = reportsDirectory;
54 this.reportNameSuffix = reportNameSuffix;
55 }
56
57 @Override
58 public void testSetStarting( ReportEntry reportEntry )
59 {
60 close();
61 reportEntryName = reportEntry.getName();
62 }
63
64 @Override
65 public void testSetCompleted( ReportEntry report )
66 {
67 }
68
69 @Override
70 @SuppressWarnings( "checkstyle:emptyblock" )
71 public void close()
72 {
73 if ( fileOutputStream != null )
74 {
75
76 try
77 {
78 fileOutputStream.flush();
79 }
80 catch ( IOException e )
81 {
82 }
83 finally
84 {
85 try
86 {
87 fileOutputStream.close();
88 }
89 catch ( IOException ignored )
90 {
91 }
92 }
93 fileOutputStream = null;
94 }
95 }
96
97 @Override
98 public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
99 {
100 try
101 {
102 if ( fileOutputStream == null )
103 {
104 if ( !reportsDirectory.exists() )
105 {
106
107 reportsDirectory.mkdirs();
108 }
109 File file = getReportFile( reportsDirectory, reportEntryName, reportNameSuffix, "-output.txt" );
110 fileOutputStream = new BufferedOutputStream( new FileOutputStream( file ), 16 * 1024 );
111 }
112 fileOutputStream.write( buf, off, len );
113 }
114 catch ( IOException e )
115 {
116 throw new RuntimeException( e );
117 }
118 }
119 }