コード例 #1
0
  /** Makes an effort to remove both temporary directories. */
  @After
  public void clean() {
    if (directory != null) {
      directory.remove();
    }

    if (second_directory != null) {
      second_directory.remove();
    }
  }
コード例 #2
0
  /** Performs the tests. */
  @Test
  public void perform() {
    // Get the File objects corresponding to both directories.
    File root = directory.root();
    File second_root = second_directory.root();

    // Make sure the two directories are not the same.
    if (root.equals(second_root)) {
      Assert.fail("temporary directories created with the " + "same path");
    }

    // Make sure both directories are indeed directories and exist.
    if (!root.isDirectory() || !second_root.isDirectory()) {
      Assert.fail("temporary directory does not exist or is " + "not a directory");
    }

    // Delete the second directory - it is no longer needed.
    second_directory.remove();

    // There is a potential race condition here - the second directory could
    // be re-created before this statement runs. However, this is not the
    // common case when the library is being tested.
    if (second_root.exists()) {
      Assert.fail("temporary directory not removed");
    }

    // Try to add a file to the first directory.
    try {
      directory.add(new String[] {"subdir", "file.txt"}, "contents");
    } catch (Exception e) {
      e.printStackTrace();
      Assert.fail("unable to create file in temporary directory");
    }

    // Delete the first directory. This ensures that recursive deletion
    // works.
    directory.remove();

    if (root.exists()) Assert.fail("temporary directory not removed");
  }