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