6060import org .junit .jupiter .api .Test ;
6161import org .junit .jupiter .api .condition .DisabledOnOs ;
6262import org .junit .jupiter .api .condition .OS ;
63+ import org .junit .jupiter .api .io .TempDir ;
6364
6465/**
6566 * @author Emmanuel Venisse
6667 */
6768public 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