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
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
172
173
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
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
201
202
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
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
223
224
225
226
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
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
278
279
280
281
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
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
334
335
336 private boolean checkExists( String dir )
337 {
338 return FileUtils.fileExists( new File( dir ).getAbsolutePath() );
339 }
340
341
342
343
344
345 private boolean checkEmpty( String dir )
346 {
347 return FileUtils.sizeOfDirectory( new File( dir ).getAbsolutePath() ) == 0;
348 }
349 }