View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.report;
20  
21  import java.io.BufferedWriter;
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.OutputStreamWriter;
27  import java.nio.charset.Charset;
28  import java.util.List;
29  
30  import org.apache.maven.surefire.api.report.ReporterException;
31  import org.apache.maven.surefire.extensions.StatelessTestsetInfoFileReportEventListener;
32  
33  import static org.apache.maven.plugin.surefire.report.FileReporterUtils.stripIllegalFilenameChars;
34  import static org.apache.maven.surefire.shared.utils.StringUtils.isNotBlank;
35  
36  /**
37   * Base class for file reporters.
38   *
39   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40   * @author Kristian Rosenvold
41   */
42  public class FileReporter extends StatelessTestsetInfoFileReportEventListener<WrappedReportEntry, TestSetStats> {
43      private final boolean usePhrasedFileName;
44      private final boolean usePhrasedClassNameInRunning;
45      private final boolean usePhrasedClassNameInTestCaseSummary;
46  
47      public FileReporter(
48              File reportsDirectory,
49              String reportNameSuffix,
50              Charset encoding,
51              boolean usePhrasedFileName,
52              boolean usePhrasedClassNameInRunning,
53              boolean usePhrasedClassNameInTestCaseSummary) {
54          super(reportsDirectory, reportNameSuffix, encoding);
55          this.usePhrasedFileName = usePhrasedFileName;
56          this.usePhrasedClassNameInRunning = usePhrasedClassNameInRunning;
57          this.usePhrasedClassNameInTestCaseSummary = usePhrasedClassNameInTestCaseSummary;
58      }
59  
60      static File getReportFile(
61              File reportsDirectory, String reportEntryName, String reportNameSuffix, String fileExtension) {
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          File reportFile = getReportFile(
70                  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              writer.write("-------------------------------------------------------------------------------");
82              writer.newLine();
83  
84              String tesSet = usePhrasedClassNameInRunning ? report.getReportSourceName() : report.getSourceName();
85              writer.write("Test set: " + tesSet);
86              writer.newLine();
87  
88              writer.write("-------------------------------------------------------------------------------");
89              writer.newLine();
90  
91              writer.write(testSetStats.getTestSetSummary(report, usePhrasedClassNameInTestCaseSummary));
92              writer.newLine();
93              for (String testResult : testResults) {
94                  writer.write(testResult);
95                  writer.newLine();
96              }
97          } catch (IOException e) {
98              throw new ReporterException("Unable to create file for report: " + e.getLocalizedMessage(), e);
99          }
100     }
101 
102     private static BufferedWriter createFileReporterWriter(File reportFile, Charset encoding)
103             throws FileNotFoundException {
104         return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(reportFile), encoding), 64 * 1024);
105     }
106 }