View Javadoc
1   package org.apache.maven.shared.model.fileset.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Test the FileSet
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      /** {@inheritDoc} */
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       * @throws IOException if any
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       * @throws IOException if any
86       * @throws InterruptedException if any
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              // assume failure to create a sym link is because the system does not support them
98              // and not because the sym link creation failed.
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      * @throws IOException if any
116      * @throws InterruptedException if any
117      * @throws CommandLineException if any
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             // assume failure to create a sym link is because the system does not support them
129             // and not because the sym link creation failed.
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      * @throws IOException if any
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      * @throws Exception if any
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             // symlinks apparently not supported, skip test
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      * @throws Exception if any
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      * @throws Exception if any
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      * @throws Exception if any
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      * @throws Exception if any
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      * @throws Exception if any
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      * @param target The target file/directory of the symlink, must not be <code>null</code>.
318      * @param link The link to create, must not be <code>null</code>.
319      * @return
320      * @throws InterruptedException
321      * @throws CommandLineException
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 }