HADOOP-6394. Add a helper class to simplify FileContext related tests and improve code reusability. Contributed by Jitendra Nath Pandey.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@891511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Suresh Srinivas 2009-12-17 01:30:33 +00:00
parent 6a1e8bf452
commit ea5db0c5ab
7 changed files with 418 additions and 372 deletions

View File

@ -63,6 +63,9 @@ Trunk (unreleased changes)
HADOOP-6222. Core doesn't have TestCommonCLI facility. (cos) HADOOP-6222. Core doesn't have TestCommonCLI facility. (cos)
HADOOP-6394. Add a helper class to simplify FileContext related tests and
improve code reusability. (Jitendra Nath Pandey via suresh)
HADOOP-6426. Create ant build for running EC2 unit tests. (tomwhite) HADOOP-6426. Create ant build for running EC2 unit tests. (tomwhite)
OPTIMIZATIONS OPTIMIZATIONS

View File

@ -19,14 +19,15 @@ package org.apache.hadoop.fs;
import java.io.IOException; import java.io.IOException;
import java.util.EnumSet;
import org.apache.hadoop.fs.Options.CreateOpts;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
import org.apache.log4j.Level;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
import org.apache.commons.logging.impl.Log4JLogger;
/** /**
* <p> * <p>
@ -48,31 +49,13 @@ import org.junit.Test;
* </p> * </p>
*/ */
public class FileContextCreateMkdirBaseTest { public abstract class FileContextCreateMkdirBaseTest {
protected static FileContext fc; protected static FileContext fc;
static final String TEST_ROOT_DIR = new Path(System.getProperty(
"test.build.data", "build/test/data")).toString().replace(' ', '_')
+ "/test";
protected Path getTestRootRelativePath(String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
private Path rootPath = null;
protected Path getTestRoot() {
if (rootPath == null) {
rootPath = fc.makeQualified(new Path(TEST_ROOT_DIR));
}
return rootPath;
}
{ {
try { try {
((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger() ((Log4JLogger)FileSystem.LOG).getLogger().setLevel(Level.DEBUG);
.setLevel(org.apache.log4j.Level.DEBUG);
} }
catch(Exception e) { catch(Exception e) {
System.out.println("Cannot change log level\n" System.out.println("Cannot change log level\n"
@ -81,15 +64,14 @@ public class FileContextCreateMkdirBaseTest {
} }
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
fc.mkdir(getTestRoot(), FileContext.DEFAULT_PERM, true); fc.mkdir(getTestRootPath(fc), FileContext.DEFAULT_PERM, true);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
fc.delete(getTestRoot(), true); fc.delete(getTestRootPath(fc), true);
} }
@ -100,7 +82,7 @@ public class FileContextCreateMkdirBaseTest {
@Test @Test
public void testMkdirNonRecursiveWithExistingDir() throws IOException { public void testMkdirNonRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("aDir"); Path f = getTestRootPath(fc, "aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, false); fc.mkdir(f, FileContext.DEFAULT_PERM, false);
Assert.assertTrue(fc.isDirectory(f)); Assert.assertTrue(fc.isDirectory(f));
} }
@ -108,7 +90,7 @@ public class FileContextCreateMkdirBaseTest {
@Test @Test
public void testMkdirNonRecursiveWithNonExistingDir() { public void testMkdirNonRecursiveWithNonExistingDir() {
try { try {
fc.mkdir(getTestRootRelativePath("NonExistant/aDir"), fc.mkdir(getTestRootPath(fc,"NonExistant/aDir"),
FileContext.DEFAULT_PERM, false); FileContext.DEFAULT_PERM, false);
Assert.fail("Mkdir with non existing parent dir should have failed"); Assert.fail("Mkdir with non existing parent dir should have failed");
} catch (IOException e) { } catch (IOException e) {
@ -119,7 +101,7 @@ public class FileContextCreateMkdirBaseTest {
@Test @Test
public void testMkdirRecursiveWithExistingDir() throws IOException { public void testMkdirRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("aDir"); Path f = getTestRootPath(fc, "aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, true); fc.mkdir(f, FileContext.DEFAULT_PERM, true);
Assert.assertTrue(fc.isDirectory(f)); Assert.assertTrue(fc.isDirectory(f));
} }
@ -127,7 +109,7 @@ public class FileContextCreateMkdirBaseTest {
@Test @Test
public void testMkdirRecursiveWithNonExistingDir() throws IOException { public void testMkdirRecursiveWithNonExistingDir() throws IOException {
Path f = getTestRootRelativePath("NonExistant2/aDir"); Path f = getTestRootPath(fc, "NonExistant2/aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, true); fc.mkdir(f, FileContext.DEFAULT_PERM, true);
Assert.assertTrue(fc.isDirectory(f)); Assert.assertTrue(fc.isDirectory(f));
} }
@ -137,15 +119,15 @@ public class FileContextCreateMkdirBaseTest {
//////////////////////// ////////////////////////
@Test @Test
public void testCreateNonRecursiveWithExistingDir() throws IOException { public void testCreateNonRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("foo"); Path f = getTestRootPath(fc, "foo");
createFile(f); createFile(fc, f);
Assert.assertTrue(fc.isFile(f)); Assert.assertTrue(fc.isFile(f));
} }
@Test @Test
public void testCreateNonRecursiveWithNonExistingDir() { public void testCreateNonRecursiveWithNonExistingDir() {
try { try {
createFile(getTestRootRelativePath("NonExisting/foo")); createFileNonRecursive(fc, getTestRootPath(fc, "NonExisting/foo"));
Assert.fail("Create with non existing parent dir should have failed"); Assert.fail("Create with non existing parent dir should have failed");
} catch (IOException e) { } catch (IOException e) {
// As expected // As expected
@ -155,36 +137,16 @@ public class FileContextCreateMkdirBaseTest {
@Test @Test
public void testCreateRecursiveWithExistingDir() throws IOException { public void testCreateRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("foo"); Path f = getTestRootPath(fc,"foo");
createFile(f, CreateOpts.createParent()); createFile(fc, f);
Assert.assertTrue(fc.isFile(f)); Assert.assertTrue(fc.isFile(f));
} }
@Test @Test
public void testCreateRecursiveWithNonExistingDir() throws IOException { public void testCreateRecursiveWithNonExistingDir() throws IOException {
Path f = getTestRootRelativePath("NonExisting/foo"); Path f = getTestRootPath(fc,"NonExisting/foo");
createFile(f, CreateOpts.createParent()); createFile(fc, f);
Assert.assertTrue(fc.isFile(f)); Assert.assertTrue(fc.isFile(f));
} }
protected static int getBlockSize() {
return 1024;
}
private static byte[] data = new byte[getBlockSize() * 2]; // two blocks of data
{
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
protected void createFile(Path path,
CreateOpts.CreateParent ... opt) throws IOException {
FSDataOutputStream out = fc.create(path,EnumSet.of(CreateFlag.CREATE), opt);
out.write(data, 0, data.length);
out.close();
}
} }

View File

@ -30,6 +30,8 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/** /**
* <p> * <p>
* A collection of tests for the {@link FileContext}. * A collection of tests for the {@link FileContext}.
@ -55,38 +57,10 @@ public abstract class FileContextMainOperationsBaseTest {
private static String TEST_DIR_AAA = "test/hadoop/aaa"; private static String TEST_DIR_AAA = "test/hadoop/aaa";
private static String TEST_DIR_AXA = "test/hadoop/axa"; private static String TEST_DIR_AXA = "test/hadoop/axa";
private static String TEST_DIR_AXX = "test/hadoop/axx"; private static String TEST_DIR_AXX = "test/hadoop/axx";
private static int numBlocks = 2;
static final String LOCAL_FS_ROOT_URI = "file:///tmp/test"; static final String LOCAL_FS_ROOT_URI = "file:///tmp/test";
static final String TEST_ROOT_DIR =
System.getProperty("test.build.data", "build/test/data").replace(' ', '_');
/**
* we need to store the absRootDir because some tests may do a setWd and
* the TEST_ROOT_DIR may itself be relative.
*/
String absTestRootDir = null;
protected String getAbsoluteTestRootDir() throws IOException {
if (absTestRootDir == null) {
if (TEST_ROOT_DIR.startsWith("/")) {
absTestRootDir = TEST_ROOT_DIR;
} else {
absTestRootDir = getDefaultWorkingDirectory().toString() + "/" +
TEST_ROOT_DIR;
}
}
return absTestRootDir;
}
protected Path getTestRootDir() throws IOException {
return fc.makeQualified(new Path(getAbsoluteTestRootDir()));
}
protected Path getTestRootPath(String pathString) throws IOException {
return fc.makeQualified(new Path(getAbsoluteTestRootDir(), pathString));
}
protected static FileContext fc; protected static FileContext fc;
@ -106,31 +80,25 @@ public abstract class FileContextMainOperationsBaseTest {
} }
}; };
private static byte[] data = new byte[getBlockSize() * 2]; // two blocks of data private static byte[] data = getFileData(numBlocks,
{ getDefaultBlockSize());
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
fc.mkdir(getTestRootPath("test"), FileContext.DEFAULT_PERM, true); fc.mkdir(getTestRootPath(fc, "test"), FileContext.DEFAULT_PERM, true);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
fc.delete(getTestRootPath("test"), true); fc.delete(getTestRootPath(fc, "test"), true);
fc.delete(new Path(LOCAL_FS_ROOT_URI), true); fc.delete(new Path(LOCAL_FS_ROOT_URI), true);
} }
protected static int getBlockSize() {
return 1024;
}
protected Path getDefaultWorkingDirectory() throws IOException { protected Path getDefaultWorkingDirectory() throws IOException {
return getTestRootPath("/user/" + System.getProperty("user.name")).makeQualified( return getTestRootPath(fc,
fc.getDefaultFileSystem().getUri(), fc.getWorkingDirectory()); "/user/" + System.getProperty("user.name")).makeQualified(
fc.getDefaultFileSystem().getUri(), fc.getWorkingDirectory());
} }
protected boolean renameSupported() { protected boolean renameSupported() {
@ -156,7 +124,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testWorkingDirectory() throws Exception { public void testWorkingDirectory() throws Exception {
// First we cd to our test root // First we cd to our test root
Path workDir = new Path(getTestRootDir(), new Path("test")); Path workDir = new Path(getTestRootDir(fc), new Path("test"));
fc.setWorkingDirectory(workDir); fc.setWorkingDirectory(workDir);
Assert.assertEquals(workDir, fc.getWorkingDirectory()); Assert.assertEquals(workDir, fc.getWorkingDirectory());
@ -169,7 +137,7 @@ public abstract class FileContextMainOperationsBaseTest {
// cd using a relative path // cd using a relative path
// Go back to our test root // Go back to our test root
workDir = new Path(getTestRootDir(), new Path("test")); workDir = getTestRootPath(fc, "test");
fc.setWorkingDirectory(workDir); fc.setWorkingDirectory(workDir);
Assert.assertEquals(workDir, fc.getWorkingDirectory()); Assert.assertEquals(workDir, fc.getWorkingDirectory());
@ -177,10 +145,9 @@ public abstract class FileContextMainOperationsBaseTest {
Path absoluteDir = new Path(workDir,"existingDir1"); Path absoluteDir = new Path(workDir,"existingDir1");
fc.mkdir(absoluteDir, FileContext.DEFAULT_PERM, true); fc.mkdir(absoluteDir, FileContext.DEFAULT_PERM, true);
fc.setWorkingDirectory(relativeDir); fc.setWorkingDirectory(relativeDir);
Assert.assertEquals(absoluteDir, Assert.assertEquals(absoluteDir, fc.getWorkingDirectory());
fc.getWorkingDirectory());
// cd using a absolute path // cd using a absolute path
absoluteDir = getTestRootPath("test/existingDir2"); absoluteDir = getTestRootPath(fc, "test/existingDir2");
fc.mkdir(absoluteDir, FileContext.DEFAULT_PERM, true); fc.mkdir(absoluteDir, FileContext.DEFAULT_PERM, true);
fc.setWorkingDirectory(absoluteDir); fc.setWorkingDirectory(absoluteDir);
Assert.assertEquals(absoluteDir, fc.getWorkingDirectory()); Assert.assertEquals(absoluteDir, fc.getWorkingDirectory());
@ -190,7 +157,7 @@ public abstract class FileContextMainOperationsBaseTest {
fc.create(absolutePath, EnumSet.of(CreateFlag.CREATE)).close(); fc.create(absolutePath, EnumSet.of(CreateFlag.CREATE)).close();
fc.open(new Path("foo")).close(); fc.open(new Path("foo")).close();
absoluteDir = getTestRootPath("nonexistingPath"); absoluteDir = getTestRootPath(fc, "nonexistingPath");
try { try {
fc.setWorkingDirectory(absoluteDir); fc.setWorkingDirectory(absoluteDir);
Assert.fail("cd to non existing dir should have failed"); Assert.fail("cd to non existing dir should have failed");
@ -209,7 +176,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testMkdirs() throws Exception { public void testMkdirs() throws Exception {
Path testDir = getTestRootPath("test/hadoop"); Path testDir = getTestRootPath(fc, "test/hadoop");
Assert.assertFalse(fc.exists(testDir)); Assert.assertFalse(fc.exists(testDir));
Assert.assertFalse(fc.isFile(testDir)); Assert.assertFalse(fc.isFile(testDir));
@ -235,14 +202,14 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testMkdirsFailsForSubdirectoryOfExistingFile() throws Exception { public void testMkdirsFailsForSubdirectoryOfExistingFile() throws Exception {
Path testDir = getTestRootPath("test/hadoop"); Path testDir = getTestRootPath(fc, "test/hadoop");
Assert.assertFalse(fc.exists(testDir)); Assert.assertFalse(fc.exists(testDir));
fc.mkdir(testDir, FsPermission.getDefault(), true); fc.mkdir(testDir, FsPermission.getDefault(), true);
Assert.assertTrue(fc.exists(testDir)); Assert.assertTrue(fc.exists(testDir));
createFile(getTestRootPath("test/hadoop/file")); createFile(getTestRootPath(fc, "test/hadoop/file"));
Path testSubDir = getTestRootPath("test/hadoop/file/subdir"); Path testSubDir = getTestRootPath(fc, "test/hadoop/file/subdir");
try { try {
fc.mkdir(testSubDir, FsPermission.getDefault(), true); fc.mkdir(testSubDir, FsPermission.getDefault(), true);
Assert.fail("Should throw IOException."); Assert.fail("Should throw IOException.");
@ -251,7 +218,7 @@ public abstract class FileContextMainOperationsBaseTest {
} }
Assert.assertFalse(fc.exists(testSubDir)); Assert.assertFalse(fc.exists(testSubDir));
Path testDeepSubDir = getTestRootPath("test/hadoop/file/deep/sub/dir"); Path testDeepSubDir = getTestRootPath(fc, "test/hadoop/file/deep/sub/dir");
try { try {
fc.mkdir(testDeepSubDir, FsPermission.getDefault(), true); fc.mkdir(testDeepSubDir, FsPermission.getDefault(), true);
Assert.fail("Should throw IOException."); Assert.fail("Should throw IOException.");
@ -266,7 +233,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testGetFileStatusThrowsExceptionForNonExistentFile() public void testGetFileStatusThrowsExceptionForNonExistentFile()
throws Exception { throws Exception {
try { try {
fc.getFileStatus(getTestRootPath("test/hadoop/file")); fc.getFileStatus(getTestRootPath(fc, "test/hadoop/file"));
Assert.fail("Should throw FileNotFoundException"); Assert.fail("Should throw FileNotFoundException");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// expected // expected
@ -276,7 +243,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testListStatusThrowsExceptionForNonExistentFile() public void testListStatusThrowsExceptionForNonExistentFile()
throws Exception { throws Exception {
try { try {
fc.listStatus(getTestRootPath("test/hadoop/file")); fc.listStatus(getTestRootPath(fc, "test/hadoop/file"));
Assert.fail("Should throw FileNotFoundException"); Assert.fail("Should throw FileNotFoundException");
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
// expected // expected
@ -285,36 +252,41 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testListStatus() throws Exception { public void testListStatus() throws Exception {
Path[] testDirs = { getTestRootPath("test/hadoop/a"), Path[] testDirs = {
getTestRootPath("test/hadoop/b"), getTestRootPath(fc, "test/hadoop/a"),
getTestRootPath("test/hadoop/c/1"), }; getTestRootPath(fc, "test/hadoop/b"),
getTestRootPath(fc, "test/hadoop/c/1"), };
Assert.assertFalse(fc.exists(testDirs[0])); Assert.assertFalse(fc.exists(testDirs[0]));
for (Path path : testDirs) { for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true); fc.mkdir(path, FsPermission.getDefault(), true);
} }
FileStatus[] paths = fc.listStatus(getTestRootPath("test")); FileStatus[] paths = fc.listStatus(getTestRootPath(fc, "test"));
Assert.assertEquals(1, paths.length); Assert.assertEquals(1, paths.length);
Assert.assertEquals(getTestRootPath("test/hadoop"), paths[0].getPath()); Assert.assertEquals(getTestRootPath(fc, "test/hadoop"), paths[0].getPath());
paths = fc.listStatus(getTestRootPath("test/hadoop")); paths = fc.listStatus(getTestRootPath(fc, "test/hadoop"));
Assert.assertEquals(3, paths.length); Assert.assertEquals(3, paths.length);
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop/a"), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop/a"),
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop/b"), paths)); paths));
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop/c"), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop/b"),
paths));
paths = fc.listStatus(getTestRootPath("test/hadoop/a")); Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop/c"),
paths));
paths = fc.listStatus(getTestRootPath(fc, "test/hadoop/a"));
Assert.assertEquals(0, paths.length); Assert.assertEquals(0, paths.length);
} }
@Test @Test
public void testListStatusFilterWithNoMatches() throws Exception { public void testListStatusFilterWithNoMatches() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA2), Path[] testDirs = {
getTestRootPath(TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AAA2),
getTestRootPath(TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), }; getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -322,18 +294,19 @@ public abstract class FileContextMainOperationsBaseTest {
} }
} }
//listStatus with filters returns empty correctly // listStatus with filters returns empty correctly
FileStatus[] filteredPaths = fc.util().listStatus(getTestRootPath("test"), FileStatus[] filteredPaths = fc.util().listStatus(
TEST_X_FILTER); getTestRootPath(fc, "test"), TEST_X_FILTER);
Assert.assertEquals(0,filteredPaths.length); Assert.assertEquals(0,filteredPaths.length);
} }
public void testListStatusFilterWithSomeMatches() throws Exception { public void testListStatusFilterWithSomeMatches() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AAA2), }; getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -341,19 +314,23 @@ public abstract class FileContextMainOperationsBaseTest {
} }
} }
//should return 2 paths ("/test/hadoop/axa" and "/test/hadoop/axx") // should return 2 paths ("/test/hadoop/axa" and "/test/hadoop/axx")
FileStatus[] filteredPaths = fc.util().listStatus(getTestRootPath("test/hadoop"), FileStatus[] filteredPaths = fc.util()
TEST_X_FILTER); .listStatus(getTestRootPath(fc, "test/hadoop"),
TEST_X_FILTER);
Assert.assertEquals(2,filteredPaths.length); Assert.assertEquals(2,filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths)); Assert.assertTrue(containsPath(getTestRootPath(fc,
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths)); TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), filteredPaths));
} }
@Test @Test
public void testGlobStatusThrowsExceptionForNonExistentFile() throws Exception { public void testGlobStatusThrowsExceptionForNonExistentFile() throws Exception {
try { try {
//This should throw a FileNotFoundException // This should throw a FileNotFoundException
fc.util().globStatus(getTestRootPath("test/hadoopfsdf/?")); fc.util().globStatus(
getTestRootPath(fc, "test/hadoopfsdf/?"));
Assert.fail("Should throw FileNotFoundException"); Assert.fail("Should throw FileNotFoundException");
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
// expected // expected
@ -362,10 +339,11 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testGlobStatusWithNoMatchesInPath() throws Exception { public void testGlobStatusWithNoMatchesInPath() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AAA2), }; getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -373,17 +351,19 @@ public abstract class FileContextMainOperationsBaseTest {
} }
} }
//should return nothing // should return nothing
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop/?")); FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/?"));
Assert.assertEquals(0, paths.length); Assert.assertEquals(0, paths.length);
} }
@Test @Test
public void testGlobStatusSomeMatchesInDirectories() throws Exception { public void testGlobStatusSomeMatchesInDirectories() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AAA2), }; getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -391,19 +371,23 @@ public abstract class FileContextMainOperationsBaseTest {
} }
} }
//Should return two items ("/test/hadoop" and "/test/hadoop2") // Should return two items ("/test/hadoop" and "/test/hadoop2")
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop*")); FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop*"));
Assert.assertEquals(2, paths.length); Assert.assertEquals(2, paths.length);
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop"), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc,
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop2"), paths)); "test/hadoop"), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
"test/hadoop2"), paths));
} }
@Test @Test
public void testGlobStatusWithMultipleWildCardMatches() throws Exception { public void testGlobStatusWithMultipleWildCardMatches() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AAA2), }; getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -413,20 +397,22 @@ public abstract class FileContextMainOperationsBaseTest {
//Should return all 4 items ("/test/hadoop/aaa", "/test/hadoop/axa" //Should return all 4 items ("/test/hadoop/aaa", "/test/hadoop/axa"
//"/test/hadoop/axx", and "/test/hadoop2/axx") //"/test/hadoop/axx", and "/test/hadoop2/axx")
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop*/*")); FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop*/*"));
Assert.assertEquals(4, paths.length); Assert.assertEquals(4, paths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA), paths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA), paths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX), paths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA2), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA2), paths));
} }
@Test @Test
public void testGlobStatusWithMultipleMatchesOfSingleChar() throws Exception { public void testGlobStatusWithMultipleMatchesOfSingleChar() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AAA2), }; getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -435,18 +421,22 @@ public abstract class FileContextMainOperationsBaseTest {
} }
//Should return only 2 items ("/test/hadoop/axa", "/test/hadoop/axx") //Should return only 2 items ("/test/hadoop/axa", "/test/hadoop/axx")
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop/ax?")); FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/ax?"));
Assert.assertEquals(2, paths.length); Assert.assertEquals(2, paths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc,
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), paths)); TEST_DIR_AXA), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), paths));
} }
@Test @Test
public void testGlobStatusFilterWithEmptyPathResults() throws Exception { public void testGlobStatusFilterWithEmptyPathResults() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX), }; getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -455,17 +445,20 @@ public abstract class FileContextMainOperationsBaseTest {
} }
//This should return an empty set //This should return an empty set
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/?"), FileStatus[] filteredPaths = fc.util().globStatus(
DEFAULT_FILTER); getTestRootPath(fc, "test/hadoop/?"),
DEFAULT_FILTER);
Assert.assertEquals(0,filteredPaths.length); Assert.assertEquals(0,filteredPaths.length);
} }
@Test @Test
public void testGlobStatusFilterWithSomePathMatchesAndTrivialFilter() throws Exception { public void testGlobStatusFilterWithSomePathMatchesAndTrivialFilter()
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), throws Exception {
getTestRootPath(TEST_DIR_AXA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), }; getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -474,20 +467,26 @@ public abstract class FileContextMainOperationsBaseTest {
} }
//This should return all three (aaa, axa, axx) //This should return all three (aaa, axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/*"), FileStatus[] filteredPaths = fc.util().globStatus(
DEFAULT_FILTER); getTestRootPath(fc, "test/hadoop/*"),
Assert.assertEquals(3,filteredPaths.length); DEFAULT_FILTER);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA), filteredPaths)); Assert.assertEquals(3, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths)); Assert.assertTrue(containsPath(getTestRootPath(fc,
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths)); TEST_DIR_AAA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), filteredPaths));
} }
@Test @Test
public void testGlobStatusFilterWithMultipleWildCardMatchesAndTrivialFilter() throws Exception { public void testGlobStatusFilterWithMultipleWildCardMatchesAndTrivialFilter()
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), throws Exception {
getTestRootPath(TEST_DIR_AXA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), }; getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -496,20 +495,26 @@ public abstract class FileContextMainOperationsBaseTest {
} }
//This should return all three (aaa, axa, axx) //This should return all three (aaa, axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/a??"), FileStatus[] filteredPaths = fc.util().globStatus(
DEFAULT_FILTER); getTestRootPath(fc, "test/hadoop/a??"),
Assert.assertEquals(3,filteredPaths.length); DEFAULT_FILTER);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA), filteredPaths)); Assert.assertEquals(3, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA),
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths)); filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA),
filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX),
filteredPaths));
} }
@Test @Test
public void testGlobStatusFilterWithMultiplePathMatchesAndNonTrivialFilter() throws Exception { public void testGlobStatusFilterWithMultiplePathMatchesAndNonTrivialFilter()
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), throws Exception {
getTestRootPath(TEST_DIR_AXA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), }; getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -518,19 +523,24 @@ public abstract class FileContextMainOperationsBaseTest {
} }
//This should return two (axa, axx) //This should return two (axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/*"), FileStatus[] filteredPaths = fc.util().globStatus(
TEST_X_FILTER); getTestRootPath(fc, "test/hadoop/*"),
Assert.assertEquals(2,filteredPaths.length); TEST_X_FILTER);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths)); Assert.assertEquals(2, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths)); Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), filteredPaths));
} }
@Test @Test
public void testGlobStatusFilterWithNoMatchingPathsAndNonTrivialFilter() throws Exception { public void testGlobStatusFilterWithNoMatchingPathsAndNonTrivialFilter()
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), throws Exception {
getTestRootPath(TEST_DIR_AXA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), }; getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -539,17 +549,20 @@ public abstract class FileContextMainOperationsBaseTest {
} }
//This should return an empty set //This should return an empty set
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/?"), FileStatus[] filteredPaths = fc.util().globStatus(
TEST_X_FILTER); getTestRootPath(fc, "test/hadoop/?"),
TEST_X_FILTER);
Assert.assertEquals(0,filteredPaths.length); Assert.assertEquals(0,filteredPaths.length);
} }
@Test @Test
public void testGlobStatusFilterWithMultiplePathWildcardsAndNonTrivialFilter() throws Exception { public void testGlobStatusFilterWithMultiplePathWildcardsAndNonTrivialFilter()
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA), throws Exception {
getTestRootPath(TEST_DIR_AXA), Path[] testDirs = {
getTestRootPath(TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXX), }; getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) { if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) { for (Path path : testDirs) {
@ -558,11 +571,14 @@ public abstract class FileContextMainOperationsBaseTest {
} }
//This should return two (axa, axx) //This should return two (axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/a??"), FileStatus[] filteredPaths = fc.util().globStatus(
TEST_X_FILTER); getTestRootPath(fc, "test/hadoop/a??"),
Assert.assertEquals(2,filteredPaths.length); TEST_X_FILTER);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths)); Assert.assertEquals(2, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA),
filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX),
filteredPaths));
} }
@Test @Test
@ -572,31 +588,33 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testWriteReadAndDeleteHalfABlock() throws Exception { public void testWriteReadAndDeleteHalfABlock() throws Exception {
writeReadAndDelete(getBlockSize() / 2); writeReadAndDelete(getDefaultBlockSize() / 2);
} }
@Test @Test
public void testWriteReadAndDeleteOneBlock() throws Exception { public void testWriteReadAndDeleteOneBlock() throws Exception {
writeReadAndDelete(getBlockSize()); writeReadAndDelete(getDefaultBlockSize());
} }
@Test @Test
public void testWriteReadAndDeleteOneAndAHalfBlocks() throws Exception { public void testWriteReadAndDeleteOneAndAHalfBlocks() throws Exception {
writeReadAndDelete(getBlockSize() + (getBlockSize() / 2)); int blockSize = getDefaultBlockSize();
writeReadAndDelete(blockSize + (blockSize / 2));
} }
@Test @Test
public void testWriteReadAndDeleteTwoBlocks() throws Exception { public void testWriteReadAndDeleteTwoBlocks() throws Exception {
writeReadAndDelete(getBlockSize() * 2); writeReadAndDelete(getDefaultBlockSize() * 2);
} }
private void writeReadAndDelete(int len) throws IOException { private void writeReadAndDelete(int len) throws IOException {
Path path = getTestRootPath("test/hadoop/file"); Path path = getTestRootPath(fc, "test/hadoop/file");
fc.mkdir(path.getParent(), FsPermission.getDefault(), true); fc.mkdir(path.getParent(), FsPermission.getDefault(), true);
FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE), FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE),
CreateOpts.repFac((short) 1), CreateOpts.blockSize(getBlockSize())); CreateOpts.repFac((short) 1), CreateOpts
.blockSize(getDefaultBlockSize()));
out.write(data, 0, len); out.write(data, 0, len);
out.close(); out.close();
@ -621,7 +639,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testOverwrite() throws IOException { public void testOverwrite() throws IOException {
Path path = getTestRootPath("test/hadoop/file"); Path path = getTestRootPath(fc, "test/hadoop/file");
fc.mkdir(path.getParent(), FsPermission.getDefault(), true); fc.mkdir(path.getParent(), FsPermission.getDefault(), true);
@ -648,7 +666,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testWriteInNonExistentDirectory() throws IOException { public void testWriteInNonExistentDirectory() throws IOException {
Path path = getTestRootPath("test/hadoop/file"); Path path = getTestRootPath(fc, "test/hadoop/file");
Assert.assertFalse("Parent doesn't exist", fc.exists(path.getParent())); Assert.assertFalse("Parent doesn't exist", fc.exists(path.getParent()));
createFile(path); createFile(path);
@ -659,16 +677,16 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testDeleteNonExistentFile() throws IOException { public void testDeleteNonExistentFile() throws IOException {
Path path = getTestRootPath("test/hadoop/file"); Path path = getTestRootPath(fc, "test/hadoop/file");
Assert.assertFalse("Doesn't exist", fc.exists(path)); Assert.assertFalse("Doesn't exist", fc.exists(path));
Assert.assertFalse("No deletion", fc.delete(path, true)); Assert.assertFalse("No deletion", fc.delete(path, true));
} }
@Test @Test
public void testDeleteRecursively() throws IOException { public void testDeleteRecursively() throws IOException {
Path dir = getTestRootPath("test/hadoop"); Path dir = getTestRootPath(fc, "test/hadoop");
Path file = getTestRootPath("test/hadoop/file"); Path file = getTestRootPath(fc, "test/hadoop/file");
Path subdir = getTestRootPath("test/hadoop/subdir"); Path subdir = getTestRootPath(fc, "test/hadoop/subdir");
createFile(file); createFile(file);
fc.mkdir(subdir,FsPermission.getDefault(), true); fc.mkdir(subdir,FsPermission.getDefault(), true);
@ -695,7 +713,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testDeleteEmptyDirectory() throws IOException { public void testDeleteEmptyDirectory() throws IOException {
Path dir = getTestRootPath("test/hadoop"); Path dir = getTestRootPath(fc, "test/hadoop");
fc.mkdir(dir, FsPermission.getDefault(), true); fc.mkdir(dir, FsPermission.getDefault(), true);
Assert.assertTrue("Dir exists", fc.exists(dir)); Assert.assertTrue("Dir exists", fc.exists(dir));
Assert.assertTrue("Deleted", fc.delete(dir, false)); Assert.assertTrue("Deleted", fc.delete(dir, false));
@ -705,8 +723,8 @@ public abstract class FileContextMainOperationsBaseTest {
@Test @Test
public void testRenameNonExistentPath() throws Exception { public void testRenameNonExistentPath() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/nonExistent"); Path src = getTestRootPath(fc, "test/hadoop/nonExistent");
Path dst = getTestRootPath("test/new/newpath"); Path dst = getTestRootPath(fc, "test/new/newpath");
try { try {
rename(src, dst, false, false, false, Rename.NONE); rename(src, dst, false, false, false, Rename.NONE);
Assert.fail("Should throw FileNotFoundException"); Assert.fail("Should throw FileNotFoundException");
@ -726,9 +744,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileToNonExistentDirectory() throws Exception { public void testRenameFileToNonExistentDirectory() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file"); Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src); createFile(src);
Path dst = getTestRootPath("test/nonExistent/newfile"); Path dst = getTestRootPath(fc, "test/nonExistent/newfile");
try { try {
rename(src, dst, false, true, false, Rename.NONE); rename(src, dst, false, true, false, Rename.NONE);
@ -749,9 +767,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileToDestinationWithParentFile() throws Exception { public void testRenameFileToDestinationWithParentFile() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file"); Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src); createFile(src);
Path dst = getTestRootPath("test/parentFile/newfile"); Path dst = getTestRootPath(fc, "test/parentFile/newfile");
createFile(dst.getParent()); createFile(dst.getParent());
try { try {
@ -771,9 +789,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileToExistingParent() throws Exception { public void testRenameFileToExistingParent() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file"); Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src); createFile(src);
Path dst = getTestRootPath("test/new/newfile"); Path dst = getTestRootPath(fc, "test/new/newfile");
fc.mkdir(dst.getParent(), FileContext.DEFAULT_PERM, true); fc.mkdir(dst.getParent(), FileContext.DEFAULT_PERM, true);
rename(src, dst, true, false, true, Rename.OVERWRITE); rename(src, dst, true, false, true, Rename.OVERWRITE);
} }
@ -782,9 +800,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileAsExistingFile() throws Exception { public void testRenameFileAsExistingFile() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file"); Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src); createFile(src);
Path dst = getTestRootPath("test/new/existingFile"); Path dst = getTestRootPath(fc, "test/new/existingFile");
createFile(dst); createFile(dst);
// Fails without overwrite option // Fails without overwrite option
@ -803,9 +821,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileAsExistingDirectory() throws Exception { public void testRenameFileAsExistingDirectory() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file"); Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src); createFile(src);
Path dst = getTestRootPath("test/new/existingDir"); Path dst = getTestRootPath(fc, "test/new/existingDir");
fc.mkdir(dst, FileContext.DEFAULT_PERM, true); fc.mkdir(dst, FileContext.DEFAULT_PERM, true);
// Fails without overwrite option // Fails without overwrite option
@ -827,9 +845,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameDirectoryToNonExistentParent() throws Exception { public void testRenameDirectoryToNonExistentParent() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir"); Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true); fc.mkdir(src, FileContext.DEFAULT_PERM, true);
Path dst = getTestRootPath("test/nonExistent/newdir"); Path dst = getTestRootPath(fc, "test/nonExistent/newdir");
try { try {
rename(src, dst, false, true, false, Rename.NONE); rename(src, dst, false, true, false, Rename.NONE);
@ -856,37 +874,37 @@ public abstract class FileContextMainOperationsBaseTest {
private void testRenameDirectoryAsNonExistentDirectory(Rename... options) throws Exception { private void testRenameDirectoryAsNonExistentDirectory(Rename... options) throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir"); Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true); fc.mkdir(src, FileContext.DEFAULT_PERM, true);
createFile(getTestRootPath("test/hadoop/dir/file1")); createFile(getTestRootPath(fc, "test/hadoop/dir/file1"));
createFile(getTestRootPath("test/hadoop/dir/subdir/file2")); createFile(getTestRootPath(fc, "test/hadoop/dir/subdir/file2"));
Path dst = getTestRootPath("test/new/newdir"); Path dst = getTestRootPath(fc, "test/new/newdir");
fc.mkdir(dst.getParent(), FileContext.DEFAULT_PERM, true); fc.mkdir(dst.getParent(), FileContext.DEFAULT_PERM, true);
rename(src, dst, true, false, true, options); rename(src, dst, true, false, true, options);
Assert.assertFalse("Nested file1 exists", Assert.assertFalse("Nested file1 exists",
fc.exists(getTestRootPath("test/hadoop/dir/file1"))); fc.exists(getTestRootPath(fc, "test/hadoop/dir/file1")));
Assert.assertFalse("Nested file2 exists", Assert.assertFalse("Nested file2 exists",
fc.exists(getTestRootPath("test/hadoop/dir/subdir/file2"))); fc.exists(getTestRootPath(fc, "test/hadoop/dir/subdir/file2")));
Assert.assertTrue("Renamed nested file1 exists", Assert.assertTrue("Renamed nested file1 exists", fc
fc.exists(getTestRootPath("test/new/newdir/file1"))); .exists(getTestRootPath(fc, "test/new/newdir/file1")));
Assert.assertTrue("Renamed nested exists", Assert.assertTrue("Renamed nested exists",
fc.exists(getTestRootPath("test/new/newdir/subdir/file2"))); fc.exists(getTestRootPath(fc, "test/new/newdir/subdir/file2")));
} }
@Test @Test
public void testRenameDirectoryAsNonEmptyDirectory() throws Exception { public void testRenameDirectoryAsNonEmptyDirectory() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir"); Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true); fc.mkdir(src, FileContext.DEFAULT_PERM, true);
createFile(getTestRootPath("test/hadoop/dir/file1")); createFile(getTestRootPath(fc, "test/hadoop/dir/file1"));
createFile(getTestRootPath("test/hadoop/dir/subdir/file2")); createFile(getTestRootPath(fc, "test/hadoop/dir/subdir/file2"));
Path dst = getTestRootPath("test/new/newdir"); Path dst = getTestRootPath(fc, "test/new/newdir");
fc.mkdir(dst, FileContext.DEFAULT_PERM, true); fc.mkdir(dst, FileContext.DEFAULT_PERM, true);
createFile(getTestRootPath("test/new/newdir/file1")); createFile(getTestRootPath(fc, "test/new/newdir/file1"));
// Fails without overwrite option // Fails without overwrite option
try { try {
rename(src, dst, false, true, false, Rename.NONE); rename(src, dst, false, true, false, Rename.NONE);
@ -907,9 +925,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameDirectoryAsFile() throws Exception { public void testRenameDirectoryAsFile() throws Exception {
if (!renameSupported()) return; if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir"); Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true); fc.mkdir(src, FileContext.DEFAULT_PERM, true);
Path dst = getTestRootPath("test/new/newfile"); Path dst = getTestRootPath(fc, "test/new/newfile");
createFile(dst); createFile(dst);
// Fails without overwrite option // Fails without overwrite option
try { try {
@ -929,7 +947,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testInputStreamClosedTwice() throws IOException { public void testInputStreamClosedTwice() throws IOException {
//HADOOP-4760 according to Closeable#close() closing already-closed //HADOOP-4760 according to Closeable#close() closing already-closed
//streams should have no effect. //streams should have no effect.
Path src = getTestRootPath("test/hadoop/file"); Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src); createFile(src);
FSDataInputStream in = fc.open(src); FSDataInputStream in = fc.open(src);
in.close(); in.close();
@ -940,7 +958,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testOutputStreamClosedTwice() throws IOException { public void testOutputStreamClosedTwice() throws IOException {
//HADOOP-4760 according to Closeable#close() closing already-closed //HADOOP-4760 according to Closeable#close() closing already-closed
//streams should have no effect. //streams should have no effect.
Path src = getTestRootPath("test/hadoop/file"); Path src = getTestRootPath(fc, "test/hadoop/file");
FSDataOutputStream out = fc.create(src, EnumSet.of(CreateFlag.CREATE), FSDataOutputStream out = fc.create(src, EnumSet.of(CreateFlag.CREATE),
Options.CreateOpts.createParent()); Options.CreateOpts.createParent());
@ -955,9 +973,10 @@ public abstract class FileContextMainOperationsBaseTest {
out.write(data, 0, data.length); out.write(data, 0, data.length);
out.close(); out.close();
} }
private void rename(Path src, Path dst, boolean renameShouldSucceed, private void rename(Path src, Path dst, boolean renameShouldSucceed,
boolean srcExists, boolean dstExists, Rename... options) throws IOException { boolean srcExists, boolean dstExists, Rename... options)
throws IOException {
fc.rename(src, dst, options); fc.rename(src, dst, options);
if (!renameShouldSucceed) if (!renameShouldSucceed)
Assert.fail("rename should have thrown exception"); Assert.fail("rename should have thrown exception");
@ -967,7 +986,8 @@ public abstract class FileContextMainOperationsBaseTest {
private boolean containsPath(Path path, FileStatus[] filteredPaths) private boolean containsPath(Path path, FileStatus[] filteredPaths)
throws IOException { throws IOException {
for(int i = 0; i < filteredPaths.length; i ++) { for(int i = 0; i < filteredPaths.length; i ++) {
if(getTestRootPath(path.toString()).equals(filteredPaths[i].getPath())) if (getTestRootPath(fc, path.toString()).equals(
filteredPaths[i].getPath()))
return true; return true;
} }
return false; return false;

View File

@ -19,7 +19,6 @@ package org.apache.hadoop.fs;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -32,6 +31,8 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/** /**
* <p> * <p>
* A collection of permission tests for the {@link FileContext}. * A collection of permission tests for the {@link FileContext}.
@ -51,24 +52,8 @@ import org.junit.Test;
* @AfterClass public static void ClusterShutdownAtEnd() * @AfterClass public static void ClusterShutdownAtEnd()
* </p> * </p>
*/ */
public class FileContextPermissionBase { public abstract class FileContextPermissionBase {
static final String TEST_ROOT_DIR = new Path(System.getProperty(
"test.build.data", "/tmp")).toString().replace(' ', '_')
+ "/" + TestLocalFileSystemPermission.class.getSimpleName() + "_";
protected Path getTestRootRelativePath(String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
private Path rootPath = null;
protected Path getTestRootPath() {
if (rootPath == null) {
rootPath = fc.makeQualified(new Path(TEST_ROOT_DIR));
}
return rootPath;
}
{ {
try { try {
((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger() ((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger()
@ -84,23 +69,14 @@ public class FileContextPermissionBase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
fc.mkdir(getTestRootPath(), FileContext.DEFAULT_PERM, true); fc.mkdir(getTestRootPath(fc), FileContext.DEFAULT_PERM, true);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
fc.delete(getTestRootPath(), true); fc.delete(getTestRootPath(fc), true);
} }
private Path writeFile(FileContext theFc, String name) throws IOException {
Path f = getTestRootRelativePath(name);
FSDataOutputStream stm = theFc.create(f, EnumSet.of(CreateFlag.CREATE));
stm.writeBytes("42\n");
stm.close();
return f;
}
private void cleanupFile(FileContext theFc, Path name) throws IOException { private void cleanupFile(FileContext theFc, Path name) throws IOException {
Assert.assertTrue(theFc.exists(name)); Assert.assertTrue(theFc.exists(name));
theFc.delete(name, true); theFc.delete(name, true);
@ -114,7 +90,7 @@ public class FileContextPermissionBase {
return; return;
} }
String filename = "foo"; String filename = "foo";
Path f = writeFile(fc, filename); Path f = createFile(fc, filename);
doFilePermissionCheck(FileContext.DEFAULT_PERM.applyUMask(fc.getUMask()), doFilePermissionCheck(FileContext.DEFAULT_PERM.applyUMask(fc.getUMask()),
fc.getFileStatus(f).getPermission()); fc.getFileStatus(f).getPermission());
} }
@ -128,7 +104,7 @@ public class FileContextPermissionBase {
} }
String filename = "foo"; String filename = "foo";
Path f = writeFile(fc, filename); Path f = createFile(fc, filename);
try { try {
// create files and manipulate them. // create files and manipulate them.
@ -152,7 +128,7 @@ public class FileContextPermissionBase {
} }
String filename = "bar"; String filename = "bar";
Path f = writeFile(fc, filename); Path f = createFile(fc, filename);
List<String> groups = null; List<String> groups = null;
try { try {
groups = getGroups(); groups = getGroups();

View File

@ -0,0 +1,119 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs;
import java.io.IOException;
import java.util.EnumSet;
import org.apache.hadoop.fs.Options.CreateOpts;
import org.apache.hadoop.fs.Options.CreateOpts.BlockSize;
/**
* Helper class for unit tests.
*/
public final class FileContextTestHelper {
private static final String TEST_ROOT_DIR = System.getProperty("test.build.data",
"build/test/data") + "/test";
private static final int DEFAULT_BLOCK_SIZE = 1024;
private static final int DEFAULT_NUM_BLOCKS = 2;
private static String absTestRootDir = null;
/** Hidden constructor */
private FileContextTestHelper() {}
public static int getDefaultBlockSize() {
return DEFAULT_BLOCK_SIZE;
}
public static byte[] getFileData(int numOfBlocks, long blockSize) {
byte[] data = new byte[(int) (numOfBlocks * blockSize)];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
return data;
}
public static Path getTestRootPath(FileContext fc) {
return fc.makeQualified(new Path(TEST_ROOT_DIR));
}
public static Path getTestRootPath(FileContext fc, String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
public static String getAbsoluteTestRootDir(FileContext fc)
throws IOException {
if (absTestRootDir == null) {
if (TEST_ROOT_DIR.startsWith("/")) {
absTestRootDir = TEST_ROOT_DIR;
} else {
absTestRootDir = getDefaultWorkingDirectory(fc).toString() + "/"
+ TEST_ROOT_DIR;
}
}
return absTestRootDir;
}
public static Path getTestRootDir(FileContext fc) throws IOException {
return fc.makeQualified(new Path(getAbsoluteTestRootDir(fc)));
}
public static Path getDefaultWorkingDirectory(FileContext fc)
throws IOException {
return getTestRootPath(fc, "/user/" + System.getProperty("user.name"))
.makeQualified(fc.getDefaultFileSystem().getUri(),
fc.getWorkingDirectory());
}
/*
* Create files with numBlocks blocks each with block size blockSize.
*/
public static void createFile(FileContext fc, Path path, int numBlocks,
CreateOpts... options) throws IOException {
BlockSize blockSizeOpt =
(BlockSize) CreateOpts.getOpt(CreateOpts.BlockSize.class, options);
long blockSize = blockSizeOpt != null ? blockSizeOpt.getValue()
: DEFAULT_BLOCK_SIZE;
FSDataOutputStream out =
fc.create(path, EnumSet.of(CreateFlag.CREATE), options);
byte[] data = getFileData(numBlocks, blockSize);
out.write(data, 0, data.length);
out.close();
}
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 void createFile(FileContext fc, Path path) throws IOException {
createFile(fc, path, DEFAULT_NUM_BLOCKS, CreateOpts.createParent());
}
public static Path createFile(FileContext fc, String name) throws IOException {
Path path = getTestRootPath(fc, name);
createFile(fc, path);
return path;
}
public static void createFileNonRecursive(FileContext fc, Path path)
throws IOException {
createFile(fc, path, DEFAULT_NUM_BLOCKS, CreateOpts.donotCreateParent());
}
}

View File

@ -20,14 +20,16 @@ package org.apache.hadoop.fs;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet;
import junit.framework.Assert; import junit.framework.Assert;
import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/** /**
* <p> * <p>
* A collection of tests for the {@link FileContext} to test path names passed * A collection of tests for the {@link FileContext} to test path names passed
@ -49,32 +51,18 @@ import org.junit.Test;
*/ */
public abstract class FileContextURIBase { public abstract class FileContextURIBase {
private static final String basePath = System.getProperty("test.build.data", private static final String basePath = System.getProperty("test.build.data",
"build/test/data") + "/testContextURI"; "build/test/data") + "/testContextURI";
private static final Path BASE = new Path(basePath); private static final Path BASE = new Path(basePath);
protected FileContext fc1; protected FileContext fc1;
protected FileContext fc2; protected FileContext fc2;
private static int BLOCK_SIZE = 1024;
private static byte[] data = new byte[BLOCK_SIZE * 2]; // two blocks of data
{
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
//Helper method to make path qualified //Helper method to make path qualified
protected Path qualifiedPath(String path, FileContext fc) { protected Path qualifiedPath(String path, FileContext fc) {
return fc.makeQualified(new Path(BASE, path)); return fc.makeQualified(new Path(BASE, path));
} }
// Helper method to create file and write data to file @Before
protected void createFile(Path path, FileContext fc) throws IOException { public void setUp() throws Exception { }
FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE),
Options.CreateOpts.createParent());
out.write(data, 0, data.length);
out.close();
}
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
@ -100,7 +88,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath)); Assert.assertFalse(fc2.exists(testPath));
// Now create file // Now create file
createFile(testPath, fc1); createFile(fc1, testPath);
// Ensure fc2 has the created file // Ensure fc2 has the created file
Assert.assertTrue(fc2.exists(testPath)); Assert.assertTrue(fc2.exists(testPath));
} }
@ -117,7 +105,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath)); Assert.assertFalse(fc2.exists(testPath));
// Create a file on fc2's file system using fc1 // Create a file on fc2's file system using fc1
createFile(testPath, fc1); createFile(fc1, testPath);
Assert.fail("Create file with null name should throw IllegalArgumentException."); Assert.fail("Create file with null name should throw IllegalArgumentException.");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
@ -134,11 +122,11 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath)); Assert.assertFalse(fc2.exists(testPath));
// Create a file on fc2's file system using fc1 // Create a file on fc2's file system using fc1
createFile(testPath, fc1); createFile(fc1, testPath);
// Create same file with fc1 // Create same file with fc1
try { try {
createFile(testPath, fc2); createFile(fc2, testPath);
Assert.fail("Create existing file should throw an IOException."); Assert.fail("Create existing file should throw an IOException.");
} catch (IOException e) { } catch (IOException e) {
// expected // expected
@ -158,7 +146,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath)); Assert.assertFalse(fc2.exists(testPath));
// Create a file on fc2's file system using fc1 // Create a file on fc2's file system using fc1
createFile(testPath, fc1); createFile(fc1, testPath);
// Ensure using fc2 that file is created // Ensure using fc2 that file is created
Assert.assertTrue(fc2.isDirectory(testPath.getParent())); Assert.assertTrue(fc2.isDirectory(testPath.getParent()));
@ -240,7 +228,7 @@ public abstract class FileContextURIBase {
Assert.assertTrue(fc2.exists(testDir)); Assert.assertTrue(fc2.exists(testDir));
// Create file on fc1 using fc2 context // Create file on fc1 using fc2 context
createFile(qualifiedPath("test/hadoop/file", fc2), fc1); createFile(fc1, qualifiedPath("test/hadoop/file", fc2));
Path testSubDir = qualifiedPath("test/hadoop/file/subdir", fc2); Path testSubDir = qualifiedPath("test/hadoop/file/subdir", fc2);
try { try {
@ -292,7 +280,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath)); Assert.assertFalse(fc2.exists(testPath));
// First create a file on file system using fc1 // First create a file on file system using fc1
createFile(testPath, fc1); createFile(fc1, testPath);
// Ensure file exist // Ensure file exist
Assert.assertTrue(fc2.exists(testPath)); Assert.assertTrue(fc2.exists(testPath));
@ -319,7 +307,7 @@ public abstract class FileContextURIBase {
// TestCase2 : Create , Delete , Delete file // TestCase2 : Create , Delete , Delete file
// Create a file on fc2's file system using fc1 // Create a file on fc2's file system using fc1
createFile(testPath, fc1); createFile(fc1, testPath);
// Ensure file exist // Ensure file exist
Assert.assertTrue(fc2.exists(testPath)); Assert.assertTrue(fc2.exists(testPath));
@ -346,7 +334,7 @@ public abstract class FileContextURIBase {
// TestCase2 : Create , Delete , Delete file // TestCase2 : Create , Delete , Delete file
// Create a file on fc2's file system using fc1 // Create a file on fc2's file system using fc1
createFile(testPath, fc1); createFile(fc1, testPath);
// Ensure file exist // Ensure file exist
Assert.assertTrue(fc2.exists(testPath)); Assert.assertTrue(fc2.exists(testPath));
@ -440,7 +428,7 @@ public abstract class FileContextURIBase {
Path testPath = qualifiedPath(testFile, fc2); Path testPath = qualifiedPath(testFile, fc2);
// Create a file on fc2's file system using fc1 // Create a file on fc2's file system using fc1
createFile(testPath, fc1); createFile(fc1, testPath);
// Get modification time using fc2 and fc1 // Get modification time using fc2 and fc1
fc1ModificationTime = fc1.getFileStatus(testPath).getModificationTime(); fc1ModificationTime = fc1.getFileStatus(testPath).getModificationTime();
fc2ModificationTime = fc2.getFileStatus(testPath).getModificationTime(); fc2ModificationTime = fc2.getFileStatus(testPath).getModificationTime();
@ -454,7 +442,7 @@ public abstract class FileContextURIBase {
Path path2 = fc2.makeQualified(new Path(BASE, fileName)); Path path2 = fc2.makeQualified(new Path(BASE, fileName));
// Create a file on fc2's file system using fc1 // Create a file on fc2's file system using fc1
createFile(path2, fc1); createFile(fc1, path2);
FsStatus fc2Status = fc2.getFsStatus(path2); FsStatus fc2Status = fc2.getFsStatus(path2);
// FsStatus , used, free and capacity are non-negative longs // FsStatus , used, free and capacity are non-negative longs

View File

@ -18,29 +18,21 @@
package org.apache.hadoop.fs; package org.apache.hadoop.fs;
import java.io.IOException; import java.io.IOException;
import java.util.EnumSet;
import java.util.Set; import java.util.Set;
import junit.framework.Assert; import junit.framework.Assert;
import org.apache.hadoop.fs.Options.CreateOpts;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/** /**
* Tests {@link FileContext.#deleteOnExit(Path)} functionality. * Tests {@link FileContext.#deleteOnExit(Path)} functionality.
*/ */
public class TestFileContextDeleteOnExit { public class TestFileContextDeleteOnExit {
private static String TEST_ROOT_DIR = private static int blockSize = 1024;
System.getProperty("test.build.data", "/tmp") + "/test"; private static int numBlocks = 2;
private static byte[] data = new byte[1024 * 2]; // two blocks of data
{
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
private FileContext fc; private FileContext fc;
@ -51,23 +43,9 @@ public class TestFileContextDeleteOnExit {
@After @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
fc.delete(getTestRootPath(), true); fc.delete(getTestRootPath(fc), true);
} }
private Path getTestRootPath() {
return fc.makeQualified(new Path(TEST_ROOT_DIR));
}
private Path getTestPath(String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
private void createFile(FileContext fc, Path path) throws IOException {
FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE),
CreateOpts.createParent());
out.write(data, 0, data.length);
out.close();
}
private void checkDeleteOnExitData(int size, FileContext fc, Path... paths) { private void checkDeleteOnExitData(int size, FileContext fc, Path... paths) {
Assert.assertEquals(size, FileContext.DELETE_ON_EXIT.size()); Assert.assertEquals(size, FileContext.DELETE_ON_EXIT.size());
@ -81,21 +59,21 @@ public class TestFileContextDeleteOnExit {
@Test @Test
public void testDeleteOnExit() throws Exception { public void testDeleteOnExit() throws Exception {
// Create deleteOnExit entries // Create deleteOnExit entries
Path file1 = getTestPath("file1"); Path file1 = getTestRootPath(fc, "file1");
createFile(fc, file1); createFile(fc, file1, numBlocks, blockSize);
fc.deleteOnExit(file1); fc.deleteOnExit(file1);
checkDeleteOnExitData(1, fc, file1); checkDeleteOnExitData(1, fc, file1);
// Ensure shutdown hook is added // Ensure shutdown hook is added
Assert.assertTrue(Runtime.getRuntime().removeShutdownHook(FileContext.FINALIZER)); Assert.assertTrue(Runtime.getRuntime().removeShutdownHook(FileContext.FINALIZER));
Path file2 = getTestPath("dir1/file2"); Path file2 = getTestRootPath(fc, "dir1/file2");
createFile(fc, file2); createFile(fc, file2, numBlocks, blockSize);
fc.deleteOnExit(file2); fc.deleteOnExit(file2);
checkDeleteOnExitData(1, fc, file1, file2); checkDeleteOnExitData(1, fc, file1, file2);
Path dir = getTestPath("dir3/dir4/dir5/dir6"); Path dir = getTestRootPath(fc, "dir3/dir4/dir5/dir6");
createFile(fc, dir); createFile(fc, dir, numBlocks, blockSize);
fc.deleteOnExit(dir); fc.deleteOnExit(dir);
checkDeleteOnExitData(1, fc, file1, file2, dir); checkDeleteOnExitData(1, fc, file1, file2, dir);