@Test public void testMkdirsFailsForSubdirectoryOfExistingFile() throws Exception { Path testDir = qualifiedPath("test/hadoop", fc2); Assert.assertFalse(exists(fc2, testDir)); fc2.mkdir(testDir, FsPermission.getDefault(), true); Assert.assertTrue(exists(fc2, testDir)); // Create file on fc1 using fc2 context createFile(fc1, qualifiedPath("test/hadoop/file", fc2)); Path testSubDir = qualifiedPath("test/hadoop/file/subdir", fc2); try { fc1.mkdir(testSubDir, FsPermission.getDefault(), true); Assert.fail("Should throw IOException."); } catch (IOException e) { // expected } Assert.assertFalse(exists(fc1, testSubDir)); Path testDeepSubDir = qualifiedPath("test/hadoop/file/deep/sub/dir", fc1); try { fc2.mkdir(testDeepSubDir, FsPermission.getDefault(), true); Assert.fail("Should throw IOException."); } catch (IOException e) { // expected } Assert.assertFalse(exists(fc1, testDeepSubDir)); }
@Test public void testDeleteDirectory() throws IOException { String dirName = "dirTest"; Path testDirPath = qualifiedPath(dirName, fc2); // Ensure directory does not exist Assert.assertFalse(exists(fc2, testDirPath)); // Create a directory on fc2's file system using fc1 fc1.mkdir(testDirPath, FsPermission.getDefault(), true); // Ensure dir is created Assert.assertTrue(exists(fc2, testDirPath)); Assert.assertTrue(isDir(fc2, testDirPath)); fc2.delete(testDirPath, true); // Ensure that directory is deleted Assert.assertFalse(isDir(fc2, testDirPath)); // TestCase - Create and delete multiple directories String dirNames[] = { "deleteTest/testDir", "deleteTest/test Dir", "deleteTest/test*Dir", "deleteTest/test#Dir", "deleteTest/test1234", "deleteTest/1234Test", "deleteTest/test)Dir", "deleteTest/test_DIr", "deleteTest/()&^%$#@!~_+}{><?", " ", "^ " }; for (String f : dirNames) { // Create a file on fc2's file system using fc1 Path testPath = qualifiedPath(f, fc2); // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // Now create directory fc1.mkdir(testPath, FsPermission.getDefault(), true); // Ensure fc2 has the created directory Assert.assertTrue(exists(fc2, testPath)); Assert.assertTrue(isDir(fc2, testPath)); // Delete dir Assert.assertTrue(fc2.delete(testPath, true)); // verify if directory is deleted Assert.assertFalse(exists(fc2, testPath)); Assert.assertFalse(isDir(fc2, testPath)); } }
@Test public void testDeleteNonExistingDirectory() throws IOException { String testDirName = "testFile"; Path testPath = qualifiedPath(testDirName, fc2); // TestCase1 : Test delete on directory never existed // Ensure directory does not exist Assert.assertFalse(exists(fc2, testPath)); // Delete on non existing directory should return false Assert.assertFalse(fc2.delete(testPath, false)); // TestCase2 : Create dir, Delete dir, Delete dir // Create a file on fc2's file system using fc1 fc1.mkdir(testPath, FsPermission.getDefault(), true); // Ensure dir exist Assert.assertTrue(exists(fc2, testPath)); // Delete test file, deleting existing file should return true Assert.assertTrue(fc2.delete(testPath, false)); // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // Delete on non existing file should return false Assert.assertFalse(fc2.delete(testPath, false)); }
@Test public void testIsDirectory() throws IOException { String dirName = "dirTest"; String invalidDir = "nonExistantDir"; String rootDir = "/"; Path existingPath = qualifiedPath(dirName, fc2); Path nonExistingPath = qualifiedPath(invalidDir, fc2); Path pathToRootDir = qualifiedPath(rootDir, fc2); // Create a directory on fc2's file system using fc1 fc1.mkdir(existingPath, FsPermission.getDefault(), true); // Ensure fc2 has directory Assert.assertTrue(isDir(fc2, existingPath)); Assert.assertTrue(isDir(fc2, pathToRootDir)); // Negative test case Assert.assertFalse(isDir(fc2, nonExistingPath)); }
@Test public void testListStatus() throws Exception { final String hPrefix = "test/hadoop"; final String[] dirs = { hPrefix + "/a", hPrefix + "/b", hPrefix + "/c", hPrefix + "/1", hPrefix + "/#@#@", hPrefix + "/&*#$#$@234" }; ArrayList<Path> testDirs = new ArrayList<Path>(); for (String d : dirs) { testDirs.add(qualifiedPath(d, fc2)); } Assert.assertFalse(exists(fc1, testDirs.get(0))); for (Path path : testDirs) { fc1.mkdir(path, FsPermission.getDefault(), true); } // test listStatus that returns an array of FileStatus FileStatus[] paths = fc1.util().listStatus(qualifiedPath("test", fc1)); Assert.assertEquals(1, paths.length); Assert.assertEquals(qualifiedPath(hPrefix, fc1), paths[0].getPath()); paths = fc1.util().listStatus(qualifiedPath(hPrefix, fc1)); Assert.assertEquals(6, paths.length); for (int i = 0; i < dirs.length; i++) { boolean found = false; for (int j = 0; j < paths.length; j++) { if (qualifiedPath(dirs[i], fc1).equals(paths[j].getPath())) { found = true; } } Assert.assertTrue(dirs[i] + " not found", found); } paths = fc1.util().listStatus(qualifiedPath(dirs[0], fc1)); Assert.assertEquals(0, paths.length); // test listStatus that returns an iterator of FileStatus RemoteIterator<FileStatus> pathsItor = fc1.listStatus(qualifiedPath("test", fc1)); Assert.assertEquals(qualifiedPath(hPrefix, fc1), pathsItor.next().getPath()); Assert.assertFalse(pathsItor.hasNext()); pathsItor = fc1.listStatus(qualifiedPath(hPrefix, fc1)); int dirLen = 0; for (; pathsItor.hasNext(); dirLen++) { boolean found = false; FileStatus stat = pathsItor.next(); for (int j = 0; j < dirs.length; j++) { if (qualifiedPath(dirs[j], fc1).equals(stat.getPath())) { found = true; break; } } Assert.assertTrue(stat.getPath() + " not found", found); } Assert.assertEquals(6, dirLen); pathsItor = fc1.listStatus(qualifiedPath(dirs[0], fc1)); Assert.assertFalse(pathsItor.hasNext()); }
@Test public void testCreateDirectory() throws IOException { Path path = qualifiedPath("test/hadoop", fc2); Path falsePath = qualifiedPath("path/doesnot.exist", fc2); Path subDirPath = qualifiedPath("dir0", fc2); // Ensure that testPath does not exist in fc1 Assert.assertFalse(exists(fc1, path)); Assert.assertFalse(isFile(fc1, path)); Assert.assertFalse(isDir(fc1, path)); // Create a directory on fc2's file system using fc1 fc1.mkdir(path, FsPermission.getDefault(), true); // Ensure fc2 has directory Assert.assertTrue(isDir(fc2, path)); Assert.assertTrue(exists(fc2, path)); Assert.assertFalse(isFile(fc2, path)); // Test to create same dir twice, (HDFS mkdir is similar to mkdir -p ) fc1.mkdir(subDirPath, FsPermission.getDefault(), true); // This should not throw exception fc1.mkdir(subDirPath, FsPermission.getDefault(), true); // Create Sub Dirs fc1.mkdir(subDirPath, FsPermission.getDefault(), true); // Check parent dir Path parentDir = path.getParent(); Assert.assertTrue(exists(fc2, parentDir)); Assert.assertFalse(isFile(fc2, parentDir)); // Check parent parent dir Path grandparentDir = parentDir.getParent(); Assert.assertTrue(exists(fc2, grandparentDir)); Assert.assertFalse(isFile(fc2, grandparentDir)); // Negative test cases Assert.assertFalse(exists(fc2, falsePath)); Assert.assertFalse(isDir(fc2, falsePath)); // TestCase - Create multiple directories String dirNames[] = { "createTest/testDir", "createTest/test Dir", "deleteTest/test*Dir", "deleteTest/test#Dir", "deleteTest/test1234", "deleteTest/test_DIr", "deleteTest/1234Test", "deleteTest/test)Dir", "deleteTest/()&^%$#@!~_+}{><?", " ", "^ " }; for (String f : dirNames) { // Create a file on fc2's file system using fc1 Path testPath = qualifiedPath(f, fc2); // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // Now create directory fc1.mkdir(testPath, FsPermission.getDefault(), true); // Ensure fc2 has the created directory Assert.assertTrue(exists(fc2, testPath)); Assert.assertTrue(isDir(fc2, testPath)); } }