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.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   * Base class for file reporters.
37   *
38   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39   * @author Kristian Rosenvold
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          // noinspection ResultOfMethodCallIgnored
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 }