@@ -175,17 +175,18 @@ private static void setDosReadOnly(final Path p, final boolean readOnly) throws
175175 Files .setAttribute (p , "dos:readonly" , readOnly , LinkOption .NOFOLLOW_LINKS );
176176 }
177177 }
178+
178179 private File testFile1 ;
180+
179181 private File testFile2 ;
182+
180183 private long testFile1Size ;
181184
182185 private long testFile2Size ;
183186
184187 private void assertContentMatchesAfterCopyURLToFileFor (final String resourceName , final File destination ) throws IOException {
185188 FileUtils .copyURLToFile (getClass ().getResource (resourceName ), destination );
186-
187- try (InputStream fis = Files .newInputStream (destination .toPath ());
188- InputStream expected = getClass ().getResourceAsStream (resourceName )) {
189+ try (InputStream fis = Files .newInputStream (destination .toPath ()); InputStream expected = getClass ().getResourceAsStream (resourceName )) {
189190 assertTrue (IOUtils .contentEquals (expected , fis ), "Content is not equal." );
190191 }
191192 }
@@ -623,9 +624,8 @@ void testContentEquals() throws Exception {
623624 assertTrue (FileUtils .contentEquals (objFile1 , objFile1 ));
624625 assertTrue (FileUtils .contentEquals (objFile1b , objFile1b ));
625626 assertTrue (FileUtils .contentEquals (objFile2 , objFile2 ));
626- // Equal files
627- file .createNewFile ();
628- file2 .createNewFile ();
627+ assertCreateNewFile (file );
628+ assertCreateNewFile (file2 );
629629 assertTrue (FileUtils .contentEquals (file , file ));
630630 assertTrue (FileUtils .contentEquals (file , file2 ));
631631 }
@@ -698,9 +698,8 @@ void testContentEqualsIgnoreEOL() throws Exception {
698698 assertFalse (FileUtils .contentEquals (cr , lf ));
699699 assertFalse (FileUtils .contentEquals (crlf , lf ));
700700
701- // Equal files
702- file1 .createNewFile ();
703- file2 .createNewFile ();
701+ assertCreateNewFile (file1 );
702+ assertCreateNewFile (file2 );
704703 assertTrue (FileUtils .contentEqualsIgnoreEOL (file1 , file1 , null ));
705704 assertTrue (FileUtils .contentEqualsIgnoreEOL (file1 , file2 , null ));
706705 }
@@ -716,7 +715,7 @@ void testContentEqualsIgnoreEOL() throws Exception {
716715 void testCopyDir_SymbolicLink () throws Exception {
717716 // Make a directory
718717 final File realDirectory = new File (tempDirFile , "real_directory" );
719- realDirectory . mkdir ( );
718+ assertMkdir ( true , realDirectory );
720719 final File content = new File (realDirectory , "hello.txt" );
721720 FileUtils .writeStringToFile (content , "HELLO WORLD" , "UTF8" );
722721
@@ -744,11 +743,11 @@ void testCopyDir_SymbolicLink() throws Exception {
744743 void testCopyDir_SymbolicLinkCycle () throws Exception {
745744 // Make a directory
746745 final File topDirectory = new File (tempDirFile , "topDirectory" );
747- topDirectory . mkdir ( );
746+ assertMkdir ( true , topDirectory );
748747 final File content = new File (topDirectory , "hello.txt" );
749748 FileUtils .writeStringToFile (content , "HELLO WORLD" , "UTF8" );
750749 final File childDirectory = new File (topDirectory , "child_directory" );
751- childDirectory . mkdir ( );
750+ assertMkdir ( true , childDirectory );
752751
753752 // Make a symlink to the top directory
754753 final Path linkPath = childDirectory .toPath ().resolve ("link_to_top" );
@@ -778,7 +777,7 @@ void testCopyDir_SymbolicLinkCycle() throws Exception {
778777 void testCopyDirectory_brokenSymbolicLink () throws IOException {
779778 // Make a file
780779 final File sourceDirectory = new File (tempDirFile , "source_directory" );
781- sourceDirectory . mkdir ( );
780+ assertMkdir ( true , sourceDirectory );
782781 final File targetFile = new File (sourceDirectory , "hello.txt" );
783782 FileUtils .writeStringToFile (targetFile , "HELLO WORLD" , "UTF8" );
784783
@@ -813,7 +812,7 @@ void testCopyDirectory_brokenSymbolicLink() throws IOException {
813812 void testCopyDirectory_SymbolicLink () throws IOException {
814813 // Make a file
815814 final File sourceDirectory = new File (tempDirFile , "source_directory" );
816- sourceDirectory . mkdir ( );
815+ assertMkdir ( true , sourceDirectory );
817816 final File targetFile = new File (sourceDirectory , "hello.txt" );
818817 FileUtils .writeStringToFile (targetFile , "HELLO WORLD" , "UTF8" );
819818
@@ -848,7 +847,7 @@ void testCopyDirectory_SymbolicLinkExternalFile() throws Exception {
848847
849848 // Make a directory
850849 final File realDirectory = new File (tempDirFile , "real_directory" );
851- realDirectory . mkdir ( );
850+ assertMkdir ( true , realDirectory );
852851
853852 // Make a symlink to the file
854853 final Path linkPath = realDirectory .toPath ().resolve ("link_to_file" );
@@ -912,8 +911,8 @@ void testCopyDirectoryPreserveDates() throws Exception {
912911 final File sourceFile = new File (sourceDirectory , "hello.txt" );
913912
914913 // Prepare source data
915- source . mkdirs ( );
916- sourceDirectory . mkdir ( );
914+ assertMkdir ( true , source );
915+ assertMkdir ( true , sourceDirectory );
917916 FileUtils .writeStringToFile (sourceFile , "HELLO WORLD" , "UTF8" );
918917 // Set dates in reverse order to avoid overwriting previous values
919918 // Also, use full seconds (arguments are in ms) close to today
@@ -1007,7 +1006,7 @@ void testCopyDirectoryToDirectory_NonExistingDest() throws Exception {
10071006 }
10081007 final File srcDir = tempDirFile ;
10091008 final File subDir = new File (srcDir , "sub" );
1010- subDir . mkdir ( );
1009+ assertMkdir ( true , subDir );
10111010 final File subFile = new File (subDir , "A.txt" );
10121011 FileUtils .writeStringToFile (subFile , "HELLO WORLD" , "UTF8" );
10131012 final File destDir = new File (FileUtils .getTempDirectoryPath (), "tmp-FileUtilsTestCase" );
@@ -1043,7 +1042,7 @@ void testCopyDirectoryToExistingDest() throws Exception {
10431042 }
10441043 final File srcDir = tempDirFile ;
10451044 final File subDir = new File (srcDir , "sub" );
1046- subDir . mkdir ( );
1045+ assertMkdir ( true , subDir );
10471046 final File subFile = new File (subDir , "A.txt" );
10481047 FileUtils .writeStringToFile (subFile , "HELLO WORLD" , "UTF8" );
10491048 final File destDir = new File (SystemProperties .getJavaIoTmpdir (), "tmp-FileUtilsTestCase" );
@@ -1101,7 +1100,7 @@ void testCopyDirectoryToNonExistingDest() throws Exception {
11011100 }
11021101 final File srcDir = tempDirFile ;
11031102 final File subDir = new File (srcDir , "sub" );
1104- subDir . mkdir ( );
1103+ assertMkdir ( true , subDir );
11051104 final File subFile = new File (subDir , "A.txt" );
11061105 FileUtils .writeStringToFile (subFile , "HELLO WORLD" , "UTF8" );
11071106 final File destDir = new File (FileUtils .getTempDirectoryPath (), "tmp-FileUtilsTestCase" );
@@ -1149,7 +1148,7 @@ void testCopyDirectoryWithPotentialFalsePartialMatch() throws IOException {
11491148 void testCopyFile_SymbolicLink () throws Exception {
11501149 // Make a file
11511150 final File sourceDirectory = new File (tempDirFile , "source_directory" );
1152- sourceDirectory . mkdir ( );
1151+ assertMkdir ( true , sourceDirectory );
11531152 final File targetFile = new File (sourceDirectory , "hello.txt" );
11541153 FileUtils .writeStringToFile (targetFile , "HELLO WORLD" , "UTF8" );
11551154
@@ -1647,7 +1646,7 @@ void testFileUtils() throws Exception {
16471646 @ Test
16481647 void testForceDeleteAFile1 () throws Exception {
16491648 final File destination = new File (tempDirFile , "copy1.txt" );
1650- destination . createNewFile ( );
1649+ assertCreateNewFile ( destination );
16511650 assertTrue (destination .exists (), "Copy1.txt doesn't exist to delete" );
16521651 FileUtils .forceDelete (destination );
16531652 assertFalse (destination .exists (), "Check No Exist" );
@@ -1656,7 +1655,7 @@ void testForceDeleteAFile1() throws Exception {
16561655 @ Test
16571656 void testForceDeleteAFile2 () throws Exception {
16581657 final File destination = new File (tempDirFile , "copy2.txt" );
1659- destination . createNewFile ( );
1658+ assertCreateNewFile ( destination );
16601659 assertTrue (destination .exists (), "Copy2.txt doesn't exist to delete" );
16611660 FileUtils .forceDelete (destination );
16621661 assertFalse (destination .exists (), "Check No Exist" );
@@ -1865,13 +1864,13 @@ void testForceMkdir() throws Exception {
18651864
18661865 // Creates test file
18671866 final File testFile = new File (tempDirFile , getName ());
1868- testFile . createNewFile ( );
1867+ assertCreateNewFile ( testFile );
18691868 assertTrue (testFile .exists (), "Test file does not exist." );
18701869
18711870 // Tests with existing file
18721871 assertThrows (IOException .class , () -> FileUtils .forceMkdir (testFile ));
18731872
1874- testFile . delete ( );
1873+ assertDelete ( true , testFile );
18751874
18761875 // Tests with non-existent directory
18771876 FileUtils .forceMkdir (testFile );
@@ -1886,7 +1885,7 @@ void testForceMkdirParent() throws Exception {
18861885 // Tests with existing directory
18871886 assertTrue (tempDirFile .exists ());
18881887 final File testParentDir = new File (tempDirFile , "testForceMkdirParent" );
1889- testParentDir . delete ( );
1888+ assertDelete ( false , testParentDir );
18901889 assertFalse (testParentDir .exists ());
18911890 final File testFile = new File (testParentDir , "test.txt" );
18921891 assertFalse (testParentDir .exists ());
@@ -2173,9 +2172,9 @@ void testIterateFiles() throws Exception {
21732172 final File subDir = new File (srcDir , "list_test" );
21742173 final File subSubDir = new File (subDir , "subSubDir" );
21752174 final File notSubSubDir = new File (subDir , "notSubSubDir" );
2176- assertTrue ( subDir . mkdir () );
2177- assertTrue ( subSubDir . mkdir () );
2178- assertTrue ( notSubSubDir . mkdir () );
2175+ assertMkdir ( true , subDir );
2176+ assertMkdir ( true , subSubDir );
2177+ assertMkdir ( true , notSubSubDir );
21792178 Iterator <File > iterator = null ;
21802179 try {
21812180 // Need list to be appendable
@@ -2221,9 +2220,9 @@ void testIterateFiles() throws Exception {
22212220 assertEquals (expectedFileNames , actualFileNames );
22222221 } finally {
22232222 consumeRemaining (iterator );
2224- notSubSubDir . delete ( );
2225- subSubDir . delete ( );
2226- subDir . delete ( );
2223+ assertDelete ( false , notSubSubDir );
2224+ assertDelete ( false , subSubDir );
2225+ assertDelete ( false , subDir );
22272226 }
22282227 }
22292228
@@ -2240,10 +2239,10 @@ void testIterateFilesAndDirs() throws IOException {
22402239 final File subDir2 = new File (subDir1 , "subdir2" );
22412240 final File subDir3 = new File (subDir2 , "subdir3" );
22422241 final File subDir4 = new File (subDir2 , "subdir4" );
2243- assertTrue ( subDir1 . mkdir () );
2244- assertTrue ( subDir2 . mkdir () );
2245- assertTrue ( subDir3 . mkdir () );
2246- assertTrue ( subDir4 . mkdir () );
2242+ assertMkdir ( true , subDir1 );
2243+ assertMkdir ( true , subDir2 );
2244+ assertMkdir ( true , subDir3 );
2245+ assertMkdir ( true , subDir4 );
22472246 final File someFile = new File (subDir2 , "a.txt" );
22482247 final WildcardFileFilter fileFilterAllFiles = WildcardFileFilter .builder ().setWildcards ("*.*" ).get ();
22492248 final WildcardFileFilter fileFilterAllDirs = WildcardFileFilter .builder ().setWildcards ("*" ).get ();
@@ -2272,7 +2271,7 @@ void testIterateFilesAndDirs() throws IOException {
22722271 @ Test
22732272 void testIterateFilesOnlyNoDirs () throws IOException {
22742273 final File directory = tempDirFile ;
2275- assertTrue ( new File (directory , "TEST" ). mkdir ( ));
2274+ assertMkdir ( true , new File (directory , "TEST" ));
22762275 assertTrue (new File (directory , "test.txt" ).createNewFile ());
22772276
22782277 final IOFileFilter filter = WildcardFileFilter .builder ().setWildcards ("*" ).setIoCase (IOCase .INSENSITIVE ).get ();
@@ -2284,8 +2283,8 @@ void testListFiles() throws Exception {
22842283 final File srcDir = tempDirFile ;
22852284 final File subDir = new File (srcDir , "list_test" );
22862285 final File subDir2 = new File (subDir , "subdir" );
2287- subDir . mkdir ( );
2288- subDir2 . mkdir ( );
2286+ assertMkdir ( true , subDir );
2287+ assertMkdir ( true , subDir2 );
22892288 final String [] expectedFileNames = { "a.txt" , "b.txt" , "c.txt" , "d.txt" , "e.txt" , "f.txt" };
22902289 final int [] fileSizes = { 123 , 234 , 345 , 456 , 678 , 789 };
22912290 for (int i = 0 ; i < expectedFileNames .length ; ++i ) {
@@ -2321,7 +2320,7 @@ void testListFiles() throws Exception {
23212320 @ Test
23222321 void testListFilesOnlyNoDirs () throws IOException {
23232322 final File directory = tempDirFile ;
2324- assertTrue ( new File (directory , "TEST" ). mkdir ( ));
2323+ assertMkdir ( true , new File (directory , "TEST" ));
23252324 assertTrue (new File (directory , "test.txt" ).createNewFile ());
23262325
23272326 final IOFileFilter filter = WildcardFileFilter .builder ().setWildcards ("*" ).setIoCase (IOCase .INSENSITIVE ).get ();
@@ -2335,8 +2334,8 @@ void testListFilesWithDirs() throws IOException {
23352334 final File srcDir = tempDirFile ;
23362335 final File subDir1 = new File (srcDir , "subdir" );
23372336 final File subDir2 = new File (subDir1 , "subdir2" );
2338- subDir1 . mkdir ( );
2339- subDir2 . mkdir ( );
2337+ assertMkdir ( true , subDir1 );
2338+ assertMkdir ( true , subDir2 );
23402339 final File someFile = new File (subDir2 , "a.txt" );
23412340 if (!someFile .getParentFile ().exists ()) {
23422341 fail ("Cannot create file " + someFile + " as the parent directory does not exist" );
@@ -2345,7 +2344,7 @@ void testListFilesWithDirs() throws IOException {
23452344 TestUtils .generateTestData (output , 100 );
23462345 }
23472346 final File subDir3 = new File (subDir2 , "subdir3" );
2348- subDir3 . mkdir ( );
2347+ assertMkdir ( true , subDir3 );
23492348 // @formatter:off
23502349 final Collection <File > files = FileUtils .listFilesAndDirs (subDir1 ,
23512350 WildcardFileFilter .builder ().setWildcards ("*.*" ).get (),
@@ -2412,8 +2411,8 @@ void testMoveDirectory_Errors() throws Exception {
24122411 assertThrows (IllegalArgumentException .class , () -> FileUtils .moveDirectory (testFile , new File ("foo" )));
24132412 final File testSrcFile = new File (tempDirFile , "testMoveDirectorySource" );
24142413 final File testDestFile = new File (tempDirFile , "testMoveDirectoryDest" );
2415- testSrcFile . mkdir ( );
2416- testDestFile . mkdir ( );
2414+ assertMkdir ( true , testSrcFile );
2415+ assertMkdir ( true , testDestFile );
24172416 assertThrows (FileExistsException .class , () -> FileUtils .moveDirectory (testSrcFile , testDestFile ),
24182417 "Expected FileExistsException when dest already exists" );
24192418
@@ -2932,11 +2931,10 @@ void testSizeOf() throws Exception {
29322931 assertThrows (NullPointerException .class , () -> FileUtils .sizeOf (null ));
29332932 // Non-existent file
29342933 assertThrows (IllegalArgumentException .class , () -> FileUtils .sizeOf (file ));
2935- // Creates file
2936- file .createNewFile ();
2934+ assertCreateNewFile (file );
29372935 // New file
29382936 assertEquals (0 , FileUtils .sizeOf (file ));
2939- file . delete ( );
2937+ assertDelete ( true , file );
29402938 // Existing file
29412939 assertEquals (testFile1Size , FileUtils .sizeOf (testFile1 ), "Unexpected files size" );
29422940 // Existing directory
@@ -2950,11 +2948,10 @@ void testSizeOfAsBigInteger() throws Exception {
29502948 assertThrows (NullPointerException .class , () -> FileUtils .sizeOfAsBigInteger (null ));
29512949 // Non-existent file
29522950 assertThrows (IllegalArgumentException .class , () -> FileUtils .sizeOfAsBigInteger (file ));
2953- // Creates file
2954- file .createNewFile ();
2951+ assertCreateNewFile (file );
29552952 // New file
29562953 assertEquals (BigInteger .ZERO , FileUtils .sizeOfAsBigInteger (file ));
2957- file . delete ( );
2954+ assertDelete ( true , file );
29582955 // Existing file
29592956 assertEquals (BigInteger .valueOf (testFile1Size ), FileUtils .sizeOfAsBigInteger (testFile1 ), "Unexpected files size" );
29602957 // Existing directory
@@ -2976,12 +2973,11 @@ void testSizeOfDirectory() throws Exception {
29762973 // Non-existent file
29772974 assertThrows (IllegalArgumentException .class , () -> FileUtils .sizeOfAsBigInteger (file ));
29782975 // Creates file
2979- file . createNewFile ( );
2976+ assertCreateNewFile ( file );
29802977 // Existing file
29812978 assertThrows (IllegalArgumentException .class , () -> FileUtils .sizeOfDirectory (file ));
2982- // Existing directory
2983- file .delete ();
2984- file .mkdir ();
2979+ assertDelete (true , file );
2980+ assertMkdir (true , file );
29852981 // Create a cyclic symlink
29862982 createCircularSymbolicLink (file );
29872983 assertEquals (TEST_DIRECTORY_SIZE , FileUtils .sizeOfDirectory (file ), "Unexpected directory size" );
@@ -3001,18 +2997,15 @@ void testSizeOfDirectoryAsBigInteger() throws Exception {
30012997 assertThrows (NullPointerException .class , () -> FileUtils .sizeOfDirectoryAsBigInteger (null ));
30022998 // Non-existent file
30032999 assertThrows (UncheckedIOException .class , () -> FileUtils .sizeOfDirectoryAsBigInteger (file ));
3004- // Creates file
3005- file .createNewFile ();
3000+ assertCreateNewFile (file );
30063001 // Existing file
30073002 assertThrows (IllegalArgumentException .class , () -> FileUtils .sizeOfDirectoryAsBigInteger (file ));
3008- // Existing directory
3009- file .delete ();
3010- file .mkdir ();
3003+ assertDelete (true , file );
3004+ assertMkdir (true , file );
30113005 createCircularSymbolicLink (file );
30123006 assertEquals (TEST_DIRECTORY_SIZE_BI , FileUtils .sizeOfDirectoryAsBigInteger (file ), "Unexpected directory size" );
3013- // Existing directory which size is greater than zero
3014- file .delete ();
3015- file .mkdir ();
3007+ assertDelete (false , file );
3008+ assertMkdir (false , file );
30163009 final File nonEmptyFile = new File (file , "non-emptyFile" + System .nanoTime ());
30173010 assertTrue (nonEmptyFile .getParentFile ().exists (), () -> "Cannot create file " + nonEmptyFile + " as the parent directory does not exist" );
30183011 final OutputStream output = new BufferedOutputStream (Files .newOutputStream (nonEmptyFile .toPath ()));
@@ -3022,8 +3015,8 @@ void testSizeOfDirectoryAsBigInteger() throws Exception {
30223015 IOUtils .closeQuietly (output );
30233016 }
30243017 assertEquals (TEST_DIRECTORY_SIZE_GT_ZERO_BI , FileUtils .sizeOfDirectoryAsBigInteger (file ), "Unexpected directory size" );
3025- nonEmptyFile . delete ( );
3026- file . delete ( );
3018+ assertDelete ( true , nonEmptyFile );
3019+ assertDelete ( false , file );
30273020 }
30283021
30293022 @ Test
@@ -3124,7 +3117,7 @@ void testTouch() throws IOException {
31243117 assertThrows (NullPointerException .class , () -> FileUtils .touch (null ));
31253118 final File file = new File (tempDirFile , "touch.txt" );
31263119 if (file .exists ()) {
3127- file . delete ( );
3120+ assertDelete ( true , file );
31283121 }
31293122 assertFalse (file .exists (), "Bad test: test file still exists" );
31303123 FileUtils .touch (file );
@@ -3149,10 +3142,10 @@ void testTouch() throws IOException {
31493142
31503143 @ Test
31513144 void testTouchDirDoesNotExist () throws Exception {
3152- final File file = new File ("target/does-not-exist" , "touchme.txt" );
3145+ final File file = new File (new File ( tempDirFile , "dir-touch" ) , "touchme.txt" );
31533146 final File parentDir = file .getParentFile ();
3154- file . delete ( );
3155- parentDir . delete ( );
3147+ assertDelete ( false , file );
3148+ assertDelete ( false , parentDir );
31563149 assertFalse (parentDir .exists ());
31573150 assertFalse (file .exists ());
31583151 FileUtils .touch (file );
0 commit comments