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