Skip to content

Commit 49de4c3

Browse files
Use JUnit TempDir to manage temporary files in tests
- allow JUnit to manage temporary files - use Java NIO for creating temporary files/directory
1 parent c84755c commit 49de4c3

File tree

5 files changed

+184
-282
lines changed

5 files changed

+184
-282
lines changed

src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818

1919
import org.codehaus.plexus.archiver.ArchiverException;
2020
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.io.TempDir;
2122

2223
public class JarArchiverTest
2324
extends BaseJarArchiverTest
2425
{
2526

27+
@TempDir
28+
private Path tempDir;
29+
2630
@Test
2731
public void testCreateManifestOnlyJar()
2832
throws IOException, ManifestException, ArchiverException
2933
{
30-
File jarFile = File.createTempFile( "JarArchiverTest.", ".jar" );
31-
jarFile.deleteOnExit();
34+
File jarFile = Files.createTempFile( tempDir, "JarArchiverTest.", ".jar" ).toFile();
3235

3336
JarArchiver archiver = getJarArchiver();
3437
archiver.setDestFile( jarFile );
@@ -62,15 +65,11 @@ public void testVeryLargeJar()
6265
{
6366
// Generate some number of random files that is likely to be
6467
// two or three times the number of available file handles
65-
File tmpDir = File.createTempFile( "veryLargeJar", null );
66-
tmpDir.delete();
67-
tmpDir.mkdirs();
6868
Random rand = new Random();
6969
for ( int i = 0; i < 45000; i++ )
7070
{
71-
File f = new File( tmpDir, "file" + i );
72-
f.deleteOnExit();
73-
OutputStream out = Files.newOutputStream( f.toPath() );
71+
Path path = tempDir.resolve( "file" + i );
72+
OutputStream out = Files.newOutputStream( path );
7473
byte[] data = new byte[ 512 ]; // 512bytes per file
7574
rand.nextBytes( data );
7675
out.write( data );
@@ -82,7 +81,7 @@ public void testVeryLargeJar()
8281

8382
JarArchiver archiver = getJarArchiver();
8483
archiver.setDestFile( jarFile );
85-
archiver.addDirectory( tmpDir );
84+
archiver.addDirectory( tempDir.toFile() );
8685
archiver.createArchive();
8786
}
8887

@@ -109,8 +108,7 @@ private void createReproducibleBuild( String timeZoneId )
109108
try
110109
{
111110
String tzName = timeZoneId.substring( timeZoneId.lastIndexOf( '/' ) + 1 );
112-
Path jarFile = Files.createTempFile( "JarArchiverTest-" + tzName + "-", ".jar" );
113-
jarFile.toFile().deleteOnExit();
111+
Path jarFile = Files.createTempFile( tempDir, "JarArchiverTest-" + tzName + "-", ".jar" );
114112

115113
Manifest manifest = new Manifest();
116114
Manifest.Attribute attribute = new Manifest.Attribute( "Main-Class", "com.example.app.Main" );

src/test/java/org/codehaus/plexus/archiver/tar/TarArchiverTest.java

+78-98
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@
6060
import org.junit.jupiter.api.Test;
6161
import org.junit.jupiter.api.condition.DisabledOnOs;
6262
import org.junit.jupiter.api.condition.OS;
63+
import org.junit.jupiter.api.io.TempDir;
6364

6465
/**
6566
* @author Emmanuel Venisse
6667
*/
6768
public class TarArchiverTest
6869
extends TestSupport
6970
{
71+
@TempDir
72+
private File tempDir;
7073

7174
@Test
7275
@DisabledOnOs( OS.WINDOWS )
@@ -93,138 +96,115 @@ public void testCreateArchiveWithDetectedModes()
9396
int confMode = 0600;
9497
int logMode = 0640;
9598

96-
File tmpDir = null;
97-
try
99+
for ( String executablePath : executablePaths )
98100
{
99-
tmpDir = File.createTempFile( "tbz2-with-chmod.", ".dir" );
100-
tmpDir.delete();
101+
writeFile( tempDir, executablePath, exeMode );
102+
}
101103

102-
tmpDir.mkdirs();
104+
for ( String confPath : confPaths )
105+
{
106+
writeFile( tempDir, confPath, confMode );
107+
}
103108

104-
for ( String executablePath : executablePaths )
105-
{
106-
writeFile( tmpDir, executablePath, exeMode );
107-
}
109+
for ( String logPath : logPaths )
110+
{
111+
writeFile( tempDir, logPath, logMode );
112+
}
108113

109-
for ( String confPath : confPaths )
114+
{
115+
Map attributesByPath = PlexusIoResourceAttributeUtils.getFileAttributesByPath( tempDir );
116+
for ( String path : executablePaths )
110117
{
111-
writeFile( tmpDir, confPath, confMode );
112-
}
118+
PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path );
119+
if ( attrs == null )
120+
{
121+
attrs = (PlexusIoResourceAttributes) attributesByPath.get(
122+
new File( tempDir, path ).getAbsolutePath() );
123+
}
113124

114-
for ( String logPath : logPaths )
115-
{
116-
writeFile( tmpDir, logPath, logMode );
125+
assertNotNull( attrs );
126+
assertEquals( exeMode, attrs.getOctalMode(), "Wrong mode for: " + path );
117127
}
118128

129+
for ( String path : confPaths )
119130
{
120-
Map attributesByPath = PlexusIoResourceAttributeUtils.getFileAttributesByPath( tmpDir );
121-
for ( String path : executablePaths )
131+
PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path );
132+
if ( attrs == null )
122133
{
123-
PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path );
124-
if ( attrs == null )
125-
{
126-
attrs = (PlexusIoResourceAttributes) attributesByPath.get(
127-
new File( tmpDir, path ).getAbsolutePath() );
128-
}
129-
130-
assertNotNull( attrs );
131-
assertEquals( exeMode, attrs.getOctalMode(), "Wrong mode for: " + path );
134+
attrs = (PlexusIoResourceAttributes) attributesByPath.get(
135+
new File( tempDir, path ).getAbsolutePath() );
132136
}
133137

134-
for ( String path : confPaths )
135-
{
136-
PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path );
137-
if ( attrs == null )
138-
{
139-
attrs = (PlexusIoResourceAttributes) attributesByPath.get(
140-
new File( tmpDir, path ).getAbsolutePath() );
141-
}
142-
143-
assertNotNull( attrs );
144-
assertEquals( confMode, attrs.getOctalMode(), "Wrong mode for: " + path );
145-
}
138+
assertNotNull( attrs );
139+
assertEquals( confMode, attrs.getOctalMode(), "Wrong mode for: " + path );
140+
}
146141

147-
for ( String path : logPaths )
142+
for ( String path : logPaths )
143+
{
144+
PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path );
145+
if ( attrs == null )
148146
{
149-
PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get( path );
150-
if ( attrs == null )
151-
{
152-
attrs = (PlexusIoResourceAttributes) attributesByPath.get(
153-
new File( tmpDir, path ).getAbsolutePath() );
154-
}
155-
156-
assertNotNull( attrs );
157-
assertEquals( logMode, attrs.getOctalMode(), "Wrong mode for: " + path );
147+
attrs = (PlexusIoResourceAttributes) attributesByPath.get(
148+
new File( tempDir, path ).getAbsolutePath() );
158149
}
150+
151+
assertNotNull( attrs );
152+
assertEquals( logMode, attrs.getOctalMode(), "Wrong mode for: " + path );
159153
}
154+
}
160155

