1 package org.apache.maven.shared.model.fileset.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URL;
25 import java.net.URLDecoder;
26 import java.util.HashSet;
27 import java.util.Set;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.shared.model.fileset.FileSet;
31
32 import org.codehaus.plexus.util.cli.CommandLineException;
33 import org.codehaus.plexus.util.cli.Commandline;
34 import org.junit.After;
35 import org.junit.Test;
36
37 import static org.junit.Assert.*;
38
39
40
41
42 public class FileSetUtilsTest
43 {
44 private final Set<File> testDirectories = new HashSet<>();
45
46 private final Set<File> linkFiles = new HashSet<>();
47
48
49 @After
50 public void tearDown()
51 throws IOException
52 {
53 for ( File linkFile : linkFiles )
54 {
55 linkFile.delete();
56 }
57
58 for ( File dir : testDirectories )
59 {
60 FileUtils.deleteDirectory( dir );
61 }
62 }
63
64
65
66
67 @Test
68 public void testGetIncludedFiles()
69 throws IOException
70 {
71 File directory = setupTestDirectory( "testGetIncludedFiles" );
72
73 FileSet set = new FileSet();
74 set.setDirectory( directory.getPath() );
75 set.addInclude( "**/included.txt" );
76
77 FileSetManager fileSetManager = new FileSetManager();
78
79 String[] included = fileSetManager.getIncludedFiles( set );
80
81 assertEquals( 1, included.length );
82 }
83
84
85
86
87
88 @Test
89 public void testIncludesDontFollowSymlinks()
90 throws IOException, InterruptedException, CommandLineException
91 {
92 File directory = setupTestDirectory( "testIncludesDontFollowSymlinks" );
93 File subdir = new File( directory, directory.getName() );
94
95 if ( !createSymlink( directory, subdir ) )
96 {
97
98
99 return;
100 }
101
102 FileSet set = new FileSet();
103 set.setDirectory( directory.getPath() );
104 set.addInclude( "**/included.txt" );
105 set.setFollowSymlinks( false );
106
107 FileSetManager fileSetManager = new FileSetManager();
108
109 String[] included = fileSetManager.getIncludedFiles( set );
110
111 assertEquals( 1, included.length );
112 }
113
114
115
116
117
118
119 @Test
120 public void testDeleteDontFollowSymlinks()
121 throws IOException, InterruptedException, CommandLineException
122 {
123 File directory = setupTestDirectory( "testDeleteDontFollowSymlinks" );
124 File subdir = new File( directory, directory.getName() );
125
126 if ( !createSymlink( directory, subdir ) )
127 {
128
129
130 return;
131 }
132
133 FileSet set = new FileSet();
134 set.setDirectory( directory.getPath() );
135 set.addInclude( "**/included.txt" );
136 set.addInclude( "**/" + subdir.getName() );
137 set.setFollowSymlinks( false );
138
139 FileSetManager fileSetManager = new FileSetManager();
140
141 fileSetManager.delete( set );
142
143 assertFalse( subdir.exists() );
144 }
145
146
147
148
149 @Test
150 public void testDelete()
151 throws IOException
152 {
153 File directory = setupTestDirectory( "testDelete" );
154 File subdirFile = new File( directory, "subdir/excluded.txt" );
155
156 FileSet set = new FileSet();
157 set.setDirectory( directory.getPath() );
158 set.addInclude( "**/included.txt" );
159 set.addInclude( "**/subdir" );
160
161 FileSetManager fileSetManager = new FileSetManager();
162
163 fileSetManager.delete( set );
164
165 assertFalse( "file in marked subdirectory still exists.", subdirFile.exists() );
166 }
167
168
169
170
171 @Test
172 public void testDeleteDanglingSymlink()
173 throws Exception
174 {
175 File directory = setupTestDirectory( "testDeleteDanglingSymlink" );
176 File targetFile = new File( directory, "test.txt" );
177 File linkFile = new File( directory, "symlink" );
178
179 if ( !createSymlink( targetFile, linkFile ) )
180 {
181
182 return;
183 }
184 targetFile.delete();
185
186 FileSet set = new FileSet();
187 set.setDirectory( directory.getPath() );
188 set.addInclude( "**" );
189
190 FileSetManager fileSetManager = new FileSetManager();
191
192 fileSetManager.delete( set );
193
194 assertFalse( "directory still exists", directory.exists() );
195 }
196
197
198
199
200 @Test
201 public void testDeleteExcludeParentOfExcludedFile()
202 throws Exception
203 {
204 File directory = setupTestDirectory( "testDeleteExcludeParentOfExcludedFile" );
205
206 FileSet set = new FileSet();
207 set.setDirectory( directory.getPath() );
208 set.addExclude( "*excluded*" );
209 set.setFollowSymlinks( true );
210
211 FileSetManager fileSetManager = new FileSetManager();
212
213 fileSetManager.delete( set );
214
215 assertTrue( "excluded file has been deleted", new File( directory, "excluded.txt" ).exists() );
216 }
217
218
219
220
221 @Test
222 public void testDeleteExcludeParentOfExcludedDir()
223 throws Exception
224 {
225 File directory = setupTestDirectory( "testDeleteExcludeParentOfExcludedDir" );
226
227 FileSet set = new FileSet();
228 set.setDirectory( directory.getPath() );
229 set.addExclude( "*excluded*" );
230 set.setFollowSymlinks( true );
231
232 FileSetManager fileSetManager = new FileSetManager();
233
234 fileSetManager.delete( set );
235
236 assertTrue( "excluded directory has been deleted", new File( directory, "excluded" ).exists() );
237 }
238
239
240
241
242 @Test
243 public void testDeleteExcludeFollowSymlinks()
244 throws Exception
245 {
246 File directory = setupTestDirectory( "testDeleteExcludeFollowSymlinks" );
247
248 FileSet set = new FileSet();
249 set.setDirectory( directory.getPath() );
250 set.addExclude( "*excluded*" );
251 set.setFollowSymlinks( true );
252
253 FileSetManager fileSetManager = new FileSetManager();
254
255 fileSetManager.delete( set );
256
257 assertTrue( "excluded file has been deleted", new File( directory, "excluded.txt" ).exists() );
258 assertTrue( "excluded directory has been deleted", new File( directory, "excluded" ).exists() );
259 assertFalse( "included file has not been deleted", new File( directory, "included.txt" ).exists() );
260 }
261
262
263
264
265 @Test
266 public void testDeleteExcludeDontFollowSymlinks()
267 throws Exception
268 {
269 File directory = setupTestDirectory( "testDeleteExcludeDontFollowSymlinks" );
270
271 FileSet set = new FileSet();
272 set.setDirectory( directory.getPath() );
273 set.addExclude( "*excluded*" );
274 set.setFollowSymlinks( false );
275
276 FileSetManager fileSetManager = new FileSetManager();
277
278 fileSetManager.delete( set );
279
280 assertTrue( "excluded file has been deleted", new File( directory, "excluded.txt" ).exists() );
281 assertTrue( "excluded directory has been deleted", new File( directory, "excluded" ).exists() );
282 assertFalse( "included file has not been deleted", new File( directory, "included.txt" ).exists() );
283 }
284
285
286
287
288 @Test
289 public void testDeleteDontFollowSymlinksButDeleteThem()
290 throws Exception
291 {
292 File directory = setupTestDirectory( "testDeleteDontFollowSymlinksButDeleteThem" );
293
294 createSymlink( new File( directory, "excluded" ), new File( directory, "dirlink" ) );
295 createSymlink( new File( directory, "excluded.txt" ), new File( directory, "filelink" ) );
296 createSymlink( new File( directory, "excluded" ), new File( directory, "dir0/dirlink" ) );
297 createSymlink( new File( directory, "excluded.txt" ), new File( directory, "dir1/filelink" ) );
298
299 FileSet set = new FileSet();
300 set.setDirectory( directory.getPath() );
301 set.addExclude( "*excluded*" );
302 set.setFollowSymlinks( false );
303
304 FileSetManager fileSetManager = new FileSetManager();
305
306 fileSetManager.delete( set );
307
308 assertTrue( "excluded file has been deleted", new File( directory, "excluded.txt" ).exists() );
309 assertTrue( "excluded directory has been deleted", new File( directory, "excluded" ).exists() );
310 assertFalse( "included dirlink has not been deleted", new File( directory, "dirlink" ).exists() );
311 assertFalse( "included filelink has not been deleted", new File( directory, "filelink" ).exists() );
312 assertFalse( "included directory has not been deleted", new File( directory, "dir0" ).exists() );
313 assertFalse( "included directory has not been deleted", new File( directory, "dir1" ).exists() );
314 }
315
316
317
318
319
320
321
322
323 private boolean createSymlink( File target, File link )
324 throws InterruptedException, CommandLineException
325
326 {
327 if ( link.exists() )
328 {
329 link.delete();
330 }
331
332 Commandline cli = new Commandline();
333 cli.setExecutable( "ln" );
334 cli.createArg().setValue( "-s" );
335 cli.createArg().setValue( target.getPath() );
336 cli.createArg().setValue( link.getPath() );
337
338 int result = cli.execute().waitFor();
339
340 linkFiles.add( link );
341
342 return result == 0;
343 }
344
345 private File setupTestDirectory( String directoryName )
346 throws IOException
347 {
348 URL sourceResource = getClass().getClassLoader().getResource( directoryName );
349
350 if ( sourceResource == null )
351 {
352 fail( "Source directory for test: " + directoryName + " cannot be found." );
353 }
354
355 File sourceDir = new File( URLDecoder.decode( sourceResource.getPath(), "UTF-8" ) );
356
357 String basedir = System.getProperty( "basedir", System.getProperty( "user.dir" ) );
358 String testBase = System.getProperty( "testBase", "target/test-directories" );
359
360 File testDir = new File( basedir, testBase + "/" + directoryName );
361 if ( testDir.mkdirs() ) {
362 FileUtils.copyDirectory( sourceDir, testDir );
363 testDirectories.add( testDir );
364 return testDir;
365 } else {
366 throw new IOException( "Could not create test directory " + testDir );
367 }
368 }
369 }