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