1   package org.apache.maven.plugin.clean;
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.RandomAccessFile;
24  import java.nio.channels.FileChannel;
25  import java.nio.channels.FileLock;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  import org.codehaus.plexus.util.FileUtils;
30  
31  /**
32   * Test the clean mojo.
33   *
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   * @version $Id: CleanMojoTest.html 828299 2012-08-07 22:03:58Z hboutemy $
36   */
37  public class CleanMojoTest
38      extends AbstractMojoTestCase
39  {
40      /** {@inheritDoc} */
41      protected void setUp()
42          throws Exception
43      {
44          super.setUp();
45      }
46  
47      /** {@inheritDoc} */
48      protected void tearDown()
49          throws Exception
50      {
51          super.tearDown();
52      }
53  
54      /**
55       * Tests the simple removal of directories
56       *
57       * @throws Exception
58       */
59      public void testBasicClean()
60          throws Exception
61      {
62          String pluginPom = getBasedir() + "/src/test/resources/unit/basic-clean-test/plugin-pom.xml";
63  
64          // safety
65          FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/basic-clean-test" ),
66                                   new File( getBasedir(), "target/test-classes/unit/basic-clean-test" ), null, "**/.svn,**/.svn/**" );
67  
68          CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
69          assertNotNull( mojo );
70  
71          mojo.execute();
72  
73          assertFalse( "Directory exists", checkExists( getBasedir() + "/target/test-classes/unit/"
74              + "basic-clean-test/buildDirectory" ) );
75          assertFalse( "Directory exists", checkExists( getBasedir() + "/target/test-classes/unit/basic-clean-test/"
76              + "buildOutputDirectory" ) );
77          assertFalse( "Directory exists", checkExists( getBasedir() + "/target/test-classes/unit/basic-clean-test/"
78              + "buildTestDirectory" ) );
79      }
80  
81      /**
82       * Tests the removal of files and nested directories
83       *
84       * @throws Exception
85       */
86      public void testCleanNestedStructure()
87          throws Exception
88      {
89          String pluginPom = getBasedir() + "/src/test/resources/unit/nested-clean-test/plugin-pom.xml";
90  
91          // safety
92          FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/nested-clean-test" ),
93                                   new File( getBasedir(), "target/test-classes/unit/nested-clean-test" ), null, "**/.svn,**/.svn/**" );
94  
95          CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
96          assertNotNull( mojo );
97  
98          mojo.execute();
99  
100         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/nested-clean-test/target" ) );
101         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/nested-clean-test/target/classes" ) );
102         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/nested-clean-test/target/test-classes" ) );
103     }
104 
105     /**
106      * Tests that no exception is thrown when all internal variables are empty and that it doesn't
107      * just remove whats there
108      *
109      * @throws Exception
110      */
111     public void testCleanEmptyDirectories()
112         throws Exception
113     {
114         String pluginPom = getBasedir() + "/src/test/resources/unit/empty-clean-test/plugin-pom.xml";
115 
116         // safety
117         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/empty-clean-test" ),
118                                  new File( getBasedir(), "target/test-classes/unit/empty-clean-test" ), null, "**/.svn,**/.svn/**" );
119 
120         CleanMojo mojo = (CleanMojo) lookupEmptyMojo( "clean", pluginPom );
121         assertNotNull( mojo );
122 
123         mojo.execute();
124 
125         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/empty-clean-test/testDirectoryStructure" ) );
126         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/empty-clean-test/"
127             + "testDirectoryStructure/file.txt" ) );
128         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/empty-clean-test/"
129             + "testDirectoryStructure/outputDirectory" ) );
130         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/empty-clean-test/"
131             + "testDirectoryStructure/outputDirectory/file.txt" ) );
132     }
133 
134     /**
135      * Tests the removal of files using fileset
136      *
137      * @throws Exception
138      */
139     public void testFilesetsClean()
140         throws Exception
141     {
142         String pluginPom = getBasedir() + "/src/test/resources/unit/fileset-clean-test/plugin-pom.xml";
143 
144         // safety
145         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/fileset-clean-test" ),
146                                  new File( getBasedir(), "target/test-classes/unit/fileset-clean-test" ), null, "**/.svn,**/.svn/**" );
147 
148         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
149         assertNotNull( mojo );
150 
151         mojo.execute();
152 
153         // fileset 1
154         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/target" ) );
155         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/target/classes" ) );
156         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/target/test-classes" ) );
157         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/target/subdir" ) );
158         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/target/classes/file.txt" ) );
159         assertTrue( checkEmpty( getBasedir() + "/target/test-classes/unit/fileset-clean-test/target/classes" ) );
160         assertTrue( checkEmpty( getBasedir() + "/target/test-classes/unit/fileset-clean-test/target/subdir" ) );
161 
162         // fileset 2
163         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/"
164             + "buildOutputDirectory" ) );
165         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/"
166             + "buildOutputDirectory/file.txt" ) );
167     }
168 
169     /**
170      * Tests the removal of a directory as file
171      *
172      * @throws Exception
173      */
174     public void testCleanInvalidDirectory()
175         throws Exception
176     {
177         String pluginPom = getBasedir() + "/src/test/resources/unit/invalid-directory-test/plugin-pom.xml";
178 
179         // safety
180         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/invalid-directory-test" ),
181                                  new File( getBasedir(), "target/test-classes/unit/invalid-directory-test" ), null, "**/.svn,**/.svn/**" );
182 
183         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
184         assertNotNull( mojo );
185 
186         try
187         {
188             mojo.execute();
189 
190             fail( "Should fail to delete a file treated as a directory" );
191         }
192         catch ( MojoExecutionException expected )
193         {
194             assertTrue( true );
195         }
196     }
197 
198     /**
199      * Tests the removal of a missing directory
200      *
201      * @throws Exception
202      */
203     public void testMissingDirectory()
204         throws Exception
205     {
206         String pluginPom = getBasedir() + "/src/test/resources/unit/missing-directory-test/plugin-pom.xml";
207 
208         // safety
209         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/missing-directory-test" ),
210                                  new File( getBasedir(), "target/test-classes/unit/missing-directory-test" ), null, "**/.svn,**/.svn/**" );
211 
212         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
213         assertNotNull( mojo );
214 
215         mojo.execute();
216 
217         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/missing-directory-test/does-not-exist" ) );
218     }
219 
220     /**
221      * Test the removal of a locked file on Windows systems.
222      * <br/>
223      * Note: Unix systems doesn't lock any files.
224      *
225      * @throws Exception
226      */
227     public void testCleanLockedFile()
228         throws Exception
229     {
230         if ( System.getProperty( "os.name" ).toLowerCase().indexOf( "windows" ) == -1 )
231         {
232             assertTrue( "Ignored this test on none Windows based systems", true );
233             return;
234         }
235 
236         String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
237 
238         // safety
239         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/locked-file-test" ),
240                                  new File( getBasedir(), "target/test-classes/unit/locked-file-test" ), null, "**/.svn,**/.svn/**" );
241 
242         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
243         assertNotNull( mojo );
244 
245         File f = new File( getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt" );
246         FileChannel channel = null;
247         FileLock lock = null;
248         try
249         {
250             channel = new RandomAccessFile( f, "rw" ).getChannel();
251             lock = channel.lock();
252 
253             mojo.execute();
254 
255             fail( "Should fail to delete a file that is locked" );
256         }
257         catch ( MojoExecutionException expected )
258         {
259             assertTrue( true );
260         }
261         finally
262         {
263             if ( lock != null )
264             {
265                 lock.release();
266             }
267 
268             if ( channel != null )
269             {
270                 channel.close();
271             }
272         }
273     }
274 
275     /**
276      * Test the removal of a locked file on Windows systems.
277      * <br/>
278      * Note: Unix systems doesn't lock any files.
279      *
280      * @throws Exception
281      */
282     public void testCleanLockedFileWithNoError()
283         throws Exception
284     {
285         if ( System.getProperty( "os.name" ).toLowerCase().indexOf( "windows" ) == -1 )
286         {
287             assertTrue( "Ignored this test on none Windows based systems", true );
288             return;
289         }
290 
291         String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
292 
293         // safety
294         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/locked-file-test" ),
295                                  new File( getBasedir(), "target/test-classes/unit/locked-file-test" ), null, "**/.svn,**/.svn/**" );
296 
297         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
298         setVariableValueToObject( mojo, "failOnError", Boolean.FALSE );
299         assertNotNull( mojo );
300 
301         File f = new File( getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt" );
302         FileChannel channel = null;
303         FileLock lock = null;
304         try
305         {
306             channel = new RandomAccessFile( f, "rw" ).getChannel();
307             lock = channel.lock();
308 
309             mojo.execute();
310 
311             assertTrue( true );
312         }
313         catch ( MojoExecutionException expected )
314         {
315             fail( "Should display a warning when deleting a file that is locked" );
316         }
317         finally
318         {
319             if ( lock != null )
320             {
321                 lock.release();
322             }
323 
324             if ( channel != null )
325             {
326                 channel.close();
327             }
328         }
329     }
330 
331     /**
332      * @param dir a dir or a file
333      * @return true if a file/dir exists, false otherwise
334      */
335     private boolean checkExists( String dir )
336     {
337         return FileUtils.fileExists( new File( dir ).getAbsolutePath() );
338     }
339 
340     /**
341      * @param dir a directory
342      * @return true if a dir is empty, false otherwise
343      */
344     private boolean checkEmpty( String dir )
345     {
346         return FileUtils.sizeOfDirectory( new File( dir ).getAbsolutePath() ) == 0;
347     }
348 }