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