60
60
import org .junit .jupiter .api .Test ;
61
61
import org .junit .jupiter .api .condition .DisabledOnOs ;
62
62
import org .junit .jupiter .api .condition .OS ;
63
+ import org .junit .jupiter .api .io .TempDir ;
63
64
64
65
/**
65
66
* @author Emmanuel Venisse
66
67
*/
67
68
public class TarArchiverTest
68
69
extends TestSupport
69
70
{
71
+ @ TempDir
72
+ private File tempDir ;
70
73
71
74
@ Test
72
75
@ DisabledOnOs ( OS .WINDOWS )
@@ -93,138 +96,115 @@ public void testCreateArchiveWithDetectedModes()
93
96
int confMode = 0600 ;
94
97
int logMode = 0640 ;
95
98
96
- File tmpDir = null ;
97
- try
99
+ for ( String executablePath : executablePaths )
98
100
{
99
- tmpDir = File . createTempFile ( "tbz2-with-chmod." , ".dir" );
100
- tmpDir . delete ();
101
+ writeFile ( tempDir , executablePath , exeMode );
102
+ }
101
103
102
- tmpDir .mkdirs ();
104
+ for ( String confPath : confPaths )
105
+ {
106
+ writeFile ( tempDir , confPath , confMode );
107
+ }
103
108
104
- for ( String executablePath : executablePaths )
105
- {
106
- writeFile ( tmpDir , executablePath , exeMode );
107
- }
109
+ for ( String logPath : logPaths )
110
+ {
111
+ writeFile ( tempDir , logPath , logMode );
112
+ }
108
113
109
- for ( String confPath : confPaths )
114
+ {
115
+ Map attributesByPath = PlexusIoResourceAttributeUtils .getFileAttributesByPath ( tempDir );
116
+ for ( String path : executablePaths )
110
117
{
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
+ }
113
124
114
- for ( String logPath : logPaths )
115
- {
116
- writeFile ( tmpDir , logPath , logMode );
125
+ assertNotNull ( attrs );
126
+ assertEquals ( exeMode , attrs .getOctalMode (), "Wrong mode for: " + path );
117
127
}
118
128
129
+ for ( String path : confPaths )
119
130
{
120
- Map attributesByPath = PlexusIoResourceAttributeUtils . getFileAttributesByPath ( tmpDir );
121
- for ( String path : executablePaths )
131
+ PlexusIoResourceAttributes attrs = ( PlexusIoResourceAttributes ) attributesByPath . get ( path );
132
+ if ( attrs == null )
122
133
{
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 () );
132
136
}
133
137
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
+ }
146
141
147
- for ( String path : logPaths )
142
+ for ( String path : logPaths )
143
+ {
144
+ PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes ) attributesByPath .get ( path );
145
+ if ( attrs == null )
148
146
{
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 () );
158
149
}
150
+
151
+ assertNotNull ( attrs );
152
+ assertEquals ( logMode , attrs .getOctalMode (), "Wrong mode for: " + path );
159
153
}
154
+ }
160
155
161
- File tarFile = getTestFile ( "target/output/tar-with-modes.tar" );
156
+ File tarFile = getTestFile ( "target/output/tar-with-modes.tar" );
162
157
163
- TarArchiver archiver = getPosixTarArchiver ();
164
- archiver .setDestFile ( tarFile );
158
+ TarArchiver archiver = getPosixTarArchiver ();
159
+ archiver .setDestFile ( tarFile );
165
160
166
- archiver .addDirectory ( tmpDir );
167
- archiver .createArchive ();
161
+ archiver .addDirectory (tempDir );
162
+ archiver .createArchive ();
168
163
169
- assertTrue ( tarFile .exists () );
164
+ assertTrue ( tarFile .exists () );
170
165
171
- File tarFile2 = getTestFile ( "target/output/tar-with-modes-L2.tar" );
166
+ File tarFile2 = getTestFile ( "target/output/tar-with-modes-L2.tar" );
172
167
173
- archiver = getPosixTarArchiver ();
174
- archiver .setDestFile ( tarFile2 );
168
+ archiver = getPosixTarArchiver ();
169
+ archiver .setDestFile ( tarFile2 );
175
170
176
- archiver .addArchivedFileSet ( tarFile );
177
- archiver .createArchive ();
171
+ archiver .addArchivedFileSet ( tarFile );
172
+ archiver .createArchive ();
178
173
179
- TarFile tf = new TarFile ( tarFile2 );
174
+ TarFile tf = new TarFile ( tarFile2 );
180
175
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
+ }
187
182
188
- for ( String path : executablePaths )
189
- {
190
- TarArchiveEntry te = entriesByPath .get ( path );
183
+ for ( String path : executablePaths )
184
+ {
185
+ TarArchiveEntry te = entriesByPath .get ( path );
191
186
192
- int mode = te .getMode () & UnixStat .PERM_MASK ;
187
+ int mode = te .getMode () & UnixStat .PERM_MASK ;
193
188
194
- assertEquals ( exeMode , mode , "Wrong mode for: " + path );
195
- }
189
+ assertEquals ( exeMode , mode , "Wrong mode for: " + path );
190
+ }
196
191
197
- for ( String path : confPaths )
198
- {
199
- TarArchiveEntry te = entriesByPath .get ( path );
192
+ for ( String path : confPaths )
193
+ {
194
+ TarArchiveEntry te = entriesByPath .get ( path );
200
195
201
- int mode = te .getMode () & UnixStat .PERM_MASK ;
196
+ int mode = te .getMode () & UnixStat .PERM_MASK ;
202
197
203
- assertEquals ( confMode , mode , "Wrong mode for: " + path );
204
- }
198
+ assertEquals ( confMode , mode , "Wrong mode for: " + path );
199
+ }
205
200
206
- for ( String path : logPaths )
207
- {
208
- TarArchiveEntry te = entriesByPath .get ( path );
201
+ for ( String path : logPaths )
202
+ {
203
+ TarArchiveEntry te = entriesByPath .get ( path );
209
204
210
- int mode = te .getMode () & UnixStat .PERM_MASK ;
205
+ int mode = te .getMode () & UnixStat .PERM_MASK ;
211
206
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 );
228
208
}
229
209
}
230
210
0 commit comments