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