View Javadoc
1   package org.apache.maven.plugin.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.ReportEntry;
23  import org.apache.maven.surefire.report.ReporterException;
24  
25  import java.io.File;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  import java.util.List;
30  
31  import static org.apache.maven.plugin.surefire.report.FileReporterUtils.stripIllegalFilenameChars;
32  import static org.apache.maven.surefire.util.internal.StringUtils.isNotBlank;
33  
34  /**
35   * Base class for file reporters.
36   *
37   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38   * @author Kristian Rosenvold
39   */
40  public class FileReporter
41  {
42      private final File reportsDirectory;
43  
44      private final String reportNameSuffix;
45  
46      public FileReporter( File reportsDirectory, String reportNameSuffix )
47      {
48          this.reportsDirectory = reportsDirectory;
49          this.reportNameSuffix = reportNameSuffix;
50      }
51  
52      private PrintWriter testSetStarting( ReportEntry report )
53      {
54          File reportFile = getReportFile( reportsDirectory, report.getName(), reportNameSuffix, ".txt" );
55  
56          File reportDir = reportFile.getParentFile();
57  
58          // noinspection ResultOfMethodCallIgnored
59          reportDir.mkdirs();
60  
61          try
62          {
63              PrintWriter writer = new PrintWriter( new FileWriter( reportFile ) );
64  
65              writer.println( "-------------------------------------------------------------------------------" );
66  
67              writer.println( "Test set: " + report.getName() );
68  
69              writer.println( "-------------------------------------------------------------------------------" );
70  
71              return writer;
72          }
73          catch ( IOException e )
74          {
75              throw new ReporterException( "Unable to create file for report: " + e.getMessage(), e );
76          }
77      }
78  
79      public static File getReportFile( File reportsDirectory, String reportEntryName, String reportNameSuffix,
80                                        String fileExtension )
81      {
82          String fileName =
83                  reportEntryName + ( isNotBlank( reportNameSuffix ) ? "-" + reportNameSuffix : "" ) + fileExtension;
84          return new File( reportsDirectory, stripIllegalFilenameChars( fileName ) );
85      }
86  
87      public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults )
88      {
89          PrintWriter writer = testSetStarting( report );
90          try
91          {
92              writer.println( testSetStats.getTestSetSummary( report ) );
93              for ( String testResult : testResults )
94              {
95                  writer.println( testResult );
96              }
97              writer.flush();
98          }
99          finally
100         {
101             writer.close();
102         }
103     }
104 }