[SUREFIRE-1619] FileReporter should not use PintWriter because i/o errors are not...
authorTibor17 <tibordigana@apache.org>
Sun, 30 Dec 2018 18:10:24 +0000 (19:10 +0100)
committerTibor17 <tibordigana@apache.org>
Sun, 30 Dec 2018 18:10:24 +0000 (19:10 +0100)
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporter.java

index 290c252..5d25e6a 100644 (file)
@@ -19,16 +19,14 @@ package org.apache.maven.plugin.surefire.report;
  * under the License.
  */
 
-import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.ReporterException;
 
 import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.io.Writer;
 import java.nio.charset.Charset;
 import java.util.List;
 
@@ -54,7 +52,15 @@ public class FileReporter
         this.encoding = encoding;
     }
 
-    private PrintWriter testSetStarting( ReportEntry report )
+    static File getReportFile( File reportsDirectory, String reportEntryName, String reportNameSuffix,
+                                      String fileExtension )
+    {
+        String fileName =
+                reportEntryName + ( isNotBlank( reportNameSuffix ) ? "-" + reportNameSuffix : "" ) + fileExtension;
+        return new File( reportsDirectory, stripIllegalFilenameChars( fileName ) );
+    }
+
+    public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults )
     {
         File reportFile = getReportFile( reportsDirectory, report.getName(), reportNameSuffix, ".txt" );
 
@@ -63,43 +69,34 @@ public class FileReporter
         // noinspection ResultOfMethodCallIgnored
         reportDir.mkdirs();
 
-        try
+        try ( BufferedWriter writer = createFileReporterWriter( reportFile, encoding ) )
         {
-            Writer encodedStream = new OutputStreamWriter( new FileOutputStream( reportFile ), encoding );
-
-            PrintWriter writer = new PrintWriter( new BufferedWriter( encodedStream, 16 * 1024 ) );
-
-            writer.println( "-------------------------------------------------------------------------------" );
+            writer.write( "-------------------------------------------------------------------------------" );
+            writer.newLine();
 
-            writer.println( "Test set: " + report.getName() );
+            writer.write( "Test set: " + report.getName() );
+            writer.newLine();
 
-            writer.println( "-------------------------------------------------------------------------------" );
+            writer.write( "-------------------------------------------------------------------------------" );
+            writer.newLine();
 
-            return writer;
+            writer.write( testSetStats.getTestSetSummary( report ) );
+            writer.newLine();
+            for ( String testResult : testResults )
+            {
+                writer.write( testResult );
+                writer.newLine();
+            }
         }
         catch ( IOException e )
         {
-            throw new ReporterException( "Unable to create file for report: " + e.getMessage(), e );
+            throw new ReporterException( "Unable to create file for report: " + e.getLocalizedMessage(), e );
         }
     }
 
-    static File getReportFile( File reportsDirectory, String reportEntryName, String reportNameSuffix,
-                                      String fileExtension )
-    {
-        String fileName =
-                reportEntryName + ( isNotBlank( reportNameSuffix ) ? "-" + reportNameSuffix : "" ) + fileExtension;
-        return new File( reportsDirectory, stripIllegalFilenameChars( fileName ) );
-    }
-
-    public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults )
+    private static BufferedWriter createFileReporterWriter( File reportFile, Charset encoding )
+            throws FileNotFoundException
     {
-        try ( PrintWriter writer = testSetStarting( report ) )
-        {
-            writer.println( testSetStats.getTestSetSummary( report ) );
-            for ( String testResult : testResults )
-            {
-                writer.println( testResult );
-            }
-        }
+        return new BufferedWriter( new OutputStreamWriter( new FileOutputStream( reportFile ), encoding ), 64 * 1024 );
     }
 }