From c939c3b498f575efd02a5030068391f5d1112d22 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Mon, 1 May 2023 21:59:34 +0200 Subject: [PATCH] Use JUnit TempDir to manage temporary files in tests - allow JUnit to manage temporary files - use Java NIO for creating temporary files/directory --- .../plexus/archiver/jar/JarArchiverTest.java | 20 +- .../plexus/archiver/tar/TarArchiverTest.java | 176 ++++++++---------- .../archiver/tar/TarFileAttributesTest.java | 94 ++-------- .../archiver/util/ArchiveEntryUtilsTest.java | 10 +- .../plexus/archiver/zip/ZipArchiverTest.java | 166 +++++++---------- 5 files changed, 184 insertions(+), 282 deletions(-) diff --git a/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java index 8e1d53cd0..3bcd86c77 100644 --- a/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java @@ -18,17 +18,20 @@ import org.codehaus.plexus.archiver.ArchiverException; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class JarArchiverTest extends BaseJarArchiverTest { + @TempDir + private Path tempDir; + @Test public void testCreateManifestOnlyJar() throws IOException, ManifestException, ArchiverException { - File jarFile = File.createTempFile( "JarArchiverTest.", ".jar" ); - jarFile.deleteOnExit(); + File jarFile = Files.createTempFile( tempDir, "JarArchiverTest.", ".jar" ).toFile(); JarArchiver archiver = getJarArchiver(); archiver.setDestFile( jarFile ); @@ -62,15 +65,11 @@ public void testVeryLargeJar() { // Generate some number of random files that is likely to be // two or three times the number of available file handles - File tmpDir = File.createTempFile( "veryLargeJar", null ); - tmpDir.delete(); - tmpDir.mkdirs(); Random rand = new Random(); for ( int i = 0; i < 45000; i++ ) { - File f = new File( tmpDir, "file" + i ); - f.deleteOnExit(); - OutputStream out = Files.newOutputStream( f.toPath() ); + Path path = tempDir.resolve( "file" + i ); + OutputStream out = Files.newOutputStream( path ); byte[] data = new byte[ 512 ]; // 512bytes per file rand.nextBytes( data ); out.write( data ); @@ -82,7 +81,7 @@ public void testVeryLargeJar() JarArchiver archiver = getJarArchiver(); archiver.setDestFile( jarFile ); - archiver.addDirectory( tmpDir ); + archiver.addDirectory( tempDir.toFile() ); archiver.createArchive(); } @@ -109,8 +108,7 @@ private void createReproducibleBuild( String timeZoneId ) try { String tzName = timeZoneId.substring( timeZoneId.lastIndexOf( '/' ) + 1 ); - Path jarFile = Files.createTempFile( "JarArchiverTest-" + tzName + "-", ".jar" ); - jarFile.toFile().deleteOnExit(); + Path jarFile = Files.createTempFile( tempDir, "JarArchiverTest-" + tzName + "-", ".jar" ); Manifest manifest = new Manifest(); Manifest.Attribute attribute = new Manifest.Attribute( "Main-Class", "com.example.app.Main" ); diff --git a/src/test/java/org/codehaus/plexus/archiver/tar/TarArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/tar/TarArchiverTest.java index 19f88fdac..915d3e231 100644 --- a/src/test/java/org/codehaus/plexus/archiver/tar/TarArchiverTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/tar/TarArchiverTest.java @@ -60,6 +60,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; /** * @author Emmanuel Venisse @@ -67,6 +68,8 @@ public class TarArchiverTest extends TestSupport { + @TempDir + private File tempDir; @Test @DisabledOnOs( OS.WINDOWS ) @@ -93,138 +96,115 @@ public void testCreateArchiveWithDetectedModes() int confMode = 0600; int logMode = 0640; - File tmpDir = null; - try + for ( String executablePath : executablePaths ) { - tmpDir = File.createTempFile( "tbz2-with-chmod.", ".dir" ); - tmpDir.delete(); + writeFile( tempDir, executablePath, exeMode ); + } - tmpDir.mkdirs(); + for ( String confPath : confPaths ) + { + writeFile( tempDir, confPath, confMode ); + } - for ( String executablePath : executablePaths ) - { - writeFile( tmpDir, executablePath, exeMode ); - } + for ( String logPath : logPaths ) + { + writeFile( tempDir, logPath, logMode ); + } - for ( String confPath : confPaths ) + { + Map attributesByPath = PlexusIoResourceAttributeUtils.getFileAttributesByPath( tempDir ); + for ( String path : executablePaths ) { - writeFile( tmpDir, confPath, confMode ); - } + PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path ); + if ( attrs == null ) + { + attrs = (PlexusIoResourceAttributes) attributesByPath.get( + new File( tempDir, path ).getAbsolutePath() ); + } - for ( String logPath : logPaths ) - { - writeFile( tmpDir, logPath, logMode ); + assertNotNull( attrs ); + assertEquals( exeMode, attrs.getOctalMode(), "Wrong mode for: " + path ); } + for ( String path : confPaths ) { - Map attributesByPath = PlexusIoResourceAttributeUtils.getFileAttributesByPath( tmpDir ); - for ( String path : executablePaths ) + PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path ); + if ( attrs == null ) { - PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path ); - if ( attrs == null ) - { - attrs = (PlexusIoResourceAttributes) attributesByPath.get( - new File( tmpDir, path ).getAbsolutePath() ); - } - - assertNotNull( attrs ); - assertEquals( exeMode, attrs.getOctalMode(), "Wrong mode for: " + path ); + attrs = (PlexusIoResourceAttributes) attributesByPath.get( + new File( tempDir, path ).getAbsolutePath() ); } - for ( String path : confPaths ) - { - PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path ); - if ( attrs == null ) - { - attrs = (PlexusIoResourceAttributes) attributesByPath.get( - new File( tmpDir, path ).getAbsolutePath() ); - } - - assertNotNull( attrs ); - assertEquals( confMode, attrs.getOctalMode(), "Wrong mode for: " + path ); - } + assertNotNull( attrs ); + assertEquals( confMode, attrs.getOctalMode(), "Wrong mode for: " + path ); + } - for ( String path : logPaths ) + for ( String path : logPaths ) + { + PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path ); + if ( attrs == null ) { - PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path ); - if ( attrs == null ) - { - attrs = (PlexusIoResourceAttributes) attributesByPath.get( - new File( tmpDir, path ).getAbsolutePath() ); - } - - assertNotNull( attrs ); - assertEquals( logMode, attrs.getOctalMode(), "Wrong mode for: " + path ); + attrs = (PlexusIoResourceAttributes) attributesByPath.get( + new File( tempDir, path ).getAbsolutePath() ); } + + assertNotNull( attrs ); + assertEquals( logMode, attrs.getOctalMode(), "Wrong mode for: " + path ); } + } - File tarFile = getTestFile( "target/output/tar-with-modes.tar" ); + File tarFile = getTestFile( "target/output/tar-with-modes.tar" ); - TarArchiver archiver = getPosixTarArchiver(); - archiver.setDestFile( tarFile ); + TarArchiver archiver = getPosixTarArchiver(); + archiver.setDestFile( tarFile ); - archiver.addDirectory( tmpDir ); - archiver.createArchive(); + archiver.addDirectory(tempDir ); + archiver.createArchive(); - assertTrue( tarFile.exists() ); + assertTrue( tarFile.exists() ); - File tarFile2 = getTestFile( "target/output/tar-with-modes-L2.tar" ); + File tarFile2 = getTestFile( "target/output/tar-with-modes-L2.tar" ); - archiver = getPosixTarArchiver(); - archiver.setDestFile( tarFile2 ); + archiver = getPosixTarArchiver(); + archiver.setDestFile( tarFile2 ); - archiver.addArchivedFileSet( tarFile ); - archiver.createArchive(); + archiver.addArchivedFileSet( tarFile ); + archiver.createArchive(); - TarFile tf = new TarFile( tarFile2 ); + TarFile tf = new TarFile( tarFile2 ); - Map entriesByPath = new LinkedHashMap(); - for ( Enumeration e = tf.getEntries(); e.hasMoreElements(); ) - { - TarArchiveEntry te = (TarArchiveEntry) e.nextElement(); - entriesByPath.put( te.getName(), te ); - } + Map entriesByPath = new LinkedHashMap(); + for ( Enumeration e = tf.getEntries(); e.hasMoreElements(); ) + { + TarArchiveEntry te = (TarArchiveEntry) e.nextElement(); + entriesByPath.put( te.getName(), te ); + } - for ( String path : executablePaths ) - { - TarArchiveEntry te = entriesByPath.get( path ); + for ( String path : executablePaths ) + { + TarArchiveEntry te = entriesByPath.get( path ); - int mode = te.getMode() & UnixStat.PERM_MASK; + int mode = te.getMode() & UnixStat.PERM_MASK; - assertEquals( exeMode, mode, "Wrong mode for: " + path ); - } + assertEquals( exeMode, mode, "Wrong mode for: " + path ); + } - for ( String path : confPaths ) - { - TarArchiveEntry te = entriesByPath.get( path ); + for ( String path : confPaths ) + { + TarArchiveEntry te = entriesByPath.get( path ); - int mode = te.getMode() & UnixStat.PERM_MASK; + int mode = te.getMode() & UnixStat.PERM_MASK; - assertEquals( confMode, mode, "Wrong mode for: " + path ); - } + assertEquals( confMode, mode, "Wrong mode for: " + path ); + } - for ( String path : logPaths ) - { - TarArchiveEntry te = entriesByPath.get( path ); + for ( String path : logPaths ) + { + TarArchiveEntry te = entriesByPath.get( path ); - int mode = te.getMode() & UnixStat.PERM_MASK; + int mode = te.getMode() & UnixStat.PERM_MASK; - assertEquals( logMode, mode, "Wrong mode for: " + path ); - } - } - finally - { - if ( tmpDir != null && tmpDir.exists() ) - { - try - { - FileUtils.forceDelete( tmpDir ); - } - catch ( IOException e ) - { - e.printStackTrace(); - } - } + assertEquals( logMode, mode, "Wrong mode for: " + path ); } } diff --git a/src/test/java/org/codehaus/plexus/archiver/tar/TarFileAttributesTest.java b/src/test/java/org/codehaus/plexus/archiver/tar/TarFileAttributesTest.java index 4f44a62da..d1699a17a 100644 --- a/src/test/java/org/codehaus/plexus/archiver/tar/TarFileAttributesTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/tar/TarFileAttributesTest.java @@ -1,12 +1,10 @@ package org.codehaus.plexus.archiver.tar; import java.io.File; -import java.io.IOException; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.ArrayList; -import java.util.List; +import java.nio.file.Path; import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.archiver.TestSupport; import org.codehaus.plexus.archiver.UnArchiver; @@ -14,12 +12,11 @@ import org.codehaus.plexus.components.io.attributes.AttributeUtils; import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils; import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes; -import org.codehaus.plexus.util.FileUtils; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -28,7 +25,8 @@ public class TarFileAttributesTest extends TestSupport { - private final List toDelete = new ArrayList(); + @TempDir + private Path tempDir; @Override @BeforeEach @@ -42,38 +40,6 @@ public void setUp() System.out.println( "Octal 0440 is decimal " + 0440 ); } - @Override - @AfterEach - public void tearDown() - throws Exception - { - super.tearDown(); - - if ( !toDelete.isEmpty() ) - { - for ( File f : toDelete ) - { - System.out.println( "Deleting: " + f ); - if ( f.isDirectory() ) - { - try - { - FileUtils.deleteDirectory( f ); - } - catch ( IOException e ) - { - System.out.println( "Error deleting test directory: " + f ); - } - } - else - { - f.delete(); - f.deleteOnExit(); - } - } - } - } - private void printTestHeader() { StackTraceElement e = new Throwable().getStackTrace()[1]; @@ -87,8 +53,7 @@ public void testUseAttributesFromTarArchiveInputInTarArchiverOutput() { printTestHeader(); - File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" ); - toDelete.add( tempFile ); + File tempFile = Files.createTempFile( tempDir, "tar-file-attributes.", ".tmp" ).toFile(); try ( Writer writer = Files.newBufferedWriter( tempFile.toPath(), StandardCharsets.UTF_8 ) ) { @@ -99,8 +64,7 @@ public void testUseAttributesFromTarArchiveInputInTarArchiverOutput() TarArchiver tarArchiver = getPosixCompliantTarArchiver(); - File tempTarFile = File.createTempFile( "tar-file.", ".tar" ); - toDelete.add( tempTarFile ); + File tempTarFile = Files.createTempFile( tempDir, "tar-file.", ".tar" ).toFile(); tarArchiver.setDestFile( tempTarFile ); tarArchiver.addFile( tempFile, tempFile.getName(), 0660 ); @@ -109,8 +73,7 @@ public void testUseAttributesFromTarArchiveInputInTarArchiverOutput() TarArchiver tarArchiver2 = getPosixCompliantTarArchiver(); - File tempTarFile2 = File.createTempFile( "tar-file.", ".tar" ); - toDelete.add( tempTarFile2 ); + File tempTarFile2 = Files.createTempFile( tempDir, "tar-file.", ".tar" ).toFile(); tarArchiver2.setDestFile( tempTarFile2 ); @@ -124,11 +87,7 @@ public void testUseAttributesFromTarArchiveInputInTarArchiverOutput() // Cut from here, and feed it into a new tar archiver...then unarchive THAT. TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.class, "tar" ); - File tempTarDir = File.createTempFile( "tar-test.", ".dir" ); - tempTarDir.delete(); - tempTarDir.mkdirs(); - - toDelete.add( tempTarDir ); + File tempTarDir = Files.createTempDirectory( tempDir, "tar-test." ).toFile(); tarUnArchiver.setDestDirectory( tempTarDir ); tarUnArchiver.setSourceFile( tempTarFile2 ); @@ -149,8 +108,7 @@ public void testUseDetectedFileAttributes() { printTestHeader(); - File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" ); - toDelete.add( tempFile ); + File tempFile = Files.createTempFile( tempDir, "tar-file-attributes.", ".tmp" ).toFile(); try ( Writer writer = Files.newBufferedWriter( tempFile.toPath(), StandardCharsets.UTF_8 ) ) { @@ -165,8 +123,7 @@ public void testUseDetectedFileAttributes() TarArchiver tarArchiver = getPosixCompliantTarArchiver(); - File tempTarFile = File.createTempFile( "tar-file.", ".tar" ); - toDelete.add( tempTarFile ); + File tempTarFile = Files.createTempFile( tempDir, "tar-file.", ".tar" ).toFile(); tarArchiver.setDestFile( tempTarFile ); tarArchiver.addFile( tempFile, tempFile.getName() ); @@ -175,11 +132,7 @@ public void testUseDetectedFileAttributes() TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.class, "tar" ); - File tempTarDir = File.createTempFile( "tar-test.", ".dir" ); - tempTarDir.delete(); - tempTarDir.mkdirs(); - - toDelete.add( tempTarDir ); + File tempTarDir = Files.createTempDirectory( tempDir, "tar-test." ).toFile(); tarUnArchiver.setDestDirectory( tempTarDir ); tarUnArchiver.setSourceFile( tempTarFile ); @@ -189,7 +142,6 @@ public void testUseDetectedFileAttributes() fileAttributes = PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) ); assertEquals( 0440, fileAttributes.getOctalMode() ); - } @Test @@ -199,8 +151,7 @@ public void testOverrideDetectedFileAttributes() { printTestHeader(); - File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" ); - toDelete.add( tempFile ); + File tempFile = Files.createTempFile( tempDir, "tar-file-attributes.", ".tmp" ).toFile(); try ( Writer writer = Files.newBufferedWriter( tempFile.toPath(), StandardCharsets.UTF_8 ) ) { @@ -211,8 +162,7 @@ public void testOverrideDetectedFileAttributes() TarArchiver tarArchiver = getPosixCompliantTarArchiver(); - File tempTarFile = File.createTempFile( "tar-file.", ".tar" ); - toDelete.add( tempTarFile ); + File tempTarFile = Files.createTempFile( tempDir, "tar-file.", ".tar" ).toFile(); tarArchiver.setDestFile( tempTarFile ); tarArchiver.addFile( tempFile, tempFile.getName(), 0660 ); @@ -221,11 +171,7 @@ public void testOverrideDetectedFileAttributes() TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.class, "tar" ); - File tempTarDir = File.createTempFile( "tar-test.", ".dir" ); - tempTarDir.delete(); - tempTarDir.mkdirs(); - - toDelete.add( tempTarDir ); + File tempTarDir = Files.createTempDirectory( tempDir, "tar-test." ).toFile(); tarUnArchiver.setDestDirectory( tempTarDir ); tarUnArchiver.setSourceFile( tempTarFile ); @@ -252,8 +198,7 @@ public void testOverrideDetectedFileAttributesUsingFileMode() throws Exception { printTestHeader(); - File tempFile = File.createTempFile( "tar-file-attributes.", ".tmp" ); - toDelete.add( tempFile ); + File tempFile = Files.createTempFile( tempDir, "tar-file-attributes.", ".tmp" ).toFile(); try ( Writer writer = Files.newBufferedWriter( tempFile.toPath(), StandardCharsets.UTF_8 ) ) { @@ -264,8 +209,7 @@ public void testOverrideDetectedFileAttributesUsingFileMode() TarArchiver tarArchiver = getPosixCompliantTarArchiver(); - File tempTarFile = File.createTempFile( "tar-file.", ".tar" ); - toDelete.add( tempTarFile ); + File tempTarFile = Files.createTempFile( tempDir, "tar-file.", ".tar" ).toFile(); tarArchiver.setDestFile( tempTarFile ); tarArchiver.setFileMode( 0660 ); @@ -275,11 +219,7 @@ public void testOverrideDetectedFileAttributesUsingFileMode() TarUnArchiver tarUnArchiver = (TarUnArchiver) lookup( UnArchiver.class, "tar" ); - File tempTarDir = File.createTempFile( "tar-test.", ".dir" ); - tempTarDir.delete(); - tempTarDir.mkdirs(); - - toDelete.add( tempTarDir ); + File tempTarDir = Files.createTempDirectory( tempDir, "tar-test." ).toFile(); tarUnArchiver.setDestDirectory( tempTarDir ); tarUnArchiver.setSourceFile( tempTarFile ); diff --git a/src/test/java/org/codehaus/plexus/archiver/util/ArchiveEntryUtilsTest.java b/src/test/java/org/codehaus/plexus/archiver/util/ArchiveEntryUtilsTest.java index c8163d8cc..a3543a92f 100644 --- a/src/test/java/org/codehaus/plexus/archiver/util/ArchiveEntryUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/util/ArchiveEntryUtilsTest.java @@ -2,11 +2,14 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashMap; import org.codehaus.plexus.components.io.attributes.FileAttributes; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -14,11 +17,14 @@ public class ArchiveEntryUtilsTest { + @TempDir + private Path tempDir; + @Test @DisabledOnOs( OS.WINDOWS ) public void testChmodForFileWithDollarPLXCOMP164() throws Exception { - File temp = File.createTempFile( "A$A", "BB$" ); + File temp = Files.createTempFile( tempDir, "A$A", "BB$" ).toFile(); ArchiveEntryUtils.chmod( temp, 0770 ); assert0770( temp ); } @@ -27,7 +33,7 @@ public void testChmodForFileWithDollarPLXCOMP164() throws Exception @DisabledOnOs( OS.WINDOWS ) public void testChmodWithJava7() throws Exception { - File temp = File.createTempFile( "D$D", "BB$" ); + File temp = Files.createTempFile( tempDir, "D$D", "BB$" ).toFile(); ArchiveEntryUtils.chmod( temp, 0770 ); assert0770( temp ); } diff --git a/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java index a5258d338..327ec757b 100644 --- a/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java @@ -60,7 +60,6 @@ import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException; import org.codehaus.plexus.archiver.tar.TarArchiver; import org.codehaus.plexus.archiver.tar.TarFile; -import org.codehaus.plexus.archiver.tar.TarLongFileMode; import org.codehaus.plexus.archiver.util.ArchiveEntryUtils; import org.codehaus.plexus.archiver.util.DefaultArchivedFileSet; import org.codehaus.plexus.archiver.util.DefaultFileSet; @@ -81,6 +80,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -96,6 +96,9 @@ public class ZipArchiverTest extends BasePlexusArchiverTest { + @TempDir + private File tempDir; + @Test public void testImplicitPermissions() throws IOException @@ -174,128 +177,105 @@ public void testCreateArchiveWithDetectedModes() int confMode = 0600; int logMode = 0640; - File tmpDir = null; - try + for ( String executablePath : executablePaths ) { - tmpDir = File.createTempFile( "zip-with-chmod.", ".dir" ); - tmpDir.delete(); + writeFile( tempDir, executablePath, exeMode ); + } - tmpDir.mkdirs(); + for ( String confPath : confPaths ) + { + writeFile( tempDir, confPath, confMode ); + } - for ( String executablePath : executablePaths ) - { - writeFile( tmpDir, executablePath, exeMode ); - } + for ( String logPath : logPaths ) + { + writeFile( tempDir, logPath, logMode ); + } - for ( String confPath : confPaths ) + { + Map attributesByPath = + PlexusIoResourceAttributeUtils.getFileAttributesByPath( tempDir ); + for ( String path : executablePaths ) { - writeFile( tmpDir, confPath, confMode ); - } + PlexusIoResourceAttributes attrs = attributesByPath.get( path ); + if ( attrs == null ) + { + attrs = attributesByPath.get( new File( tempDir, path ).getAbsolutePath() ); + } - for ( String logPath : logPaths ) - { - writeFile( tmpDir, logPath, logMode ); + assertNotNull( attrs ); + assertEquals( exeMode, attrs.getOctalMode(), "Wrong mode for: " + path ); } + for ( String path : confPaths ) { - Map attributesByPath = - PlexusIoResourceAttributeUtils.getFileAttributesByPath( tmpDir ); - for ( String path : executablePaths ) + PlexusIoResourceAttributes attrs = attributesByPath.get( path ); + if ( attrs == null ) { - PlexusIoResourceAttributes attrs = attributesByPath.get( path ); - if ( attrs == null ) - { - attrs = attributesByPath.get( new File( tmpDir, path ).getAbsolutePath() ); - } - - assertNotNull( attrs ); - assertEquals( exeMode, attrs.getOctalMode(), "Wrong mode for: " + path ); + attrs = attributesByPath.get( new File( tempDir, path ).getAbsolutePath() ); } - for ( String path : confPaths ) - { - PlexusIoResourceAttributes attrs = attributesByPath.get( path ); - if ( attrs == null ) - { - attrs = attributesByPath.get( new File( tmpDir, path ).getAbsolutePath() ); - } - - assertNotNull( attrs ); - assertEquals( confMode, attrs.getOctalMode(), "Wrong mode for: " + path ); - } + assertNotNull( attrs ); + assertEquals( confMode, attrs.getOctalMode(), "Wrong mode for: " + path ); + } - for ( String path : logPaths ) + for ( String path : logPaths ) + { + PlexusIoResourceAttributes attrs = attributesByPath.get( path ); + if ( attrs == null ) { - PlexusIoResourceAttributes attrs = attributesByPath.get( path ); - if ( attrs == null ) - { - attrs = attributesByPath.get( new File( tmpDir, path ).getAbsolutePath() ); - } - - assertNotNull( attrs ); - assertEquals( logMode, attrs.getOctalMode(), "Wrong mode for: " + path ); + attrs = attributesByPath.get( new File( tempDir, path ).getAbsolutePath() ); } + + assertNotNull( attrs ); + assertEquals( logMode, attrs.getOctalMode(), "Wrong mode for: " + path ); } + } - File zipFile = getTestFile( "target/output/zip-with-modes.zip" ); + File zipFile = getTestFile( "target/output/zip-with-modes.zip" ); - ZipArchiver archiver = getZipArchiver( zipFile ); + ZipArchiver archiver = getZipArchiver( zipFile ); - archiver.addDirectory( tmpDir ); - archiver.createArchive(); + archiver.addDirectory( tempDir ); + archiver.createArchive(); - assertTrue( zipFile.exists() ); + assertTrue( zipFile.exists() ); - File zipFile2 = getTestFile( "target/output/zip-with-modes-L2.zip" ); + File zipFile2 = getTestFile( "target/output/zip-with-modes-L2.zip" ); - archiver = getZipArchiver(); - archiver.setDestFile( zipFile2 ); + archiver = getZipArchiver(); + archiver.setDestFile( zipFile2 ); - archiver.addArchivedFileSet( zipFile ); - archiver.createArchive(); + archiver.addArchivedFileSet( zipFile ); + archiver.createArchive(); - ZipFile zf = new ZipFile( zipFile2 ); + ZipFile zf = new ZipFile( zipFile2 ); - for ( String path : executablePaths ) - { - ZipArchiveEntry ze = zf.getEntry( path ); + for ( String path : executablePaths ) + { + ZipArchiveEntry ze = zf.getEntry( path ); - int mode = ze.getUnixMode() & UnixStat.PERM_MASK; + int mode = ze.getUnixMode() & UnixStat.PERM_MASK; - assertEquals( exeMode, mode, "Wrong mode for: " + path ); - } + assertEquals( exeMode, mode, "Wrong mode for: " + path ); + } - for ( String path : confPaths ) - { - ZipArchiveEntry ze = zf.getEntry( path ); + for ( String path : confPaths ) + { + ZipArchiveEntry ze = zf.getEntry( path ); - int mode = ze.getUnixMode() & UnixStat.PERM_MASK; + int mode = ze.getUnixMode() & UnixStat.PERM_MASK; - assertEquals( confMode, mode, "Wrong mode for: " + path ); - } + assertEquals( confMode, mode, "Wrong mode for: " + path ); + } - for ( String path : logPaths ) - { - ZipArchiveEntry ze = zf.getEntry( path ); + for ( String path : logPaths ) + { + ZipArchiveEntry ze = zf.getEntry( path ); - int mode = ze.getUnixMode() & UnixStat.PERM_MASK; + int mode = ze.getUnixMode() & UnixStat.PERM_MASK; - assertEquals( logMode, mode, "Wrong mode for: " + path ); - } - } - finally - { - if ( tmpDir != null && tmpDir.exists() ) - { - try - { - FileUtils.forceDelete( tmpDir ); - } - catch ( IOException e ) - { - e.printStackTrace(); - } - } + assertEquals( logMode, mode, "Wrong mode for: " + path ); } } @@ -643,12 +623,10 @@ public void testSymlinkArchivedFileSet() public void testLastModifiedTimeRounding() throws Exception { - Path oddSecondsTimestampFile = Files.createTempFile( "odd-seconds-timestamp", null ); - oddSecondsTimestampFile.toFile().deleteOnExit(); + Path oddSecondsTimestampFile = Files.createTempFile( tempDir.toPath(), "odd-seconds-timestamp", null ); // The milliseconds part is set to zero as not all filesystem support timestamp more granular than second. Files.setLastModifiedTime( oddSecondsTimestampFile, FileTime.fromMillis( 1534189011_000L ) ); - Path evenSecondsTimestampFile = Files.createTempFile( "even-seconds-timestamp", null ); - evenSecondsTimestampFile.toFile().deleteOnExit(); + Path evenSecondsTimestampFile = Files.createTempFile( tempDir.toPath(), "even-seconds-timestamp", null ); Files.setLastModifiedTime( evenSecondsTimestampFile, FileTime.fromMillis( 1534189012_000L ) ); File destFile = getTestFile( "target/output/last-modified-time.zip" );