Beispiel #1
1
  /** Test copy from an input stream to a file */
  static void testCopyInputStreamToFile() throws IOException {
    testCopyInputStreamToFile(0);
    for (int i = 0; i < 100; i++) {
      testCopyInputStreamToFile(rand.nextInt(32000));
    }

    // FileAlreadyExistsException
    Path target = createTempFile("blah", null);
    try {
      InputStream in = new ByteArrayInputStream(new byte[0]);
      try {
        copy(in, target);
        throw new RuntimeException("FileAlreadyExistsException expected");
      } catch (FileAlreadyExistsException ignore) {
      }
    } finally {
      delete(target);
    }
    Path tmpdir = createTempDirectory("blah");
    try {
      if (TestUtil.supportsLinks(tmpdir)) {
        Path link = createSymbolicLink(tmpdir.resolve("link"), tmpdir.resolve("target"));
        try {
          InputStream in = new ByteArrayInputStream(new byte[0]);
          try {
            copy(in, link);
            throw new RuntimeException("FileAlreadyExistsException expected");
          } catch (FileAlreadyExistsException ignore) {
          }
        } finally {
          delete(link);
        }
      }
    } finally {
      delete(tmpdir);
    }

    // nulls
    try {
      copy((InputStream) null, target);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException ignore) {
    }
    try {
      copy(new ByteArrayInputStream(new byte[0]), (Path) null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException ignore) {
    }
  }
  public static void main(String[] args) throws Exception {
    Path dir1 = TestUtil.createTemporaryDirectory();
    try {

      // Same directory
      testCopyFileToFile(dir1, dir1, TestUtil.supportsLinks(dir1));
      testMove(dir1, dir1, TestUtil.supportsLinks(dir1));

      // Different directories. Use test.dir if possible as it might be
      // a different volume/file system and so improve test coverage.
      String testDir = System.getProperty("test.dir", ".");
      Path dir2 = TestUtil.createTemporaryDirectory(testDir);
      try {
        boolean testSymbolicLinks = TestUtil.supportsLinks(dir1) && TestUtil.supportsLinks(dir2);
        testCopyFileToFile(dir1, dir2, testSymbolicLinks);
        testMove(dir1, dir2, testSymbolicLinks);
      } finally {
        TestUtil.removeAll(dir2);
      }

      // Target is location associated with custom provider
      Path dir3 = PassThroughFileSystem.create().getPath(dir1.toString());
      testCopyFileToFile(dir1, dir3, false);
      testMove(dir1, dir3, false);

      // Test copy(InputStream,Path) and copy(Path,OutputStream)
      testCopyInputStreamToFile();
      testCopyFileToOuputStream();

    } finally {
      TestUtil.removeAll(dir1);
    }
  }
Beispiel #3
0
  public static void main(String[] args) throws IOException {
    Path dir = TestUtil.createTemporaryDirectory();
    try {

      testEvents(dir);
      testCancel(dir);
      testAutomaticCancel(dir);
      testWakeup(dir);
      testExceptions(dir);
      testTwoWatchers(dir);

    } finally {
      TestUtil.removeAll(dir);
    }
  }
Beispiel #4
0
  public static void main(String[] args) throws IOException {
    // open file but do not close it. Its existance will be checked by
    // the calling script.
    Paths.get(args[0]).newByteChannel(READ, WRITE, DELETE_ON_CLOSE);

    // check temporary file has been deleted after closing it
    Path file = File.createTempFile("blah", "tmp").toPath();
    file.newByteChannel(READ, WRITE, DELETE_ON_CLOSE).close();
    if (file.exists()) throw new RuntimeException("Temporary file was not deleted");

    Path dir = TestUtil.createTemporaryDirectory();
    try {
      // check that DELETE_ON_CLOSE fails when file is a sym link
      if (TestUtil.supportsLinks(dir)) {
        file = dir.resolve("foo").createFile();
        Path link = dir.resolve("link").createSymbolicLink(file);
        try {
          link.newByteChannel(READ, WRITE, DELETE_ON_CLOSE);
          throw new RuntimeException("IOException expected");
        } catch (IOException ignore) {
        }
      }

      // check that DELETE_ON_CLOSE works with files created via open
      // directories
      DirectoryStream stream = dir.newDirectoryStream();
      try {
        if (stream instanceof SecureDirectoryStream) {
          SecureDirectoryStream secure = (SecureDirectoryStream) stream;
          file = Paths.get("foo");

          Set<OpenOption> opts = new HashSet<OpenOption>();
          opts.add(WRITE);
          opts.add(DELETE_ON_CLOSE);
          secure.newByteChannel(file, opts).close();

          if (dir.resolve(file).exists()) throw new RuntimeException("File not deleted");
        }
      } finally {
        stream.close();
      }
    } finally {
      TestUtil.removeAll(dir);
    }
  }