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$
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         //TODO: MCLEAN-7
164         assertTrue( checkExists( getBasedir() + "/target/test-classes/unit/fileset-clean-test/"
165             + "buildOutputDirectory" ) );
166         assertFalse( checkExists( getBasedir() + "/target/test-classes/fileset-clean-test/"
167             + "buildOutputDirectory/file.txt" ) );
168     }
169 
170     /**
171      * Tests the removal of a directory as file
172      *
173      * @throws Exception
174      */
175     public void testCleanInvalidDirectory()
176         throws Exception
177     {
178         String pluginPom = getBasedir() + "/src/test/resources/unit/invalid-directory-test/plugin-pom.xml";
179 
180         // safety
181         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/invalid-directory-test" ),
182                                  new File( getBasedir(), "target/test-classes/unit/invalid-directory-test" ), null, "**/.svn,**/.svn/**" );
183 
184         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
185         assertNotNull( mojo );
186 
187         try
188         {
189             mojo.execute();
190 
191             fail( "Should fail to delete a file treated as a directory" );
192         }
193         catch ( MojoExecutionException expected )
194         {
195             assertTrue( true );
196         }
197     }
198 
199     /**
200      * Tests the removal of a missing directory
201      *
202      * @throws Exception
203      */
204     public void testMissingDirectory()
205         throws Exception
206     {
207         String pluginPom = getBasedir() + "/src/test/resources/unit/missing-directory-test/plugin-pom.xml";
208 
209         // safety
210         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/missing-directory-test" ),
211                                  new File( getBasedir(), "target/test-classes/unit/missing-directory-test" ), null, "**/.svn,**/.svn/**" );
212 
213         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
214         assertNotNull( mojo );
215 
216         mojo.execute();
217 
218         assertFalse( checkExists( getBasedir() + "/target/test-classes/unit/missing-directory-test/does-not-exist" ) );
219     }
220 
221     /**
222      * Test the removal of a locked file on Windows systems.
223      * <br/>
224      * Note: Unix systems doesn't lock any files.
225      *
226      * @throws Exception
227      */
228     public void testCleanLockedFile()
229         throws Exception
230     {
231         if ( System.getProperty( "os.name" ).toLowerCase().indexOf( "windows" ) == -1 )
232         {
233             assertTrue( "Ignored this test on none Windows based systems", true );
234             return;
235         }
236 
237         String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
238 
239         // safety
240         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/locked-file-test" ),
241                                  new File( getBasedir(), "target/test-classes/unit/locked-file-test" ), null, "**/.svn,**/.svn/**" );
242 
243         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
244         assertNotNull( mojo );
245 
246         File f = new File( getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt" );
247         FileChannel channel = null;
248         FileLock lock = null;
249         try
250         {
251             channel = new RandomAccessFile( f, "rw" ).getChannel();
252             lock = channel.lock();
253 
254             mojo.execute();
255 
256             fail( "Should fail to delete a file that is locked" );
257         }
258         catch ( MojoExecutionException expected )
259         {
260             assertTrue( true );
261         }
262         finally
263         {
264             if ( lock != null )
265             {
266                 lock.release();
267             }
268 
269             if ( channel != null )
270             {
271                 channel.close();
272             }
273         }
274     }
275 
276     /**
277      * Test the removal of a locked file on Windows systems.
278      * <br/>
279      * Note: Unix systems doesn't lock any files.
280      *
281      * @throws Exception
282      */
283     public void testCleanLockedFileWithNoError()
284         throws Exception
285     {
286         if ( System.getProperty( "os.name" ).toLowerCase().indexOf( "windows" ) == -1 )
287         {
288             assertTrue( "Ignored this test on none Windows based systems", true );
289             return;
290         }
291 
292         String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
293 
294         // safety
295         FileUtils.copyDirectory( new File( getBasedir(), "src/test/resources/unit/locked-file-test" ),
296                                  new File( getBasedir(), "target/test-classes/unit/locked-file-test" ), null, "**/.svn,**/.svn/**" );
297 
298         CleanMojo mojo = (CleanMojo) lookupMojo( "clean", pluginPom );
299         setVariableValueToObject( mojo, "failOnError", Boolean.FALSE );
300         assertNotNull( mojo );
301 
302         File f = new File( getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt" );
303         FileChannel channel = null;
304         FileLock lock = null;
305         try
306         {
307             channel = new RandomAccessFile( f, "rw" ).getChannel();
308             lock = channel.lock();
309 
310             mojo.execute();
311 
312             assertTrue( true );
313         }
314         catch ( MojoExecutionException expected )
315         {
316             fail( "Should display a warning when deleting a file that is locked" );
317         }
318         finally
319         {
320             if ( lock != null )
321             {
322                 lock.release();
323             }
324 
325             if ( channel != null )
326             {
327                 channel.close();
328             }
329         }
330     }
331 
332     /**
333      * @param dir a dir or a file
334      * @return true if a file/dir exists, false otherwise
335      */
336     private boolean checkExists( String dir )
337     {
338         return FileUtils.fileExists( new File( dir ).getAbsolutePath() );
339     }
340 
341     /**
342      * @param dir a directory
343      * @return true if a dir is empty, false otherwise
344      */
345     private boolean checkEmpty( String dir )
346     {
347         return FileUtils.sizeOfDirectory( new File( dir ).getAbsolutePath() ) == 0;
348     }
349 }