161-
File tarFile = getTestFile( "target/output/tar-with-modes.tar" );
156+
File tarFile = getTestFile( "target/output/tar-with-modes.tar" );
162157

163-
TarArchiver archiver = getPosixTarArchiver();
164-
archiver.setDestFile( tarFile );
158+
TarArchiver archiver = getPosixTarArchiver();
159+
archiver.setDestFile( tarFile );
165160

166-
archiver.addDirectory( tmpDir );
167-
archiver.createArchive();
161+
archiver.addDirectory(tempDir );
162+
archiver.createArchive();
168163

169-
assertTrue( tarFile.exists() );
164+
assertTrue( tarFile.exists() );
170165

171-
File tarFile2 = getTestFile( "target/output/tar-with-modes-L2.tar" );
166+
File tarFile2 = getTestFile( "target/output/tar-with-modes-L2.tar" );
172167

173-
archiver = getPosixTarArchiver();
174-
archiver.setDestFile( tarFile2 );
168+
archiver = getPosixTarArchiver();
169+
archiver.setDestFile( tarFile2 );
175170

176-
archiver.addArchivedFileSet( tarFile );
177-
archiver.createArchive();
171+
archiver.addArchivedFileSet( tarFile );
172+
archiver.createArchive();
178173

179-
TarFile tf = new TarFile( tarFile2 );
174+
TarFile tf = new TarFile( tarFile2 );
180175

