Example #1
0
  /** Tests all possible ways to invoke copy to copy a file to a file */
  static void testCopyFileToFile(Path dir1, Path dir2, boolean supportsLinks) throws IOException {
    Path source, target, link, entry;

    // -- regular file --

    /** Test: move regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: copy regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists and is empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists and is non-empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /** Test: copy regular file + attributes */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, COPY_ATTRIBUTES);
    delete(source);
    delete(target);

    // -- directory --

    /*
     * Test: copy directory, target does not exist
     */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: copy directory, target does not exist */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists and is empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists and is non-empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      copyAndVerify(source, target, REPLACE_EXISTING);
      throw new RuntimeException("DirectoryNotEmptyException expected");
    } catch (DirectoryNotEmptyException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /*
     * Test: copy directory + attributes
     */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, COPY_ATTRIBUTES);
    delete(source);
    delete(target);

    // -- symbolic links --

    /** Test: Follow link */
    if (supportsLinks) {
      source = createSourceFile(dir1);
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target);
      delete(link);
      delete(source);
    }

    /** Test: Copy link (to file) */
    if (supportsLinks) {
      source = createSourceFile(dir1);
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
      delete(source);
    }

    /** Test: Copy link (to directory) */
    if (supportsLinks) {
      source = dir1.resolve("mydir");
      createDirectory(source);
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
      delete(source);
    }

    /** Test: Copy broken link */
    if (supportsLinks) {
      assertTrue(notExists(source));
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
    }

    /** Test: Copy link to UNC (Windows only) */
    if (supportsLinks && System.getProperty("os.name").startsWith("Windows")) {
      Path unc = Paths.get("\\\\rialto\\share\\file");
      link = dir1.resolve("link");
      createSymbolicLink(link, unc);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
    }

    // -- misc. tests --

    /** Test nulls */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      copy(source, null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      copy(source, target, (CopyOption[]) null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      CopyOption[] opts = {REPLACE_EXISTING, null};
      copy(source, target, opts);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    delete(source);

    /** Test UOE */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      copy(source, target, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    try {
      copy(source, target, REPLACE_EXISTING, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    delete(source);
  }
Example #2
0
  /** Tests all possible ways to invoke move */
  static void testMove(Path dir1, Path dir2, boolean supportsLinks) throws IOException {
    Path source, target, entry;

    boolean sameDevice = getFileStore(dir1).equals(getFileStore(dir2));

    // -- regular file --

    /** Test: move regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target);
    delete(target);

    /** Test: move regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: move regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move regular file, target exists and is empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move regular file, target exists and is non-empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /** Test atomic move of regular file (same file store) */
    source = createSourceFile(dir1);
    target = getTargetFile(dir1);
    moveAndVerify(source, target, ATOMIC_MOVE);
    delete(target);

    /** Test atomic move of regular file (different file store) */
    if (!sameDevice) {
      source = createSourceFile(dir1);
      target = getTargetFile(dir2);
      try {
        moveAndVerify(source, target, ATOMIC_MOVE);
        throw new RuntimeException("AtomicMoveNotSupportedException expected");
      } catch (AtomicMoveNotSupportedException x) {
      }
      delete(source);
    }

    // -- directories --

    /*
     * Test: move empty directory, target does not exist
     */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target);
    delete(target);

    /** Test: move empty directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: move empty directory, target does not exist */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move empty directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move empty, target exists and is empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move empty directory, target exists and is non-empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      moveAndVerify(source, target, REPLACE_EXISTING);
      throw new RuntimeException("DirectoryNotEmptyException expected");
    } catch (DirectoryNotEmptyException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /** Test: move non-empty directory (same file system) */
    source = createSourceDirectory(dir1);
    createFile(source.resolve("foo"));
    target = getTargetFile(dir1);
    moveAndVerify(source, target);
    delete(target.resolve("foo"));
    delete(target);

    /** Test: move non-empty directory (different file store) */
    if (!sameDevice) {
      source = createSourceDirectory(dir1);
      createFile(source.resolve("foo"));
      target = getTargetFile(dir2);
      try {
        moveAndVerify(source, target);
        throw new RuntimeException("IOException expected");
      } catch (IOException x) {
      }
      delete(source.resolve("foo"));
      delete(source);
    }

    /** Test atomic move of directory (same file store) */
    source = createSourceDirectory(dir1);
    createFile(source.resolve("foo"));
    target = getTargetFile(dir1);
    moveAndVerify(source, target, ATOMIC_MOVE);
    delete(target.resolve("foo"));
    delete(target);

    // -- symbolic links --

    /** Test: Move symbolic link to file, target does not exist */
    if (supportsLinks) {
      Path tmp = createSourceFile(dir1);
      source = dir1.resolve("link");
      createSymbolicLink(source, tmp);
      target = getTargetFile(dir2);
      moveAndVerify(source, target);
      delete(target);
      delete(tmp);
    }

    /** Test: Move symbolic link to directory, target does not exist */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      moveAndVerify(source, target);
      delete(target);
    }

    /** Test: Move broken symbolic link, target does not exists */
    if (supportsLinks) {
      Path tmp = Paths.get("doesnotexist");
      source = dir1.resolve("link");
      createSymbolicLink(source, tmp);
      target = getTargetFile(dir2);
      moveAndVerify(source, target);
      delete(target);
    }

    /** Test: Move symbolic link, target exists */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createFile(target);
      try {
        moveAndVerify(source, target);
        throw new RuntimeException("FileAlreadyExistsException expected");
      } catch (FileAlreadyExistsException x) {
      }
      delete(source);
      delete(target);
    }

    /** Test: Move regular file, target exists */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createFile(target);
      moveAndVerify(source, target, REPLACE_EXISTING);
      delete(target);
    }

    /** Test: move symbolic link, target exists and is empty directory */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createDirectory(target);
      moveAndVerify(source, target, REPLACE_EXISTING);
      delete(target);
    }

    /** Test: symbolic link, target exists and is non-empty directory */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createDirectory(target);
      entry = target.resolve("foo");
      createFile(entry);
      try {
        moveAndVerify(source, target);
        throw new RuntimeException("FileAlreadyExistsException expected");
      } catch (FileAlreadyExistsException x) {
      }
      delete(entry);
      delete(source);
      delete(target);
    }

    /** Test atomic move of symbolic link (same file store) */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir1);
      target = getTargetFile(dir2);
      createFile(target);
      moveAndVerify(source, target, REPLACE_EXISTING);
      delete(target);
    }

    // -- misc. tests --

    /** Test nulls */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      move(null, target);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      move(source, null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      move(source, target, (CopyOption[]) null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      CopyOption[] opts = {REPLACE_EXISTING, null};
      move(source, target, opts);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    delete(source);

    /** Test UOE */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      move(source, target, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    try {
      move(source, target, REPLACE_EXISTING, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    delete(source);
  }