From d0e92885e911c96be917574a5f804bf64d622cf9 Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Sun, 24 Mar 2013 01:06:29 +0100 Subject: Extend FileUtils.rename to common git semantics Unlike the OS or Java rename this method will (on *nix) try (on Windows) replace the target with the source provided the target does not exist, the target does exist and is a file, or if it is a directory which only contains directories. In the latter case the directory hierarchy will be deleted. If the initial rename fails and the target is an existing file the the target file will be deleted first and then the rename is retried. Change-Id: Iae75c49c85445ada7795246a02ce02f7c248d956 Signed-off-by: Christian Halstrick --- .../tst/org/eclipse/jgit/util/FileUtilTest.java | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java index d0680cab74..f6f6753c75 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java @@ -45,6 +45,7 @@ package org.eclipse.jgit.util; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.endsWith; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -52,6 +53,7 @@ import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; +import org.eclipse.jgit.junit.JGitTestUtil; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -321,4 +323,71 @@ public class FileUtilTest { assertTrue(f.exists()); assertFalse(e.exists()); } + + @Test + public void testRenameOverNonExistingFile() throws IOException { + File d = new File(trash, "d"); + FileUtils.mkdirs(d); + File f1 = new File(trash, "d/f"); + File f2 = new File(trash, "d/g"); + JGitTestUtil.write(f1, "f1"); + // test + FileUtils.rename(f1, f2); + assertFalse(f1.exists()); + assertTrue(f2.exists()); + assertEquals("f1", JGitTestUtil.read(f2)); + } + + @Test + public void testRenameOverExistingFile() throws IOException { + File d = new File(trash, "d"); + FileUtils.mkdirs(d); + File f1 = new File(trash, "d/f"); + File f2 = new File(trash, "d/g"); + JGitTestUtil.write(f1, "f1"); + JGitTestUtil.write(f2, "f2"); + // test + FileUtils.rename(f1, f2); + assertFalse(f1.exists()); + assertTrue(f2.exists()); + assertEquals("f1", JGitTestUtil.read(f2)); + } + + @Test + public void testRenameOverExistingNonEmptyDirectory() throws IOException { + File d = new File(trash, "d"); + FileUtils.mkdirs(d); + File f1 = new File(trash, "d/f"); + File f2 = new File(trash, "d/g"); + File d1 = new File(trash, "d/g/h/i"); + File f3 = new File(trash, "d/g/h/f"); + FileUtils.mkdirs(d1); + JGitTestUtil.write(f1, "f1"); + JGitTestUtil.write(f3, "f3"); + // test + try { + FileUtils.rename(f1, f2); + fail("rename to non-empty directory should fail"); + } catch (IOException e) { + assertEquals("f1", JGitTestUtil.read(f1)); // untouched source + assertEquals("f3", JGitTestUtil.read(f3)); // untouched + // empty directories within f2 may or may not have been deleted + } + } + + @Test + public void testRenameOverExistingEmptyDirectory() throws IOException { + File d = new File(trash, "d"); + FileUtils.mkdirs(d); + File f1 = new File(trash, "d/f"); + File f2 = new File(trash, "d/g"); + File d1 = new File(trash, "d/g/h/i"); + FileUtils.mkdirs(d1); + JGitTestUtil.write(f1, "f1"); + // test + FileUtils.rename(f1, f2); + assertFalse(f1.exists()); + assertTrue(f2.exists()); + assertEquals("f1", JGitTestUtil.read(f2)); + } } -- cgit v1.2.3