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