Ejemplo n.º 1
0
  /**
   * Recursively delete a directory / file
   *
   * <p>For safety this method only deletes files created under the workspace
   *
   * @param file
   */
  private static final void deleteRecursive(File f) throws IllegalArgumentException {
    // Ensure that the file being deleted is a child of the workspace
    // root to prevent anything nasty happening
    if (!f.getAbsolutePath()
        .startsWith(
            ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getAbsolutePath()))
      throw new IllegalArgumentException("File must exist within the workspace!");

    if (f.isDirectory()) for (File f1 : f.listFiles()) deleteRecursive(f1);
    f.delete();
  }
Ejemplo n.º 2
0
  /**
   * Clean-up any files created as part of a unit test. This method removes *all* Workspace
   * IResources and any external files / folders created with the #createWorkspaceFile
   * #createWorkspaceFolder methods in this class
   */
  public static void cleanUp() throws CoreException, IOException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    root.refreshLocal(IResource.DEPTH_INFINITE, NULL_MONITOR);

    // Delete all external files & folders created using ResourceHelper
    for (String loc : externalFilesCreated) {
      File f = new File(loc);
      if (f.exists()) deleteRecursive(f);
    }
    externalFilesCreated.clear();

    // Remove IResources created by this helper
    for (IResource r : resourcesCreated) {
      if (r.exists())
        try {
          r.delete(true, NULL_MONITOR);
        } catch (CoreException e) {
          // Ignore
        }
    }
    resourcesCreated.clear();
  }