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