View Javadoc
1   package org.apache.maven.shared.utils.io;
2   
3   import static org.hamcrest.CoreMatchers.containsString;
4   import static org.hamcrest.CoreMatchers.hasItems;
5   import static org.hamcrest.CoreMatchers.is;
6   import static org.hamcrest.CoreMatchers.not;
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.junit.Assert.assertEquals;
9   import static org.junit.Assert.assertFalse;
10  import static org.junit.Assert.assertTrue;
11  import static org.junit.Assert.fail;
12  import static org.junit.Assume.assumeFalse;
13  import static org.junit.Assume.assumeThat;
14  
15  import org.apache.commons.io.IOUtils;
16  import org.apache.maven.shared.utils.Os;
17  import org.apache.maven.shared.utils.testhelpers.FileTestHelper;
18  import org.codehaus.plexus.util.InterpolationFilterReader;
19  import org.hamcrest.CoreMatchers;
20  import org.junit.Before;
21  import org.junit.Ignore;
22  import org.junit.Rule;
23  import org.junit.Test;
24  import org.junit.rules.TemporaryFolder;
25  import org.junit.rules.TestName;
26  
27  import javax.annotation.Nonnull;
28  import java.io.BufferedOutputStream;
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileNotFoundException;
32  import java.io.FileOutputStream;
33  import java.io.FileReader;
34  import java.io.FileWriter;
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.io.OutputStream;
38  import java.io.Reader;
39  import java.io.Writer;
40  import java.net.URL;
41  import java.nio.file.Files;
42  import java.util.Arrays;
43  import java.util.HashMap;
44  import java.util.HashSet;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.concurrent.TimeUnit;
48  
49  /*
50   * Licensed to the Apache Software Foundation (ASF) under one
51   * or more contributor license agreements.  See the NOTICE file
52   * distributed with this work for additional information
53   * regarding copyright ownership.  The ASF licenses this file
54   * to you under the Apache License, Version 2.0 (the
55   * "License"); you may not use this file except in compliance
56   * with the License.  You may obtain a copy of the License at
57   *
58   *  http://www.apache.org/licenses/LICENSE-2.0
59   *
60   * Unless required by applicable law or agreed to in writing,
61   * software distributed under the License is distributed on an
62   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
63   * KIND, either express or implied.  See the License for the
64   * specific language governing permissions and limitations
65   * under the License.
66   */
67  
68  /**
69   * This is used to test FileUtils for correctness.
70   *
71   * @author Peter Donald
72   * @author Matthew Hawthorne
73   * @author Stephen Colebourne
74   * @author Jim Harrington
75   * @version $Id: FileUtilsTestCase.java 1081025 2011-03-13 00:45:10Z niallp $
76   * @see FileUtils
77   */
78  @SuppressWarnings( "deprecation" )
79  public class FileUtilsTest
80  {
81  
82      // Test data
83  
84      @Rule
85      public TemporaryFolder tempFolder = new TemporaryFolder();
86  
87      @Rule
88      public TestName name = new TestName();
89  
90      /**
91       * Size of test directory.
92       */
93      private static final int TEST_DIRECTORY_SIZE = 0;
94  
95      private File testFile1;
96  
97      private File testFile2;
98  
99      private long testFile1Size;
100 
101     private long testFile2Size;
102 
103     /**
104      * @see junit.framework.TestCase#setUp()
105      */
106     @Before
107     public void setUp()
108         throws Exception
109     {
110         testFile1 = tempFolder.newFile( "file1-test.txt" );
111         testFile2 = tempFolder.newFile( "file1a-test.txt" );
112 
113         testFile1Size = (int) testFile1.length();
114         testFile2Size = (int) testFile2.length();
115 
116         tempFolder.getRoot().mkdirs();
117         createFile( testFile1, testFile1Size );
118         createFile( testFile2, testFile2Size );
119         FileUtils.deleteDirectory( tempFolder.getRoot() );
120         tempFolder.getRoot().mkdirs();
121         createFile( testFile1, testFile1Size );
122         createFile( testFile2, testFile2Size );
123     }
124 
125     private static void createFile( File file, long size )
126         throws IOException
127     {
128         if ( !file.getParentFile().exists() )
129         {
130             throw new IOException( "Cannot create file " + file + " as the parent directory does not exist" );
131         }
132         
133         try (OutputStream out = new BufferedOutputStream( new FileOutputStream( file ) ) )
134         {
135             FileTestHelper.generateTestData( out, size );
136         }
137     }
138 
139 
140     /**
141      * Assert that the content of a file is equal to that in a byte[].
142      */
143     private void assertEqualContent( byte[] b0, File file )
144         throws IOException
145     {
146         int count = 0, numRead = 0;
147         byte[] b1 = new byte[b0.length];
148         try ( InputStream is = new FileInputStream( file ) )
149         {
150             while ( count < b0.length && numRead >= 0 )
151             {
152                 numRead = is.read( b1, count, b0.length );
153                 count += numRead;
154             }
155             assertThat( "Different number of bytes: ", count, is( b0.length ) );
156             for ( int i = 0; i < count; i++ )
157             {
158                 assertEquals( "byte " + i + " differs", b1[i], b0[i] );
159             }
160         }
161     }
162 
163     private void deleteFile( File file )
164     {
165         if ( file.exists() )
166         {
167             assertTrue( "Couldn't delete file: " + file, file.delete() );
168         }
169     }
170 
171 
172     //-----------------------------------------------------------------------
173     @Test
174     public void toFile1()
175         throws Exception
176     {
177         URL url = new URL( "file", null, "a/b/c/file.txt" );
178         File file = FileUtils.toFile( url );
179         assertThat( file.toString(), containsString( "file.txt" ) );
180     }
181 
182     @Test
183     public void toFile2()
184         throws Exception
185     {
186         URL url = new URL( "file", null, "a/b/c/file%20n%61me%2520.tx%74" );
187         File file = FileUtils.toFile( url );
188         assertThat( file.toString(), containsString( "file name%20.txt" ) );
189     }
190 
191     @Test
192     public void toFile3()
193         throws Exception
194     {
195         assertThat( FileUtils.toFile( null ), CoreMatchers.nullValue() );
196         assertThat( FileUtils.toFile( new URL( "http://jakarta.apache.org" ) ), CoreMatchers.nullValue() );
197     }
198 
199     @Test( expected = NumberFormatException.class )
200     public void toFile4()
201         throws Exception
202     {
203         URL url = new URL( "file", null, "a/b/c/file%%20%me.txt%" );
204         File file = FileUtils.toFile( url );
205         assertThat( file.toString(), containsString( "file% %me.txt%" ) );
206     }
207 
208     /**
209      * IO-252
210      */
211     @Test
212     public void toFile5()
213         throws Exception
214     {
215         URL url = new URL( "file", null, "both%20are%20100%20%25%20true" );
216         File file = FileUtils.toFile( url );
217         assertThat( file.toString(), is( "both are 100 % true" ) );
218     }
219 
220     @Test
221     public void toFileUtf8()
222         throws Exception
223     {
224         URL url = new URL( "file", null, "/home/%C3%A4%C3%B6%C3%BC%C3%9F" );
225         File file = FileUtils.toFile( url );
226         assertThat( file.toString(), not( containsString( "\u00E4\u00F6\u00FC\u00DF" ) ) );
227     }
228 
229     // toURLs
230 
231     @Test
232     public void toURLs1()
233         throws Exception
234     {
235         File[] files = new File[]{ new File( tempFolder.getRoot(), "file1.txt" ), new File( tempFolder.getRoot(), "file2.txt" ),
236             new File( tempFolder.getRoot(), "test file.txt" ), };
237         URL[] urls = FileUtils.toURLs( files );
238 
239         assertThat( urls.length, is( files.length ) );
240         assertThat( urls[0].toExternalForm().startsWith( "file:" ), is( true ) );
241         assertThat( urls[0].toExternalForm().contains( "file1.txt" ), is( true ) );
242         assertThat( urls[1].toExternalForm().startsWith( "file:" ), is( true ) );
243         assertThat( urls[1].toExternalForm(), containsString( "file2.txt" ) );
244 
245         // Test escaped char
246         assertThat( urls[2].toExternalForm().startsWith( "file:" ), is( true ) );
247         assertThat( urls[2].toExternalForm(), containsString( "test%20file.txt" ) );
248     }
249 
250     // contentEquals
251 
252     @Test
253     public void contentEquals()
254         throws Exception
255     {
256         // Non-existent files
257         File file = new File( tempFolder.getRoot(), name.getMethodName() );
258         File file2 = new File( tempFolder.getRoot(), name.getMethodName() + "2" );
259         // both don't  exist
260         assertThat( FileUtils.contentEquals( file, file ), is( true ) );
261         assertThat( FileUtils.contentEquals( file, file2 ), is( true ) );
262         assertThat( FileUtils.contentEquals( file2, file2 ), is( true ) );
263         assertThat( FileUtils.contentEquals( file2, file ), is( true ) );
264 
265         // Directories
266         FileUtils.contentEquals( tempFolder.getRoot(), tempFolder.getRoot() );
267 
268         // Different files
269         File objFile1 = new File( tempFolder.getRoot(), name.getMethodName() + ".object" );
270         objFile1.deleteOnExit();
271         FileUtils.copyURLToFile( getClass().getResource( "/java/lang/Object.class" ), objFile1 );
272 
273         File objFile1b = new File( tempFolder.getRoot(), name.getMethodName() + ".object2" );
274         objFile1.deleteOnExit();
275         FileUtils.copyURLToFile( getClass().getResource( "/java/lang/Object.class" ), objFile1b );
276 
277         File objFile2 = new File( tempFolder.getRoot(), name.getMethodName() + ".collection" );
278         objFile2.deleteOnExit();
279         FileUtils.copyURLToFile( getClass().getResource( "/java/util/Collection.class" ), objFile2 );
280 
281         assertThat( FileUtils.contentEquals( objFile1, objFile2 ), is( false ) );
282         assertThat( FileUtils.contentEquals( objFile1b, objFile2 ), is( false ) );
283         assertThat( FileUtils.contentEquals( objFile1, objFile1b ), is( true ) );
284 
285         assertThat( FileUtils.contentEquals( objFile1, objFile1 ), is( true ) );
286         assertThat( FileUtils.contentEquals( objFile1b, objFile1b ), is( true ) );
287         assertThat( FileUtils.contentEquals( objFile2, objFile2 ), is( true ) );
288 
289         // Equal files
290         file.createNewFile();
291         file2.createNewFile();
292         assertThat( FileUtils.contentEquals( file, file ), is( true ) );
293         assertThat( FileUtils.contentEquals( file, file2 ), is( true ) );
294     }
295 
296     // copyURLToFile
297 
298     @Test
299     public void copyURLToFile()
300         throws Exception
301     {
302         // Creates file
303         File file = new File( tempFolder.getRoot(), name.getMethodName() );
304         file.deleteOnExit();
305 
306         // Loads resource
307         String resourceName = "/java/lang/Object.class";
308         FileUtils.copyURLToFile( getClass().getResource( resourceName ), file );
309 
310         // Tests that resource was copied correctly
311         try ( FileInputStream fis = new FileInputStream( file ) )
312         {
313             assertThat( "Content is not equal.",
314                         IOUtil.contentEquals( getClass().getResourceAsStream( resourceName ), fis ), is( true ) );
315         }
316         //TODO Maybe test copy to itself like for copyFile()
317     }
318 
319     // forceMkdir
320 
321     @Test
322     public void forceMkdir()
323         throws Exception
324     {
325         // Tests with existing directory
326         FileUtils.forceMkdir( tempFolder.getRoot() );
327 
328         // Creates test file
329         File testFile = new File( tempFolder.getRoot(), name.getMethodName() );
330         testFile.deleteOnExit();
331         testFile.createNewFile();
332         assertThat( "Test file does not exist.", testFile.exists(), is( true ) );
333 
334         // Tests with existing file
335         try
336         {
337             FileUtils.forceMkdir( testFile );
338             fail( "Exception expected." );
339         }
340         catch ( IOException ex )
341         {
342         }
343 
344         testFile.delete();
345 
346         // Tests with non-existent directory
347         FileUtils.forceMkdir( testFile );
348         assertThat( "Directory was not created.", testFile.exists(), is( true ) );
349     }
350 
351     // sizeOfDirectory
352 
353     @Test
354     public void sizeOfDirectory()
355         throws Exception
356     {
357         File file = new File( tempFolder.getRoot(), name.getMethodName() );
358 
359         // Non-existent file
360         try
361         {
362             FileUtils.sizeOfDirectory( file );
363             fail( "Exception expected." );
364         }
365         catch ( IllegalArgumentException ex )
366         {
367         }
368 
369         // Creates file
370         file.createNewFile();
371         file.deleteOnExit();
372 
373         // Existing file
374         try
375         {
376             FileUtils.sizeOfDirectory( file );
377             fail( "Exception expected." );
378         }
379         catch ( IllegalArgumentException ex )
380         {
381         }
382 
383         // Existing directory
384         file.delete();
385         file.mkdir();
386 
387         assertThat( "Unexpected directory size", FileUtils.sizeOfDirectory( file ), is( (long) TEST_DIRECTORY_SIZE ) );
388     }
389 
390     // copyFile
391 
392     @Test
393     public void copyFile1()
394         throws Exception
395     {
396         File destination = new File( tempFolder.getRoot(), "copy1.txt" );
397 
398         //Thread.sleep(LAST_MODIFIED_DELAY);
399         //This is to slow things down so we can catch if
400         //the lastModified date is not ok
401 
402         FileUtils.copyFile( testFile1, destination );
403         assertThat( "Check Exist", destination.exists(), is( true ) );
404         assertThat( "Check Full copy", destination.length(), is( testFile1Size ) );
405         /* disabled: Thread.sleep doesn't work reliantly for this case
406         assertTrue("Check last modified date preserved",
407             testFile1.lastModified() == destination.lastModified());*/
408     }
409 
410     /** A time today, rounded down to the previous minute */
411     private static long MODIFIED_TODAY = (System.currentTimeMillis() / TimeUnit.MINUTES.toMillis( 1 )) * TimeUnit.MINUTES.toMillis( 1 );
412 
413     /** A time yesterday, rounded down to the previous minute */
414     private static long MODIFIED_YESTERDAY = MODIFIED_TODAY - TimeUnit.DAYS.toMillis( 1 );
415 
416     /** A time last week, rounded down to the previous minute */
417     private static long MODIFIED_LAST_WEEK = MODIFIED_TODAY - TimeUnit.DAYS.toMillis( 7 );
418 
419     @Test
420     public void copyFileWithNoFiltersAndNoDestination()
421         throws Exception
422     {
423         File from = write(
424             "from.txt",
425             MODIFIED_YESTERDAY,
426             "Hello World!"
427         );
428         File to = new File(
429             tempFolder.getRoot(),
430             "to.txt"
431         );
432 
433         FileUtils.copyFile( from, to, null, ( FileUtils.FilterWrapper[] ) null);
434 
435         assertTrue(
436             "to.txt did not exist so should have been written",
437             to.lastModified() >= MODIFIED_TODAY
438         );
439         assertFileContent( to, "Hello World!" );
440     }
441 
442     @Test
443     public void copyFileWithNoFiltersAndOutdatedDestination()
444             throws Exception
445     {
446         File from = write(
447             "from.txt",
448             MODIFIED_YESTERDAY,
449             "Hello World!"
450         );
451         File to = write(
452             "to.txt",
453             MODIFIED_LAST_WEEK,
454             "Older content"
455         );
456 
457         FileUtils.copyFile( from, to, null, ( FileUtils.FilterWrapper[] ) null);
458 
459         assertTrue(
460             "to.txt was outdated so should have been overwritten",
461             to.lastModified() >= MODIFIED_TODAY
462         );
463         assertFileContent( to, "Hello World!" );
464     }
465 
466     @Test
467     public void copyFileWithNoFiltersAndNewerDestination()
468             throws Exception
469     {
470         File from = write(
471             "from.txt",
472             MODIFIED_LAST_WEEK,
473             "Hello World!"
474         );
475         File to = write(
476             "to.txt",
477             MODIFIED_YESTERDAY,
478             "Older content"
479         );
480 
481         FileUtils.copyFile( from, to, null, ( FileUtils.FilterWrapper[] ) null);
482 
483         assertTrue(
484             "to.txt was newer so should have been left alone",
485             to.lastModified() < MODIFIED_TODAY
486         );
487         assertFileContent( to, "Older content" );
488     }
489 
490     @Test
491     public void copyFileWithNoFiltersAndNewerDestinationButForcedOverwrite()
492             throws Exception
493     {
494         File from = write(
495             "from.txt",
496             MODIFIED_LAST_WEEK,
497             "Hello World!"
498         );
499         File to = write(
500             "to.txt",
501             MODIFIED_YESTERDAY,
502             "Older content"
503         );
504 
505         FileUtils.copyFile( from, to, null, null, true);
506 
507         assertTrue(
508             "to.txt was newer but the overwrite should have been forced",
509             to.lastModified() >= MODIFIED_TODAY
510         );
511         assertFileContent( to, "Hello World!" );
512     }
513 
514     @Test
515     public void copyFileWithFilteringButNoFilters()
516             throws Exception
517     {
518         File from = write(
519             "from.txt",
520             MODIFIED_YESTERDAY,
521             "Hello ${name}!"
522         );
523         File to = write(
524             "to.txt",
525             MODIFIED_LAST_WEEK,
526             "Older content"
527         );
528 
529         FileUtils.copyFile( from, to, null );
530 
531         assertTrue(
532             "to.txt was outdated so should have been overwritten",
533             to.lastModified() >= MODIFIED_TODAY
534         );
535         assertFileContent( to, "Hello ${name}!" );
536     }
537 
538     @Test
539     public void copyFileWithFilteringAndNoDestination()
540             throws Exception
541     {
542         File from = write(
543             "from.txt",
544             MODIFIED_YESTERDAY,
545             "Hello ${name}!"
546         );
547         File to = new File(
548             tempFolder.getRoot(),
549             "to.txt"
550         );
551 
552         FileUtils.copyFile( from, to, null, wrappers( "name", "Bob" ) );
553 
554         assertTrue(
555             "to.txt did not exist so should have been written",
556             to.lastModified() >= MODIFIED_TODAY
557         );
558         assertFileContent( to, "Hello Bob!" );
559     }
560 
561     @Test
562     public void copyFileWithFilteringAndOutdatedDestination()
563             throws Exception
564     {
565         File from = write(
566             "from.txt",
567             MODIFIED_YESTERDAY,
568             "Hello ${name}!"
569         );
570         File to = write(
571             "to.txt",
572             MODIFIED_LAST_WEEK,
573             "Older content"
574         );
575 
576         FileUtils.copyFile( from, to, null, wrappers( "name", "Bob" ) );
577 
578         assertTrue(
579             "to.txt was outdated so should have been overwritten",
580             to.lastModified() >= MODIFIED_TODAY
581         );
582         assertFileContent( to, "Hello Bob!" );
583     }
584 
585     @Test
586     public void copyFileWithFilteringAndNewerDestinationButForcedOverwrite()
587             throws Exception
588     {
589         File from = write(
590             "from.txt",
591             MODIFIED_LAST_WEEK,
592             "Hello ${name}!"
593         );
594         File to = write(
595             "to.txt",
596             MODIFIED_YESTERDAY,
597             "Older content"
598         );
599 
600         FileUtils.copyFile( from, to, null, wrappers( "name", "Bob" ), true );
601 
602         assertTrue(
603             "to.txt was newer but the overwrite should have been forced",
604             to.lastModified() >= MODIFIED_TODAY
605         );
606         assertFileContent( to, "Hello Bob!" );
607     }
608 
609     @Test
610     public void copyFileWithFilteringAndNewerDestinationButModifiedContent()
611             throws Exception
612     {
613         File from = write(
614             "from.txt",
615             MODIFIED_LAST_WEEK,
616             "Hello ${name}!"
617         );
618         File to = write(
619             "to.txt",
620             MODIFIED_YESTERDAY,
621             "Hello Charlie!"
622         );
623 
624         FileUtils.copyFile( from, to, null, wrappers( "name", "Bob" ) );
625 
626         assertTrue(
627             "to.txt was outdated so should have been overwritten",
628             to.lastModified() >= MODIFIED_TODAY
629         );
630         assertFileContent( to, "Hello Bob!" );
631     }
632 
633     @Test
634     public void copyFileWithFilteringAndNewerDestinationAndMatchingContent()
635             throws Exception
636     {
637         File from = write(
638             "from.txt",
639             MODIFIED_LAST_WEEK,
640             "Hello ${name}!"
641         );
642         File to = write(
643             "to.txt",
644             MODIFIED_YESTERDAY,
645             "Hello Bob!"
646         );
647 
648         FileUtils.copyFile( from, to, null, wrappers( "name", "Bob" ) );
649 
650         assertFileContent( to, "Hello Bob!" );
651         assertTrue(
652             "to.txt content should be unchanged and have been left alone",
653             to.lastModified() < MODIFIED_TODAY
654         );
655     }
656 
657     private static FileUtils.FilterWrapper[] wrappers( String key, String value )
658     {
659         final Map<String, Object> map = new HashMap<>();
660         map.put( key, value );
661         return new FileUtils.FilterWrapper[]
662             {
663                 new FileUtils.FilterWrapper()
664                 {
665                     @Override
666                     public Reader getReader( Reader reader )
667                     {
668                         return new InterpolationFilterReader( reader, map );
669                     }
670                 }
671             };
672     }
673 
674     private File write( @Nonnull String name, long lastModified, @Nonnull String text) throws IOException
675     {
676         final File file = new File( tempFolder.getRoot(), name );
677         try ( final Writer writer = new FileWriter( file ) ) {
678             writer.write(text);
679         }
680         assertTrue( file.setLastModified( lastModified ) );
681         assertEquals( "Failed to set lastModified date on " + file.getPath(), lastModified, file.lastModified() );
682         return file;
683     }
684 
685     private static void assertFileContent( @Nonnull File file, @Nonnull String expected ) throws IOException
686     {
687         try ( Reader in = new FileReader( file ))
688         {
689             assertEquals(
690                 "Expected " + file.getPath() + " to contain: " + expected,
691                 expected,
692                 IOUtils.toString( in )
693             );
694         }
695     }
696 
697     @Test
698     public void copyFileThatIsSymlink()
699             throws Exception
700     {
701         assumeFalse( Os.isFamily( Os.FAMILY_WINDOWS ) );
702 
703         File destination = new File( tempFolder.getRoot(), "symCopy.txt" );
704 
705         File testDir = SymlinkTestSetup.createStandardSymlinkTestDir( new File( "target/test/symlinkCopy" ) );
706 
707         FileUtils.copyFile( new File( testDir, "symR" ), destination );
708 
709         assertTrue( Files.isSymbolicLink( destination.toPath() ) );
710     }
711 
712 
713     @Test
714     public void deleteFile()
715         throws Exception
716     {
717         File destination = new File( tempFolder.getRoot(), "copy1.txt" );
718         FileUtils.copyFile( testFile1, destination );
719         FileUtils.delete( destination );
720         assertThat( "Check Exist", destination.exists(), is( false ) );
721     }
722 
723     @Test(expected = IOException.class)
724     public void deleteFileNofile()
725         throws Exception
726     {
727         File destination = new File( "abc/cde" );
728         FileUtils.delete( destination );
729     }
730 
731     @Test
732     public void deleteFileLegacy()
733         throws Exception
734     {
735         File destination = new File( tempFolder.getRoot(), "copy1.txt" );
736         FileUtils.copyFile( testFile1, destination );
737         assertTrue( FileUtils.deleteLegacyStyle( destination ) );
738     }
739 
740     @Test
741     public void deleteFileLegacyNofile()
742         throws Exception
743     {
744         File destination = new File( "abc/cde" );
745         assertFalse( FileUtils.deleteLegacyStyle( destination ) );
746     }
747 
748     @Test
749     public void copyFileWithPermissions()
750         throws Exception
751     {
752         File source = new File( "src/test/resources/executable" );
753         source.setExecutable( true );
754         assumeThat( "Need an existing file to copy", source.exists(), is( true ) );
755         assumeThat( "Need an executable file to copy", source.canExecute(), is( true ) );
756 
757         File destination = new File( tempFolder.getRoot(), "executable-copy" );
758 
759         FileUtils.copyFile( source, destination );
760 
761         assertThat( "destination not exists: " + destination.getAbsolutePath()
762                         + ", directory content: " + Arrays.asList( destination.getParentFile().list() ),
763                     Files.exists( destination.toPath() ), is( true ) );
764 
765         assertThat( "Check copy executable", destination.canExecute(), is( true ) );
766     }
767 
768     @Test
769     public void copyFile2()
770         throws Exception
771     {
772         File destination = new File( tempFolder.getRoot(), "copy2.txt" );
773 
774         //Thread.sleep(LAST_MODIFIED_DELAY);
775         //This is to slow things down so we can catch if
776         //the lastModified date is not ok
777 
778         FileUtils.copyFile( testFile1, destination );
779         assertThat( "Check Exist", destination.exists(), is( true ) );
780         assertThat( "Check Full copy", destination.length(), is( testFile2Size ) );
781         /* disabled: Thread.sleep doesn't work reliably for this case
782         assertTrue("Check last modified date preserved",
783             testFile1.lastModified() == destination.lastModified());*/
784     }
785 
786     @Test
787     public void copyToSelf() throws IOException
788     {
789         File destination = new File( tempFolder.getRoot(), "copy3.txt" );
790         //Prepare a test file
791         FileUtils.copyFile( testFile1, destination );
792 
793         FileUtils.copyFile( destination, destination );
794     }
795 
796     @Test
797     public void copyDirectoryToNonExistingDest()
798         throws Exception
799     {
800         createFile( testFile1, 1234 );
801         createFile( testFile2, 4321 );
802         File srcDir = tempFolder.getRoot();
803         File subDir = new File( srcDir, "sub" );
804         subDir.mkdir();
805         File subFile = new File( subDir, "A.txt" );
806         FileUtils.fileWrite( subFile, "UTF8", "HELLO WORLD" );
807         File destDir = new File( System.getProperty( "java.io.tmpdir" ), "tmp-FileUtilsTestCase" );
808         FileUtils.deleteDirectory( destDir );
809 
810         FileUtils.copyDirectory( srcDir, destDir );
811 
812         assertTrue( destDir.exists() );
813         assertEquals( FileUtils.sizeOfDirectory( destDir ), FileUtils.sizeOfDirectory( srcDir ) );
814         assertTrue( new File( destDir, "sub/A.txt" ).exists() );
815         FileUtils.deleteDirectory( destDir );
816     }
817 
818     @Test
819     public void copyDirectoryToExistingDest() throws IOException
820     {
821         createFile( testFile1, 1234 );
822         createFile( testFile2, 4321 );
823         File srcDir = tempFolder.getRoot();
824         File subDir = new File( srcDir, "sub" );
825         assertTrue( subDir.mkdir() );
826         File subFile = new File( subDir, "A.txt" );
827         FileUtils.fileWrite( subFile, "UTF8", "HELLO WORLD" );
828         File destDir = new File( System.getProperty( "java.io.tmpdir" ), "tmp-FileUtilsTestCase" );
829         FileUtils.deleteDirectory( destDir );
830         assertTrue ( destDir.mkdirs() );
831 
832         FileUtils.copyDirectory( srcDir, destDir );
833 
834         assertEquals( FileUtils.sizeOfDirectory( destDir ), FileUtils.sizeOfDirectory( srcDir ) );
835         assertTrue( new File( destDir, "sub/A.txt" ).exists() );
836     }
837 
838     @Test
839     public void copyDirectoryErrors_nullDestination() throws IOException {
840         try
841         {
842             FileUtils.copyDirectory( new File( "a" ), null );
843             fail();
844         }
845         catch ( NullPointerException ex )
846         {
847         }
848     }
849 
850     @Test
851     public void copyDirectoryErrors_copyToSelf() {
852         try
853         {
854             FileUtils.copyDirectory( tempFolder.getRoot(), tempFolder.getRoot() );
855             fail();
856         }
857         catch ( IOException ex )
858         {
859         }
860     }
861 
862     @Test
863     public void copyDirectoryErrors()
864         throws IOException
865     {
866         try
867         {
868             FileUtils.copyDirectory( null, null );
869             fail();
870         }
871         catch ( NullPointerException ex )
872         {
873         }
874         try
875         {
876             FileUtils.copyDirectory( null, new File( "a" ) );
877             fail();
878         }
879         catch ( NullPointerException ex )
880         {
881         }
882         try
883         {
884             FileUtils.copyDirectory( tempFolder.getRoot(), testFile1 );
885             fail();
886         }
887         catch ( IOException ex )
888         {
889         }
890     }
891 
892     // forceDelete
893 
894     @Test
895     public void forceDeleteAFile1()
896         throws Exception
897     {
898         File destination = new File( tempFolder.getRoot(), "copy1.txt" );
899         destination.createNewFile();
900         assertTrue( "Copy1.txt doesn't exist to delete", destination.exists() );
901         FileUtils.forceDelete( destination );
902         assertFalse( destination.exists() );
903     }
904 
905     @Test
906     public void forceDeleteAFile2()
907         throws Exception
908     {
909         File destination = new File( tempFolder.getRoot(), "copy2.txt" );
910         destination.createNewFile();
911         assertThat( "Copy2.txt doesn't exist to delete", destination.exists(), is( true ) );
912         FileUtils.forceDelete( destination );
913         assertThat( "Check No Exist", !destination.exists(), is( true ) );
914     }
915 
916     @Test
917     @Ignore( "Commons test case that is failing for plexus" )
918     public void forceDeleteAFile3()
919         throws Exception
920     {
921         File destination = new File( tempFolder.getRoot(), "no_such_file" );
922         assertThat( "Check No Exist", !destination.exists(), is( true ) );
923         try
924         {
925             FileUtils.forceDelete( destination );
926             fail( "Should generate FileNotFoundException" );
927         }
928         catch ( FileNotFoundException ignored )
929         {
930         }
931     }
932 
933     // copyFileToDirectory
934 
935     @Test
936     @Ignore( "Commons test case that is failing for plexus" )
937     public void copyFile1ToDir()
938         throws Exception
939     {
940         File directory = new File( tempFolder.getRoot(), "subdir" );
941         if ( !directory.exists() )
942         {
943             directory.mkdirs();
944         }
945         File destination = new File( directory, testFile1.getName() );
946 
947         //Thread.sleep(LAST_MODIFIED_DELAY);
948         //This is to slow things down so we can catch if
949         //the lastModified date is not ok
950 
951         FileUtils.copyFileToDirectory( testFile1, directory );
952         assertThat( "Check Exist", destination.exists(), is( true ) );
953         assertThat( "Check Full copy", destination.length(), is( testFile1Size ) );
954         /* disabled: Thread.sleep doesn't work reliantly for this case
955         assertTrue("Check last modified date preserved",
956             testFile1.lastModified() == destination.lastModified());*/
957 
958         try
959         {
960             FileUtils.copyFileToDirectory( destination, directory );
961             fail( "Should not be able to copy a file into the same directory as itself" );
962         }
963         catch ( IOException ioe )
964         {
965             //we want that, cannot copy to the same directory as the original file
966         }
967     }
968 
969     @Test
970     public void copyFile2ToDir()
971         throws Exception
972     {
973         File directory = new File( tempFolder.getRoot(), "subdir" );
974         if ( !directory.exists() )
975         {
976             directory.mkdirs();
977         }
978         File destination = new File( directory, testFile1.getName() );
979 
980         //Thread.sleep(LAST_MODIFIED_DELAY);
981         //This is to slow things down so we can catch if
982         //the lastModified date is not ok
983 
984         FileUtils.copyFileToDirectory( testFile1, directory );
985         assertThat( "Check Exist", destination.exists(), is( true ) );
986         assertThat( "Check Full copy", destination.length(), is( testFile2Size ) );
987         /* disabled: Thread.sleep doesn't work reliantly for this case
988         assertTrue("Check last modified date preserved",
989             testFile1.lastModified() == destination.lastModified());*/
990     }
991 
992     // forceDelete
993 
994     @Test
995     public void forceDeleteDir()
996         throws Exception
997     {
998         File testDirectory = tempFolder.newFolder( name.getMethodName() );
999         FileUtils.forceDelete( testDirectory.getParentFile() );
1000         assertThat( "Check No Exist", !testDirectory.getParentFile().exists(), is( true ) );
1001     }
1002 
1003     /**
1004      * Test the FileUtils implementation.
1005      */
1006     @Test
1007     public void fileUtils()
1008         throws Exception
1009     {
1010         // Loads file from classpath
1011         File file1 = new File( tempFolder.getRoot(), "test.txt" );
1012         String filename = file1.getAbsolutePath();
1013 
1014         //Create test file on-the-fly (used to be in CVS)
1015         try ( OutputStream out = new java.io.FileOutputStream( file1 ) )
1016         {
1017             out.write( "This is a test".getBytes( "UTF-8" ) );
1018         }
1019 
1020         File file2 = new File( tempFolder.getRoot(), "test2.txt" );
1021 
1022         FileUtils.fileWrite( file2, "UTF-8", filename );
1023         assertThat( file2.exists(), is( true ) );
1024         assertThat( file2.length() > 0, is( true ) );
1025 
1026         String file2contents = FileUtils.fileRead( file2, "UTF-8" );
1027         assertThat( "Second file's contents correct", filename.equals( file2contents ), is( true ) );
1028 
1029         assertThat( file2.delete(), is( true ) );
1030 
1031         String contents = FileUtils.fileRead( new File( filename ), "UTF-8" );
1032         assertThat( "FileUtils.fileRead()", contents.equals( "This is a test" ), is( true ) );
1033 
1034     }
1035 
1036     @Test
1037     public void fileReadWithDefaultEncoding()
1038         throws Exception
1039     {
1040         File file = new File( tempFolder.getRoot(), "read.obj" );
1041         FileOutputStream out = new FileOutputStream( file );
1042         byte[] text = "Hello /u1234".getBytes();
1043         out.write( text );
1044         out.close();
1045 
1046         String data = FileUtils.fileRead( file );
1047         assertThat( data, is( "Hello /u1234" ) );
1048     }
1049 
1050     @Test
1051     public void fileReadWithEncoding()
1052         throws Exception
1053     {
1054         File file = new File( tempFolder.getRoot(), "read.obj" );
1055         FileOutputStream out = new FileOutputStream( file );
1056         byte[] text = "Hello /u1234".getBytes( "UTF8" );
1057         out.write( text );
1058         out.close();
1059 
1060         String data = FileUtils.fileRead( file, "UTF8" );
1061         assertThat( data, is( "Hello /u1234" ) );
1062     }
1063 
1064     @Test
1065     @Ignore( "Commons test case that is failing for plexus" )
1066     public void readLines()
1067         throws Exception
1068     {
1069         File file = FileTestHelper.newFile( tempFolder, "lines.txt" );
1070         try
1071         {
1072             String[] data = new String[]{ "hello", "/u1234", "", "this is", "some text" };
1073             FileTestHelper.createLineBasedFile( file, data );
1074 
1075             List<String> lines = FileUtils.loadFile( file );
1076             assertThat( lines, is( Arrays.asList( data ) ) );
1077         }
1078         finally
1079         {
1080             deleteFile( file );
1081         }
1082     }
1083 
1084     @Test
1085     public void writeStringToFile1()
1086         throws Exception
1087     {
1088         File file = new File( tempFolder.getRoot(), "write.txt" );
1089         FileUtils.fileWrite( file, "UTF8", "Hello /u1234" );
1090         byte[] text = "Hello /u1234".getBytes( "UTF8" );
1091         assertEqualContent( text, file );
1092     }
1093 
1094     @Test
1095     public void writeStringToFile2()
1096         throws Exception
1097     {
1098         File file = new File( tempFolder.getRoot(), "write.txt" );
1099         FileUtils.fileWrite( file, null, "Hello /u1234" );
1100         byte[] text = "Hello /u1234".getBytes();
1101         assertEqualContent( text, file );
1102     }
1103 
1104     @Test
1105     public void writeCharSequence1()
1106         throws Exception
1107     {
1108         File file = new File( tempFolder.getRoot(), "write.txt" );
1109         FileUtils.fileWrite( file, "UTF8", "Hello /u1234" );
1110         byte[] text = "Hello /u1234".getBytes( "UTF8" );
1111         assertEqualContent( text, file );
1112     }
1113 
1114     @Test
1115     public void writeCharSequence2()
1116         throws Exception
1117     {
1118         File file = new File( tempFolder.getRoot(), "write.txt" );
1119         FileUtils.fileWrite( file, null, "Hello /u1234" );
1120         byte[] text = "Hello /u1234".getBytes();
1121         assertEqualContent( text, file );
1122     }
1123 
1124 
1125     @Test
1126     public void writeStringToFileWithEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines()
1127         throws Exception
1128     {
1129         File file = FileTestHelper.newFile( tempFolder, "lines.txt" );
1130         FileUtils.fileWrite( file, null, "This line was there before you..." );
1131 
1132         FileUtils.fileAppend( file.getAbsolutePath(), "this is brand new data" );
1133 
1134         String expected = "This line was there before you..." + "this is brand new data";
1135         String actual = FileUtils.fileRead( file );
1136         assertThat( actual, is( expected ) );
1137     }
1138 
1139     @Test
1140     public void writeStringToFile_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines()
1141         throws Exception
1142     {
1143         File file = FileTestHelper.newFile( tempFolder, "lines.txt" );
1144         FileUtils.fileWrite( file, null, "This line was there before you..." );
1145 
1146         FileUtils.fileAppend( file.getAbsolutePath(), "this is brand new data" );
1147 
1148         String expected = "This line was there before you..." + "this is brand new data";
1149         String actual = FileUtils.fileRead( file );
1150         assertThat( actual, is( expected ) );
1151     }
1152 
1153     @Test
1154     public void writeStringArrayToFile()
1155             throws Exception
1156     {
1157         File file = new File( tempFolder.getRoot(), "writeArray.txt" );
1158         FileUtils.fileWriteArray( file, new String[]{"line1", "line2", "line3"} );
1159 
1160         byte[] text = "line1\nline2\nline3".getBytes( "UTF8" );
1161         assertEqualContent( text, file );
1162     }
1163 
1164     @Test
1165     public void writeStringArrayToFileWithEncoding()
1166             throws Exception
1167     {
1168         File file = new File( tempFolder.getRoot(), "writeArray.txt" );
1169         FileUtils.fileWriteArray( file, "UTF8", new String[]{"line1", "line2", "line3"} );
1170 
1171         byte[] text = "line1\nline2\nline3".getBytes( "UTF8" );
1172         assertEqualContent( text, file );
1173     }
1174 
1175 
1176     @Test
1177     public void writeWithEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines()
1178         throws Exception
1179     {
1180         File file = FileTestHelper.newFile( tempFolder, "lines.txt" );
1181         FileUtils.fileWrite( file, "UTF-8", "This line was there before you..." );
1182 
1183         FileUtils.fileAppend( file.getAbsolutePath(), "UTF-8", "this is brand new data" );
1184 
1185         String expected = "This line was there before you..." + "this is brand new data";
1186         String actual = FileUtils.fileRead( file );
1187         assertThat( actual, is( expected ) );
1188     }
1189 
1190     @Test
1191     public void write_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines()
1192         throws Exception
1193     {
1194         File file = FileTestHelper.newFile( tempFolder, "lines.txt" );
1195         FileUtils.fileWrite( file, null, "This line was there before you..." );
1196 
1197         FileUtils.fileAppend( file.getAbsolutePath(), "this is brand new data" );
1198 
1199         String expected = "This line was there before you..." + "this is brand new data";
1200         String actual = FileUtils.fileRead( file );
1201         assertThat( actual, is( expected ) );
1202     }
1203 
1204     @Test( expected = NullPointerException.class )
1205     public void blowUpOnNull()
1206         throws IOException
1207     {
1208         FileUtils.deleteDirectory( (File) null );
1209     }
1210 
1211     @Test
1212     public void deleteQuietlyDir()
1213         throws IOException
1214     {
1215         File testDirectory = new File( tempFolder.getRoot(), "testDeleteQuietlyDir" );
1216         File testFile = new File( testDirectory, "testDeleteQuietlyFile" );
1217         testDirectory.mkdirs();
1218         createFile( testFile, 0 );
1219 
1220         assertThat( testDirectory.exists(), is( true ) );
1221         assertThat( testFile.exists(), is( true ) );
1222         FileUtils.deleteDirectory( testDirectory );
1223         assertThat( "Check No Exist", testDirectory.exists(), is( false ) );
1224         assertThat( "Check No Exist", testFile.exists(), is( false ) );
1225     }
1226 
1227     @Test
1228     public void deleteQuietlyFile()
1229         throws IOException
1230     {
1231         File testFile = new File( tempFolder.getRoot(), "testDeleteQuietlyFile" );
1232         createFile( testFile, 0 );
1233 
1234         assertThat( testFile.exists(), is( true ) );
1235         FileUtils.deleteDirectory( testFile );
1236         assertThat( "Check No Exist", testFile.exists(), is( false ) );
1237     }
1238 
1239     @Test
1240     public void deleteQuietlyNonExistent()
1241         throws IOException
1242     {
1243         File testFile = new File( tempFolder.getRoot(), "testDeleteQuietlyNonExistent" );
1244         assertThat( testFile.exists(), is( false ) );
1245 
1246         FileUtils.deleteDirectory( testFile );
1247     }
1248 
1249 
1250     ////  getDefaultExcludes
1251 
1252     @Test
1253     public void getDefaultExcludes()
1254         throws Exception
1255     {
1256         assertThat( Arrays.asList( FileUtils.getDefaultExcludes() ), hasItems( MINIMUM_DEFAULT_EXCLUDES ) );
1257     }
1258 
1259 
1260     //// getDefaultExcludesAsList
1261 
1262     @Test
1263     public void getDefaultExcludesAsList()
1264         throws Exception
1265     {
1266         assertThat( FileUtils.getDefaultExcludesAsList(), hasItems( MINIMUM_DEFAULT_EXCLUDES ) );
1267     }
1268 
1269 
1270     //// getDefaultExcludesAsString
1271 
1272     @Test
1273     public void getDefaultExcludesAsString()
1274         throws Exception
1275     {
1276         assertThat( new HashSet<String>( Arrays.asList( FileUtils.getDefaultExcludesAsString().split( "," ) ) ),
1277                     hasItems( MINIMUM_DEFAULT_EXCLUDES ) );
1278     }
1279 
1280 
1281 
1282     //// dirname(String)
1283 
1284     @SuppressWarnings("ConstantConditions")
1285     @Test( expected = NullPointerException.class )
1286     public void nlowUpOnDirnameNull()
1287         throws Exception
1288     {
1289         FileUtils.dirname( null );
1290     }
1291 
1292     @Test
1293     public void dirnameEmpty()
1294         throws Exception
1295     {
1296         assertThat( FileUtils.dirname( "" ), is( "" ) );
1297     }
1298 
1299     @Test
1300     public void dirnameFilename()
1301         throws Exception
1302     {
1303         assertThat( FileUtils.dirname( "foo.bar.txt" ), is( "" ) );
1304     }
1305 
1306     @Test
1307     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1308     public void dirnameWindowsRootPathOnUnix()
1309         throws Exception
1310     {
1311         assumeThat( File.separatorChar, is( '/' ) );
1312         assertThat( FileUtils.dirname( "C:\\foo.bar.txt" ), is( "" ) );
1313     }
1314 
1315     @Test
1316     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1317     public void dirnameWindowsNonRootPathOnUnix()
1318         throws Exception
1319     {
1320         assumeThat( File.separatorChar, is( '/' ) );
1321         assertThat( FileUtils.dirname( "C:\\test\\foo.bar.txt" ), is( "" ) );
1322     }
1323 
1324     @Test
1325     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1326     public void dirnameUnixRootPathOnWindows()
1327         throws Exception
1328     {
1329         assumeThat( File.separatorChar, is( '\\' ) );
1330         assertThat( FileUtils.dirname( "/foo.bar.txt" ), is( "" ) );
1331     }
1332 
1333     @Test
1334     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1335     public void dirnameUnixNonRootPathOnWindows()
1336         throws Exception
1337     {
1338         assumeThat( File.separatorChar, is( '\\' ) );
1339         assertThat( FileUtils.dirname( "/test/foo.bar.txt" ), is( "" ) );
1340     }
1341 
1342     @Test
1343     public void dirnameWindowsRootPathOnWindows()
1344         throws Exception
1345     {
1346         assumeThat( File.separatorChar, is( '\\' ) );
1347         assertThat( FileUtils.dirname( "C:\\foo.bar.txt" ), is( "C:" ) );
1348     }
1349 
1350     @Test
1351     public void dirnameWindowsNonRootPathOnWindows()
1352         throws Exception
1353     {
1354         assumeThat( File.separatorChar, is( '\\' ) );
1355         assertThat( FileUtils.dirname( "C:\\test\\foo.bar.txt" ), is( "C:\\test" ) );
1356     }
1357 
1358     @Test
1359     public void dirnameUnixRootPathOnUnix()
1360         throws Exception
1361     {
1362         assumeThat( File.separatorChar, is( '/' ) );
1363         assertThat( FileUtils.dirname( "/foo.bar.txt" ), is( "" ) );
1364     }
1365 
1366     @Test
1367     public void dirnameUnixNonRootPathOnUnix()
1368         throws Exception
1369     {
1370         assumeThat( File.separatorChar, is( '/' ) );
1371         assertThat( FileUtils.dirname( "/test/foo.bar.txt" ), is( "/test" ) );
1372     }
1373 
1374     //// filename(String)
1375 
1376     @SuppressWarnings("ConstantConditions")
1377     @Test( expected = NullPointerException.class )
1378     public void blowUpOnFilenameNull()
1379         throws Exception
1380     {
1381         FileUtils.filename( null );
1382     }
1383 
1384     @Test
1385     public void filenameEmpty()
1386         throws Exception
1387     {
1388         assertThat( FileUtils.filename( "" ), is( "" ) );
1389     }
1390 
1391     @Test
1392     public void filenameFilename()
1393         throws Exception
1394     {
1395         assertThat( FileUtils.filename( "foo.bar.txt" ), is( "foo.bar.txt" ) );
1396     }
1397 
1398     @Test
1399     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1400     public void filenameWindowsRootPathOnUnix()
1401         throws Exception
1402     {
1403         assumeThat( File.separatorChar, is( '/' ) );
1404         assertThat( FileUtils.filename( "C:\\foo.bar.txt" ), is( "C:\\foo.bar.txt" ) );
1405     }
1406 
1407     @Test
1408     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1409     public void filenameWindowsNonRootPathOnUnix()
1410         throws Exception
1411     {
1412         assumeThat( File.separatorChar, is( '/' ) );
1413         assertThat( FileUtils.filename( "C:\\test\\foo.bar.txt" ), is( "C:\\test\\foo.bar.txt" ) );
1414     }
1415 
1416     @Test
1417     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1418     public void filenameUnixRootPathOnWindows()
1419         throws Exception
1420     {
1421         assumeThat( File.separatorChar, is( '\\' ) );
1422         assertThat( FileUtils.filename( "/foo.bar.txt" ), is( "/foo.bar.txt" ) );
1423     }
1424 
1425     @Test
1426     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1427     public void filenameUnixNonRootPathOnWindows()
1428         throws Exception
1429     {
1430         assumeThat( File.separatorChar, is( '\\' ) );
1431         assertThat( FileUtils.filename( "/test/foo.bar.txt" ), is( "/test/foo.bar.txt" ) );
1432     }
1433 
1434     @Test
1435     public void filenameWindowsRootPathOnWindows()
1436         throws Exception
1437     {
1438         assumeThat( File.separatorChar, is( '\\' ) );
1439         assertThat( FileUtils.filename( "C:\\foo.bar.txt" ), is( "foo.bar.txt" ) );
1440     }
1441 
1442     @Test
1443     public void filenameWindowsNonRootPathOnWindows()
1444         throws Exception
1445     {
1446         assumeThat( File.separatorChar, is( '\\' ) );
1447         assertThat( FileUtils.filename( "C:\\test\\foo.bar.txt" ), is( "foo.bar.txt" ) );
1448     }
1449 
1450     @Test
1451     public void filenameUnixRootPathOnUnix()
1452         throws Exception
1453     {
1454         assumeThat( File.separatorChar, is( '/' ) );
1455         assertThat( FileUtils.filename( "/foo.bar.txt" ), is( "foo.bar.txt" ) );
1456     }
1457 
1458     @Test
1459     public void filenameUnixNonRootPathOnUnix()
1460         throws Exception
1461     {
1462         assumeThat( File.separatorChar, is( '/' ) );
1463         assertThat( FileUtils.filename( "/test/foo.bar.txt" ), is( "foo.bar.txt" ) );
1464     }
1465 
1466     //// extension(String)
1467 
1468     @SuppressWarnings("ConstantConditions")
1469     @Test( expected = NullPointerException.class )
1470     public void blowUpOnNullExtension()
1471         throws Exception
1472     {
1473         FileUtils.extension( null );
1474     }
1475 
1476     @Test
1477     public void extensionEmpty()
1478         throws Exception
1479     {
1480         assertThat( FileUtils.extension( "" ), is( "" ) );
1481     }
1482 
1483     @Test
1484     public void extensionFileName()
1485         throws Exception
1486     {
1487         assertThat( FileUtils.extension( "foo.bar.txt" ), is( "txt" ) );
1488     }
1489 
1490     @Test
1491     public void extensionFileNameNoExtension()
1492         throws Exception
1493     {
1494         assertThat( FileUtils.extension( "foo_bar_txt" ), is( "" ) );
1495     }
1496 
1497     @Test
1498     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1499     public void extensionWindowsRootPathOnUnix()
1500         throws Exception
1501     {
1502         assumeThat( File.separatorChar, is( '/' ) );
1503         assertThat( FileUtils.extension( "C:\\foo.bar.txt" ), is( "txt" ) );
1504     }
1505 
1506     @Test
1507     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1508     public void extensionWindowsNonRootPathOnUnix()
1509         throws Exception
1510     {
1511         assumeThat( File.separatorChar, is( '/' ) );
1512         assertThat( FileUtils.extension( "C:\\test\\foo.bar.txt" ), is( "txt" ) );
1513     }
1514 
1515     @Test
1516     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1517     public void extensionUnixRootPathOnWindows()
1518         throws Exception
1519     {
1520         assumeThat( File.separatorChar, is( '\\' ) );
1521         assertThat( FileUtils.extension( "/foo.bar.txt" ), is( "txt" ) );
1522     }
1523 
1524     @Test
1525     //X @ReproducesPlexusBug( "assumes that the path is a local path" )
1526     public void extensionUnixNonRootPathOnWindows()
1527         throws Exception
1528     {
1529         assumeThat( File.separatorChar, is( '\\' ) );
1530         assertThat( FileUtils.extension( "/test/foo.bar.txt" ), is( "txt" ) );
1531     }
1532 
1533     @Test
1534     public void extensionWindowsRootPathOnWindows()
1535         throws Exception
1536     {
1537         assumeThat( File.separatorChar, is( '\\' ) );
1538         assertThat( FileUtils.extension( "C:\\foo.bar.txt" ), is( "txt" ) );
1539     }
1540 
1541     @Test
1542     public void extensionWindowsNonRootPathOnWindows()
1543         throws Exception
1544     {
1545         assumeThat( File.separatorChar, is( '\\' ) );
1546         assertThat( FileUtils.extension( "C:\\test\\foo.bar.txt" ), is( "txt" ) );
1547     }
1548 
1549     @Test
1550     @Ignore("Wait until we can run with assembly 2.5 which will support symlinks properly")
1551     public void isASymbolicLink()
1552         throws IOException
1553     {
1554         // This testcase will pass when running under java7 or higher
1555         assumeFalse( Os.isFamily( Os.FAMILY_WINDOWS ) );
1556 
1557         File file = new File( "src/test/resources/symlinks/src/symDir" );
1558         assertTrue(FileUtils.isSymbolicLink(file  ));
1559     }
1560 
1561     @Test
1562     @Ignore("Wait until we can run with assembly 2.5 which will support symlinks properly")
1563     public void notASymbolicLink()
1564         throws IOException
1565     {
1566         File file = new File( "src/test/resources/symlinks/src/" );
1567         assertFalse(FileUtils.isSymbolicLink(file  ));
1568     }
1569 
1570     @Test
1571     public void extensionUnixRootPathOnUnix()
1572         throws Exception
1573     {
1574         assumeThat( File.separatorChar, is( '/' ) );
1575         assertThat( FileUtils.extension( "/foo.bar.txt" ), is( "txt" ) );
1576     }
1577 
1578     @Test
1579     public void extensionUnixNonRootPathOnUnix()
1580         throws Exception
1581     {
1582         assumeThat( File.separatorChar, is( '/' ) );
1583         assertThat( FileUtils.extension( "/test/foo.bar.txt" ), is( "txt" ) );
1584     }
1585 
1586     @Test
1587     public void createAndReadSymlink()
1588         throws Exception
1589     {
1590         assumeFalse( Os.isFamily( Os.FAMILY_WINDOWS ) );
1591 
1592         File file = new File( "target/fzz" );
1593         FileUtils.createSymbolicLink(  file, new File("../target") );
1594 
1595         final File file1 = Files.readSymbolicLink( file.toPath() ).toFile();
1596         assertEquals( "target", file1.getName() );
1597         Files.delete( file.toPath() );
1598     }
1599 
1600     @Test
1601     public void createSymbolicLinkWithDifferentTargetOverwritesSymlink()
1602             throws Exception
1603     {
1604         assumeFalse( Os.isFamily( Os.FAMILY_WINDOWS ) );
1605 
1606         // Arrange
1607 
1608         final File symlink1 = new File( tempFolder.getRoot(), "symlink" );
1609 
1610         FileUtils.createSymbolicLink( symlink1, testFile1 );
1611 
1612         // Act
1613 
1614         final File symlink2 = FileUtils.createSymbolicLink( symlink1, testFile2 );
1615 
1616         // Assert
1617 
1618         assertThat(
1619             Files.readSymbolicLink( symlink2.toPath() ).toFile(),
1620             CoreMatchers.equalTo( testFile2 )
1621         );
1622     }
1623 
1624     //// constants for testing
1625 
1626     private static final String[] MINIMUM_DEFAULT_EXCLUDES = {
1627         // Miscellaneous typical temporary files
1628         "**/*~", "**/#*#", "**/.#*", "**/%*%", "**/._*",
1629 
1630         // CVS
1631         "**/CVS", "**/CVS/**", "**/.cvsignore",
1632 
1633         // Subversion
1634         "**/.svn", "**/.svn/**",
1635 
1636         // Arch
1637         "**/.arch-ids", "**/.arch-ids/**",
1638 
1639         //Bazaar
1640         "**/.bzr", "**/.bzr/**",
1641 
1642         //SurroundSCM
1643         "**/.MySCMServerInfo",
1644 
1645         // Mac
1646         "**/.DS_Store",
1647 
1648         // Serena Dimensions Version 10
1649         "**/.metadata", "**/.metadata/**",
1650 
1651         // Mercurial
1652         "**/.hg", "**/.hg/**",
1653 
1654         // git
1655         "**/.git", "**/.git/**",
1656 
1657         // BitKeeper
1658         "**/BitKeeper", "**/BitKeeper/**", "**/ChangeSet", "**/ChangeSet/**",
1659 
1660         // darcs
1661         "**/_darcs", "**/_darcs/**", "**/.darcsrepo", "**/.darcsrepo/**", "**/-darcs-backup*", "**/.darcs-temp-mail" };
1662 
1663 }