-
Notifications
You must be signed in to change notification settings - Fork 662
/
FileUtilsTestCase.java
2649 lines (2241 loc) · 105 KB
/
FileUtilsTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.io;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.io.testtools.FileBasedTestCase;
import org.junit.Assert;
/**
* This is used to test FileUtils for correctness.
*
* @version $Id$
* @see FileUtils
*/
@SuppressWarnings("deprecation") // unit tests include tests of many deprecated methods
public class FileUtilsTestCase extends FileBasedTestCase {
// Test data
/**
* Size of test directory.
*/
private static final int TEST_DIRECTORY_SIZE = 0;
/**
* Size of test directory.
*/
private static final BigInteger TEST_DIRECTORY_SIZE_BI = BigInteger.ZERO;
/**
* Size (greater of zero) of test file.
*/
private static final BigInteger TEST_DIRECTORY_SIZE_GT_ZERO_BI = BigInteger.valueOf(100);
/**
* List files recursively
*/
private static final ListDirectoryWalker LIST_WALKER = new ListDirectoryWalker();
/** Delay in milliseconds to make sure test for "last modified date" are accurate */
//private static final int LAST_MODIFIED_DELAY = 600;
private final File testFile1;
private final File testFile2;
private final int testFile1Size;
private final int testFile2Size;
public FileUtilsTestCase(final String name) {
super(name);
testFile1 = new File(getTestDirectory(), "file1-test.txt");
testFile2 = new File(getTestDirectory(), "file1a-test.txt");
testFile1Size = (int)testFile1.length();
testFile2Size = (int)testFile2.length();
}
/** @see junit.framework.TestCase#setUp() */
@Override
protected void setUp() throws Exception {
getTestDirectory().mkdirs();
createFile(testFile1, testFile1Size);
createFile(testFile2, testFile2Size);
FileUtils.deleteDirectory(getTestDirectory());
getTestDirectory().mkdirs();
createFile(testFile1, testFile1Size);
createFile(testFile2, testFile2Size);
}
/** @see junit.framework.TestCase#tearDown() */
@Override
protected void tearDown() throws Exception {
FileUtils.deleteDirectory(getTestDirectory());
}
//-----------------------------------------------------------------------
/**
* Tests the {@link FileUtils#getFile(String...)} method.
*/
public void testGetFile() {
final File expected_A = new File("src");
final File expected_B = new File(expected_A, "main");
final File expected_C = new File(expected_B, "java");
assertEquals("A", expected_A, FileUtils.getFile("src"));
assertEquals("B", expected_B, FileUtils.getFile("src", "main"));
assertEquals("C", expected_C, FileUtils.getFile("src", "main", "java"));
try {
FileUtils.getFile((String[])null);
fail("Expected NullPointerException");
} catch (final NullPointerException e) {
// expected
}
}
/**
* Tests the {@link FileUtils#getFile(File, String...)} method.
*/
public void testGetFile_Parent() {
final File parent = new File("parent");
final File expected_A = new File(parent, "src");
final File expected_B = new File(expected_A, "main");
final File expected_C = new File(expected_B, "java");
assertEquals("A", expected_A, FileUtils.getFile(parent, "src"));
assertEquals("B", expected_B, FileUtils.getFile(parent, "src", "main"));
assertEquals("C", expected_C, FileUtils.getFile(parent, "src", "main", "java"));
try {
FileUtils.getFile(parent, (String[])null);
fail("Expected NullPointerException");
} catch (final NullPointerException e) {
// expected
}
try {
FileUtils.getFile((File)null, "src");
fail("Expected NullPointerException");
} catch (final NullPointerException e) {
// expected
}
}
/**
* Tests the {@link FileUtils#getTempDirectoryPath()} method.
*/
public void testGetTempDirectoryPath() {
assertEquals(System.getProperty("java.io.tmpdir"),
FileUtils.getTempDirectoryPath());
}
/**
* Tests the {@link FileUtils#getTempDirectory()} method.
*/
public void testGetTempDirectory() {
final File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
assertEquals(tempDirectory, FileUtils.getTempDirectory());
}
/**
* Tests the {@link FileUtils#getUserDirectoryPath()} method.
*/
public void testGetUserDirectoryPath() {
assertEquals(System.getProperty("user.home"),
FileUtils.getUserDirectoryPath());
}
/**
* Tests the {@link FileUtils#getUserDirectory()} method.
*/
public void testGetUserDirectory() {
final File userDirectory = new File(System.getProperty("user.home"));
assertEquals(userDirectory, FileUtils.getUserDirectory());
}
//-----------------------------------------------------------------------
public void test_openInputStream_exists() throws Exception {
final File file = new File(getTestDirectory(), "test.txt");
createLineBasedFile(file, new String[] {"Hello"});
FileInputStream in = null;
try {
in = FileUtils.openInputStream(file);
assertEquals('H', in.read());
} finally {
IOUtils.closeQuietly(in);
}
}
public void test_openInputStream_existsButIsDirectory() throws Exception {
final File directory = new File(getTestDirectory(), "subdir");
directory.mkdirs();
FileInputStream in = null;
try {
in = FileUtils.openInputStream(directory);
fail();
} catch (final IOException ioe) {
// expected
} finally {
IOUtils.closeQuietly(in);
}
}
public void test_openInputStream_notExists() throws Exception {
final File directory = new File(getTestDirectory(), "test.txt");
FileInputStream in = null;
try {
in = FileUtils.openInputStream(directory);
fail();
} catch (final IOException ioe) {
// expected
} finally {
IOUtils.closeQuietly(in);
}
}
//-----------------------------------------------------------------------
void openOutputStream_noParent(final boolean createFile) throws Exception {
final File file = new File("test.txt");
assertNull(file.getParentFile());
try {
if (createFile) {
createLineBasedFile(file, new String[]{"Hello"});}
FileOutputStream out = null;
try {
out = FileUtils.openOutputStream(file);
out.write(0);
} finally {
IOUtils.closeQuietly(out);
}
assertTrue(file.exists());
} finally {
if (file.delete() == false) {
file.deleteOnExit();
}
}
}
public void test_openOutputStream_noParentCreateFile() throws Exception {
openOutputStream_noParent(true);
}
public void test_openOutputStream_noParentNoFile() throws Exception {
openOutputStream_noParent(false);
}
public void test_openOutputStream_exists() throws Exception {
final File file = new File(getTestDirectory(), "test.txt");
createLineBasedFile(file, new String[] {"Hello"});
FileOutputStream out = null;
try {
out = FileUtils.openOutputStream(file);
out.write(0);
} finally {
IOUtils.closeQuietly(out);
}
assertTrue(file.exists());
}
public void test_openOutputStream_existsButIsDirectory() throws Exception {
final File directory = new File(getTestDirectory(), "subdir");
directory.mkdirs();
FileOutputStream out = null;
try {
out = FileUtils.openOutputStream(directory);
fail();
} catch (final IOException ioe) {
// expected
} finally {
IOUtils.closeQuietly(out);
}
}
public void test_openOutputStream_notExists() throws Exception {
final File file = new File(getTestDirectory(), "a/test.txt");
FileOutputStream out = null;
try {
out = FileUtils.openOutputStream(file);
out.write(0);
} finally {
IOUtils.closeQuietly(out);
}
assertTrue(file.exists());
}
public void test_openOutputStream_notExistsCannotCreate() throws Exception {
// according to Wikipedia, most filing systems have a 256 limit on filename
final String longStr =
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"; // 300 chars
final File file = new File(getTestDirectory(), "a/" + longStr + "/test.txt");
FileOutputStream out = null;
try {
out = FileUtils.openOutputStream(file);
fail();
} catch (final IOException ioe) {
// expected
} finally {
IOUtils.closeQuietly(out);
}
}
//-----------------------------------------------------------------------
// byteCountToDisplaySize
public void testByteCountToDisplaySizeBigInteger() {
final BigInteger b1023 = BigInteger.valueOf(1023);
final BigInteger b1025 = BigInteger.valueOf(1025);
final BigInteger KB1 = BigInteger.valueOf(1024);
final BigInteger MB1 = KB1.multiply(KB1);
final BigInteger GB1 = MB1.multiply(KB1);
final BigInteger GB2 = GB1.add(GB1);
final BigInteger TB1 = GB1.multiply(KB1);
final BigInteger PB1 = TB1.multiply(KB1);
final BigInteger EB1 = PB1.multiply(KB1);
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.ZERO), "0 bytes");
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.ONE), "1 bytes");
assertEquals(FileUtils.byteCountToDisplaySize(b1023), "1023 bytes");
assertEquals(FileUtils.byteCountToDisplaySize(KB1), "1 KB");
assertEquals(FileUtils.byteCountToDisplaySize(b1025), "1 KB");
assertEquals(FileUtils.byteCountToDisplaySize(MB1.subtract(BigInteger.ONE)), "1023 KB");
assertEquals(FileUtils.byteCountToDisplaySize(MB1), "1 MB");
assertEquals(FileUtils.byteCountToDisplaySize(MB1.add(BigInteger.ONE)), "1 MB");
assertEquals(FileUtils.byteCountToDisplaySize(GB1.subtract(BigInteger.ONE)), "1023 MB");
assertEquals(FileUtils.byteCountToDisplaySize(GB1), "1 GB");
assertEquals(FileUtils.byteCountToDisplaySize(GB1.add(BigInteger.ONE)), "1 GB");
assertEquals(FileUtils.byteCountToDisplaySize(GB2), "2 GB");
assertEquals(FileUtils.byteCountToDisplaySize(GB2.subtract(BigInteger.ONE)), "1 GB");
assertEquals(FileUtils.byteCountToDisplaySize(TB1), "1 TB");
assertEquals(FileUtils.byteCountToDisplaySize(PB1), "1 PB");
assertEquals(FileUtils.byteCountToDisplaySize(EB1), "1 EB");
assertEquals(FileUtils.byteCountToDisplaySize(Long.MAX_VALUE), "7 EB");
// Other MAX_VALUEs
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Character.MAX_VALUE)), "63 KB");
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Short.MAX_VALUE)), "31 KB");
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Integer.MAX_VALUE)), "1 GB");
}
public void testByteCountToDisplaySizeLong() {
assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
assertEquals(FileUtils.byteCountToDisplaySize(1), "1 bytes");
assertEquals(FileUtils.byteCountToDisplaySize(1023), "1023 bytes");
assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB");
assertEquals(FileUtils.byteCountToDisplaySize(1025), "1 KB");
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1023), "1023 KB");
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024), "1 MB");
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1025), "1 MB");
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1023), "1023 MB");
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024), "1 GB");
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1025), "1 GB");
assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 2), "2 GB");
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024 * 2 - 1), "1 GB");
assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024), "1 TB");
assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024), "1 PB");
assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024 * 1024), "1 EB");
assertEquals(FileUtils.byteCountToDisplaySize(Long.MAX_VALUE), "7 EB");
// Other MAX_VALUEs
assertEquals(FileUtils.byteCountToDisplaySize(Character.MAX_VALUE), "63 KB");
assertEquals(FileUtils.byteCountToDisplaySize(Short.MAX_VALUE), "31 KB");
assertEquals(FileUtils.byteCountToDisplaySize(Integer.MAX_VALUE), "1 GB");
}
//-----------------------------------------------------------------------
public void testToFile1() throws Exception {
final URL url = new URL("file", null, "a/b/c/file.txt");
final File file = FileUtils.toFile(url);
assertTrue(file.toString().indexOf("file.txt") >= 0);
}
public void testToFile2() throws Exception {
final URL url = new URL("file", null, "a/b/c/file%20n%61me%2520.tx%74");
final File file = FileUtils.toFile(url);
assertTrue(file.toString().indexOf("file name%20.txt") >= 0);
}
public void testToFile3() throws Exception {
assertEquals(null, FileUtils.toFile((URL) null));
assertEquals(null, FileUtils.toFile(new URL("http://jakarta.apache.org")));
}
public void testToFile4() throws Exception {
final URL url = new URL("file", null, "a/b/c/file%%20%me.txt%");
final File file = FileUtils.toFile(url);
assertTrue(file.toString().indexOf("file% %me.txt%") >= 0);
}
/** IO-252 */
public void testToFile5() throws Exception {
final URL url = new URL("file", null, "both%20are%20100%20%25%20true");
final File file = FileUtils.toFile(url);
assertEquals("both are 100 % true", file.toString());
}
public void testToFileUtf8() throws Exception {
final URL url = new URL("file", null, "/home/%C3%A4%C3%B6%C3%BC%C3%9F");
final File file = FileUtils.toFile(url);
assertTrue(file.toString().indexOf("\u00E4\u00F6\u00FC\u00DF") >= 0);
}
public void testDecodeUrl() {
assertEquals("", FileUtils.decodeUrl(""));
assertEquals("foo", FileUtils.decodeUrl("foo"));
assertEquals("+", FileUtils.decodeUrl("+"));
assertEquals("% ", FileUtils.decodeUrl("%25%20"));
assertEquals("%20", FileUtils.decodeUrl("%2520"));
assertEquals("jar:file:/C:/dir/sub dir/1.0/foo-1.0.jar!/org/Bar.class", FileUtils
.decodeUrl("jar:file:/C:/dir/sub%20dir/1.0/foo-1.0.jar!/org/Bar.class"));
}
public void testDecodeUrlLenient() {
assertEquals(" ", FileUtils.decodeUrl(" "));
assertEquals("\u00E4\u00F6\u00FC\u00DF", FileUtils.decodeUrl("\u00E4\u00F6\u00FC\u00DF"));
assertEquals("%", FileUtils.decodeUrl("%"));
assertEquals("% ", FileUtils.decodeUrl("%%20"));
assertEquals("%2", FileUtils.decodeUrl("%2"));
assertEquals("%2G", FileUtils.decodeUrl("%2G"));
}
public void testDecodeUrlNullSafe() {
assertNull(FileUtils.decodeUrl(null));
}
public void testDecodeUrlEncodingUtf8() {
assertEquals("\u00E4\u00F6\u00FC\u00DF", FileUtils.decodeUrl("%C3%A4%C3%B6%C3%BC%C3%9F"));
}
// toFiles
public void testToFiles1() throws Exception {
final URL[] urls = new URL[] {
new URL("file", null, "file1.txt"),
new URL("file", null, "file2.txt"),
};
final File[] files = FileUtils.toFiles(urls);
assertEquals(urls.length, files.length);
assertEquals("File: " + files[0], true, files[0].toString().indexOf("file1.txt") >= 0);
assertEquals("File: " + files[1], true, files[1].toString().indexOf("file2.txt") >= 0);
}
public void testToFiles2() throws Exception {
final URL[] urls = new URL[] {
new URL("file", null, "file1.txt"),
null,
};
final File[] files = FileUtils.toFiles(urls);
assertEquals(urls.length, files.length);
assertEquals("File: " + files[0], true, files[0].toString().indexOf("file1.txt") >= 0);
assertEquals("File: " + files[1], null, files[1]);
}
public void testToFiles3() throws Exception {
final URL[] urls = null;
final File[] files = FileUtils.toFiles(urls);
assertEquals(0, files.length);
}
public void testToFiles3a() throws Exception {
final URL[] urls = new URL[0]; // empty array
final File[] files = FileUtils.toFiles(urls);
assertEquals(0, files.length);
}
public void testToFiles4() throws Exception {
final URL[] urls = new URL[] {
new URL("file", null, "file1.txt"),
new URL("http", "jakarta.apache.org", "file1.txt"),
};
try {
FileUtils.toFiles(urls);
fail();
} catch (final IllegalArgumentException ex) {}
}
// toURLs
public void testToURLs1() throws Exception {
final File[] files = new File[] {
new File(getTestDirectory(), "file1.txt"),
new File(getTestDirectory(), "file2.txt"),
new File(getTestDirectory(), "test file.txt"),
};
final URL[] urls = FileUtils.toURLs(files);
assertEquals(files.length, urls.length);
assertTrue(urls[0].toExternalForm().startsWith("file:"));
assertTrue(urls[0].toExternalForm().indexOf("file1.txt") >= 0);
assertTrue(urls[1].toExternalForm().startsWith("file:"));
assertTrue(urls[1].toExternalForm().indexOf("file2.txt") >= 0);
// Test escaped char
assertTrue(urls[2].toExternalForm().startsWith("file:"));
assertTrue(urls[2].toExternalForm().indexOf("test%20file.txt") >= 0);
}
// public void testToURLs2() throws Exception {
// File[] files = new File[] {
// new File(getTestDirectory(), "file1.txt"),
// null,
// };
// URL[] urls = FileUtils.toURLs(files);
//
// assertEquals(files.length, urls.length);
// assertTrue(urls[0].toExternalForm().startsWith("file:"));
// assertTrue(urls[0].toExternalForm().indexOf("file1.txt") > 0);
// assertEquals(null, urls[1]);
// }
//
// public void testToURLs3() throws Exception {
// File[] files = null;
// URL[] urls = FileUtils.toURLs(files);
//
// assertEquals(0, urls.length);
// }
public void testToURLs3a() throws Exception {
final File[] files = new File[0]; // empty array
final URL[] urls = FileUtils.toURLs(files);
assertEquals(0, urls.length);
}
// contentEquals
public void testContentEquals() throws Exception {
// Non-existent files
final File file = new File(getTestDirectory(), getName());
final File file2 = new File(getTestDirectory(), getName() + "2");
// both don't exist
assertTrue(FileUtils.contentEquals(file, file));
assertTrue(FileUtils.contentEquals(file, file2));
assertTrue(FileUtils.contentEquals(file2, file2));
assertTrue(FileUtils.contentEquals(file2, file));
// Directories
try {
FileUtils.contentEquals(getTestDirectory(), getTestDirectory());
fail("Comparing directories should fail with an IOException");
} catch (final IOException ioe) {
//expected
}
// Different files
final File objFile1 =
new File(getTestDirectory(), getName() + ".object");
objFile1.deleteOnExit();
FileUtils.copyURLToFile(
getClass().getResource("/java/lang/Object.class"),
objFile1);
final File objFile1b =
new File(getTestDirectory(), getName() + ".object2");
objFile1.deleteOnExit();
FileUtils.copyURLToFile(
getClass().getResource("/java/lang/Object.class"),
objFile1b);
final File objFile2 =
new File(getTestDirectory(), getName() + ".collection");
objFile2.deleteOnExit();
FileUtils.copyURLToFile(
getClass().getResource("/java/util/Collection.class"),
objFile2);
assertFalse(FileUtils.contentEquals(objFile1, objFile2));
assertFalse(FileUtils.contentEquals(objFile1b, objFile2));
assertTrue(FileUtils.contentEquals(objFile1, objFile1b));
assertTrue(FileUtils.contentEquals(objFile1, objFile1));
assertTrue(FileUtils.contentEquals(objFile1b, objFile1b));
assertTrue(FileUtils.contentEquals(objFile2, objFile2));
// Equal files
file.createNewFile();
file2.createNewFile();
assertTrue(FileUtils.contentEquals(file, file));
assertTrue(FileUtils.contentEquals(file, file2));
}
public void testContentEqualsIgnoreEOL() throws Exception {
// Non-existent files
final File file1 = new File(getTestDirectory(), getName());
final File file2 = new File(getTestDirectory(), getName() + "2");
// both don't exist
assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file1, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file2, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(file2, file2, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(file2, file1, null));
// Directories
try {
FileUtils.contentEqualsIgnoreEOL(getTestDirectory(), getTestDirectory(), null);
fail("Comparing directories should fail with an IOException");
} catch (final IOException ioe) {
//expected
}
// Different files
final File tfile1 = new File(getTestDirectory(), getName() + ".txt1");
tfile1.deleteOnExit();
FileUtils.write(tfile1,"123\r");
final File tfile2 = new File(getTestDirectory(), getName() + ".txt2");
tfile1.deleteOnExit();
FileUtils.write(tfile2,"123\n");
final File tfile3 = new File(getTestDirectory(), getName() + ".collection");
tfile3.deleteOnExit();
FileUtils.write(tfile3,"123\r\n2");
assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile1, tfile1, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile2, tfile2, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile3, tfile3, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile1, tfile2, null));
assertFalse(FileUtils.contentEqualsIgnoreEOL(tfile1, tfile3, null));
assertFalse(FileUtils.contentEqualsIgnoreEOL(tfile2, tfile3, null));
final URL urlCR = getClass().getResource("FileUtilsTestDataCR.dat");
assertNotNull(urlCR);
final File cr = new File(urlCR.getPath());
assertTrue(cr.exists());
final URL urlCRLF = getClass().getResource("FileUtilsTestDataCRLF.dat");
assertNotNull(urlCRLF);
final File crlf = new File(urlCRLF.getPath());
assertTrue(crlf.exists());
final URL urlLF = getClass().getResource("FileUtilsTestDataLF.dat");
assertNotNull(urlLF);
final File lf = new File(urlLF.getPath());
assertTrue(lf.exists());
assertTrue(FileUtils.contentEqualsIgnoreEOL(cr, cr, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(crlf, crlf, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(lf, lf, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(cr, crlf, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(cr, lf, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(crlf, lf, null));
// Check the files behave OK when EOL is not ignored
assertTrue(FileUtils.contentEquals(cr, cr));
assertTrue(FileUtils.contentEquals(crlf, crlf));
assertTrue(FileUtils.contentEquals(lf, lf));
assertFalse(FileUtils.contentEquals(cr, crlf));
assertFalse(FileUtils.contentEquals(cr, lf));
assertFalse(FileUtils.contentEquals(crlf, lf));
// Equal files
file1.createNewFile();
file2.createNewFile();
assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file1, null));
assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file2, null));
}
// copyURLToFile
public void testCopyURLToFile() throws Exception {
// Creates file
final File file = new File(getTestDirectory(), getName());
file.deleteOnExit();
// Loads resource
final String resourceName = "/java/lang/Object.class";
FileUtils.copyURLToFile(getClass().getResource(resourceName), file);
// Tests that resuorce was copied correctly
final FileInputStream fis = new FileInputStream(file);
try {
assertTrue(
"Content is not equal.",
IOUtils.contentEquals(
getClass().getResourceAsStream(resourceName),
fis));
} finally {
fis.close();
}
//TODO Maybe test copy to itself like for copyFile()
}
public void testCopyURLToFileWithTimeout() throws Exception {
// Creates file
final File file = new File(getTestDirectory(), "testCopyURLToFileWithTimeout");
file.deleteOnExit();
// Loads resource
final String resourceName = "/java/lang/Object.class";
FileUtils.copyURLToFile(getClass().getResource(resourceName), file, 500, 500);
// Tests that resuorce was copied correctly
final FileInputStream fis = new FileInputStream(file);
try {
assertTrue(
"Content is not equal.",
IOUtils.contentEquals(
getClass().getResourceAsStream(resourceName),
fis));
} finally {
fis.close();
}
//TODO Maybe test copy to itself like for copyFile()
}
// forceMkdir
public void testForceMkdir() throws Exception {
// Tests with existing directory
FileUtils.forceMkdir(getTestDirectory());
// Creates test file
final File testFile = new File(getTestDirectory(), getName());
testFile.deleteOnExit();
testFile.createNewFile();
assertTrue("Test file does not exist.", testFile.exists());
// Tests with existing file
try {
FileUtils.forceMkdir(testFile);
fail("Exception expected.");
} catch (final IOException ex) {}
testFile.delete();
// Tests with non-existent directory
FileUtils.forceMkdir(testFile);
assertTrue("Directory was not created.", testFile.exists());
}
public void testForceMkdirParent() throws Exception {
// Tests with existing directory
assertTrue(getTestDirectory().exists());
final File testParentDir = new File(getTestDirectory(), "testForceMkdirParent");
try {
testParentDir.delete();
assertFalse(testParentDir.exists());
final File testFile = new File(testParentDir, "test.txt");
assertFalse(testParentDir.exists());
assertFalse(testFile.exists());
// Create
FileUtils.forceMkdirParent(testFile);
assertTrue(testParentDir.exists());
assertFalse(testFile.exists());
// Again
FileUtils.forceMkdirParent(testFile);
assertTrue(testParentDir.exists());
assertFalse(testFile.exists());
} finally {
testParentDir.delete();
}
}
// sizeOfDirectory
public void testSizeOfDirectory() throws Exception {
final File file = new File(getTestDirectory(), getName());
// Non-existent file
try {
FileUtils.sizeOfDirectory(file);
fail("Exception expected.");
} catch (final IllegalArgumentException ex) {}
// Creates file
file.createNewFile();
file.deleteOnExit();
// Existing file
try {
FileUtils.sizeOfDirectory(file);
fail("Exception expected.");
} catch (final IllegalArgumentException ex) {}
// Existing directory
file.delete();
file.mkdir();
// Create a cyclic symlink
this.createCircularSymLink(file);
assertEquals(
"Unexpected directory size",
TEST_DIRECTORY_SIZE,
FileUtils.sizeOfDirectory(file));
}
private void createCircularSymLink(final File file) throws IOException {
if(!FilenameUtils.isSystemWindows()) {
Runtime.getRuntime()
.exec("ln -s " + file + "/.. " + file + "/cycle");
} else {
try {
Runtime.getRuntime()
.exec("mklink /D " + file + "/cycle" + file + "/.. ");
} catch(final IOException ioe) { // So that tests run in FAT filesystems
//don't fail
}
}
}
public void testSizeOfDirectoryAsBigInteger() throws Exception {
final File file = new File(getTestDirectory(), getName());
// Non-existent file
try {
FileUtils.sizeOfDirectoryAsBigInteger(file);
fail("Exception expected.");
} catch (final IllegalArgumentException ex) {
}
// Creates file
file.createNewFile();
file.deleteOnExit();
// Existing file
try {
FileUtils.sizeOfDirectoryAsBigInteger(file);
fail("Exception expected.");
} catch (final IllegalArgumentException ex) {
}
// Existing directory
file.delete();
file.mkdir();
this.createCircularSymLink(file);
assertEquals("Unexpected directory size", TEST_DIRECTORY_SIZE_BI, FileUtils.sizeOfDirectoryAsBigInteger(file));
// Existing directory which size is greater than zero
file.delete();
file.mkdir();
final File nonEmptyFile = new File(file, "nonEmptyFile" + System.nanoTime());
this.createFile(nonEmptyFile, TEST_DIRECTORY_SIZE_GT_ZERO_BI.longValue());
nonEmptyFile.deleteOnExit();
assertEquals("Unexpected directory size", TEST_DIRECTORY_SIZE_GT_ZERO_BI,
FileUtils.sizeOfDirectoryAsBigInteger(file));
nonEmptyFile.delete();
file.delete();
}
// Compare sizes of a directory tree using long and BigInteger methods
public void testCompareSizeOf() {
final File start = new File("src/test/java");
final long sizeLong1 = FileUtils.sizeOf(start);
final BigInteger sizeBig = FileUtils.sizeOfAsBigInteger(start);
final long sizeLong2 = FileUtils.sizeOf(start);
assertEquals("Size should not change",sizeLong1, sizeLong2);
assertEquals("longSize should equal BigSize",sizeLong1, sizeBig.longValue());
}
/**
* Tests the {@link FileUtils#sizeOf(File)} method.
* @throws Exception
*/
public void testSizeOf() throws Exception {
final File file = new File(getTestDirectory(), getName());
// Null argument
try {
FileUtils.sizeOf(null);
fail("Exception expected.");
} catch (final NullPointerException ex) {}
// Non-existent file
try {
FileUtils.sizeOf(file);
fail("Exception expected.");
} catch (final IllegalArgumentException ex) {}
// Creates file
file.createNewFile();
file.deleteOnExit();
// New file
assertEquals(0, FileUtils.sizeOf(file));
file.delete();
// Existing file
assertEquals("Unexpected files size",
testFile1Size,
FileUtils.sizeOf(testFile1));
// Existing directory
assertEquals("Unexpected directory size",
TEST_DIRECTORY_SIZE,
FileUtils.sizeOf(getTestDirectory()));
}
/**
* Tests the {@link FileUtils#sizeOf(File)} method.
* @throws Exception
*/
public void testSizeOfAsBigInteger() throws Exception {
final File file = new File(getTestDirectory(), getName());
// Null argument
try {
FileUtils.sizeOfAsBigInteger(null);
fail("Exception expected.");
} catch (final NullPointerException ex) {}
// Non-existent file
try {
FileUtils.sizeOfAsBigInteger(file);
fail("Exception expected.");
} catch (final IllegalArgumentException ex) {}
// Creates file
file.createNewFile();
file.deleteOnExit();
// New file
assertEquals(BigInteger.ZERO, FileUtils.sizeOfAsBigInteger(file));
file.delete();
// Existing file
assertEquals("Unexpected files size",
BigInteger.valueOf(testFile1Size),
FileUtils.sizeOfAsBigInteger(testFile1));
// Existing directory
assertEquals("Unexpected directory size",
TEST_DIRECTORY_SIZE_BI,
FileUtils.sizeOfAsBigInteger(getTestDirectory()));
}
// isFileNewer / isFileOlder
public void testIsFileNewerOlder() throws Exception {
final File reference = new File(getTestDirectory(), "FileUtils-reference.txt");
final File oldFile = new File(getTestDirectory(), "FileUtils-old.txt");
final File newFile = new File(getTestDirectory(), "FileUtils-new.txt");
final File invalidFile = new File(getTestDirectory(), "FileUtils-invalid-file.txt");
// Create Files
createFile(oldFile, 0);
do {
try {
Thread.sleep(1000);
} catch(final InterruptedException ie) {
// ignore
}
createFile(reference, 0);
} while( oldFile.lastModified() == reference.lastModified() );
final Date date = new Date();
final long now = date.getTime();
do {
try {
Thread.sleep(1000);
} catch(final InterruptedException ie) {
// ignore
}
createFile(newFile, 0);
} while( reference.lastModified() == newFile.lastModified() );
// Test isFileNewer()
assertFalse("Old File - Newer - File", FileUtils.isFileNewer(oldFile, reference));
assertFalse("Old File - Newer - Date", FileUtils.isFileNewer(oldFile, date));
assertFalse("Old File - Newer - Mili", FileUtils.isFileNewer(oldFile, now));
assertTrue("New File - Newer - File", FileUtils.isFileNewer(newFile, reference));
assertTrue("New File - Newer - Date", FileUtils.isFileNewer(newFile, date));
assertTrue("New File - Newer - Mili", FileUtils.isFileNewer(newFile, now));
assertFalse("Invalid - Newer - File", FileUtils.isFileNewer(invalidFile, reference));
final String invalidFileName = invalidFile.getName();
try {
FileUtils.isFileNewer(newFile, invalidFile);
fail("Should have cause IllegalArgumentException");
} catch (final IllegalArgumentException iae){
final String message = iae.getMessage();
assertTrue("Message should contain: "+invalidFileName+ " but was: "+message,message.contains(invalidFileName));
}
// Test isFileOlder()
assertTrue("Old File - Older - File", FileUtils.isFileOlder(oldFile, reference));