Skip to content

Commit

Permalink
[SUREFIRE-2086] Management of temporary files (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm authored Jun 3, 2022
1 parent bce6b43 commit 559db4c
Show file tree
Hide file tree
Showing 15 changed files with 434 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.maven.plugin.failsafe.util.FailsafeSummaryXmlUtils;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.util.SureFireFileManager;
import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void shouldMarshallAndUnmarshallSameXml() throws Exception
+ "\n\tat org.apache.maven.plugin.surefire.booterclient.ForkStarter"
+ ".awaitResultsDone(ForkStarter.java:489)", true );

File xml = File.createTempFile( "failsafe-summary", ".xml" );
File xml = SureFireFileManager.createTempFile( "failsafe-summary", ".xml" );
FailsafeSummaryXmlUtils.writeSummary( expected, xml, false );

RunResult actual = FailsafeSummaryXmlUtils.toRunResult( xml );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.maven.plugin.failsafe.util.FailsafeSummaryXmlUtils;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.util.SureFireFileManager;
import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void testAppendSerialization()
RunResult simpleAggregate = getSimpleAggregate();
RunResult additional = new RunResult( 2, 1, 2, 2, "msg " + ( (char) 0x0E01 ), true );

File summary = File.createTempFile( "failsafe", "test" );
File summary = SureFireFileManager.createTempFile( "failsafe", "test" );
FailsafeSummaryXmlUtils.writeSummary( simpleAggregate, summary, false );
FailsafeSummaryXmlUtils.writeSummary( additional, summary, true );
RunResult actual = FailsafeSummaryXmlUtils.toRunResult( summary );
Expand Down Expand Up @@ -121,7 +122,7 @@ public void testAppendSerialization()
private void writeReadCheck( RunResult expected )
throws Exception
{
File tmp = File.createTempFile( "test", "xml" );
File tmp = SureFireFileManager.createTempFile( "test", "xml" );
FailsafeSummaryXmlUtils.fromRunResultToFile( expected, tmp );

RunResult actual = FailsafeSummaryXmlUtils.toRunResult( tmp );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.Commandline;
import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton;
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.util.TempFileManager;
import org.apache.maven.surefire.booter.Classpath;
import org.apache.maven.surefire.booter.StartupConfiguration;
import org.apache.maven.surefire.booter.SurefireBooterForkException;
Expand Down Expand Up @@ -113,7 +114,7 @@ private File createJar( @Nonnull List<String> classPath, @Nonnull String startCl
@Nonnull File dumpLogDirectory )
throws IOException
{
File file = File.createTempFile( "surefirebooter", ".jar", getTempDirectory() );
File file = TempFileManager.instance( getTempDirectory() ).createTempFile( "surefirebooter", ".jar" );
if ( !isDebug() )
{
file.deleteOnExit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.Commandline;
import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton;
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.util.TempFileManager;
import org.apache.maven.surefire.booter.AbstractPathConfiguration;
import org.apache.maven.surefire.booter.Classpath;
import org.apache.maven.surefire.booter.ModularClasspath;
Expand All @@ -42,7 +43,6 @@
import java.util.Map;
import java.util.Properties;

import static java.io.File.createTempFile;
import static java.io.File.pathSeparatorChar;
import static org.apache.maven.plugin.surefire.SurefireHelper.escapeToPlatformPath;
import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
Expand Down Expand Up @@ -118,7 +118,7 @@ File createArgsFile( @Nonnull String moduleName, @Nonnull List<String> modulePat
@Nonnull List<String[]> providerJpmsArguments )
throws IOException
{
File surefireArgs = createTempFile( "surefireargs", "", getTempDirectory() );
File surefireArgs = TempFileManager.instance( getTempDirectory() ).createTempFile( "surefireargs", "" );
if ( isDebug() )
{
getLogger().debug( "Path to args file: " + surefireArgs.getCanonicalPath() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import static java.util.Objects.requireNonNull;
import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;

import org.apache.maven.surefire.api.util.SureFireFileManager;

/**
* A deferred file output stream decorator that recodes the bytes written into the stream from the VM default encoding
* to UTF-8.
Expand Down Expand Up @@ -66,7 +68,8 @@ public synchronized void write( String output, boolean newLine )

if ( storage == null )
{
file = Files.createTempFile( channel, "deferred" );

file = SureFireFileManager.createTempFile( channel, "deferred" ).toPath();
storage = new RandomAccessFile( file.toFile(), "rw" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.util.DefaultScanResult;
import org.apache.maven.surefire.api.util.SureFireFileManager;
import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
import org.apache.maven.surefire.booter.Classpath;
import org.apache.maven.surefire.booter.ModularClasspathConfiguration;
Expand Down Expand Up @@ -2522,7 +2523,7 @@ public void shouldNotPerformMethodFilteringOnIncludes() throws Exception
{
Mojo plugin = new Mojo();

File includesExcludes = File.createTempFile( "surefire", "-includes" );
File includesExcludes = SureFireFileManager.createTempFile( "surefire", "includes" );
FileUtils.write( includesExcludes, "AnotherTest#method" , UTF_8 );
plugin.setIncludesFile( includesExcludes );

Expand All @@ -2533,7 +2534,7 @@ public void shouldNotPerformMethodFilteringOnIncludes() throws Exception
VersionRange version = VersionRange.createFromVersion( "1.0" );
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler );
File artifactFile = File.createTempFile( "surefire", ".jar" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", ".jar" );
artifactFile.deleteOnExit();
testDeps.setFile( artifactFile );
plugin.setProjectTestArtifacts( singletonList( testDeps ) );
Expand All @@ -2550,14 +2551,14 @@ public void shouldFilterTestsOnIncludesFile() throws Exception

plugin.setLogger( mock( Logger.class ) );

File includes = File.createTempFile( "surefire", "-includes" );
File includes = SureFireFileManager.createTempFile( "surefire", "includes" );
FileUtils.write( includes, "AnotherTest#method" , UTF_8 );
plugin.setIncludesFile( includes );

VersionRange version = VersionRange.createFromVersion( "1.0" );
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler );
File artifactFile = File.createTempFile( "surefire", "-classes" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", "classes" );
String classDir = artifactFile.getCanonicalPath();
assertThat( artifactFile.delete() ).isTrue();
File classes = new File( classDir );
Expand All @@ -2580,14 +2581,14 @@ public void shouldFilterTestsOnExcludesFile() throws Exception

plugin.setLogger( mock( Logger.class ) );

File excludes = File.createTempFile( "surefire", "-excludes" );
File excludes = SureFireFileManager.createTempFile( "surefire", "-excludes" );
FileUtils.write( excludes, "AnotherTest" , UTF_8 );
plugin.setExcludesFile( excludes );

VersionRange version = VersionRange.createFromVersion( "1.0" );
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler );
File artifactFile = File.createTempFile( "surefire", "-classes" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", "-classes" );
String classDir = artifactFile.getCanonicalPath();
assertThat( artifactFile.delete() ).isTrue();
File classes = new File( classDir );
Expand Down Expand Up @@ -2615,7 +2616,7 @@ public void shouldFilterTestsOnExcludes() throws Exception
VersionRange version = VersionRange.createFromVersion( "1.0" );
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler );
File artifactFile = File.createTempFile( "surefire", "-classes" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", "-classes" );
String classDir = artifactFile.getCanonicalPath();
assertThat( artifactFile.delete() ).isTrue();
File classes = new File( classDir );
Expand All @@ -2638,15 +2639,15 @@ public void shouldUseOnlySpecificTests() throws Exception

plugin.setLogger( mock( Logger.class ) );

File includes = File.createTempFile( "surefire", "-includes" );
File includes = SureFireFileManager.createTempFile( "surefire", "-includes" );
FileUtils.write( includes, "AnotherTest" , UTF_8 );
plugin.setIncludesFile( includes );
plugin.setTest( "DifferentTest" );

VersionRange version = VersionRange.createFromVersion( "1.0" );
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler );
File artifactFile = File.createTempFile( "surefire", "-classes" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", "-classes" );
String classDir = artifactFile.getCanonicalPath();
assertThat( artifactFile.delete() ).isTrue();
File classes = new File( classDir );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.maven.surefire.extensions.ForkNodeFactory;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.util.DefaultScanResult;
import org.apache.maven.surefire.api.util.SureFireFileManager;
import org.apache.maven.toolchain.Toolchain;
import org.codehaus.plexus.logging.Logger;
import org.junit.Test;
Expand Down Expand Up @@ -213,7 +214,7 @@ public void scanDependenciesShouldReturnNullWithExistingWAR()
VersionRange version = VersionRange.createFromVersion( "1.0" );
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "war", null, handler );
File artifactFile = File.createTempFile( "surefire", ".war" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", ".war" );
testDeps.setFile( artifactFile );
List<Artifact> projectTestArtifacts = singletonList( testDeps );
String[] dependenciesToScan = { "g:a" };
Expand All @@ -231,7 +232,7 @@ public void scanDependenciesShouldReturnClassWithExistingTestJAR()
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler );

File artifactFile = File.createTempFile( "surefire", ".jar" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", ".jar" );
testDeps.setFile( artifactFile );
try ( ZipOutputStream os = new ZipOutputStream( new FileOutputStream( artifactFile ) ) )
{
Expand Down Expand Up @@ -266,7 +267,7 @@ public void scanDependenciesShouldReturnNullWithEmptyTestJAR()
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler );

File artifactFile = File.createTempFile( "surefire", ".jar" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", ".jar" );
testDeps.setFile( artifactFile );
try ( ZipOutputStream os = new ZipOutputStream( new FileOutputStream( artifactFile ) ) )
{
Expand Down Expand Up @@ -296,7 +297,7 @@ public void scanDependenciesShouldReturnClassWithDirectory()
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler );

File artifactFile = File.createTempFile( "surefire", "-classes" );
File artifactFile = SureFireFileManager.createTempFile( "surefire", "-classes" );
String classDir = artifactFile.getCanonicalPath();
assertThat( artifactFile.delete() ).isTrue();
File classes = new File( classDir );
Expand Down Expand Up @@ -331,7 +332,7 @@ public void scanMultipleDependencies()
ArtifactHandler handler = new DefaultArtifactHandler();
Artifact testDep1 = new DefaultArtifact( "g", "x", version, "compile", "jar", null, handler );

File artifactFile1 = File.createTempFile( "surefire", "-classes" );
File artifactFile1 = SureFireFileManager.createTempFile( "surefire", "-classes" );
String classDir = artifactFile1.getCanonicalPath();
assertThat( artifactFile1.delete() ).isTrue();
File classes = new File( classDir );
Expand All @@ -343,7 +344,7 @@ public void scanMultipleDependencies()
.isTrue();

Artifact testDep2 = new DefaultArtifact( "g", "a", version, "test", "jar", null, handler );
File artifactFile2 = File.createTempFile( "surefire", ".jar" );
File artifactFile2 = SureFireFileManager.createTempFile( "surefire", ".jar" );
testDep2.setFile( artifactFile2 );
try ( ZipOutputStream os = new ZipOutputStream( new FileOutputStream( artifactFile2 ) ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.Commandline;
import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger;
import org.apache.maven.surefire.api.util.SureFireFileManager;
import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
import org.apache.maven.surefire.booter.Classpath;
import org.apache.maven.surefire.booter.ForkedBooter;
Expand All @@ -36,7 +37,6 @@
import java.util.List;
import java.util.Properties;

import static java.io.File.createTempFile;
import static java.io.File.pathSeparatorChar;
import static java.io.File.separator;
import static java.io.File.separatorChar;
Expand Down Expand Up @@ -147,7 +147,7 @@ public void shouldCreateModularArgsFile() throws Exception
modularClasspathConfiguration, clc, null, Collections.<String[]>emptyList() );
Commandline cli = new Commandline();
config.resolveClasspath( cli, ForkedBooter.class.getName(), startupConfiguration,
createTempFile( "surefire", "surefire-reports" ) );
SureFireFileManager.createTempFile( "surefire", "surefire-reports" ) );

assertThat( cli.getArguments() ).isNotNull();
assertThat( cli.getArguments() ).hasSize( 1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.maven.surefire.api.report.SimpleReportEntry;

import junit.framework.TestCase;
import org.apache.maven.surefire.api.util.SureFireFileManager;
import org.apache.maven.surefire.api.util.internal.ClassMethod;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -85,7 +86,7 @@ private InputStream getStatisticsFile()
public void testSerializeClass()
throws Exception
{
File data = File.createTempFile( "surefire-unit", "test" );
File data = SureFireFileManager.createTempFile( "surefire-unit", "test" );
RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();
ReportEntry reportEntry = new SimpleReportEntry( NORMAL_RUN, 0L,
"abc", null, null, null, 42 );
Expand All @@ -107,7 +108,7 @@ public void testSerializeClass()
public void testDeserializeClass()
throws Exception
{
File data = File.createTempFile( "surefire-unit", "test" );
File data = SureFireFileManager.createTempFile( "surefire-unit", "test" );
Files.write( data.toPath(), "1,42,abc".getBytes( UTF_8 ) );
RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile( data );
Map<?, ?> runEntryStatistics = getInternalState( existingEntries, "runEntryStatistics" );
Expand All @@ -129,7 +130,7 @@ public void testDeserializeClass()
public void testSerialize()
throws Exception
{
File data = File.createTempFile( "surefire-unit", "test" );
File data = SureFireFileManager.createTempFile( "surefire-unit", "test" );
RunEntryStatisticsMap existingEntries = RunEntryStatisticsMap.fromFile( data );
RunEntryStatisticsMap newResults = new RunEntryStatisticsMap();

Expand Down Expand Up @@ -186,7 +187,7 @@ public void testSerialize()
@SuppressWarnings( "checkstyle:magicnumber" )
public void testMultiLineTestMethodName() throws IOException
{
File data = File.createTempFile( "surefire-unit", "test" );
File data = SureFireFileManager.createTempFile( "surefire-unit", "test" );
RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile( data );
ReportEntry reportEntry = new SimpleReportEntry( NORMAL_RUN, 0L,
"abc", null, "line1\nline2" + NL + " line3", null, 42 );
Expand Down Expand Up @@ -222,7 +223,7 @@ public void testMultiLineTestMethodName() throws IOException
@SuppressWarnings( "checkstyle:magicnumber" )
public void testCombinedMethodNames() throws IOException
{
File data = File.createTempFile( "surefire-unit", "test" );
File data = SureFireFileManager.createTempFile( "surefire-unit", "test" );
RunEntryStatisticsMap reportEntries = RunEntryStatisticsMap.fromFile( data );
reportEntries.add( reportEntries.createNextGeneration( new SimpleReportEntry( NORMAL_RUN, 0L,
"abc", null, "line1\nline2", null, 42 ) ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.apache.maven.surefire.api.util;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.File;

/**
* Centralized file management of surefire.
*
* @author Markus Spann
*/
public final class SureFireFileManager
{

public static File createTempFile( String prefix, String suffix )
{

return TempFileManager.instance( "surefire" ).createTempFile( prefix, suffix );

}

}
Loading

0 comments on commit 559db4c

Please sign in to comment.