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