1 | |
package org.apache.maven.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.File; |
23 | |
import java.io.FileWriter; |
24 | |
import java.io.IOException; |
25 | |
import java.io.PrintWriter; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public abstract class AbstractFileReporter |
33 | |
extends AbstractTextReporter |
34 | |
{ |
35 | |
private final File reportsDirectory; |
36 | |
|
37 | |
private final boolean deleteOnStarting; |
38 | |
|
39 | |
private final String reportNameSuffix; |
40 | |
|
41 | |
AbstractFileReporter( boolean trimStackTrace, String format, File reportsDirectory ) |
42 | |
{ |
43 | 0 | this( trimStackTrace, format, reportsDirectory, null ); |
44 | 0 | } |
45 | |
|
46 | |
AbstractFileReporter( boolean trimStackTrace, String format, File reportsDirectory, String reportNameSuffix ) |
47 | |
{ |
48 | 0 | super( trimStackTrace, format ); |
49 | 0 | this.reportsDirectory = reportsDirectory; |
50 | 0 | this.deleteOnStarting = false; |
51 | 0 | this.reportNameSuffix = reportNameSuffix; |
52 | 0 | } |
53 | |
|
54 | |
public void testSetStarting( ReportEntry report ) |
55 | |
throws ReporterException |
56 | |
{ |
57 | 0 | super.testSetStarting( report ); |
58 | |
|
59 | 0 | File reportFile = getReportFile( reportsDirectory, report.getName(), reportNameSuffix, ".txt" ); |
60 | |
|
61 | 0 | File reportDir = reportFile.getParentFile(); |
62 | |
|
63 | |
|
64 | 0 | reportDir.mkdirs(); |
65 | |
|
66 | 0 | if ( deleteOnStarting && reportFile.exists() ) |
67 | |
{ |
68 | |
|
69 | 0 | reportFile.delete(); |
70 | |
} |
71 | |
|
72 | |
try |
73 | |
{ |
74 | 0 | PrintWriter writer = new PrintWriter( new FileWriter( reportFile ) ); |
75 | |
|
76 | 0 | writer.println( "-------------------------------------------------------------------------------" ); |
77 | |
|
78 | 0 | writer.println( "Test set: " + report.getName() ); |
79 | |
|
80 | 0 | writer.println( "-------------------------------------------------------------------------------" ); |
81 | |
|
82 | 0 | setWriter( writer ); |
83 | |
} |
84 | 0 | catch ( IOException e ) |
85 | |
{ |
86 | 0 | throw new ReporterException( "Unable to create file for report: " + e.getMessage(), e ); |
87 | 0 | } |
88 | 0 | } |
89 | |
|
90 | |
public static File getReportFile( File reportsDirectory, String reportEntryName, |
91 | |
String reportNameSuffix, String fileExtension ) |
92 | |
{ |
93 | |
File reportFile; |
94 | |
|
95 | 0 | if ( reportNameSuffix != null && reportNameSuffix.length() > 0) |
96 | |
{ |
97 | 0 | reportFile = new File( reportsDirectory, reportEntryName + "-" + reportNameSuffix + fileExtension ); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | reportFile = new File( reportsDirectory, reportEntryName + fileExtension ); |
102 | |
} |
103 | 0 | return reportFile; |
104 | |
} |
105 | |
|
106 | |
public void testSetCompleted( ReportEntry report ) |
107 | |
throws ReporterException |
108 | |
{ |
109 | 0 | super.testSetCompleted( report ); |
110 | |
|
111 | 0 | writer.flush(); |
112 | |
|
113 | 0 | writer.close(); |
114 | |
|
115 | 0 | writer = null; |
116 | 0 | } |
117 | |
} |