1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.net.URL;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.zip.CRC32;
34 import java.util.zip.Checksum;
35
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38 import junit.textui.TestRunner;
39
40 import org.apache.commons.io.filefilter.WildcardFilter;
41 import org.apache.commons.io.testtools.FileBasedTestCase;
42
43
44
45
46
47
48
49
50
51
52
53 public class FileUtilsTestCase extends FileBasedTestCase {
54
55
56
57
58
59
60 private static final int TEST_DIRECTORY_SIZE = 0;
61
62
63
64
65 private File testFile1;
66 private File testFile2;
67
68 private static int testFile1Size;
69 private static int testFile2Size;
70
71 public static void main(String[] args) {
72 TestRunner.run(suite());
73 }
74
75 public static Test suite() {
76 return new TestSuite(FileUtilsTestCase.class);
77 }
78
79 public FileUtilsTestCase(String name) throws IOException {
80 super(name);
81
82 testFile1 = new File(getTestDirectory(), "file1-test.txt");
83 testFile2 = new File(getTestDirectory(), "file1a-test.txt");
84
85 testFile1Size = (int)testFile1.length();
86 testFile2Size = (int)testFile2.length();
87 }
88
89
90 protected void setUp() throws Exception {
91 getTestDirectory().mkdirs();
92 createFile(testFile1, testFile1Size);
93 createFile(testFile2, testFile2Size);
94 FileUtils.deleteDirectory(getTestDirectory());
95 getTestDirectory().mkdirs();
96 createFile(testFile1, testFile1Size);
97 createFile(testFile2, testFile2Size);
98 }
99
100
101 protected void tearDown() throws Exception {
102 FileUtils.deleteDirectory(getTestDirectory());
103 }
104
105
106 public void test_openInputStream_exists() throws Exception {
107 File file = new File(getTestDirectory(), "test.txt");
108 createLineBasedFile(file, new String[] {"Hello"});
109 FileInputStream in = null;
110 try {
111 in = FileUtils.openInputStream(file);
112 assertEquals('H', in.read());
113 } finally {
114 IOUtils.closeQuietly(in);
115 }
116 }
117
118 public void test_openInputStream_existsButIsDirectory() throws Exception {
119 File directory = new File(getTestDirectory(), "subdir");
120 directory.mkdirs();
121 FileInputStream in = null;
122 try {
123 in = FileUtils.openInputStream(directory);
124 fail();
125 } catch (IOException ioe) {
126
127 } finally {
128 IOUtils.closeQuietly(in);
129 }
130 }
131
132 public void test_openInputStream_notExists() throws Exception {
133 File directory = new File(getTestDirectory(), "test.txt");
134 FileInputStream in = null;
135 try {
136 in = FileUtils.openInputStream(directory);
137 fail();
138 } catch (IOException ioe) {
139
140 } finally {
141 IOUtils.closeQuietly(in);
142 }
143 }
144
145
146 void openOutputStream_noParent(boolean createFile) throws Exception {
147 File file = new File("test.txt");
148 assertNull(file.getParentFile());
149 try {
150 if (createFile) {
151 createLineBasedFile(file, new String[]{"Hello"});}
152 FileOutputStream out = null;
153 try {
154 out = FileUtils.openOutputStream(file);
155 out.write(0);
156 } finally {
157 IOUtils.closeQuietly(out);
158 }
159 assertEquals(true, file.exists());
160 } finally {
161 if (file.delete() == false) {
162 file.deleteOnExit();
163 }
164 }
165 }
166
167 public void test_openOutputStream_noParentCreateFile() throws Exception {
168 openOutputStream_noParent(true);
169 }
170
171 public void test_openOutputStream_noParentNoFile() throws Exception {
172 openOutputStream_noParent(false);
173 }
174
175
176 public void test_openOutputStream_exists() throws Exception {
177 File file = new File(getTestDirectory(), "test.txt");
178 createLineBasedFile(file, new String[] {"Hello"});
179 FileOutputStream out = null;
180 try {
181 out = FileUtils.openOutputStream(file);
182 out.write(0);
183 } finally {
184 IOUtils.closeQuietly(out);
185 }
186 assertEquals(true, file.exists());
187 }
188
189 public void test_openOutputStream_existsButIsDirectory() throws Exception {
190 File directory = new File(getTestDirectory(), "subdir");
191 directory.mkdirs();
192 FileOutputStream out = null;
193 try {
194 out = FileUtils.openOutputStream(directory);
195 fail();
196 } catch (IOException ioe) {
197
198 } finally {
199 IOUtils.closeQuietly(out);
200 }
201 }
202
203 public void test_openOutputStream_notExists() throws Exception {
204 File file = new File(getTestDirectory(), "a/test.txt");
205 FileOutputStream out = null;
206 try {
207 out = FileUtils.openOutputStream(file);
208 out.write(0);
209 } finally {
210 IOUtils.closeQuietly(out);
211 }
212 assertEquals(true, file.exists());
213 }
214
215 public void test_openOutputStream_notExistsCannotCreate() throws Exception {
216
217 String longStr =
218 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
219 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
220 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
221 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
222 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
223 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz";
224 File file = new File(getTestDirectory(), "a/" + longStr + "/test.txt");
225 FileOutputStream out = null;
226 try {
227 out = FileUtils.openOutputStream(file);
228 fail();
229 } catch (IOException ioe) {
230
231 } finally {
232 IOUtils.closeQuietly(out);
233 }
234 }
235
236
237
238 public void testByteCountToDisplaySize() {
239 assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
240 assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB");
241 assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024), "1 MB");
242 assertEquals(
243 FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024),
244 "1 GB");
245 }
246
247
248 public void testToFile1() throws Exception {
249 URL url = new URL("file", null, "a/b/c/file.txt");
250 File file = FileUtils.toFile(url);
251 assertEquals(true, file.toString().indexOf("file.txt") >= 0);
252 }
253
254 public void testToFile2() throws Exception {
255 URL url = new URL("file", null, "a/b/c/file%20n%61me.tx%74");
256 File file = FileUtils.toFile(url);
257 assertEquals(true, file.toString().indexOf("file name.txt") >= 0);
258 }
259
260 public void testToFile3() throws Exception {
261 assertEquals(null, FileUtils.toFile((URL) null));
262 assertEquals(null, FileUtils.toFile(new URL("http://jakarta.apache.org")));
263 }
264
265 public void testToFile4() throws Exception {
266 URL url = new URL("file", null, "a/b/c/file%2Xn%61me.txt");
267 try {
268 FileUtils.toFile(url);
269 fail();
270 } catch (IllegalArgumentException ex) {}
271 }
272
273
274
275 public void testToFiles1() throws Exception {
276 URL[] urls = new URL[] {
277 new URL("file", null, "file1.txt"),
278 new URL("file", null, "file2.txt"),
279 };
280 File[] files = FileUtils.toFiles(urls);
281
282 assertEquals(urls.length, files.length);
283 assertEquals("File: " + files[0], true, files[0].toString().indexOf("file1.txt") >= 0);
284 assertEquals("File: " + files[1], true, files[1].toString().indexOf("file2.txt") >= 0);
285 }
286
287 public void testToFiles2() throws Exception {
288 URL[] urls = new URL[] {
289 new URL("file", null, "file1.txt"),
290 null,
291 };
292 File[] files = FileUtils.toFiles(urls);
293
294 assertEquals(urls.length, files.length);
295 assertEquals("File: " + files[0], true, files[0].toString().indexOf("file1.txt") >= 0);
296 assertEquals("File: " + files[1], null, files[1]);
297 }
298
299 public void testToFiles3() throws Exception {
300 URL[] urls = null;
301 File[] files = FileUtils.toFiles(urls);
302
303 assertEquals(0, files.length);
304 }
305
306 public void testToFiles4() throws Exception {
307 URL[] urls = new URL[] {
308 new URL("file", null, "file1.txt"),
309 new URL("http", "jakarta.apache.org", "file1.txt"),
310 };
311 try {
312 FileUtils.toFiles(urls);
313 fail();
314 } catch (IllegalArgumentException ex) {}
315 }
316
317
318
319 public void testToURLs1() throws Exception {
320 File[] files = new File[] {
321 new File(getTestDirectory(), "file1.txt"),
322 new File(getTestDirectory(), "file2.txt"),
323 };
324 URL[] urls = FileUtils.toURLs(files);
325
326 assertEquals(files.length, urls.length);
327 assertEquals(true, urls[0].toExternalForm().startsWith("file:"));
328 assertEquals(true, urls[0].toExternalForm().indexOf("file1.txt") >= 0);
329 assertEquals(true, urls[1].toExternalForm().startsWith("file:"));
330 assertEquals(true, urls[1].toExternalForm().indexOf("file2.txt") >= 0);
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 public void testContentEquals() throws Exception {
356
357 File file = new File(getTestDirectory(), getName());
358 File file2 = new File(getTestDirectory(), getName() + "2");
359
360 assertTrue(FileUtils.contentEquals(file, file));
361 assertTrue(FileUtils.contentEquals(file, file2));
362 assertTrue(FileUtils.contentEquals(file2, file2));
363 assertTrue(FileUtils.contentEquals(file2, file));
364
365
366 try {
367 FileUtils.contentEquals(getTestDirectory(), getTestDirectory());
368 fail("Comparing directories should fail with an IOException");
369 } catch (IOException ioe) {
370
371 }
372
373
374 File objFile1 =
375 new File(getTestDirectory(), getName() + ".object");
376 objFile1.deleteOnExit();
377 FileUtils.copyURLToFile(
378 getClass().getResource("/java/lang/Object.class"),
379 objFile1);
380
381 File objFile1b =
382 new File(getTestDirectory(), getName() + ".object2");
383 objFile1.deleteOnExit();
384 FileUtils.copyURLToFile(
385 getClass().getResource("/java/lang/Object.class"),
386 objFile1b);
387
388 File objFile2 =
389 new File(getTestDirectory(), getName() + ".collection");
390 objFile2.deleteOnExit();
391 FileUtils.copyURLToFile(
392 getClass().getResource("/java/util/Collection.class"),
393 objFile2);
394
395 assertEquals(false, FileUtils.contentEquals(objFile1, objFile2));
396 assertEquals(false, FileUtils.contentEquals(objFile1b, objFile2));
397 assertEquals(true, FileUtils.contentEquals(objFile1, objFile1b));
398
399 assertEquals(true, FileUtils.contentEquals(objFile1, objFile1));
400 assertEquals(true, FileUtils.contentEquals(objFile1b, objFile1b));
401 assertEquals(true, FileUtils.contentEquals(objFile2, objFile2));
402
403
404 file.createNewFile();
405 file2.createNewFile();
406 assertEquals(true, FileUtils.contentEquals(file, file));
407 assertEquals(true, FileUtils.contentEquals(file, file2));
408 }
409
410
411
412 public void testCopyURLToFile() throws Exception {
413
414 File file = new File(getTestDirectory(), getName());
415 file.deleteOnExit();
416
417
418 String resourceName = "/java/lang/Object.class";
419 FileUtils.copyURLToFile(getClass().getResource(resourceName), file);
420
421
422 FileInputStream fis = new FileInputStream(file);
423 try {
424 assertTrue(
425 "Content is not equal.",
426 IOUtils.contentEquals(
427 getClass().getResourceAsStream(resourceName),
428 fis));
429 } finally {
430 fis.close();
431 }
432
433 }
434
435
436
437 public void testForceMkdir() throws Exception {
438
439 FileUtils.forceMkdir(getTestDirectory());
440
441
442 File testFile = new File(getTestDirectory(), getName());
443 testFile.deleteOnExit();
444 testFile.createNewFile();
445 assertTrue("Test file does not exist.", testFile.exists());
446
447
448 try {
449 FileUtils.forceMkdir(testFile);
450 fail("Exception expected.");
451 } catch (IOException ex) {}
452
453 testFile.delete();
454
455
456 FileUtils.forceMkdir(testFile);
457 assertTrue("Directory was not created.", testFile.exists());
458 }
459
460
461
462 public void testSizeOfDirectory() throws Exception {
463 File file = new File(getTestDirectory(), getName());
464
465
466 try {
467 FileUtils.sizeOfDirectory(file);
468 fail("Exception expected.");
469 } catch (IllegalArgumentException ex) {}
470
471
472 file.createNewFile();
473 file.deleteOnExit();
474
475
476 try {
477 FileUtils.sizeOfDirectory(file);
478 fail("Exception expected.");
479 } catch (IllegalArgumentException ex) {}
480
481
482 file.delete();
483 file.mkdir();
484
485 assertEquals(
486 "Unexpected directory size",
487 TEST_DIRECTORY_SIZE,
488 FileUtils.sizeOfDirectory(file));
489 }
490
491
492 public void testIsFileNewerOlder() throws Exception {
493 File reference = new File(getTestDirectory(), "FileUtils-reference.txt");
494 File oldFile = new File(getTestDirectory(), "FileUtils-old.txt");
495 File newFile = new File(getTestDirectory(), "FileUtils-new.txt");
496 File invalidFile = new File(getTestDirectory(), "FileUtils-invalid-file.txt");
497
498
499 createFile(oldFile, 0);
500
501 do {
502 try {
503 Thread.sleep(1000);
504 } catch(InterruptedException ie) {
505
506 }
507 createFile(reference, 0);
508 } while( oldFile.lastModified() == reference.lastModified() );
509
510 Date date = new Date();
511 long now = date.getTime();
512
513 do {
514 try {
515 Thread.sleep(1000);
516 } catch(InterruptedException ie) {
517
518 }
519 createFile(newFile, 0);
520 } while( reference.lastModified() == newFile.lastModified() );
521
522
523 assertFalse("Old File - Newer - File", FileUtils.isFileNewer(oldFile, reference));
524 assertFalse("Old File - Newer - Date", FileUtils.isFileNewer(oldFile, date));
525 assertFalse("Old File - Newer - Mili", FileUtils.isFileNewer(oldFile, now));
526 assertTrue("New File - Newer - File", FileUtils.isFileNewer(newFile, reference));
527 assertTrue("New File - Newer - Date", FileUtils.isFileNewer(newFile, date));
528 assertTrue("New File - Newer - Mili", FileUtils.isFileNewer(newFile, now));
529 assertFalse("Invalid - Newer - File", FileUtils.isFileNewer(invalidFile, reference));
530
531
532 assertTrue("Old File - Older - File", FileUtils.isFileOlder(oldFile, reference));
533 assertTrue("Old File - Older - Date", FileUtils.isFileOlder(oldFile, date));
534 assertTrue("Old File - Older - Mili", FileUtils.isFileOlder(oldFile, now));
535 assertFalse("New File - Older - File", FileUtils.isFileOlder(newFile, reference));
536 assertFalse("New File - Older - Date", FileUtils.isFileOlder(newFile, date));
537 assertFalse("New File - Older - Mili", FileUtils.isFileOlder(newFile, now));
538 assertFalse("Invalid - Older - File", FileUtils.isFileOlder(invalidFile, reference));
539
540
541
542
543 try {
544 FileUtils.isFileNewer(null, now);
545 fail("Newer Null, expected IllegalArgumentExcepion");
546 } catch (IllegalArgumentException expected) {
547
548 }
549
550
551 try {
552 FileUtils.isFileNewer(oldFile, (File)null);
553 fail("Newer Null reference, expected IllegalArgumentExcepion");
554 } catch (IllegalArgumentException expected) {
555
556 }
557
558
559 try {
560 FileUtils.isFileNewer(oldFile, invalidFile);
561 fail("Newer invalid reference, expected IllegalArgumentExcepion");
562 } catch (IllegalArgumentException expected) {
563
564 }
565
566
567 try {
568 FileUtils.isFileNewer(oldFile, (Date)null);
569 fail("Newer Null date, expected IllegalArgumentExcepion");
570 } catch (IllegalArgumentException expected) {
571
572 }
573
574
575
576
577 try {
578 FileUtils.isFileOlder(null, now);
579 fail("Older Null, expected IllegalArgumentExcepion");
580 } catch (IllegalArgumentException expected) {
581
582 }
583
584
585 try {
586 FileUtils.isFileOlder(oldFile, (File)null);
587 fail("Older Null reference, expected IllegalArgumentExcepion");
588 } catch (IllegalArgumentException expected) {
589
590 }
591
592
593 try {
594 FileUtils.isFileOlder(oldFile, invalidFile);
595 fail("Older invalid reference, expected IllegalArgumentExcepion");
596 } catch (IllegalArgumentException expected) {
597
598 }
599
600
601 try {
602 FileUtils.isFileOlder(oldFile, (Date)null);
603 fail("Older Null date, expected IllegalArgumentExcepion");
604 } catch (IllegalArgumentException expected) {
605
606 }
607
608 }
609
610
611
612
613
614
615
616
617
618 public void testCopyFile1() throws Exception {
619 File destination = new File(getTestDirectory(), "copy1.txt");
620
621
622
623
624
625 FileUtils.copyFile(testFile1, destination);
626 assertTrue("Check Exist", destination.exists());
627 assertTrue("Check Full copy", destination.length() == testFile1Size);
628
629
630
631 }
632
633 public void testCopyFile2() throws Exception {
634 File destination = new File(getTestDirectory(), "copy2.txt");
635
636
637
638
639
640 FileUtils.copyFile(testFile1, destination);
641 assertTrue("Check Exist", destination.exists());
642 assertTrue("Check Full copy", destination.length() == testFile2Size);
643
644
645
646 }
647
648 public void testCopyToSelf() throws Exception {
649 File destination = new File(getTestDirectory(), "copy3.txt");
650
651 FileUtils.copyFile(testFile1, destination);
652
653 try {
654 FileUtils.copyFile(destination, destination);
655 fail("file copy to self should not be possible");
656 } catch (IOException ioe) {
657
658 }
659 }
660
661 public void testCopyFile2WithoutFileDatePreservation() throws Exception {
662 File destination = new File(getTestDirectory(), "copy2.txt");
663
664
665
666
667
668 FileUtils.copyFile(testFile1, destination, false);
669 assertTrue("Check Exist", destination.exists());
670 assertTrue("Check Full copy", destination.length() == testFile2Size);
671
672
673
674 }
675
676 public void testCopyDirectoryToDirectory_NonExistingDest() throws Exception {
677 createFile(testFile1, 1234);
678 createFile(testFile2, 4321);
679 File srcDir = getTestDirectory();
680 File subDir = new File(srcDir, "sub");
681 subDir.mkdir();
682 File subFile = new File(subDir, "A.txt");
683 FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
684 File destDir = new File(System.getProperty("java.io.tmpdir"), "tmp-FileUtilsTestCase");
685 FileUtils.deleteDirectory(destDir);
686 File actualDestDir = new File(destDir, srcDir.getName());
687
688 FileUtils.copyDirectoryToDirectory(srcDir, destDir);
689
690 assertTrue("Check exists", destDir.exists());
691 assertTrue("Check exists", actualDestDir.exists());
692 assertEquals("Check size", FileUtils.sizeOfDirectory(srcDir), FileUtils.sizeOfDirectory(actualDestDir));
693 assertEquals(true, new File(actualDestDir, "sub/A.txt").exists());
694 FileUtils.deleteDirectory(destDir);
695 }
696
697 public void testCopyDirectoryToNonExistingDest() throws Exception {
698 createFile(testFile1, 1234);
699 createFile(testFile2, 4321);
700 File srcDir = getTestDirectory();
701 File subDir = new File(srcDir, "sub");
702 subDir.mkdir();
703 File subFile = new File(subDir, "A.txt");
704 FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
705 File destDir = new File(System.getProperty("java.io.tmpdir"), "tmp-FileUtilsTestCase");
706 FileUtils.deleteDirectory(destDir);
707
708 FileUtils.copyDirectory(srcDir, destDir);
709
710 assertTrue("Check exists", destDir.exists());
711 assertEquals("Check size", FileUtils.sizeOfDirectory(srcDir), FileUtils.sizeOfDirectory(destDir));
712 assertEquals(true, new File(destDir, "sub/A.txt").exists());
713 FileUtils.deleteDirectory(destDir);
714 }
715
716 public void testCopyDirectoryToExistingDest() throws Exception {
717 createFile(testFile1, 1234);
718 createFile(testFile2, 4321);
719 File srcDir = getTestDirectory();
720 File subDir = new File(srcDir, "sub");
721 subDir.mkdir();
722 File subFile = new File(subDir, "A.txt");
723 FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
724 File destDir = new File(System.getProperty("java.io.tmpdir"), "tmp-FileUtilsTestCase");
725 FileUtils.deleteDirectory(destDir);
726 destDir.mkdirs();
727
728 FileUtils.copyDirectory(srcDir, destDir);
729
730 assertEquals(FileUtils.sizeOfDirectory(srcDir), FileUtils.sizeOfDirectory(destDir));
731 assertEquals(true, new File(destDir, "sub/A.txt").exists());
732 }
733
734 public void testCopyDirectoryErrors() throws Exception {
735 try {
736 FileUtils.copyDirectory(null, null);
737 fail();
738 } catch (NullPointerException ex) {}
739 try {
740 FileUtils.copyDirectory(new File("a"), null);
741 fail();
742 } catch (NullPointerException ex) {}
743 try {
744 FileUtils.copyDirectory(null, new File("a"));
745 fail();
746 } catch (NullPointerException ex) {}
747 try {
748 FileUtils.copyDirectory(new File("doesnt-exist"), new File("a"));
749 fail();
750 } catch (IOException ex) {}
751 try {
752 FileUtils.copyDirectory(testFile1, new File("a"));
753 fail();
754 } catch (IOException ex) {}
755 try {
756 FileUtils.copyDirectory(getTestDirectory(), testFile1);
757 fail();
758 } catch (IOException ex) {}
759 try {
760 FileUtils.copyDirectory(getTestDirectory(), getTestDirectory());
761 fail();
762 } catch (IOException ex) {}
763 }
764
765
766
767 public void testForceDeleteAFile1() throws Exception {
768 File destination = new File(getTestDirectory(), "copy1.txt");
769 destination.createNewFile();
770 assertTrue("Copy1.txt doesn't exist to delete", destination.exists());
771 FileUtils.forceDelete(destination);
772 assertTrue("Check No Exist", !destination.exists());
773 }
774
775 public void testForceDeleteAFile2() throws Exception {
776 File destination = new File(getTestDirectory(), "copy2.txt");
777 destination.createNewFile();
778 assertTrue("Copy2.txt doesn't exist to delete", destination.exists());
779 FileUtils.forceDelete(destination);
780 assertTrue("Check No Exist", !destination.exists());
781 }
782
783
784
785 public void testCopyFile1ToDir() throws Exception {
786 File directory = new File(getTestDirectory(), "subdir");
787 if (!directory.exists())
788 directory.mkdirs();
789 File destination = new File(directory, testFile1.getName());
790
791
792
793
794
795 FileUtils.copyFileToDirectory(testFile1, directory);
796 assertTrue("Check Exist", destination.exists());
797 assertTrue("Check Full copy", destination.length() == testFile1Size);
798
799
800
801
802 try {
803 FileUtils.copyFileToDirectory(destination, directory);
804 fail("Should not be able to copy a file into the same directory as itself");
805 } catch (IOException ioe) {
806
807 }
808 }
809
810 public void testCopyFile2ToDir() throws Exception {
811 File directory = new File(getTestDirectory(), "subdir");
812 if (!directory.exists())
813 directory.mkdirs();
814 File destination = new File(directory, testFile1.getName());
815
816
817
818
819
820 FileUtils.copyFileToDirectory(testFile1, directory);
821 assertTrue("Check Exist", destination.exists());
822 assertTrue("Check Full copy", destination.length() == testFile2Size);
823
824
825
826 }
827
828
829
830 public void testForceDeleteDir() throws Exception {
831 File testDirectory = getTestDirectory();
832 FileUtils.forceDelete(testDirectory.getParentFile());
833 assertTrue(
834 "Check No Exist",
835 !testDirectory.getParentFile().exists());
836 }
837
838
839
840
841 public void testFileUtils() throws Exception {
842
843 File file1 = new File(getTestDirectory(), "test.txt");
844 String filename = file1.getAbsolutePath();
845
846
847 OutputStream out = new java.io.FileOutputStream(file1);
848 try {
849 out.write("This is a test".getBytes("UTF-8"));
850 } finally {
851 out.close();
852 }
853
854 File file2 = new File(getTestDirectory(), "test2.txt");
855
856 FileUtils.writeStringToFile(file2, filename, "UTF-8");
857 assertTrue(file2.exists());
858 assertTrue(file2.length() > 0);
859
860 String file2contents = FileUtils.readFileToString(file2, "UTF-8");
861 assertTrue(
862 "Second file's contents correct",
863 filename.equals(file2contents));
864
865 assertTrue(file2.delete());
866
867 String contents = FileUtils.readFileToString(new File(filename), "UTF-8");
868 assertTrue("FileUtils.fileRead()", contents.equals("This is a test"));
869
870 }
871
872 public void testTouch() throws IOException {
873 File file = new File(getTestDirectory(), "touch.txt") ;
874 if (file.exists()) {
875 file.delete();
876 }
877 assertTrue("Bad test: test file still exists", !file.exists());
878 FileUtils.touch(file);
879 assertTrue("FileUtils.touch() created file", file.exists());
880 FileOutputStream out = new FileOutputStream(file) ;
881 assertEquals("Created empty file.", 0, file.length());
882 out.write(0) ;
883 out.close();
884 assertEquals("Wrote one byte to file", 1, file.length());
885 long y2k = new GregorianCalendar(2000, 0, 1).getTime().getTime();
886 boolean res = file.setLastModified(y2k);
887 assertEquals("Bad test: set lastModified failed", true, res);
888 assertEquals("Bad test: set lastModified set incorrect value", y2k, file.lastModified());
889 long now = System.currentTimeMillis();
890 FileUtils.touch(file) ;
891 assertEquals("FileUtils.touch() didn't empty the file.", 1, file.length());
892 assertEquals("FileUtils.touch() changed lastModified", false, y2k == file.lastModified());
893 assertEquals("FileUtils.touch() changed lastModified to more than now-3s", true, file.lastModified() >= (now - 3000));
894 assertEquals("FileUtils.touch() changed lastModified to less than now+3s", true, file.lastModified() <= (now + 3000));
895 }
896
897 public void testListFiles() throws Exception {
898 File srcDir = getTestDirectory();
899 File subDir = new File(srcDir, "list_test" );
900 subDir.mkdir();
901
902 String[] fileNames = {"a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "f.txt"};
903 int[] fileSizes = {123, 234, 345, 456, 678, 789};
904
905 for (int i = 0; i < fileNames.length; ++i) {
906 File theFile = new File(subDir, fileNames[i]);
907 createFile(theFile, fileSizes[i]);
908 }
909
910 Collection files = FileUtils.listFiles(subDir,
911 new WildcardFilter("*.*"),
912 new WildcardFilter("*"));
913
914 int count = files.size();
915 Object[] fileObjs = files.toArray();
916
917 assertEquals(files.size(), fileNames.length);
918
919 Map foundFileNames = new HashMap();
920
921 for (int i = 0; i < count; ++i) {
922 boolean found = false;
923 for(int j = 0; (( !found ) && (j < fileNames.length)); ++j) {
924 if ( fileNames[j].equals(((File) fileObjs[i]).getName())) {
925 foundFileNames.put(fileNames[j], fileNames[j]);
926 found = true;
927 }
928 }
929 }
930
931 assertEquals(foundFileNames.size(), fileNames.length);
932
933 subDir.delete();
934 }
935
936 public void testIterateFiles() throws Exception {
937 File srcDir = getTestDirectory();
938 File subDir = new File(srcDir, "list_test" );
939 subDir.mkdir();
940
941 String[] fileNames = {"a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "f.txt"};
942 int[] fileSizes = {123, 234, 345, 456, 678, 789};
943
944 for (int i = 0; i < fileNames.length; ++i) {
945 File theFile = new File(subDir, fileNames[i]);
946 createFile(theFile, fileSizes[i]);
947 }
948
949 Iterator files = FileUtils.iterateFiles(subDir,
950 new WildcardFilter("*.*"),
951 new WildcardFilter("*"));
952
953 Map foundFileNames = new HashMap();
954
955 while (files.hasNext()) {
956 boolean found = false;
957 String fileName = ((File) files.next()).getName();
958
959 for (int j = 0; (( !found ) && (j < fileNames.length)); ++j) {
960 if ( fileNames[j].equals(fileName)) {
961 foundFileNames.put(fileNames[j], fileNames[j]);
962 found = true;
963 }
964 }
965 }
966
967 assertEquals(foundFileNames.size(), fileNames.length);
968
969 subDir.delete();
970 }
971
972 public void testReadFileToString() throws Exception {
973 File file = new File(getTestDirectory(), "read.obj");
974 FileOutputStream out = new FileOutputStream(file);
975 byte[] text = "Hello /u1234".getBytes("UTF8");
976 out.write(text);
977 out.close();
978
979 String data = FileUtils.readFileToString(file, "UTF8");
980 assertEquals("Hello /u1234", data);
981 }
982
983 public void testReadFileToByteArray() throws Exception {
984 File file = new File(getTestDirectory(), "read.txt");
985 FileOutputStream out = new FileOutputStream(file);
986 out.write(11);
987 out.write(21);
988 out.write(31);
989 out.close();
990
991 byte[] data = FileUtils.readFileToByteArray(file);
992 assertEquals(3, data.length);
993 assertEquals(11, data[0]);
994 assertEquals(21, data[1]);
995 assertEquals(31, data[2]);
996 }
997
998 public void testReadLines() throws Exception {
999 File file = newFile("lines.txt");
1000 try {
1001 String[] data = new String[] {"hello", "/u1234", "", "this is", "some text"};
1002 createLineBasedFile(file, data);
1003
1004 List lines = FileUtils.readLines(file, "UTF-8");
1005 assertEquals(Arrays.asList(data), lines);
1006 } finally {
1007 deleteFile(file);
1008 }
1009 }
1010
1011 public void testWriteStringToFile1() throws Exception {
1012 File file = new File(getTestDirectory(), "write.txt");
1013 FileUtils.writeStringToFile(file, "Hello /u1234", "UTF8");
1014 byte[] text = "Hello /u1234".getBytes("UTF8");
1015 assertEqualContent(text, file);
1016 }
1017
1018 public void testWriteStringToFile2() throws Exception {
1019 File file = new File(getTestDirectory(), "write.txt");
1020 FileUtils.writeStringToFile(file, "Hello /u1234", null);
1021 byte[] text = "Hello /u1234".getBytes();
1022 assertEqualContent(text, file);
1023 }
1024
1025 public void testWriteByteArrayToFile() throws Exception {
1026 File file = new File(getTestDirectory(), "write.obj");
1027 byte[] data = new byte[] {11, 21, 31};
1028 FileUtils.writeByteArrayToFile(file, data);
1029 assertEqualContent(data, file);
1030 }
1031
1032 public void testWriteLines_4arg() throws Exception {
1033 Object[] data = new Object[] {
1034 "hello", new StringBuffer("world"), "", "this is", null, "some text"};
1035 List list = Arrays.asList(data);
1036
1037 File file = newFile("lines.txt");
1038 FileUtils.writeLines(file, "US-ASCII", list, "*");
1039
1040 String expected = "hello*world**this is**some text*";
1041 String actual = FileUtils.readFileToString(file, "US-ASCII");
1042 assertEquals(expected, actual);
1043 }
1044
1045 public void testWriteLines_4arg_Writer_nullData() throws Exception {
1046 File file = newFile("lines.txt");
1047 FileUtils.writeLines(file, "US-ASCII", (List) null, "*");
1048
1049 assertEquals("Sizes differ", 0, file.length());
1050 }
1051
1052 public void testWriteLines_4arg_nullSeparator() throws Exception {
1053 Object[] data = new Object[] {
1054 "hello", new StringBuffer("world"), "", "this is", null, "some text"};
1055 List list = Arrays.asList(data);
1056
1057 File file = newFile("lines.txt");
1058 FileUtils.writeLines(file, "US-ASCII", list, null);
1059
1060 String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
1061 IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
1062 IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
1063 String actual = FileUtils.readFileToString(file, "US-ASCII");
1064 assertEquals(expected, actual);
1065 }
1066
1067 public void testWriteLines_3arg_nullSeparator() throws Exception {
1068 Object[] data = new Object[] {
1069 "hello", new StringBuffer("world"), "", "this is", null, "some text"};
1070 List list = Arrays.asList(data);
1071
1072 File file = newFile("lines.txt");
1073 FileUtils.writeLines(file, "US-ASCII", list);
1074
1075 String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
1076 IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
1077 IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
1078 String actual = FileUtils.readFileToString(file, "US-ASCII");
1079 assertEquals(expected, actual);
1080 }
1081
1082
1083 public void testChecksumCRC32() throws Exception {
1084
1085 String text = "Imagination is more important than knowledge - Einstein";
1086 File file = new File(getTestDirectory(), "checksum-test.txt");
1087 FileUtils.writeStringToFile(file, text, "US-ASCII");
1088
1089
1090 Checksum expectedChecksum = new CRC32();
1091 expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
1092 long expectedValue = expectedChecksum.getValue();
1093
1094
1095 long resultValue = FileUtils.checksumCRC32(file);
1096
1097 assertEquals(expectedValue, resultValue);
1098 }
1099
1100 public void testChecksum() throws Exception {
1101
1102 String text = "Imagination is more important than knowledge - Einstein";
1103 File file = new File(getTestDirectory(), "checksum-test.txt");
1104 FileUtils.writeStringToFile(file, text, "US-ASCII");
1105
1106
1107 Checksum expectedChecksum = new CRC32();
1108 expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
1109 long expectedValue = expectedChecksum.getValue();
1110
1111
1112 Checksum testChecksum = new CRC32();
1113 Checksum resultChecksum = FileUtils.checksum(file, testChecksum);
1114 long resultValue = resultChecksum.getValue();
1115
1116 assertSame(testChecksum, resultChecksum);
1117 assertEquals(expectedValue, resultValue);
1118 }
1119
1120 public void testChecksumOnNullFile() throws Exception {
1121 try {
1122 FileUtils.checksum((File) null, new CRC32());
1123 fail();
1124 } catch (NullPointerException ex) {
1125
1126 }
1127 }
1128
1129 public void testChecksumOnNullChecksum() throws Exception {
1130
1131 String text = "Imagination is more important than knowledge - Einstein";
1132 File file = new File(getTestDirectory(), "checksum-test.txt");
1133 FileUtils.writeStringToFile(file, text, "US-ASCII");
1134 try {
1135 FileUtils.checksum(file, (Checksum) null);
1136 fail();
1137 } catch (NullPointerException ex) {
1138
1139 }
1140 }
1141
1142 public void testChecksumOnDirectory() throws Exception {
1143 try {
1144 FileUtils.checksum(new File("."), new CRC32());
1145 fail();
1146 } catch (IllegalArgumentException ex) {
1147
1148 }
1149 }
1150
1151 public void testChecksumDouble() throws Exception {
1152
1153 String text1 = "Imagination is more important than knowledge - Einstein";
1154 File file1 = new File(getTestDirectory(), "checksum-test.txt");
1155 FileUtils.writeStringToFile(file1, text1, "US-ASCII");
1156
1157
1158 String text2 = "To be or not to be - Shakespeare";
1159 File file2 = new File(getTestDirectory(), "checksum-test2.txt");
1160 FileUtils.writeStringToFile(file2, text2, "US-ASCII");
1161
1162
1163 Checksum expectedChecksum = new CRC32();
1164 expectedChecksum.update(text1.getBytes("US-ASCII"), 0, text1.length());
1165 expectedChecksum.update(text2.getBytes("US-ASCII"), 0, text2.length());
1166 long expectedValue = expectedChecksum.getValue();
1167
1168
1169 Checksum testChecksum = new CRC32();
1170 FileUtils.checksum(file1, testChecksum);
1171 FileUtils.checksum(file2, testChecksum);
1172 long resultValue = testChecksum.getValue();
1173
1174 assertEquals(expectedValue, resultValue);
1175 }
1176
1177 }