18
18
*/
19
19
package org .apache .maven .plugins .clean ;
20
20
21
+ import java .io .ByteArrayOutputStream ;
21
22
import java .io .File ;
23
+ import java .io .IOException ;
22
24
import java .io .RandomAccessFile ;
23
25
import java .nio .channels .FileChannel ;
24
26
import java .nio .channels .FileLock ;
27
+ import java .nio .file .Files ;
28
+ import java .nio .file .Path ;
29
+ import java .nio .file .Paths ;
30
+ import java .util .Collections ;
25
31
26
32
import org .apache .maven .plugin .MojoExecutionException ;
27
33
import org .apache .maven .plugin .testing .AbstractMojoTestCase ;
28
34
29
35
import static org .apache .commons .io .FileUtils .copyDirectory ;
36
+ import static org .codehaus .plexus .util .IOUtil .copy ;
30
37
31
38
/**
32
39
* Test the clean mojo.
@@ -205,7 +212,7 @@ public void testMissingDirectory() throws Exception {
205
212
*/
206
213
public void testCleanLockedFile () throws Exception {
207
214
if (!System .getProperty ("os.name" ).toLowerCase ().contains ("windows" )) {
208
- assertTrue ("Ignored this test on none Windows based systems" , true );
215
+ assertTrue ("Ignored this test on non Windows based systems" , true );
209
216
return ;
210
217
}
211
218
@@ -239,7 +246,7 @@ public void testCleanLockedFile() throws Exception {
239
246
*/
240
247
public void testCleanLockedFileWithNoError () throws Exception {
241
248
if (!System .getProperty ("os.name" ).toLowerCase ().contains ("windows" )) {
242
- assertTrue ("Ignored this test on none Windows based systems" , true );
249
+ assertTrue ("Ignore this test on non Windows based systems" , true );
243
250
return ;
244
251
}
245
252
@@ -264,6 +271,90 @@ public void testCleanLockedFileWithNoError() throws Exception {
264
271
}
265
272
}
266
273
274
+ /**
275
+ * Test the followLink option with windows junctions
276
+ * @throws Exception
277
+ */
278
+ public void testFollowLinksWithWindowsJunction () throws Exception {
279
+ if (!System .getProperty ("os.name" ).toLowerCase ().contains ("windows" )) {
280
+ assertTrue ("Ignore this test on non Windows based systems" , true );
281
+ return ;
282
+ }
283
+
284
+ testSymlink ((link , target ) -> {
285
+ Process process = new ProcessBuilder ()
286
+ .directory (link .getParent ().toFile ())
287
+ .command ("cmd" , "/c" , "mklink" , "/j" , link .getFileName ().toString (), target .toString ())
288
+ .start ();
289
+ process .waitFor ();
290
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
291
+ copy (process .getInputStream (), baos );
292
+ copy (process .getErrorStream (), baos );
293
+ if (!Files .exists (link )) {
294
+ throw new IOException ("Unable to create junction: " + baos );
295
+ }
296
+ });
297
+ }
298
+
299
+ /**
300
+ * Test the followLink option with sym link
301
+ * @throws Exception
302
+ */
303
+ public void testFollowLinksWithSymLinkOnPosix () throws Exception {
304
+ if (System .getProperty ("os.name" ).toLowerCase ().contains ("windows" )) {
305
+ assertTrue ("Ignore this test on Windows based systems" , true );
306
+ return ;
307
+ }
308
+
309
+ testSymlink ((link , target ) -> {
310
+ try {
311
+ Files .createSymbolicLink (link , target );
312
+ } catch (IOException e ) {
313
+ throw new IOException ("Unable to create symbolic link" , e );
314
+ }
315
+ });
316
+ }
317
+
318
+ @ FunctionalInterface
319
+ interface LinkCreator {
320
+ void createLink (Path link , Path target ) throws Exception ;
321
+ }
322
+
323
+ private void testSymlink (LinkCreator linkCreator ) throws Exception {
324
+ Cleaner cleaner = new Cleaner (null , null , false , null , null );
325
+ Path testDir = Paths .get ("target/test-classes/unit/test-dir" ).toAbsolutePath ();
326
+ Path dirWithLnk = testDir .resolve ("dir" );
327
+ Path orgDir = testDir .resolve ("org-dir" );
328
+ Path jctDir = dirWithLnk .resolve ("jct-dir" );
329
+ Path file = orgDir .resolve ("file.txt" );
330
+
331
+ // create directories, links and file
332
+ Files .createDirectories (dirWithLnk );
333
+ Files .createDirectories (orgDir );
334
+ Files .write (file , Collections .singleton ("Hello world" ));
335
+ linkCreator .createLink (jctDir , orgDir );
336
+ // delete
337
+ cleaner .delete (dirWithLnk .toFile (), null , false , true , false );
338
+ // verify
339
+ assertTrue (Files .exists (file ));
340
+ assertFalse (Files .exists (jctDir ));
341
+ assertTrue (Files .exists (orgDir ));
342
+ assertFalse (Files .exists (dirWithLnk ));
343
+
344
+ // create directories, links and file
345
+ Files .createDirectories (dirWithLnk );
346
+ Files .createDirectories (orgDir );
347
+ Files .write (file , Collections .singleton ("Hello world" ));
348
+ linkCreator .createLink (jctDir , orgDir );
349
+ // delete
350
+ cleaner .delete (dirWithLnk .toFile (), null , true , true , false );
351
+ // verify
352
+ assertFalse (Files .exists (file ));
353
+ assertFalse (Files .exists (jctDir ));
354
+ assertTrue (Files .exists (orgDir ));
355
+ assertFalse (Files .exists (dirWithLnk ));
356
+ }
357
+
267
358
/**
268
359
* @param dir a dir or a file
269
360
* @return true if a file/dir exists, false otherwise
0 commit comments