// Prepare directory tree for pagination tests
  private LargeDirectoryConfig prepareLargeDirectoryTest() throws IOException {
    final String filePrefix = "a_";
    final String folderPrefix = "b_";

    String topLevelDirectory = PathUtils.concatPath(mUnderfsAddress, "topLevelDir");

    final int numFiles = 100;

    String[] children = new String[numFiles + numFiles];

    // Make top level directory
    mUfs.mkdirs(topLevelDirectory, false);

    // Make the children files
    for (int i = 0; i < numFiles; ++i) {
      children[i] = PathUtils.concatPath(topLevelDirectory, filePrefix + String.format("%04d", i));
      createEmptyFile(children[i]);
    }
    // Make the children folders
    for (int i = 0; i < numFiles; ++i) {
      children[numFiles + i] =
          PathUtils.concatPath(topLevelDirectory, folderPrefix + String.format("%04d", i));
      mUfs.mkdirs(children[numFiles + i], false);
    }

    return new LargeDirectoryConfig(topLevelDirectory, children);
  }
 /** Tests if list correctly returns file names. */
 @Test
 public void list() throws IOException {
   String testDirNonEmpty = PathUtils.concatPath(mUnderfsAddress, "testDirNonEmpty1");
   String testDirNonEmptyChildDir = PathUtils.concatPath(testDirNonEmpty, "testDirNonEmpty2");
   String testDirNonEmptyChildFile = PathUtils.concatPath(testDirNonEmpty, "testDirNonEmptyF");
   String testDirNonEmptyChildDirFile =
       PathUtils.concatPath(testDirNonEmptyChildDir, "testDirNonEmptyChildDirF");
   mUfs.mkdirs(testDirNonEmpty, false);
   mUfs.mkdirs(testDirNonEmptyChildDir, false);
   createEmptyFile(testDirNonEmptyChildFile);
   createEmptyFile(testDirNonEmptyChildDirFile);
   String[] expectedResTopDir = new String[] {"testDirNonEmpty2", "testDirNonEmptyF"};
   // Some file systems may prefix with a slash
   String[] expectedResTopDir2 = new String[] {"/testDirNonEmpty2", "/testDirNonEmptyF"};
   Arrays.sort(expectedResTopDir);
   Arrays.sort(expectedResTopDir2);
   String[] resTopDir = mUfs.list(testDirNonEmpty);
   Arrays.sort(resTopDir);
   Assert.assertTrue(
       Arrays.equals(expectedResTopDir, resTopDir)
           || Arrays.equals(expectedResTopDir2, resTopDir));
   Assert.assertTrue(
       mUfs.list(testDirNonEmptyChildDir)[0].equals("testDirNonEmptyChildDirF")
           || mUfs.list(testDirNonEmptyChildDir)[0].equals("/testDirNonEmptyChildDirF"));
 }
 /**
  * Tests an empty directory can be deleted. Tests a non empty directory will not be deleted if
  * recursive is not specified. Tests a non empty directory will be deleted if recursive is
  * specified.
  */
 @Test
 public void deleteDir() throws IOException {
   String testDirEmpty = PathUtils.concatPath(mUnderfsAddress, "testDirEmpty");
   String testDirNonEmpty = PathUtils.concatPath(mUnderfsAddress, "testDirNonEmpty1");
   String testDirNonEmptyChildDir = PathUtils.concatPath(testDirNonEmpty, "testDirNonEmpty2");
   String testDirNonEmptyChildFile = PathUtils.concatPath(testDirNonEmpty, "testDirNonEmptyF");
   String testDirNonEmptyChildDirFile =
       PathUtils.concatPath(testDirNonEmptyChildDir, "testDirNonEmptyChildDirF");
   mUfs.mkdirs(testDirEmpty, false);
   mUfs.mkdirs(testDirNonEmpty, false);
   mUfs.mkdirs(testDirNonEmptyChildDir, false);
   createEmptyFile(testDirNonEmptyChildFile);
   createEmptyFile(testDirNonEmptyChildDirFile);
   mUfs.delete(testDirEmpty, false);
   Assert.assertFalse(mUfs.exists(testDirEmpty));
   try {
     mUfs.delete(testDirNonEmpty, false);
   } catch (IOException e) {
     // Some File systems may throw IOException
   }
   Assert.assertTrue(mUfs.exists(testDirNonEmpty));
   mUfs.delete(testDirNonEmpty, true);
   Assert.assertFalse(mUfs.exists(testDirNonEmpty));
   Assert.assertFalse(mUfs.exists(testDirNonEmptyChildDir));
   Assert.assertFalse(mUfs.exists(testDirNonEmptyChildFile));
   Assert.assertFalse(mUfs.exists(testDirNonEmptyChildDirFile));
 }
 // Creates a block file and write an increasing byte array into it
 private void createBlockFile(String filename, int len) throws IOException, InvalidPathException {
   UnderFileSystem ufs = UnderFileSystem.get(filename, mMasterConfiguration);
   ufs.mkdirs(PathUtils.getParent(filename), true);
   OutputStream out = ufs.create(filename);
   out.write(BufferUtils.getIncreasingByteArray(len), 0, len);
   out.close();
 }
  /** Tests if list recursive correctly returns all file names in all subdirectories. */
  @Test
  public void listRecursive() throws IOException {
    String root = mUnderfsAddress;
    // TODO(andrew): Should this directory be created in LocalAlluxioCluster creation code?
    mUfs.mkdirs(root, true);
    // Empty lsr should be empty
    Assert.assertEquals(0, mUfs.listRecursive(root).length);

    // Create a tree of subdirectories and files
    String sub1 = PathUtils.concatPath(root, "sub1");
    String sub2 = PathUtils.concatPath(root, "sub2");
    String sub11 = PathUtils.concatPath(sub1, "sub11");
    String file11 = PathUtils.concatPath(sub11, "file11");
    String file2 = PathUtils.concatPath(sub2, "file2");
    String file = PathUtils.concatPath(root, "file");
    // lsr of nonexistent path should be null
    Assert.assertNull(mUfs.listRecursive(sub1));

    mUfs.mkdirs(sub1, false);
    mUfs.mkdirs(sub2, false);
    mUfs.mkdirs(sub11, false);
    createEmptyFile(file11);
    createEmptyFile(file2);
    createEmptyFile(file);

    // lsr from root should return paths relative to the root
    String[] expectedResRoot = {
      "sub1", "sub2", "sub1/sub11", "sub1/sub11/file11", "sub2/file2", "file"
    };
    String[] actualResRoot = mUfs.listRecursive(root);
    Arrays.sort(expectedResRoot);
    Arrays.sort(actualResRoot);
    Assert.assertArrayEquals(expectedResRoot, actualResRoot);

    // lsr from sub1 should return paths relative to sub1
    String[] expectedResSub1 = {"sub11", "sub11/file11"};
    String[] actualResSub1 = mUfs.listRecursive(sub1);
    Arrays.sort(expectedResSub1);
    Arrays.sort(actualResSub1);
    Assert.assertArrayEquals(expectedResSub1, actualResSub1);

    // lsr of file should be null
    Assert.assertNull(mUfs.listRecursive(file));
  }
 /**
  * Tests if {@link UnderFileSystem#isFile(String)} correctly returns true for files and false
  * otherwise.
  */
 @Test
 public void isFile() throws IOException {
   String testFile = PathUtils.concatPath(mUnderfsAddress, "testFile");
   String testDir = PathUtils.concatPath(mUnderfsAddress, "testDir");
   Assert.assertFalse(mUfs.isFile(testFile));
   createEmptyFile(testFile);
   mUfs.mkdirs(testDir, false);
   Assert.assertTrue(mUfs.isFile(testFile));
   Assert.assertFalse(mUfs.isFile(testDir));
 }
  /**
   * Tests {@link UnderFileSystem#mkdirs(String, boolean)} correctly creates a directory. Tests
   * {@link UnderFileSystem#mkdirs(String, boolean)} correctly makes parent directories if
   * createParent is specified.
   */
  @Test
  public void mkdirs() throws IOException {
    // make sure the underfs address dir exists already
    mUfs.mkdirs(mUnderfsAddress, true);
    // empty lsr should be empty
    Assert.assertEquals(0, mUfs.listRecursive(mUnderfsAddress).length);

    String testDirTop = PathUtils.concatPath(mUnderfsAddress, "testDirTop");
    String testDir1 = PathUtils.concatPath(mUnderfsAddress, "1");
    String testDir2 = PathUtils.concatPath(testDir1, "2");
    String testDir3 = PathUtils.concatPath(testDir2, "3");
    String testDirDeep = PathUtils.concatPath(testDir3, "testDirDeep");
    mUfs.mkdirs(testDirTop, false);
    Assert.assertTrue(mUfs.exists(testDirTop));
    mUfs.mkdirs(testDirDeep, true);
    Assert.assertTrue(mUfs.exists(testDir1));
    Assert.assertTrue(mUfs.exists(testDir2));
    Assert.assertTrue(mUfs.exists(testDir3));
    Assert.assertTrue(mUfs.exists(testDirDeep));
  }
 /** Tests {@link UnderFileSystem#rename(String, String)} works file to a folder if supported. */
 @Test
 public void renameFileToFolder() throws IOException {
   String testFileSrc = PathUtils.concatPath(mUnderfsAddress, "testFileSrc");
   String testFileDst = PathUtils.concatPath(mUnderfsAddress, "testDirDst");
   String testFileFinalDst = PathUtils.concatPath(testFileDst, "testFileSrc");
   createEmptyFile(testFileSrc);
   mUfs.mkdirs(testFileDst, false);
   if (mUfs.rename(testFileSrc, testFileDst)) {
     Assert.assertFalse(mUfs.exists(testFileSrc));
     Assert.assertTrue(mUfs.exists(testFileFinalDst));
   }
 }
  /** Tests load metadata on list. */
  @Test
  public void loadMetadata() throws Exception {
    String dirName = "loadMetaDataRoot";

    String rootDir = PathUtils.concatPath(mUnderfsAddress, dirName);
    mUfs.mkdirs(rootDir, true);

    String rootFile1 = PathUtils.concatPath(rootDir, "file1");
    createEmptyFile(rootFile1);

    String rootFile2 = PathUtils.concatPath(rootDir, "file2");
    createEmptyFile(rootFile2);

    AlluxioURI rootAlluxioURI = new AlluxioURI("/" + dirName);
    FileSystem client = mLocalAlluxioClusterResource.get().getClient();
    client.listStatus(
        rootAlluxioURI, ListStatusOptions.defaults().setLoadMetadataType(LoadMetadataType.Always));

    try {
      client.createDirectory(rootAlluxioURI, CreateDirectoryOptions.defaults());
      Assert.fail("create is expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
      Assert.assertEquals(
          ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(rootAlluxioURI), e.getMessage());
    }

    AlluxioURI file1URI = rootAlluxioURI.join("file1");
    try {
      client.createFile(file1URI, CreateFileOptions.defaults()).close();
      Assert.fail("create is expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
      Assert.assertEquals(
          ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(file1URI), e.getMessage());
    }

    AlluxioURI file2URI = rootAlluxioURI.join("file2");
    try {
      client.createFile(file2URI, CreateFileOptions.defaults()).close();
      Assert.fail("create is expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
      Assert.assertEquals(
          ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(file2URI), e.getMessage());
    }
  }