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