@Test public void testDeleteOnExit() throws Exception { // Create deleteOnExit entries Path file1 = helper.getTestRootPath(fc, "file1"); createFile(fc, file1, numBlocks, blockSize); fc.deleteOnExit(file1); checkDeleteOnExitData(1, fc, file1); // Ensure shutdown hook is added Assert.assertTrue(ShutdownHookManager.get().hasShutdownHook(FileContext.FINALIZER)); Path file2 = helper.getTestRootPath(fc, "dir1/file2"); createFile(fc, file2, numBlocks, blockSize); fc.deleteOnExit(file2); checkDeleteOnExitData(1, fc, file1, file2); Path dir = helper.getTestRootPath(fc, "dir3/dir4/dir5/dir6"); createFile(fc, dir, numBlocks, blockSize); fc.deleteOnExit(dir); checkDeleteOnExitData(1, fc, file1, file2, dir); // trigger deleteOnExit and ensure the registered // paths are cleaned up FileContext.FINALIZER.run(); checkDeleteOnExitData(0, fc, new Path[0]); Assert.assertFalse(exists(fc, file1)); Assert.assertFalse(exists(fc, file2)); Assert.assertFalse(exists(fc, dir)); }
@Test public void testCreateFile() throws IOException { String fileNames[] = { "testFile", "test File", "test*File", "test#File", "test1234", "1234Test", "test)File", "test_File", "()&^%$#@!~_+}{><?", " ", "^ " }; for (String f : fileNames) { // 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 file createFile(fc1, testPath); // Ensure fc2 has the created file Assert.assertTrue(exists(fc2, testPath)); } }
@Test public void testDeleteNonExistingFileInDir() throws IOException { String testFileInDir = "testDir/testDir/TestFile"; Path testPath = qualifiedPath(testFileInDir, fc2); // TestCase1 : Test delete on file never existed // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // Delete on non existing file should return false Assert.assertFalse(fc2.delete(testPath, false)); // TestCase2 : Create , Delete , Delete file // Create a file on fc2's file system using fc1 createFile(fc1, testPath); // Ensure file 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 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 testCreateExistingFile() throws IOException { String fileName = "testFile"; Path testPath = qualifiedPath(fileName, fc2); // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // Create a file on fc2's file system using fc1 createFile(fc1, testPath); // Create same file with fc1 try { createFile(fc2, testPath); Assert.fail("Create existing file should throw an IOException."); } catch (IOException e) { // expected } // Ensure fc2 has the created file Assert.assertTrue(exists(fc2, testPath)); }
@Test public void testFileStatus() throws IOException { String fileName = "file1"; Path path2 = fc2.makeQualified(new Path(BASE, fileName)); // Create a file on fc2's file system using fc1 createFile(fc1, path2); FsStatus fc2Status = fc2.getFsStatus(path2); // FsStatus , used, free and capacity are non-negative longs Assert.assertNotNull(fc2Status); Assert.assertTrue(fc2Status.getCapacity() > 0); Assert.assertTrue(fc2Status.getRemaining() > 0); Assert.assertTrue(fc2Status.getUsed() > 0); }
@Test public void testModificationTime() throws IOException { String testFile = "file1"; long fc2ModificationTime, fc1ModificationTime; Path testPath = qualifiedPath(testFile, fc2); // Create a file on fc2's file system using fc1 createFile(fc1, testPath); // Get modification time using fc2 and fc1 fc1ModificationTime = fc1.getFileStatus(testPath).getModificationTime(); fc2ModificationTime = fc2.getFileStatus(testPath).getModificationTime(); // Ensure fc1 and fc2 reports same modification time Assert.assertEquals(fc1ModificationTime, fc2ModificationTime); }
@Test public void testCreateFileWithNullName() throws IOException { String fileName = null; try { Path testPath = qualifiedPath(fileName, fc2); // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // Create a file on fc2's file system using fc1 createFile(fc1, testPath); Assert.fail("Create file with null name should throw IllegalArgumentException."); } catch (IllegalArgumentException e) { // expected } }
@Test public void testCreateFileInNonExistingDirectory() throws IOException { String fileName = "testDir/testFile"; Path testPath = qualifiedPath(fileName, fc2); // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // Create a file on fc2's file system using fc1 createFile(fc1, testPath); // Ensure using fc2 that file is created Assert.assertTrue(isDir(fc2, testPath.getParent())); Assert.assertEquals("testDir", testPath.getParent().getName()); Assert.assertTrue(exists(fc2, testPath)); }
@Test public void testDeleteFile() throws IOException { Path testPath = qualifiedPath("testFile", fc2); // Ensure file does not exist Assert.assertFalse(exists(fc2, testPath)); // First create a file on file system using fc1 createFile(fc1, testPath); // Ensure file exist Assert.assertTrue(exists(fc2, testPath)); // Delete file using fc2 fc2.delete(testPath, false); // Ensure fc2 does not have deleted file Assert.assertFalse(exists(fc2, testPath)); }
public static void createFile(FileContext fc, Path path) throws IOException { createFile(fc, path, DEFAULT_NUM_BLOCKS, CreateOpts.createParent()); }
public static void createFile(FileContext fc, Path path, int numBlocks, int blockSize) throws IOException { createFile(fc, path, numBlocks, CreateOpts.blockSize(blockSize), CreateOpts.createParent()); }
public static Path createFile(FileContext fc, String name) throws IOException { Path path = getTestRootPath(fc, name); createFile(fc, path); return path; }