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