Skip to content

Commit

Permalink
Use JUnit TempDir to manage temporary files in tests
Browse files Browse the repository at this point in the history
- allow JUnit to manage temporary files
- use Java NIO for creating temporary files/directory
  • Loading branch information
slawekjaranowski committed May 2, 2023
1 parent c84755c commit 49de4c3
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -82,7 +81,7 @@ public void testVeryLargeJar()

JarArchiver archiver = getJarArchiver();
archiver.setDestFile( jarFile );
archiver.addDirectory( tmpDir );
archiver.addDirectory( tempDir.toFile() );
archiver.createArchive();
}

Expand All @@ -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" );
Expand Down
176 changes: 78 additions & 98 deletions src/test/java/org/codehaus/plexus/archiver/tar/TarArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@
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
*/
public class TarArchiverTest
extends TestSupport
{
@TempDir
private File tempDir;

@Test
@DisabledOnOs( OS.WINDOWS )
Expand All @@ -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<String, TarArchiveEntry> entriesByPath = new LinkedHashMap<String, TarArchiveEntry>();
for ( Enumeration e = tf.getEntries(); e.hasMoreElements(); )
{
TarArchiveEntry te = (TarArchiveEntry) e.nextElement();
entriesByPath.put( te.getName(), te );
}
Map<String, TarArchiveEntry> entriesByPath = new LinkedHashMap<String, TarArchiveEntry>();
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 );
}
}

Expand Down
Loading

0 comments on commit 49de4c3

Please sign in to comment.