181-
Map<String, TarArchiveEntry> entriesByPath = new LinkedHashMap<String, TarArchiveEntry>();
182-
for ( Enumeration e = tf.getEntries(); e.hasMoreElements(); )
183-
{
184-
TarArchiveEntry te = (TarArchiveEntry) e.nextElement();
185-
entriesByPath.put( te.getName(), te );
186-
}
176+
Map<String, TarArchiveEntry> entriesByPath = new LinkedHashMap<String, TarArchiveEntry>();
177+
for ( Enumeration e = tf.getEntries(); e.hasMoreElements(); )
178+
{
179+
TarArchiveEntry te = (TarArchiveEntry) e.nextElement();
180+
entriesByPath.put( te.getName(), te );
181+
}
187182

188-
for ( String path : executablePaths )
189-
{
190-
TarArchiveEntry te = entriesByPath.get( path );
183+
for ( String path : executablePaths )
184+
{
185+
TarArchiveEntry te = entriesByPath.get( path );
191186

192-
int mode = te.getMode() & UnixStat.PERM_MASK;
187+
int mode = te.getMode() & UnixStat.PERM_MASK;
193188

194-
assertEquals( exeMode, mode, "Wrong mode for: " + path );
195-
}
189+
assertEquals( exeMode, mode, "Wrong mode for: " + path );
190+
}
196191

197-
for ( String path : confPaths )
198-
{
199-
TarArchiveEntry te = entriesByPath.get( path );
192+
for ( String path : confPaths )
193+
{
194+
TarArchiveEntry te = entriesByPath.get( path );
200195

201-
int mode = te.getMode() & UnixStat.PERM_MASK;
196+
int mode = te.getMode() & UnixStat.PERM_MASK;
202197

203-
assertEquals( confMode, mode, "Wrong mode for: " + path );
204-
}
198+
assertEquals( confMode, mode, "Wrong mode for: " + path );
199+
}
205200

206-
for ( String path : logPaths )
207-
{
208-
TarArchiveEntry te = entriesByPath.get( path );
201+
for ( String path : logPaths )
202+
{
203+
TarArchiveEntry te = entriesByPath.get( path );
209204

210-
int mode = te.getMode() & UnixStat.PERM_MASK;
205+
int mode = te.getMode() & UnixStat.PERM_MASK;
211206

212-
assertEquals( logMode, mode, "Wrong mode for: " + path );
213-
}
214-
}
215-
finally
216-
{
217-
if ( tmpDir != null && tmpDir.exists() )
218-
{
219-
try
220-
{
221-
FileUtils.forceDelete( tmpDir );
222-
}
223-
catch ( IOException e )
224-
{
225-
e.printStackTrace();
226-
}
227-
}
207+
assertEquals( logMode, mode, "Wrong mode for: " + path );
228208
}
229209
}
230210

0 commit comments

Comments
 (0)