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