]> source.dussan.org Git - jgit.git/commitdiff
Add tests for FileUtils.delete and EMPTY_DIREECTORIES_ONLY 12/11412/3
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Sat, 23 Mar 2013 23:49:23 +0000 (00:49 +0100)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Sat, 23 Mar 2013 23:49:23 +0000 (00:49 +0100)
Change-Id: I54a46c29df5eafc7739a6ef29e5dc80fa2f6d9ba

org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java

index 7790b3024a582db05e508d3c0310695e9485d624..d0680cab744020e677e7152e232201fe1a211718 100644 (file)
@@ -116,6 +116,91 @@ public class FileUtilTest {
                }
        }
 
+       @Test
+       public void testDeleteRecursiveEmpty() throws IOException {
+               File f1 = new File(trash, "test/test/a");
+               File f2 = new File(trash, "test/a");
+               File d1 = new File(trash, "test");
+               File d2 = new File(trash, "test/test");
+               File d3 = new File(trash, "test/b");
+               FileUtils.mkdirs(f1.getParentFile());
+               FileUtils.createNewFile(f2);
+               FileUtils.createNewFile(f1);
+               FileUtils.mkdirs(d3);
+
+               // Cannot delete hierarchy since files exist
+               try {
+                       FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
+                       fail("delete should fail");
+               } catch (IOException e1) {
+                       try {
+                               FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY|FileUtils.RECURSIVE);
+                               fail("delete should fail");
+                       } catch (IOException e2) {
+                               // Everything still there
+                               assertTrue(f1.exists());
+                               assertTrue(f2.exists());
+                               assertTrue(d1.exists());
+                               assertTrue(d2.exists());
+                               assertTrue(d3.exists());
+                       }
+               }
+
+               // setup: delete files, only directories left
+               assertTrue(f1.delete());
+               assertTrue(f2.delete());
+
+               // Shall not delete hierarchy without recursive
+               try {
+                       FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
+                       fail("delete should fail");
+               } catch (IOException e2) {
+                       // Everything still there
+                       assertTrue(d1.exists());
+                       assertTrue(d2.exists());
+                       assertTrue(d3.exists());
+               }
+
+               // Now delete the empty hierarchy
+               FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
+                               | FileUtils.RECURSIVE);
+               assertFalse(d2.exists());
+
+               // Will fail to delete non-existing without SKIP_MISSING
+               try {
+                       FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY);
+                       fail("Cannot delete non-existent entity");
+               } catch (IOException e) {
+                       // ok
+               }
+
+               // ..with SKIP_MISSING there is no exception
+               FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
+                               | FileUtils.SKIP_MISSING);
+               FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
+                               | FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
+
+               // essentially the same, using IGNORE_ERRORS
+               FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
+                               | FileUtils.IGNORE_ERRORS);
+               FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
+                               | FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
+       }
+
+       @Test
+       public void testDeleteRecursiveEmptyDirectoriesOnlyButIsFile()
+                       throws IOException {
+               File f1 = new File(trash, "test/test/a");
+               FileUtils.mkdirs(f1.getParentFile());
+               FileUtils.createNewFile(f1);
+               try {
+                       FileUtils.delete(f1, FileUtils.EMPTY_DIRECTORIES_ONLY);
+                       fail("delete should fail");
+               } catch (IOException e) {
+                       assertTrue(f1.exists());
+               }
+       }
+
        @Test
        public void testMkdir() throws IOException {
                File d = new File(trash, "test");