forked from apache/commons-io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtilsTest.java
More file actions
3517 lines (3079 loc) · 158 KB
/
Copy pathFileUtilsTest.java
File metadata and controls
3517 lines (3079 loc) · 158 KB
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
*
* https://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 static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.BufferedOutputStream;
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.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
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.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.apache.commons.io.file.AbstractTempDirTest;
import org.apache.commons.io.file.Counters.PathCounters;
import org.apache.commons.io.file.PathUtils;
import org.apache.commons.io.file.TempDirectory;
import org.apache.commons.io.file.TempFile;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.io.test.TestUtils;
import org.apache.commons.lang3.SystemProperties;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
/**
* Tests {@link FileUtils}.
*/
@SuppressWarnings({"deprecation", "ResultOfMethodCallIgnored"}) // unit tests include tests of many deprecated methods
class FileUtilsTest extends AbstractTempDirTest {
/**
* DirectoryWalker implementation that recursively lists all files and directories.
*/
static class ListDirectoryWalker extends DirectoryWalker<File> {
ListDirectoryWalker() {
}
@Override
protected void handleDirectoryStart(final File directory, final int depth, final Collection<File> results) throws IOException {
// Add all directories except the starting directory
if (depth > 0) {
results.add(directory);
}
}
@Override
protected void handleFile(final File file, final int depth, final Collection<File> results) throws IOException {
results.add(file);
}
List<File> list(final File startDirectory) throws IOException {
final ArrayList<File> files = new ArrayList<>();
walk(startDirectory, files);
return files;
}
}
private static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");
private static final String UTF_8 = StandardCharsets.UTF_8.name();
/** Test data. */
private static final long DATE3 = 1_000_000_002_000L;
/** Test data. */
private static final long DATE2 = 1_000_000_001_000L;
/** Test data. */
private static final long DATE1 = 1_000_000_000_000L;
/**
* 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();
private File testFile1;
private File testFile2;
private long testFile1Size;
private long testFile2Size;
private void assertContentMatchesAfterCopyURLToFileFor(final String resourceName, final File destination) throws IOException {
FileUtils.copyURLToFile(getClass().getResource(resourceName), destination);
try (InputStream fis = Files.newInputStream(destination.toPath());
InputStream expected = getClass().getResourceAsStream(resourceName)) {
assertTrue(IOUtils.contentEquals(expected, fis), "Content is not equal.");
}
}
private void backDateFile10Minutes(final File testFile) throws IOException {
final long mins10 = 1000 * 60 * 10;
final long lastModified1 = getLastModifiedMillis(testFile);
assertTrue(setLastModifiedMillis(testFile, lastModified1 - mins10));
// ensure it was changed
assertNotEquals(getLastModifiedMillis(testFile), lastModified1, "Should have changed source date");
}
private void consumeRemaining(final Iterator<File> iterator) {
if (iterator != null) {
iterator.forEachRemaining(e -> {
// noop
});
}
}
private Path createCircularOsSymbolicLink(final String linkName, final String targetName) throws IOException {
return Files.createSymbolicLink(Paths.get(linkName), Paths.get(targetName));
}
/**
* May throw java.nio.file.FileSystemException: C:\Users\...\FileUtilsTestCase\cycle: A required privilege is not held
* by the client. On Windows, you are fine if you run a terminal with admin karma.
*/
private void createCircularSymbolicLink(final File file) throws IOException {
assertTrue(file.exists());
final String linkName = file + "/cycle";
final String targetName = file + "/..";
assertTrue(file.exists());
final Path linkPath = Paths.get(linkName);
assertFalse(Files.exists(linkPath));
final Path targetPath = Paths.get(targetName);
assertTrue(Files.exists(targetPath));
try {
// May throw java.nio.file.FileSystemException: C:\Users\...\FileUtilsTestCase\cycle: A required privilege is not held by the client.
// On Windows, you are fine if you run a terminal with admin karma.
Files.createSymbolicLink(linkPath, targetPath);
} catch (final UnsupportedOperationException e) {
createCircularOsSymbolicLink(linkName, targetName);
}
// Sanity check:
assertTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symbolic link here: " + linkName);
}
private void createFilesForTestCopyDirectory(final File grandParentDir, final File parentDir, final File childDir) throws IOException {
final File childDir2 = new File(parentDir, "child2");
final File grandChildDir = new File(childDir, "grandChild");
final File grandChild2Dir = new File(childDir2, "grandChild2");
final File file1 = new File(grandParentDir, "file1.txt");
final File file2 = new File(parentDir, "file2.txt");
final File file3 = new File(childDir, "file3.txt");
final File file4 = new File(childDir2, "file4.txt");
final File file5 = new File(grandChildDir, "file5.txt");
final File file6 = new File(grandChild2Dir, "file6.txt");
FileUtils.deleteDirectory(grandParentDir);
grandChildDir.mkdirs();
grandChild2Dir.mkdirs();
FileUtils.writeStringToFile(file1, "File 1 in grandparent", "UTF8");
FileUtils.writeStringToFile(file2, "File 2 in parent", "UTF8");
FileUtils.writeStringToFile(file3, "File 3 in child", "UTF8");
FileUtils.writeStringToFile(file4, "File 4 in child2", "UTF8");
FileUtils.writeStringToFile(file5, "File 5 in grandChild", "UTF8");
FileUtils.writeStringToFile(file6, "File 6 in grandChild2", "UTF8");
}
private ImmutablePair<Path, Path> createTempSymbolicLinkedRelativeDir() throws IOException {
final Path targetDir = tempDirPath.resolve("subdir");
final Path symLinkedDir = tempDirPath.resolve("symlinked-dir");
Files.createDirectory(targetDir);
Files.createSymbolicLink(symLinkedDir, targetDir);
return ImmutablePair.of(symLinkedDir, targetDir);
}
private Set<String> getFilePathSet(final List<File> files) {
return files.stream().map(f -> {
try {
return f.getCanonicalPath();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toSet());
}
private long getLastModifiedMillis(final File file) throws IOException {
return FileUtils.lastModified(file);
}
private String getName() {
return this.getClass().getSimpleName();
}
private void iterateFilesAndDirs(final File dir, final IOFileFilter fileFilter,
final IOFileFilter dirFilter, final Collection<File> expectedFilesAndDirs) {
final Iterator<File> iterator = FileUtils.iterateFilesAndDirs(dir, fileFilter, dirFilter);
int filesCount = 0;
try {
final List<File> actualFiles = new ArrayList<>();
while (iterator.hasNext()) {
filesCount++;
final File file = iterator.next();
actualFiles.add(file);
assertTrue(expectedFilesAndDirs.contains(file),
() -> "Unexpected directory/file " + file + ", expected one of " + expectedFilesAndDirs);
}
assertEquals(expectedFilesAndDirs.size(), filesCount, actualFiles::toString);
} finally {
// MUST consume until the end in order to close the underlying stream.
consumeRemaining(iterator);
}
}
private void openOutputStream_noParent(final boolean createFile) throws Exception {
final File file = new File("test.txt");
assertNull(file.getParentFile());
try {
if (createFile) {
TestUtils.createLineFileUtf8(file, new String[]{"Hello"});
}
try (FileOutputStream out = FileUtils.openOutputStream(file)) {
out.write(0);
}
assertTrue(file.exists());
} finally {
if (!file.delete()) {
file.deleteOnExit();
}
}
}
private boolean setLastModifiedMillis(final File testFile, final long millis) {
return testFile.setLastModified(millis);
}
@BeforeEach
public void setUp() throws Exception {
testFile1 = new File(tempDirFile, "file1-test.txt");
testFile2 = new File(tempDirFile, "file1a-test.txt");
testFile1Size = testFile1.length();
testFile2Size = testFile2.length();
if (!testFile1.getParentFile().exists()) {
fail("Cannot create file " + testFile1
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output3 =
new BufferedOutputStream(Files.newOutputStream(testFile1.toPath()))) {
TestUtils.generateTestData(output3, testFile1Size);
}
if (!testFile2.getParentFile().exists()) {
fail("Cannot create file " + testFile2
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output2 =
new BufferedOutputStream(Files.newOutputStream(testFile2.toPath()))) {
TestUtils.generateTestData(output2, testFile2Size);
}
FileUtils.deleteDirectory(tempDirFile);
tempDirFile.mkdirs();
if (!testFile1.getParentFile().exists()) {
fail("Cannot create file " + testFile1
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output1 =
new BufferedOutputStream(Files.newOutputStream(testFile1.toPath()))) {
TestUtils.generateTestData(output1, testFile1Size);
}
if (!testFile2.getParentFile().exists()) {
fail("Cannot create file " + testFile2
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output =
new BufferedOutputStream(Files.newOutputStream(testFile2.toPath()))) {
TestUtils.generateTestData(output, testFile2Size);
}
}
// byteCountToDisplaySize
@Test
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("0 bytes", FileUtils.byteCountToDisplaySize(BigInteger.ZERO));
assertEquals("1 bytes", FileUtils.byteCountToDisplaySize(BigInteger.ONE));
assertEquals("1023 bytes", FileUtils.byteCountToDisplaySize(b1023));
assertEquals("1 KB", FileUtils.byteCountToDisplaySize(KB1));
assertEquals("1 KB", FileUtils.byteCountToDisplaySize(b1025));
assertEquals("1023 KB", FileUtils.byteCountToDisplaySize(MB1.subtract(BigInteger.ONE)));
assertEquals("1 MB", FileUtils.byteCountToDisplaySize(MB1));
assertEquals("1 MB", FileUtils.byteCountToDisplaySize(MB1.add(BigInteger.ONE)));
assertEquals("1023 MB", FileUtils.byteCountToDisplaySize(GB1.subtract(BigInteger.ONE)));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(GB1));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(GB1.add(BigInteger.ONE)));
assertEquals("2 GB", FileUtils.byteCountToDisplaySize(GB2));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(GB2.subtract(BigInteger.ONE)));
assertEquals("1 TB", FileUtils.byteCountToDisplaySize(TB1));
assertEquals("1 PB", FileUtils.byteCountToDisplaySize(PB1));
assertEquals("1 EB", FileUtils.byteCountToDisplaySize(EB1));
assertEquals("7 EB", FileUtils.byteCountToDisplaySize(Long.MAX_VALUE));
// Other MAX_VALUEs
assertEquals("63 KB", FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Character.MAX_VALUE)));
assertEquals("31 KB", FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Short.MAX_VALUE)));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Integer.MAX_VALUE)));
}
@SuppressWarnings("NumericOverflow")
@Test
void testByteCountToDisplaySizeLong() {
assertEquals("0 bytes", FileUtils.byteCountToDisplaySize(0));
assertEquals("1 bytes", FileUtils.byteCountToDisplaySize(1));
assertEquals("1023 bytes", FileUtils.byteCountToDisplaySize(1023));
assertEquals("1 KB", FileUtils.byteCountToDisplaySize(1024));
assertEquals("1 KB", FileUtils.byteCountToDisplaySize(1025));
assertEquals("1023 KB", FileUtils.byteCountToDisplaySize(1024 * 1023));
assertEquals("1 MB", FileUtils.byteCountToDisplaySize(1024 * 1024));
assertEquals("1 MB", FileUtils.byteCountToDisplaySize(1024 * 1025));
assertEquals("1023 MB", FileUtils.byteCountToDisplaySize(1024 * 1024 * 1023));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(1024 * 1024 * 1025));
assertEquals("2 GB", FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 2));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 2 - 1));
assertEquals("1 TB", FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024));
assertEquals("1 PB", FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024));
assertEquals("1 EB", FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024 * 1024));
assertEquals("7 EB", FileUtils.byteCountToDisplaySize(Long.MAX_VALUE));
// Other MAX_VALUEs
assertEquals("63 KB", FileUtils.byteCountToDisplaySize(Character.MAX_VALUE));
assertEquals("31 KB", FileUtils.byteCountToDisplaySize(Short.MAX_VALUE));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(Integer.MAX_VALUE));
}
@Test
void testByteCountToDisplaySizeNumber() {
assertEquals("0 bytes", FileUtils.byteCountToDisplaySize(Integer.valueOf(0)));
assertEquals("1 bytes", FileUtils.byteCountToDisplaySize(Integer.valueOf(1)));
assertEquals("1023 bytes", FileUtils.byteCountToDisplaySize(Integer.valueOf(1023)));
assertEquals("1 KB", FileUtils.byteCountToDisplaySize(Integer.valueOf(1024)));
assertEquals("1 KB", FileUtils.byteCountToDisplaySize(Integer.valueOf(1025)));
assertEquals("1023 KB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024 * 1023)));
assertEquals("1 MB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024 * 1024)));
assertEquals("1 MB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024 * 1025)));
assertEquals("1023 MB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024 * 1024 * 1023)));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024 * 1024 * 1024)));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024 * 1024 * 1025)));
assertEquals("2 GB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024L * 1024 * 1024 * 2)));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024L * 1024 * 1024 * 2 - 1)));
assertEquals("1 TB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024L * 1024 * 1024 * 1024)));
assertEquals("1 PB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024L * 1024 * 1024 * 1024 * 1024)));
assertEquals("1 EB", FileUtils.byteCountToDisplaySize(Long.valueOf(1024L * 1024 * 1024 * 1024 * 1024 * 1024)));
assertEquals("7 EB", FileUtils.byteCountToDisplaySize(Long.valueOf(Long.MAX_VALUE)));
// Other MAX_VALUEs
assertEquals("63 KB", FileUtils.byteCountToDisplaySize(Integer.valueOf(Character.MAX_VALUE)));
assertEquals("31 KB", FileUtils.byteCountToDisplaySize(Short.valueOf(Short.MAX_VALUE)));
assertEquals("1 GB", FileUtils.byteCountToDisplaySize(Integer.valueOf(Integer.MAX_VALUE)));
}
@Test
void testChecksum() throws Exception {
// create a test file
final String text = "Imagination is more important than knowledge - Einstein";
final File file = new File(tempDirFile, "checksum-test.txt");
FileUtils.writeStringToFile(file, text, StandardCharsets.US_ASCII.name());
// compute the expected checksum
final Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text.getBytes(StandardCharsets.US_ASCII), 0, text.length());
final long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
final Checksum testChecksum = new CRC32();
final Checksum resultChecksum = FileUtils.checksum(file, testChecksum);
final long resultValue = resultChecksum.getValue();
assertSame(testChecksum, resultChecksum);
assertEquals(expectedValue, resultValue);
}
@Test
void testChecksumCRC32() throws Exception {
// create a test file
final String text = "Imagination is more important than knowledge - Einstein";
final File file = new File(tempDirFile, "checksum-test.txt");
FileUtils.writeStringToFile(file, text, StandardCharsets.US_ASCII.name());
// compute the expected checksum
final Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text.getBytes(StandardCharsets.US_ASCII), 0, text.length());
final long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
final long resultValue = FileUtils.checksumCRC32(file);
assertEquals(expectedValue, resultValue);
}
@Test
void testChecksumDouble() throws Exception {
// create a test file
final String text1 = "Imagination is more important than knowledge - Einstein";
final File file1 = new File(tempDirFile, "checksum-test.txt");
FileUtils.writeStringToFile(file1, text1, StandardCharsets.US_ASCII.name());
// create a second test file
final String text2 = "To be or not to be - Shakespeare";
final File file2 = new File(tempDirFile, "checksum-test2.txt");
FileUtils.writeStringToFile(file2, text2, StandardCharsets.US_ASCII.name());
// compute the expected checksum
final Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text1.getBytes(StandardCharsets.US_ASCII), 0, text1.length());
expectedChecksum.update(text2.getBytes(StandardCharsets.US_ASCII), 0, text2.length());
final long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
final Checksum testChecksum = new CRC32();
FileUtils.checksum(file1, testChecksum);
FileUtils.checksum(file2, testChecksum);
final long resultValue = testChecksum.getValue();
assertEquals(expectedValue, resultValue);
}
@Test
void testChecksumOnDirectory() {
assertThrows(IllegalArgumentException.class, () -> FileUtils.checksum(FileUtils.current(), new CRC32()));
}
@Test
void testChecksumOnNullChecksum() throws Exception {
// create a test file
final String text = "Imagination is more important than knowledge - Einstein";
final File file = new File(tempDirFile, "checksum-test.txt");
FileUtils.writeStringToFile(file, text, StandardCharsets.US_ASCII.name());
assertThrows(NullPointerException.class, () -> FileUtils.checksum(file, null));
}
@Test
void testChecksumOnNullFile() {
assertThrows(NullPointerException.class, () -> FileUtils.checksum(null, new CRC32()));
}
// Compare sizes of a directory tree using long and BigInteger methods
@Test
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(sizeLong1, sizeLong2, "Size should not change");
assertEquals(sizeLong1, sizeBig.longValue(), "longSize should equal BigSize");
}
@Test
void testContentEquals() throws Exception {
// Non-existent files
final File file = new File(tempDirFile, getName());
final File file2 = new File(tempDirFile, getName() + "2");
assertTrue(FileUtils.contentEquals(null, null));
assertFalse(FileUtils.contentEquals(null, file));
assertFalse(FileUtils.contentEquals(file, null));
// 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
assertThrows(IllegalArgumentException.class, () -> FileUtils.contentEquals(tempDirFile, tempDirFile));
// Different files
final File objFile1 = new File(tempDirFile, getName() + ".object");
FileUtils.copyURLToFile(getClass().getResource("/java/lang/Object.class"), objFile1);
final File objFile1b = new File(tempDirFile, getName() + ".object2");
FileUtils.copyURLToFile(getClass().getResource("/java/lang/Object.class"), objFile1b);
final File objFile2 = new File(tempDirFile, getName() + ".collection");
FileUtils.copyURLToFile(getClass().getResource("/java/util/Collection.class"), objFile2);
// equals to other
assertFalse(FileUtils.contentEquals(objFile1, objFile2));
assertFalse(FileUtils.contentEquals(objFile1b, objFile2));
assertTrue(FileUtils.contentEquals(objFile1, objFile1b));
// equals to self
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));
}
@Test
void testContentEqualsIgnoreEOL() throws Exception {
// Non-existent files
final File file1 = new File(tempDirFile, getName());
final File file2 = new File(tempDirFile, getName() + "2");
assertTrue(FileUtils.contentEqualsIgnoreEOL(null, null, null));
assertFalse(FileUtils.contentEqualsIgnoreEOL(null, file1, null));
assertFalse(FileUtils.contentEqualsIgnoreEOL(file1, null, null));
// 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
assertThrows(IllegalArgumentException.class,
() -> FileUtils.contentEqualsIgnoreEOL(tempDirFile, tempDirFile, null));
// Different files
final File tfile1 = new File(tempDirFile, getName() + ".txt1");
FileUtils.write(tfile1, "123\r");
final File tfile2 = new File(tempDirFile, getName() + ".txt2");
FileUtils.write(tfile2, "123\n");
final File tfile3 = new File(tempDirFile, getName() + ".collection");
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.toURI());
assertTrue(cr.exists());
final URL urlCRLF = getClass().getResource("FileUtilsTestDataCRLF.dat");
assertNotNull(urlCRLF);
final File crlf = new File(urlCRLF.toURI());
assertTrue(crlf.exists());
final URL urlLF = getClass().getResource("FileUtilsTestDataLF.dat");
assertNotNull(urlLF);
final File lf = new File(urlLF.toURI());
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));
}
/**
* See what happens when copyDirectory copies a directory that is a symlink
* to another directory containing non-symlinked files.
* This is a characterization test to explore current behavior, and arguably
* represents a bug. This behavior, and the test, is likely to change
* and should not be relied on.
*/
@Test
void testCopyDir_SymbolicLink() throws Exception {
// Make a directory
final File realDirectory = new File(tempDirFile, "real_directory");
realDirectory.mkdir();
final File content = new File(realDirectory, "hello.txt");
FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
// Make a symlink to the directory
final Path linkPath = tempDirFile.toPath().resolve("link_to_directory");
Files.createSymbolicLink(linkPath, realDirectory.toPath());
// Now copy symlink
final File destination = new File(tempDirFile, "destination");
// Is the copy a symlink or an actual directory?
FileUtils.copyDirectory(linkPath.toFile(), destination);
// delete the original file so that if we can read the bytes from the
// copied directory it's definitely been copied, not linked.
assumeTrue(content.delete());
assertFalse(Files.isSymbolicLink(destination.toPath()));
final File copied_content = new File(destination, "hello.txt");
final String actual = FileUtils.readFileToString(copied_content, "UTF8");
assertEquals("HELLO WORLD", actual);
}
@Test
void testCopyDir_SymbolicLinkCycle() throws Exception {
// Make a directory
final File topDirectory = new File(tempDirFile, "topDirectory");
topDirectory.mkdir();
final File content = new File(topDirectory, "hello.txt");
FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
final File childDirectory = new File(topDirectory, "child_directory");
childDirectory.mkdir();
// Make a symlink to the top directory
final Path linkPath = childDirectory.toPath().resolve("link_to_top");
Files.createSymbolicLink(linkPath, topDirectory.toPath());
// Now copy symlink
final File destination = new File(tempDirFile, "destination");
FileUtils.copyDirectory(linkPath.toFile(), destination);
// delete the original file so that if we can read the bytes from the
// copied directory it's definitely been copied, not linked.
assumeTrue(content.delete());
assertFalse(Files.isSymbolicLink(destination.toPath()));
final File copied_content = new File(destination, "hello.txt");
final String actual = FileUtils.readFileToString(copied_content, "UTF8");
assertEquals("HELLO WORLD", actual);
final File[] copied = destination.listFiles();
assertEquals(2, copied.length);
}
/**
* Tests IO-807.
*/
@Test
void testCopyDirectory_brokenSymbolicLink() throws IOException {
// Make a file
final File sourceDirectory = new File(tempDirFile, "source_directory");
sourceDirectory.mkdir();
final File targetFile = new File(sourceDirectory, "hello.txt");
FileUtils.writeStringToFile(targetFile, "HELLO WORLD", "UTF8");
// Make a symlink to the file
final Path targetPath = targetFile.toPath();
final Path linkPath = sourceDirectory.toPath().resolve("linkfile");
Files.createSymbolicLink(linkPath, targetPath);
assumeTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symlink here: " + linkPath);
assumeTrue(Files.exists(linkPath));
assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
// Delete the file file to break the symlink
assumeTrue(targetFile.delete());
assumeFalse(Files.exists(linkPath));
assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
// Now copy sourceDirectory, including the broken link, to another directory
final File destination = new File(tempDirFile, "destination");
FileUtils.copyDirectory(sourceDirectory, destination);
assertTrue(destination.exists());
final Path copiedBrokenSymlink = new File(destination, "linkfile").toPath();
// test for the existence of the copied symbolic link as a link
assertTrue(Files.isSymbolicLink(copiedBrokenSymlink));
// shouldn't be able to read through to the source of the link.
// If we can, then the link points somewhere other than the deleted file
assertFalse(Files.exists(copiedBrokenSymlink));
}
@Test
void testCopyDirectory_SymbolicLink() throws IOException {
// Make a file
final File sourceDirectory = new File(tempDirFile, "source_directory");
sourceDirectory.mkdir();
final File targetFile = new File(sourceDirectory, "hello.txt");
FileUtils.writeStringToFile(targetFile, "HELLO WORLD", "UTF8");
// Make a symlink to the file
final Path targetPath = targetFile.toPath();
final Path linkPath = sourceDirectory.toPath().resolve("linkfile");
Files.createSymbolicLink(linkPath, targetPath);
assumeTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symlink here: " + linkPath);
assumeTrue(Files.exists(linkPath));
assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
// Now copy sourceDirectory to another directory
final File destination = new File(tempDirFile, "destination");
FileUtils.copyDirectory(sourceDirectory, destination);
assertTrue(destination.exists());
final Path copiedSymlink = new File(destination, "linkfile").toPath();
// test for the existence of the copied symbolic link as a link
assertTrue(Files.isSymbolicLink(copiedSymlink));
assertTrue(Files.exists(copiedSymlink));
}
/**
* Test what happens when copyDirectory copies a directory that contains a symlink
* to a file outside the copied directory.
*/
@Test
void testCopyDirectory_SymbolicLinkExternalFile() throws Exception {
// make a file
final File content = new File(tempDirFile, "hello.txt");
FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
// Make a directory
final File realDirectory = new File(tempDirFile, "real_directory");
realDirectory.mkdir();
// Make a symlink to the file
final Path linkPath = realDirectory.toPath().resolve("link_to_file");
Files.createSymbolicLink(linkPath, content.toPath());
// Now copy the directory
final File destination = new File(tempDirFile, "destination");
FileUtils.copyDirectory(realDirectory, destination);
// test that the copied directory contains a link to the original file
final File copiedLink = new File(destination, "link_to_file");
assertTrue(Files.isSymbolicLink(copiedLink.toPath()));
final String actual = FileUtils.readFileToString(copiedLink, "UTF8");
assertEquals("HELLO WORLD", actual);
final Path source = Files.readSymbolicLink(copiedLink.toPath());
assertEquals(content.toPath(), source);
}
@Test
void testCopyDirectoryExceptions() {
//
// NullPointerException
assertThrows(NullPointerException.class, () -> FileUtils.copyDirectory(null, null));
assertThrows(NullPointerException.class, () -> FileUtils.copyDirectory(null, testFile1));
assertThrows(NullPointerException.class, () -> FileUtils.copyDirectory(testFile1, null));
assertThrows(NullPointerException.class, () -> FileUtils.copyDirectory(null, new File("a")));
//
// IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> FileUtils.copyDirectory(testFile1, new File("a")));
assertThrows(IllegalArgumentException.class, () -> FileUtils.copyDirectory(testFile1, new File("a")));
assertThrows(IllegalArgumentException.class, () -> FileUtils.copyDirectory(tempDirFile, tempDirFile));
//
// IOException
assertThrows(IOException.class, () -> FileUtils.copyDirectory(new File("doesnt-exist"), new File("a")));
assertThrows(IllegalArgumentException.class, () -> FileUtils.copyDirectory(tempDirFile, testFile1));
}
@Test
void testCopyDirectoryFiltered() throws IOException {
final File grandParentDir = new File(tempDirFile, "grandparent");
final File parentDir = new File(grandParentDir, "parent");
final File childDir = new File(parentDir, "child");
createFilesForTestCopyDirectory(grandParentDir, parentDir, childDir);
final NameFileFilter filter = new NameFileFilter("parent", "child", "file3.txt");
final File destDir = new File(tempDirFile, "copydest");
FileUtils.copyDirectory(grandParentDir, destDir, filter);
final List<File> files = LIST_WALKER.list(destDir);
assertEquals(3, files.size());
assertEquals("parent", files.get(0).getName());
assertEquals("child", files.get(1).getName());
assertEquals("file3.txt", files.get(2).getName());
}
@Test
void testCopyDirectoryPreserveDates() throws Exception {
final File source = new File(tempDirFile, "source");
final File sourceDirectory = new File(source, "directory");
final File sourceFile = new File(sourceDirectory, "hello.txt");
// Prepare source data
source.mkdirs();
sourceDirectory.mkdir();
FileUtils.writeStringToFile(sourceFile, "HELLO WORLD", "UTF8");
// Set dates in reverse order to avoid overwriting previous values
// Also, use full seconds (arguments are in ms) close to today
// but still highly unlikely to occur in the real world
assertTrue(setLastModifiedMillis(sourceFile, DATE3));
assertTrue(setLastModifiedMillis(sourceDirectory, DATE2));
assertTrue(setLastModifiedMillis(source, DATE1));
final File target = new File(tempDirFile, "target");
final File targetDirectory = new File(target, "directory");
final File targetFile = new File(targetDirectory, "hello.txt");
// Test with preserveFileDate disabled
// On Windows, the last modified time is copied by default.
FileUtils.copyDirectory(source, target, false);
assertNotEquals(DATE1, getLastModifiedMillis(target));
assertNotEquals(DATE2, getLastModifiedMillis(targetDirectory));
if (!SystemUtils.IS_OS_WINDOWS) {
assertNotEquals(DATE3, getLastModifiedMillis(targetFile));
}
// Test permission of copied file match destination folder
if (!SystemUtils.IS_OS_WINDOWS) {
final Set<PosixFilePermission> parentPerms = Files.getPosixFilePermissions(target.getParentFile().toPath());
final Set<PosixFilePermission> targetPerms = Files.getPosixFilePermissions(target.toPath());
assertEquals(parentPerms, targetPerms);
} else {
final AclFileAttributeView parentView = Files.getFileAttributeView(target.getParentFile().toPath(), AclFileAttributeView.class);
final AclFileAttributeView targetView = Files.getFileAttributeView(target.toPath(), AclFileAttributeView.class);
assertEquals(parentView.getAcl(), targetView.getAcl());
}
FileUtils.deleteDirectory(target);
// Test with preserveFileDate enabled
FileUtils.copyDirectory(source, target, true);
assertEquals(DATE1, getLastModifiedMillis(target));
assertEquals(DATE2, getLastModifiedMillis(targetDirectory));
assertEquals(DATE3, getLastModifiedMillis(targetFile));
FileUtils.deleteDirectory(target);
// also if the target directory already exists (IO-190)
target.mkdirs();
FileUtils.copyDirectory(source, target, true);
assertEquals(DATE1, getLastModifiedMillis(target));
assertEquals(DATE2, getLastModifiedMillis(targetDirectory));
assertEquals(DATE3, getLastModifiedMillis(targetFile));
FileUtils.deleteDirectory(target);
// also if the target subdirectory already exists (IO-190)
targetDirectory.mkdirs();
FileUtils.copyDirectory(source, target, true);
assertEquals(DATE1, getLastModifiedMillis(target));
assertEquals(DATE2, getLastModifiedMillis(targetDirectory));
assertEquals(DATE3, getLastModifiedMillis(targetFile));
FileUtils.deleteDirectory(target);
}
/** Tests IO-141 */
@Test
void testCopyDirectoryToChild() throws IOException {
final File grandParentDir = new File(tempDirFile, "grandparent");
final File parentDir = new File(grandParentDir, "parent");
final File childDir = new File(parentDir, "child");
createFilesForTestCopyDirectory(grandParentDir, parentDir, childDir);
final long expectedCount = LIST_WALKER.list(grandParentDir).size() + LIST_WALKER.list(parentDir).size();
final long expectedSize = FileUtils.sizeOfDirectory(grandParentDir) + FileUtils.sizeOfDirectory(parentDir);
FileUtils.copyDirectory(parentDir, childDir);
assertEquals(expectedCount, LIST_WALKER.list(grandParentDir).size());
assertEquals(expectedSize, FileUtils.sizeOfDirectory(grandParentDir));
assertTrue(expectedCount > 0, "Count > 0");
assertTrue(expectedSize > 0, "Size > 0");
}
@Test
void testCopyDirectoryToDirectory_NonExistingDest() throws Exception {
if (!testFile1.getParentFile().exists()) {
fail("Cannot create file " + testFile1
+ " as the parent directory does not exist");
}
try (OutputStream output1 = new BufferedOutputStream(Files.newOutputStream(testFile1.toPath()))) {
TestUtils.generateTestData(output1, 1234);
}
if (!testFile2.getParentFile().exists()) {
fail("Cannot create file " + testFile2
+ " as the parent directory does not exist");
}
try (OutputStream output = new BufferedOutputStream(Files.newOutputStream(testFile2.toPath()))) {
TestUtils.generateTestData(output, 4321);
}
final File srcDir = tempDirFile;
final File subDir = new File(srcDir, "sub");
subDir.mkdir();
final File subFile = new File(subDir, "A.txt");
FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
final File destDir = new File(FileUtils.getTempDirectoryPath(), "tmp-FileUtilsTestCase");
FileUtils.deleteDirectory(destDir);
final File actualDestDir = new File(destDir, srcDir.getName());
FileUtils.copyDirectoryToDirectory(srcDir, destDir);
assertTrue(destDir.exists(), "Check exists");
assertTrue(actualDestDir.exists(), "Check exists");
final long srcSize = FileUtils.sizeOfDirectory(srcDir);
assertTrue(srcSize > 0, "Size > 0");
assertEquals(srcSize, FileUtils.sizeOfDirectory(actualDestDir), "Check size");
assertTrue(new File(actualDestDir, "sub/A.txt").exists());
FileUtils.deleteDirectory(destDir);
}
@Test
void testCopyDirectoryToExistingDest() throws Exception {
if (!testFile1.getParentFile().exists()) {
fail("Cannot create file " + testFile1
+ " as the parent directory does not exist");
}
try (OutputStream output1 = new BufferedOutputStream(Files.newOutputStream(testFile1.toPath()))) {
TestUtils.generateTestData(output1, 1234);
}
if (!testFile2.getParentFile().exists()) {
fail("Cannot create file " + testFile2
+ " as the parent directory does not exist");
}
try (OutputStream output = new BufferedOutputStream(Files.newOutputStream(testFile2.toPath()))) {
TestUtils.generateTestData(output, 4321);
}
final File srcDir = tempDirFile;
final File subDir = new File(srcDir, "sub");
subDir.mkdir();
final File subFile = new File(subDir, "A.txt");
FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
final File destDir = new File(SystemProperties.getJavaIoTmpdir(), "tmp-FileUtilsTestCase");
FileUtils.deleteDirectory(destDir);
destDir.mkdirs();
FileUtils.copyDirectory(srcDir, destDir);