Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 46920200C12 for ; Sun, 5 Feb 2017 14:55:36 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 4553C160B6E; Sun, 5 Feb 2017 13:55:36 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5A86D160B32 for ; Sun, 5 Feb 2017 14:55:34 +0100 (CET) Received: (qmail 2987 invoked by uid 500); 5 Feb 2017 13:55:33 -0000 Mailing-List: contact commits-help@maven.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@maven.apache.org Delivered-To: mailing list commits@maven.apache.org Received: (qmail 2976 invoked by uid 99); 5 Feb 2017 13:55:33 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 05 Feb 2017 13:55:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 014ACE0210; Sun, 5 Feb 2017 13:55:32 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: tibordigana@apache.org To: commits@maven.apache.org Date: Sun, 05 Feb 2017 13:55:32 -0000 Message-Id: <953bb94e51704174aa9bfc8e101597bd@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [01/17] maven-surefire git commit: [SUREFIRE-1324] Surefire incorrectly suppresses exceptions when closing resources. archived-at: Sun, 05 Feb 2017 13:55:36 -0000 Repository: maven-surefire Updated Branches: refs/heads/SUREFIRE-1322 4d3673c42 -> b31db4fb9 [SUREFIRE-1324] Surefire incorrectly suppresses exceptions when closing resources. Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/e5a6b9c8 Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/e5a6b9c8 Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/e5a6b9c8 Branch: refs/heads/SUREFIRE-1322 Commit: e5a6b9c8d4f514100a01dea2acf1fb059e294968 Parents: 66bc4c0 Author: Christian Schulte Authored: Mon Jan 2 00:54:07 2017 +0100 Committer: Christian Schulte Committed: Mon Jan 2 02:21:25 2017 +0100 ---------------------------------------------------------------------- .../plugin/surefire/SurefireProperties.java | 41 ++++++------ .../booterclient/ForkConfiguration.java | 27 ++++---- .../report/ConsoleOutputFileReporter.java | 15 ++++- .../plugin/surefire/report/FileReporter.java | 65 +++++++++++++++----- .../surefire/report/TestSetRunListener.java | 37 ++++++----- .../surefire/runorder/StatisticsReporter.java | 4 +- .../surefire/util/DependenciesScannerTest.java | 8 ++- .../maven/surefire/report/FileReporterTest.java | 5 +- .../surefire/report/SurefireReportMojoTest.java | 2 + surefire-api/pom.xml | 4 ++ .../runorder/RunEntryStatisticsMap.java | 30 ++++++--- .../surefire/booter/ForkingRunListener.java | 8 +++ .../surefire/booter/MasterProcessCommand.java | 5 +- .../surefire/booter/SystemPropertyManager.java | 21 ++++++- .../maven/surefire/its/fixture/TestFile.java | 18 +++++- .../src/test/java/it/BasicTest.java | 31 +++++++--- .../src/test/java/runListener/FileHelper.java | 26 ++++++-- .../apache/maven/surefire/test/FailingTest.java | 4 +- .../maven/surefire/test/SucceedingTest.java | 3 + .../plugins/surefire/dumppid/DumpPidMojo.java | 30 ++++++--- .../src/test/java/listenReport/FileHelper.java | 30 ++++++--- .../java/testng/objectfactory/FileHelper.java | 28 +++++++-- .../testng/testrunnerfactory/FileHelper.java | 28 +++++++-- 23 files changed, 341 insertions(+), 129 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java index 3663f39..2e3aa58 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java @@ -32,13 +32,11 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; - +import static java.util.Arrays.asList; import org.apache.maven.surefire.booter.Classpath; import org.apache.maven.surefire.booter.KeyValueSource; import org.apache.maven.surefire.util.internal.StringUtils; -import static java.util.Arrays.asList; - /** * A properties implementation that preserves insertion order. */ @@ -46,6 +44,7 @@ public class SurefireProperties extends Properties implements KeyValueSource { + private static final Collection KEYS_THAT_CANNOT_BE_USED_AS_SYSTEM_PROPERTIES = asList( "java.library.path", "file.encoding", "jdk.map.althashing.threshold", "line.separator" ); @@ -153,7 +152,6 @@ public class SurefireProperties // user specified properties for SUREFIRE-121, causing SUREFIRE-491. // Not gonna do THAT any more... instead, we only propagate those system properties // that have been explicitly specified by the user via -Dkey=value on the CLI - result.copyPropertiesFrom( userProperties ); return result; } @@ -224,18 +222,32 @@ public class SurefireProperties } } - private static SurefireProperties loadProperties( InputStream inStream ) + private static SurefireProperties loadProperties( final InputStream inStream ) throws IOException { + InputStream in = inStream; + try { - Properties p = new Properties(); - p.load( inStream ); + final Properties p = new Properties(); + p.load( in ); + in.close(); + in = null; return new SurefireProperties( p ); } finally { - close( inStream ); + try + { + if ( in != null ) + { + in.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } } } @@ -245,18 +257,6 @@ public class SurefireProperties return file == null ? new SurefireProperties() : loadProperties( new FileInputStream( file ) ); } - private static void close( InputStream inputStream ) - { - try - { - inputStream.close(); - } - catch ( IOException ex ) - { - // ignore - } - } - public void setNullableProperty( String key, String value ) { if ( value != null ) @@ -264,4 +264,5 @@ public class SurefireProperties super.setProperty( key, value ); } } + } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java index 988af8f..f42c8aa 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkConfiguration.java @@ -19,15 +19,6 @@ package org.apache.maven.plugin.surefire.booterclient; * under the License. */ -import org.apache.maven.plugin.surefire.AbstractSurefireMojo; -import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.OutputStreamFlushableCommandline; -import org.apache.maven.plugin.surefire.util.Relocator; -import org.apache.maven.shared.utils.StringUtils; -import org.apache.maven.surefire.booter.Classpath; -import org.apache.maven.surefire.booter.ForkedBooter; -import org.apache.maven.surefire.booter.StartupConfiguration; -import org.apache.maven.surefire.booter.SurefireBooterForkException; - import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -38,6 +29,15 @@ import java.util.Properties; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; +import org.apache.maven.plugin.surefire.AbstractSurefireMojo; +import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.OutputStreamFlushableCommandline; +import org.apache.maven.plugin.surefire.util.Relocator; +import org.apache.maven.shared.utils.StringUtils; +import org.apache.maven.shared.utils.io.IOUtil; +import org.apache.maven.surefire.booter.Classpath; +import org.apache.maven.surefire.booter.ForkedBooter; +import org.apache.maven.surefire.booter.StartupConfiguration; +import org.apache.maven.surefire.booter.SurefireBooterForkException; /** * Configuration for forking tests. @@ -270,10 +270,10 @@ public class ForkConfiguration { file.deleteOnExit(); } - FileOutputStream fos = new FileOutputStream( file ); - JarOutputStream jos = new JarOutputStream( fos ); + JarOutputStream jos = null; try { + jos = new JarOutputStream( new FileOutputStream( file ) ); jos.setLevel( JarOutputStream.STORED ); JarEntry je = new JarEntry( "META-INF/MANIFEST.MF" ); jos.putNextEntry( je ); @@ -297,10 +297,13 @@ public class ForkConfiguration man.getMainAttributes().putValue( "Main-Class", startClassName ); man.write( jos ); + + jos.close(); + jos = null; } finally { - jos.close(); + IOUtil.close( jos ); } return file; http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleOutputFileReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleOutputFileReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleOutputFileReporter.java index f9e59fe..552c12a 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleOutputFileReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ConsoleOutputFileReporter.java @@ -69,13 +69,15 @@ public class ConsoleOutputFileReporter { try { - fileOutputStream.flush(); fileOutputStream.close(); } catch ( IOException e ) { } - fileOutputStream = null; + finally + { + fileOutputStream = null; + } } } @@ -97,6 +99,15 @@ public class ConsoleOutputFileReporter } catch ( IOException e ) { + try + { + fileOutputStream.close(); + // Intentionally no setting to null. + } + catch ( final IOException e1 ) + { + // Suppressed. + } throw new RuntimeException( e ); } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporter.java index a4d8c8e..d7a41af 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporter.java @@ -19,15 +19,13 @@ 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.FileWriter; import java.io.IOException; -import java.io.PrintWriter; import java.util.List; - +import org.apache.maven.surefire.report.ReportEntry; +import org.apache.maven.surefire.report.ReporterException; import static org.apache.maven.plugin.surefire.report.FileReporterUtils.stripIllegalFilenameChars; import static org.apache.maven.surefire.util.internal.StringUtils.isNotBlank; @@ -49,7 +47,7 @@ public class FileReporter this.reportNameSuffix = reportNameSuffix; } - private PrintWriter testSetStarting( ReportEntry report ) + private BufferedWriter testSetStarting( ReportEntry report ) { File reportFile = getReportFile( reportsDirectory, report.getName(), reportNameSuffix, ".txt" ); @@ -58,21 +56,39 @@ public class FileReporter // noinspection ResultOfMethodCallIgnored reportDir.mkdirs(); + BufferedWriter writer = null; try { - PrintWriter writer = new PrintWriter( new FileWriter( reportFile ) ); + writer = new BufferedWriter( new FileWriter( reportFile ) ); - writer.println( "-------------------------------------------------------------------------------" ); + writer.append( "-------------------------------------------------------------------------------" ); + writer.newLine(); - writer.println( "Test set: " + report.getName() ); + writer.append( "Test set: " + report.getName() ); + writer.newLine(); - writer.println( "-------------------------------------------------------------------------------" ); + writer.append( "-------------------------------------------------------------------------------" ); + writer.newLine(); return writer; } catch ( IOException e ) { - throw new ReporterException( "Unable to create file for report: " + e.getMessage(), e ); + try + { + if ( writer != null ) + { + writer.close(); + } + } + catch ( final IOException e1 ) + { + // Suppressed. + } + finally + { + throw new ReporterException( "Unable to create file for report: " + e.getMessage(), e ); + } } } @@ -85,20 +101,37 @@ public class FileReporter } public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List testResults ) + throws IOException { - PrintWriter writer = testSetStarting( report ); + BufferedWriter writer = null; try { - writer.println( testSetStats.getTestSetSummary( report ) ); + writer = testSetStarting( report ); + writer.append( testSetStats.getTestSetSummary( report ) ); + writer.newLine(); + for ( String testResult : testResults ) { - writer.println( testResult ); + writer.append( testResult ); + writer.newLine(); } - writer.flush(); + + writer.close(); + writer = null; } finally { - writer.close(); + try + { + if ( writer != null ) + { + writer.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java index f0f996d..0643896 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java @@ -151,22 +151,29 @@ public class TestSetRunListener public void testSetCompleted( ReportEntry report ) { - final WrappedReportEntry wrap = wrapTestSet( report ); - final List testResults = + try + { + final WrappedReportEntry wrap = wrapTestSet( report ); + final List testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : Collections.emptyList(); - fileReporter.testSetCompleted( wrap, detailsForThis, testResults ); - simpleXMLReporter.testSetCompleted( wrap, detailsForThis ); - statisticsReporter.testSetCompleted(); - consoleReporter.testSetCompleted( wrap, detailsForThis, testResults ); - consoleOutputReceiver.testSetCompleted( wrap ); - consoleReporter.reset(); - - wrap.getStdout().free(); - wrap.getStdErr().free(); - - addTestMethodStats(); - detailsForThis.reset(); - clearCapture(); + fileReporter.testSetCompleted( wrap, detailsForThis, testResults ); + simpleXMLReporter.testSetCompleted( wrap, detailsForThis ); + statisticsReporter.testSetCompleted(); + consoleReporter.testSetCompleted( wrap, detailsForThis, testResults ); + consoleOutputReceiver.testSetCompleted( wrap ); + consoleReporter.reset(); + + wrap.getStdout().free(); + wrap.getStdErr().free(); + + addTestMethodStats(); + detailsForThis.reset(); + clearCapture(); + } + catch ( final IOException e ) + { + throw new RuntimeException( "Unexpected IOException.e", e ); + } } // ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java index a53db02..bf4523e 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/runorder/StatisticsReporter.java @@ -20,7 +20,7 @@ package org.apache.maven.plugin.surefire.runorder; */ import java.io.File; -import java.io.FileNotFoundException; +import java.io.IOException; import org.apache.maven.surefire.report.ReportEntry; import static org.apache.maven.plugin.surefire.runorder.RunEntryStatisticsMap.fromFile; @@ -54,7 +54,7 @@ public class StatisticsReporter { newResults.serialize( dataFile ); } - catch ( FileNotFoundException e ) + catch ( IOException e ) { throw new RuntimeException( e ); } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/util/DependenciesScannerTest.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/util/DependenciesScannerTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/util/DependenciesScannerTest.java index c8dd436..a223a0d 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/util/DependenciesScannerTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/util/DependenciesScannerTest.java @@ -75,9 +75,10 @@ public class DependenciesScannerTest File output = new File( "target/DependenciesScannerTest-tests.jar" ); output.delete(); - ZipOutputStream out = new ZipOutputStream( new FileOutputStream( output ) ); + ZipOutputStream out = null; try { + out = new ZipOutputStream( new FileOutputStream( output ) ); out.putNextEntry( new ZipEntry( "org/test/TestA.class" ) ); out.closeEntry(); out.putNextEntry( new ZipEntry( "org/test/TestB.class" ) ); @@ -86,7 +87,10 @@ public class DependenciesScannerTest } finally { - out.close(); + if ( out != null ) + { + out.close(); + } } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-common/src/test/java/org/apache/maven/surefire/report/FileReporterTest.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/test/java/org/apache/maven/surefire/report/FileReporterTest.java b/maven-surefire-common/src/test/java/org/apache/maven/surefire/report/FileReporterTest.java index 7c49547..87d4b0c 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/surefire/report/FileReporterTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/surefire/report/FileReporterTest.java @@ -20,6 +20,7 @@ package org.apache.maven.surefire.report; */ import java.io.File; +import java.io.IOException; import java.util.ArrayList; import org.apache.maven.plugin.surefire.report.FileReporter; import org.apache.maven.plugin.surefire.report.ReportEntryType; @@ -38,7 +39,7 @@ public class FileReporterTest private static final String testName = "org.apache.maven.surefire.report.FileReporterTest"; - public void testFileNameWithoutSuffix() + public void testFileNameWithoutSuffix() throws IOException { File reportDir = new File( "target" ); reportEntry = new SimpleReportEntry( this.getClass().getName(), testName ); @@ -58,7 +59,7 @@ public class FileReporterTest return new TestSetStats( true, true ); } - public void testFileNameWithSuffix() + public void testFileNameWithSuffix() throws IOException { File reportDir = new File( "target" ); String suffixText = "sampleSuffixText"; http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/SurefireReportMojoTest.java ---------------------------------------------------------------------- diff --git a/maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/SurefireReportMojoTest.java b/maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/SurefireReportMojoTest.java index f138d8a..9fca007 100644 --- a/maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/SurefireReportMojoTest.java +++ b/maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/SurefireReportMojoTest.java @@ -640,6 +640,8 @@ public class SurefireReportMojoTest writer = WriterFactory.newXmlWriter( outputHtml ); renderer.generateDocument( writer, (SiteRendererSink) mojo.getSink(), context ); + writer.close(); + writer = null; } finally { http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-api/pom.xml ---------------------------------------------------------------------- diff --git a/surefire-api/pom.xml b/surefire-api/pom.xml index a35f983..2583972 100644 --- a/surefire-api/pom.xml +++ b/surefire-api/pom.xml @@ -33,6 +33,10 @@ + com.google.code.findbugs + jsr305 + + org.apache.maven.surefire surefire-logger-api http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java ---------------------------------------------------------------------- diff --git a/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java b/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java index d47e803..a66e797 100644 --- a/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java +++ b/surefire-api/src/main/java/org/apache/maven/plugin/surefire/runorder/RunEntryStatisticsMap.java @@ -23,12 +23,13 @@ package org.apache.maven.plugin.surefire.runorder; import org.apache.maven.surefire.report.ReportEntry; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; -import java.io.PrintWriter; +import java.io.OutputStreamWriter; import java.io.Reader; import java.util.ArrayList; import java.util.Comparator; @@ -40,6 +41,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.util.Collections.sort; +import org.apache.maven.shared.utils.io.IOUtil; import static org.apache.maven.plugin.surefire.runorder.RunEntryStatistics.fromReportEntry; import static org.apache.maven.plugin.surefire.runorder.RunEntryStatistics.fromString; @@ -64,9 +66,14 @@ public final class RunEntryStatisticsMap { if ( file.exists() ) { + Reader reader = null; try { - return fromReader( new FileReader( file ) ); + reader = new FileReader( file ); + final RunEntryStatisticsMap result = fromReader( reader ); + reader.close(); + reader = null; + return result; } catch ( FileNotFoundException e ) { @@ -76,6 +83,10 @@ public final class RunEntryStatisticsMap { throw new RuntimeException( e ); } + finally + { + IOUtil.close( reader ); + } } else { @@ -102,23 +113,26 @@ public final class RunEntryStatisticsMap } public void serialize( File file ) - throws FileNotFoundException + throws IOException { - FileOutputStream fos = new FileOutputStream( file ); - PrintWriter printWriter = new PrintWriter( fos ); + BufferedWriter writer = null; try { + writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ) ) ); List items = new ArrayList( runEntryStatistics.values() ); sort( items, new RunCountComparator() ); for ( RunEntryStatistics item : items ) { - printWriter.println( item.toString() ); + writer.append( item.toString() ); + writer.newLine(); } - printWriter.flush(); + + writer.close(); + writer = null; } finally { - printWriter.close(); + IOUtil.close( writer ); } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java ---------------------------------------------------------------------- diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java b/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java index 282c4d4..4558aba 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java @@ -206,6 +206,10 @@ public class ForkingRunListener synchronized ( target ) // See notes about synchronization/thread safety in class javadoc { target.write( encodeBytes, 0, encodeBytes.length ); + if ( target.checkError() ) + { + throw new RuntimeException( "Unexpected IOException." ); + } } } @@ -268,6 +272,10 @@ public class ForkingRunListener synchronized ( target ) // See notes about synchronization/thread safety in class javadoc { target.write( encodeBytes, 0, encodeBytes.length ); + if ( target.checkError() ) + { + throw new RuntimeException( "Unexpected IOException." ); + } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-api/src/main/java/org/apache/maven/surefire/booter/MasterProcessCommand.java ---------------------------------------------------------------------- diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/booter/MasterProcessCommand.java b/surefire-api/src/main/java/org/apache/maven/surefire/booter/MasterProcessCommand.java index a53a046..d2d1673 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/booter/MasterProcessCommand.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/booter/MasterProcessCommand.java @@ -124,14 +124,15 @@ public enum MasterProcessCommand int dataLength = is.readInt(); if ( dataLength > 0 ) { - byte[] buffer = new byte[dataLength]; + byte[] buffer = new byte[ dataLength ]; int read = 0; int total = 0; do { total += read; read = is.read( buffer, total, dataLength - total ); - } while ( read > 0 ); + } + while ( total < dataLength && read >= 0 ); if ( command.getDataType() == Void.class ) { http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemPropertyManager.java ---------------------------------------------------------------------- diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemPropertyManager.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemPropertyManager.java index 713d4fe..4046b7b 100644 --- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemPropertyManager.java +++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemPropertyManager.java @@ -24,6 +24,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; @@ -48,6 +49,8 @@ public class SystemPropertyManager { Properties p = new Properties(); p.load( inStream ); + inStream.close(); + inStream = null; Map map = new ConcurrentHashMap( p.size() ); // @todo use .stringPropertyNames() JDK6 instead of .keySet() for ( Map.Entry entry : p.entrySet() ) @@ -93,15 +96,27 @@ public class SystemPropertyManager public static void writePropertiesFile( File file, String name, Properties properties ) throws IOException { - FileOutputStream out = new FileOutputStream( file ); - + OutputStream out = null; try { + out = new FileOutputStream( file ); properties.store( out, name ); + out.close(); + out = null; } finally { - out.close(); + try + { + if ( out != null ) + { + out.close(); + } + } + catch ( final IOException e1 ) + { + // Suppressed. + } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java index 61736df..a2f021f 100644 --- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java @@ -94,23 +94,37 @@ public class TestFile public String slurpFile() { + BufferedReader reader = null; try { StringBuilder sb = new StringBuilder(); - BufferedReader reader; reader = new BufferedReader( new FileReader( file ) ); for ( String line = reader.readLine(); line != null; line = reader.readLine() ) { sb.append( line ); } reader.close(); + reader = null; return sb.toString(); } catch ( IOException e ) { throw new SurefireVerifierException( e ); } - + finally + { + try + { + if ( reader != null ) + { + reader.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } + } } public String readFileToString() http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/classpath-order/src/test/java/it/BasicTest.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/classpath-order/src/test/java/it/BasicTest.java b/surefire-integration-tests/src/test/resources/classpath-order/src/test/java/it/BasicTest.java index 739e134..258fd0f 100644 --- a/surefire-integration-tests/src/test/resources/classpath-order/src/test/java/it/BasicTest.java +++ b/surefire-integration-tests/src/test/resources/classpath-order/src/test/java/it/BasicTest.java @@ -43,19 +43,36 @@ public class BasicTest private Properties getProperties(String resource) { - InputStream in = getClass().getResourceAsStream( resource ); - assertNotNull( in ); + InputStream in = null; try { - Properties props = new Properties(); - props.load( in ); - return props; + in = getClass().getResourceAsStream( resource ); + assertNotNull( in ); + Properties props = new Properties(); + props.load( in ); + in.close(); + in = null; + return props; } - catch (IOException e) + catch ( IOException e ) { - fail(e.toString()); + fail( e.toString() ); return null; } + finally + { + try + { + if ( in != null ) + { + in.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } + } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/junit4-runlistener/src/test/java/runListener/FileHelper.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/junit4-runlistener/src/test/java/runListener/FileHelper.java b/surefire-integration-tests/src/test/resources/junit4-runlistener/src/test/java/runListener/FileHelper.java index 85d0a5b..1acfa60 100644 --- a/surefire-integration-tests/src/test/resources/junit4-runlistener/src/test/java/runListener/FileHelper.java +++ b/surefire-integration-tests/src/test/resources/junit4-runlistener/src/test/java/runListener/FileHelper.java @@ -22,23 +22,37 @@ package runListener; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; public class FileHelper { public static void writeFile( String fileName, String content ) { + Writer writer = null; try { - File target = new File( "target" ).getAbsoluteFile(); - File listenerOutput = new File( target, fileName ); - FileWriter out = new FileWriter( listenerOutput ); - out.write( content ); - out.flush(); - out.close(); + writer = new FileWriter( new File( new File( "target" ).getAbsoluteFile(), fileName ) ); + writer.write( content ); + writer.close(); + writer = null; } catch ( IOException e ) { throw new RuntimeException( e ); } + finally + { + try + { + if ( writer != null ) + { + writer.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } + } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/FailingTest.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/FailingTest.java b/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/FailingTest.java index a4d0cd3..30224e1 100644 --- a/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/FailingTest.java +++ b/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/FailingTest.java @@ -61,11 +61,12 @@ public class FailingTest f.getParentFile().mkdirs(); FileWriter w = null; - try { w = new FileWriter( f, true ); w.write( name.getMethodName() ); + w.close(); + w = null; } finally { @@ -77,6 +78,7 @@ public class FailingTest } catch ( final IOException e ) { + // Suppressed. } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/SucceedingTest.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/SucceedingTest.java b/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/SucceedingTest.java index c9167c1..2ed21eb 100644 --- a/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/SucceedingTest.java +++ b/surefire-integration-tests/src/test/resources/surefire-803-multiFailsafeExec-failureInFirst/src/test/java/org/apache/maven/surefire/test/SucceedingTest.java @@ -66,6 +66,8 @@ public class SucceedingTest { w = new FileWriter( f, true ); w.write( name.getMethodName() ); + w.close(); + w = null; } finally { @@ -77,6 +79,7 @@ public class SucceedingTest } catch ( final IOException e ) { + // Suppressed. } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java b/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java index 035976b..5e70556 100644 --- a/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java +++ b/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java @@ -41,30 +41,42 @@ public class DumpPidMojo public void execute() throws MojoExecutionException { - File target; + FileWriter fw = null; try { getLog().info( "Dumping PID to " + targetDir ); - + if ( !targetDir.exists() ) { targetDir.mkdirs(); } - - target = new File( targetDir, "maven.pid" ).getCanonicalFile(); - FileWriter fw = new FileWriter( target ); - String pid = ManagementFactory.getRuntimeMXBean().getName(); + final String pid = ManagementFactory.getRuntimeMXBean().getName(); + final File target = new File( targetDir, "maven.pid" ).getCanonicalFile(); + fw = new FileWriter( target ); fw.write( pid ); - fw.flush(); fw.close(); - + fw = null; + getLog().info( "Wrote " + pid + " to " + target ); - } catch ( IOException e ) { throw new MojoExecutionException( "Unable to create pid file", e ); } + finally + { + try + { + if ( fw != null ) + { + fw.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } + } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/testng-listener-reporter/src/test/java/listenReport/FileHelper.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/testng-listener-reporter/src/test/java/listenReport/FileHelper.java b/surefire-integration-tests/src/test/resources/testng-listener-reporter/src/test/java/listenReport/FileHelper.java index 6a6688a..4405996 100644 --- a/surefire-integration-tests/src/test/resources/testng-listener-reporter/src/test/java/listenReport/FileHelper.java +++ b/surefire-integration-tests/src/test/resources/testng-listener-reporter/src/test/java/listenReport/FileHelper.java @@ -22,23 +22,37 @@ package listenReport; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; public class FileHelper { - public static void writeFile(String fileName, String content) + public static void writeFile( String fileName, String content ) { + Writer writer = null; try { - File target = new File( "target" ).getAbsoluteFile(); - File listenerOutput = new File( target, fileName ); - FileWriter out = new FileWriter(listenerOutput); - out.write( content ); - out.flush(); - out.close(); + writer = new FileWriter( new File( new File( "target" ).getAbsoluteFile(), fileName ) ); + writer.write( content ); + writer.close(); + writer = null; } catch ( IOException e ) { - throw new RuntimeException(e); + throw new RuntimeException( e ); + } + finally + { + try + { + if ( writer != null ) + { + writer.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/testng-objectFactory/src/test/java/testng/objectfactory/FileHelper.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/testng-objectFactory/src/test/java/testng/objectfactory/FileHelper.java b/surefire-integration-tests/src/test/resources/testng-objectFactory/src/test/java/testng/objectfactory/FileHelper.java index 4db30b6..4525e29 100644 --- a/surefire-integration-tests/src/test/resources/testng-objectFactory/src/test/java/testng/objectfactory/FileHelper.java +++ b/surefire-integration-tests/src/test/resources/testng-objectFactory/src/test/java/testng/objectfactory/FileHelper.java @@ -3,23 +3,39 @@ package testng.objectfactory; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; public class FileHelper { public static void writeFile( String fileName, String content ) { + Writer writer = null; try { - File target = new File( System.getProperty("user.dir"), "target" ).getCanonicalFile(); - File listenerOutput = new File( target, fileName ); - FileWriter out = new FileWriter( listenerOutput, true ); - out.write( content ); - out.flush(); - out.close(); + writer = new FileWriter( new File( new File( System.getProperty( "user.dir" ), + "target" ).getCanonicalFile(), fileName ), true ); + + writer.write( content ); + writer.close(); + writer = null; } catch ( IOException exception ) { throw new RuntimeException( exception ); } + finally + { + try + { + if ( writer != null ) + { + writer.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } + } } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e5a6b9c8/surefire-integration-tests/src/test/resources/testng-testRunnerFactory/src/test/java/testng/testrunnerfactory/FileHelper.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/testng-testRunnerFactory/src/test/java/testng/testrunnerfactory/FileHelper.java b/surefire-integration-tests/src/test/resources/testng-testRunnerFactory/src/test/java/testng/testrunnerfactory/FileHelper.java index 4b998ed..b72bfc6 100644 --- a/surefire-integration-tests/src/test/resources/testng-testRunnerFactory/src/test/java/testng/testrunnerfactory/FileHelper.java +++ b/surefire-integration-tests/src/test/resources/testng-testRunnerFactory/src/test/java/testng/testrunnerfactory/FileHelper.java @@ -3,23 +3,39 @@ package testng.testrunnerfactory; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; public class FileHelper { public static void writeFile( String fileName, String content ) { + Writer writer = null; try { - File target = new File( System.getProperty("user.dir"), "target" ).getCanonicalFile(); - File listenerOutput = new File( target, fileName ); - FileWriter out = new FileWriter( listenerOutput, true ); - out.write( content ); - out.flush(); - out.close(); + writer = new FileWriter( new File( new File( System.getProperty( "user.dir" ), + "target" ).getCanonicalFile(), fileName ), true ); + + writer.write( content ); + writer.close(); + writer = null; } catch ( IOException exception ) { throw new RuntimeException( exception ); } + finally + { + try + { + if ( writer != null ) + { + writer.close(); + } + } + catch ( final IOException e ) + { + // Suppressed. + } + } } }