1 package org.apache.maven.plugin.clean;
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.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
33
34
35
36
37 public class CleanMojoTest
38 extends AbstractMojoTestCase
39 {
40
41 protected void setUp()
42 throws Exception
43 {
44 super.setUp();
45 }
46
47
48 protected void tearDown()
49 throws Exception
50 {
51 super.tearDown();
52 }
53
54
55
56
57
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
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
83
84
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
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
107
108
109
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
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
136
137
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
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
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
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
171
172
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
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
200
201
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
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
222
223
224
225
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
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
277
278
279
280
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
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
333
334
335 private boolean checkExists( String dir )
336 {
337 return FileUtils.fileExists( new File( dir ).getAbsolutePath() );
338 }
339
340
341
342
343
344 private boolean checkEmpty( String dir )
345 {
346 return FileUtils.sizeOfDirectory( new File( dir ).getAbsolutePath() ) == 0;
347 }
348 }