|
@@ -417,6 +417,88 @@ public class TestLocalFileSystem {
|
|
|
stm.close();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests a simple rename of a directory.
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testRenameDirectory() throws IOException {
|
|
|
+ Path src = new Path(TEST_ROOT_DIR, "dir1");
|
|
|
+ Path dst = new Path(TEST_ROOT_DIR, "dir2");
|
|
|
+ fileSys.delete(src, true);
|
|
|
+ fileSys.delete(dst, true);
|
|
|
+ assertTrue(fileSys.mkdirs(src));
|
|
|
+ assertTrue(fileSys.rename(src, dst));
|
|
|
+ assertTrue(fileSys.exists(dst));
|
|
|
+ assertFalse(fileSys.exists(src));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests that renaming a directory replaces the destination if the destination
|
|
|
+ * is an existing empty directory.
|
|
|
+ *
|
|
|
+ * Before:
|
|
|
+ * /dir1
|
|
|
+ * /file1
|
|
|
+ * /file2
|
|
|
+ * /dir2
|
|
|
+ *
|
|
|
+ * After rename("/dir1", "/dir2"):
|
|
|
+ * /dir2
|
|
|
+ * /file1
|
|
|
+ * /file2
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testRenameReplaceExistingEmptyDirectory() throws IOException {
|
|
|
+ Path src = new Path(TEST_ROOT_DIR, "dir1");
|
|
|
+ Path dst = new Path(TEST_ROOT_DIR, "dir2");
|
|
|
+ fileSys.delete(src, true);
|
|
|
+ fileSys.delete(dst, true);
|
|
|
+ assertTrue(fileSys.mkdirs(src));
|
|
|
+ writeFile(fileSys, new Path(src, "file1"), 1);
|
|
|
+ writeFile(fileSys, new Path(src, "file2"), 1);
|
|
|
+ assertTrue(fileSys.mkdirs(dst));
|
|
|
+ assertTrue(fileSys.rename(src, dst));
|
|
|
+ assertTrue(fileSys.exists(dst));
|
|
|
+ assertTrue(fileSys.exists(new Path(dst, "file1")));
|
|
|
+ assertTrue(fileSys.exists(new Path(dst, "file2")));
|
|
|
+ assertFalse(fileSys.exists(src));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests that renaming a directory to an existing directory that is not empty
|
|
|
+ * results in a full copy of source to destination.
|
|
|
+ *
|
|
|
+ * Before:
|
|
|
+ * /dir1
|
|
|
+ * /dir2
|
|
|
+ * /dir3
|
|
|
+ * /file1
|
|
|
+ * /file2
|
|
|
+ *
|
|
|
+ * After rename("/dir1/dir2/dir3", "/dir1"):
|
|
|
+ * /dir1
|
|
|
+ * /dir3
|
|
|
+ * /file1
|
|
|
+ * /file2
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testRenameMoveToExistingNonEmptyDirectory() throws IOException {
|
|
|
+ Path src = new Path(TEST_ROOT_DIR, "dir1/dir2/dir3");
|
|
|
+ Path dst = new Path(TEST_ROOT_DIR, "dir1");
|
|
|
+ fileSys.delete(src, true);
|
|
|
+ fileSys.delete(dst, true);
|
|
|
+ assertTrue(fileSys.mkdirs(src));
|
|
|
+ writeFile(fileSys, new Path(src, "file1"), 1);
|
|
|
+ writeFile(fileSys, new Path(src, "file2"), 1);
|
|
|
+ assertTrue(fileSys.exists(dst));
|
|
|
+ assertTrue(fileSys.rename(src, dst));
|
|
|
+ assertTrue(fileSys.exists(dst));
|
|
|
+ assertTrue(fileSys.exists(new Path(dst, "dir3")));
|
|
|
+ assertTrue(fileSys.exists(new Path(dst, "dir3/file1")));
|
|
|
+ assertTrue(fileSys.exists(new Path(dst, "dir3/file2")));
|
|
|
+ assertFalse(fileSys.exists(src));
|
|
|
+ }
|
|
|
|
|
|
private void verifyRead(FSDataInputStream stm, byte[] fileContents,
|
|
|
int seekOff, int toRead) throws IOException {
|