public void makeRenamePending(FileFolder dst) throws IOException { // Propose (but don't do) the rename. Path home = fs.getHomeDirectory(); String relativeHomeDir = getRelativePath(home.toString()); NativeAzureFileSystem.FolderRenamePending pending = new NativeAzureFileSystem.FolderRenamePending( relativeHomeDir + "/" + this.getName(), relativeHomeDir + "/" + dst.getName(), null, (NativeAzureFileSystem) fs); // Get the rename pending file contents. String renameDescription = pending.makeRenamePendingFileContents(); // Create a rename-pending file and write rename information to it. final String renamePendingStr = this.getName() + "-RenamePending.json"; Path renamePendingFile = new Path(renamePendingStr); FSDataOutputStream out = fs.create(renamePendingFile, true); assertTrue(out != null); writeString(out, renameDescription); }
/** * Test the situation where a rename pending file exists but the rename * is really done. This could happen if the rename process died just * before deleting the rename pending file. It exercises a non-standard * code path in redo(). */ @Test public void testRenameRedoFolderAlreadyDone() throws IOException { // create only destination folder String orig = "originalFolder"; String dest = "renamedFolder"; Path destPath = new Path(dest); assertTrue(fs.mkdirs(destPath)); // propose (but don't do) the rename of innerFolder2 Path home = fs.getHomeDirectory(); String relativeHomeDir = getRelativePath(home.toString()); NativeAzureFileSystem.FolderRenamePending pending = new NativeAzureFileSystem.FolderRenamePending( relativeHomeDir + "/" + orig, relativeHomeDir + "/" + dest, null, (NativeAzureFileSystem) fs); // Create a rename-pending file and write rename information to it. final String renamePendingStr = orig + FolderRenamePending.SUFFIX; Path renamePendingFile = new Path(renamePendingStr); FSDataOutputStream out = fs.create(renamePendingFile, true); assertTrue(out != null); writeString(out, pending.makeRenamePendingFileContents()); try { pending.redo(); } catch (Exception e) { fail(); } // Make sure rename pending file is gone. FileStatus[] listed = fs.listStatus(new Path("/")); assertEquals(1, listed.length); assertTrue(listed[0].isDirectory()); }
@Test public void testRedoRenameFolder() throws IOException { // create original folder String srcKey = "folderToRename"; Path originalFolder = new Path(srcKey); assertTrue(fs.mkdirs(originalFolder)); Path innerFile = new Path(originalFolder, "innerFile"); assertTrue(fs.createNewFile(innerFile)); Path innerFile2 = new Path(originalFolder, "innerFile2"); assertTrue(fs.createNewFile(innerFile2)); String dstKey = "renamedFolder"; // propose (but don't do) the rename Path home = fs.getHomeDirectory(); String relativeHomeDir = getRelativePath(home.toString()); NativeAzureFileSystem.FolderRenamePending pending = new NativeAzureFileSystem.FolderRenamePending( relativeHomeDir + "/" + srcKey, relativeHomeDir + "/" + dstKey, null, (NativeAzureFileSystem) fs); // get the rename pending file contents String renameDescription = pending.makeRenamePendingFileContents(); // Remove one file from source folder to simulate a partially done // rename operation. assertTrue(fs.delete(innerFile, false)); // Create the destination folder with just one file in it, again // to simulate a partially done rename. Path destination = new Path(dstKey); Path innerDest = new Path(destination, "innerFile"); assertTrue(fs.createNewFile(innerDest)); // Create a rename-pending file and write rename information to it. final String renamePendingStr = "folderToRename-RenamePending.json"; Path renamePendingFile = new Path(renamePendingStr); FSDataOutputStream out = fs.create(renamePendingFile, true); assertTrue(out != null); writeString(out, renameDescription); // Redo the rename operation based on the contents of the -RenamePending.json file. // Trigger the redo by checking for existence of the original folder. It must appear // to not exist. assertFalse(fs.exists(originalFolder)); // Verify that the target is there, and the source is gone. assertTrue(fs.exists(destination)); assertTrue(fs.exists(new Path(destination, innerFile.getName()))); assertTrue(fs.exists(new Path(destination, innerFile2.getName()))); assertFalse(fs.exists(originalFolder)); assertFalse(fs.exists(innerFile)); assertFalse(fs.exists(innerFile2)); // Verify that there's no RenamePending file left. assertFalse(fs.exists(renamePendingFile)); // Verify that we can list the target directory. FileStatus[] listed = fs.listStatus(destination); assertEquals(2, listed.length); // List the home directory and show the contents is a directory. Path root = fs.getHomeDirectory(); listed = fs.listStatus(root); assertEquals(1, listed.length); assertTrue(listed[0].isDirectory()); }
/** * If there is a folder to be renamed inside a parent folder, * then when you list the parent folder, you should only see * the final result, after the rename. */ @Test public void testRedoRenameFolderInFolderListing() throws IOException { // create original folder String parent = "parent"; Path parentFolder = new Path(parent); assertTrue(fs.mkdirs(parentFolder)); Path inner = new Path(parentFolder, "innerFolder"); assertTrue(fs.mkdirs(inner)); Path inner2 = new Path(parentFolder, "innerFolder2"); assertTrue(fs.mkdirs(inner2)); Path innerFile = new Path(inner2, "file"); assertTrue(fs.createNewFile(innerFile)); Path inner2renamed = new Path(parentFolder, "innerFolder2Renamed"); // propose (but don't do) the rename of innerFolder2 Path home = fs.getHomeDirectory(); String relativeHomeDir = getRelativePath(home.toString()); NativeAzureFileSystem.FolderRenamePending pending = new NativeAzureFileSystem.FolderRenamePending( relativeHomeDir + "/" + inner2, relativeHomeDir + "/" + inner2renamed, null, (NativeAzureFileSystem) fs); // Create a rename-pending file and write rename information to it. final String renamePendingStr = inner2 + FolderRenamePending.SUFFIX; Path renamePendingFile = new Path(renamePendingStr); FSDataOutputStream out = fs.create(renamePendingFile, true); assertTrue(out != null); writeString(out, pending.makeRenamePendingFileContents()); // Redo the rename operation based on the contents of the // -RenamePending.json file. Trigger the redo by checking for existence of // the original folder. It must appear to not exist. FileStatus[] listed = fs.listStatus(parentFolder); assertEquals(2, listed.length); assertTrue(listed[0].isDirectory()); assertTrue(listed[1].isDirectory()); // The rename pending file is not a directory, so at this point we know the // redo has been done. assertFalse(fs.exists(inner2)); // verify original folder is gone assertTrue(fs.exists(inner2renamed)); // verify the target is there assertTrue(fs.exists(new Path(inner2renamed, "file"))); }