private void restoreTestProject() throws Exception {
    IJavaProject javaProject = getRoot().getJavaProject();
    if (javaProject.exists()) {
      IClasspathEntry srcEntry = getRoot().getRawClasspathEntry();
      IClasspathEntry jreEntry = RefactoringTestSetup.getJRELibrary().getRawClasspathEntry();
      IClasspathEntry[] cpes = javaProject.getRawClasspath();
      ArrayList newCPEs = new ArrayList();
      boolean cpChanged = false;
      for (int i = 0; i < cpes.length; i++) {
        IClasspathEntry cpe = cpes[i];
        if (cpe.equals(srcEntry) || cpe.equals(jreEntry)) {
          newCPEs.add(cpe);
        } else {
          cpChanged = true;
        }
      }
      if (cpChanged) {
        IClasspathEntry[] newCPEsArray =
            (IClasspathEntry[]) newCPEs.toArray(new IClasspathEntry[newCPEs.size()]);
        javaProject.setRawClasspath(newCPEsArray, null);
      }

      Object[] nonJavaResources = javaProject.getNonJavaResources();
      for (int i = 0; i < nonJavaResources.length; i++) {
        Object kid = nonJavaResources[i];
        if (kid instanceof IResource) {
          IResource resource = (IResource) kid;
          if (!PROJECT_RESOURCE_CHILDREN.contains(resource.getName())) {
            JavaProjectHelper.delete(resource);
          }
        }
      }
    }
  }
  public void testJUnitWithCloneNotRaw() throws Exception {
    fAssumeCloneReturnsSameType = true;
    fLeaveUnconstrainedRaw = false;

    IJavaProject javaProject = JavaProjectHelper.createJavaProject("InferTypeArguments", "bin");
    try {
      IPackageFragmentRoot jdk = JavaProjectHelper.addRTJar(javaProject);
      Assert.assertNotNull(jdk);

      File junitSrcArchive =
          JavaTestPlugin.getDefault().getFileInPlugin(JavaProjectHelper.JUNIT_SRC_381);
      Assert.assertTrue(junitSrcArchive != null && junitSrcArchive.exists());

      IPackageFragmentRoot src =
          JavaProjectHelper.addSourceContainerWithImport(
              javaProject, "src", junitSrcArchive, JavaProjectHelper.JUNIT_SRC_ENCODING);

      boolean performed =
          perform(new IJavaElement[] {javaProject}, RefactoringStatus.OK, RefactoringStatus.OK);
      assertTrue(performed);

      compareWithZipFile(src, "junit381-noUI-clone-not-raw-src.zip");
    } finally {
      if (javaProject != null && javaProject.exists()) JavaProjectHelper.delete(javaProject);
    }
  }
  /**
   * Removes contents of {@link #getPackageP()}, of {@link #getRoot()} (except for p) and of the
   * Java project (except for src and the JRE library).
   *
   * @throws Exception in case of errors
   */
  protected void tearDown() throws Exception {
    refreshFromLocal();
    performDummySearch();

    final boolean pExists = getPackageP().exists();
    if (pExists) {
      tryDeletingAllJavaChildren(getPackageP());
      tryDeletingAllNonJavaChildResources(getPackageP());
    }

    if (getRoot().exists()) {
      IJavaElement[] packages = getRoot().getChildren();
      for (int i = 0; i < packages.length; i++) {
        IPackageFragment pack = (IPackageFragment) packages[i];
        if (!pack.equals(getPackageP()) && pack.exists() && !pack.isReadOnly())
          if (pack.isDefaultPackage()) pack.delete(true, null);
          else
            JavaProjectHelper.delete(pack.getResource()); // also delete packages with subpackages
      }
      // Restore package 'p'
      if (!pExists) getRoot().createPackageFragment("p", true, null);

      tryDeletingAllNonJavaChildResources(getRoot());
    }

    restoreTestProject();
  }
 private static void tryDeletingAllJavaChildren(IPackageFragment pack) throws CoreException {
   IJavaElement[] kids = pack.getChildren();
   for (int i = 0; i < kids.length; i++) {
     if (kids[i] instanceof ISourceManipulation) {
       if (kids[i].exists() && !kids[i].isReadOnly()) JavaProjectHelper.delete(kids[i]);
     }
   }
 }
 private static void tryDeletingAllNonJavaChildResources(IPackageFragmentRoot root)
     throws CoreException {
   Object[] nonJavaKids = root.getNonJavaResources();
   for (int i = 0; i < nonJavaKids.length; i++) {
     if (nonJavaKids[i] instanceof IResource) {
       IResource resource = (IResource) nonJavaKids[i];
       JavaProjectHelper.delete(resource);
     }
   }
 }
 protected void tearDown() throws Exception {
   if (fJavaProject != null) {
     JavaProjectHelper.delete(fJavaProject);
     fJavaProject = null;
   }
 }
  /**
   * Invokes the introduce indirection ref. Some pointers:
   *
   * @param topLevelName This is an array of fully qualified top level(!) type names with exactly
   *     one package prefix (e.g. "p.Foo"). Simple names must correspond to .java files. The first
   *     cu will be used for the invocation of the refactoring (see positioning)
   * @param newName name of indirection method
   * @param qTypeName qualified type name of the type for the indirection method. Should be one of
   *     the cus in topLevelName.
   * @param startLine starting line of selection in topLevelName[0]
   * @param startColumn starting column of selection in topLevelName[0]
   * @param endLine ending line of selection in topLevelName[0]
   * @param endColumn ending column of selection in topLevelName[0]
   * @param updateReferences true if references should be updated
   * @param shouldWarn if true, warnings will be expected in the result
   * @param shouldError if true, errors will be expected in the result
   * @param shouldFail if true, fatal errors will be expected in the result
   * @throws Exception
   * @throws JavaModelException
   * @throws CoreException
   * @throws IOException
   */
  private void helper(
      String[] topLevelName,
      String newName,
      String qTypeName,
      int startLine,
      int startColumn,
      int endLine,
      int endColumn,
      boolean updateReferences,
      boolean shouldWarn,
      boolean shouldError,
      boolean shouldFail)
      throws Exception, JavaModelException, CoreException, IOException {
    ICompilationUnit[] cu = new ICompilationUnit[topLevelName.length];
    for (int i = 0; i < topLevelName.length; i++) {
      String packName = topLevelName[i].substring(0, topLevelName[i].indexOf('.'));
      String className = topLevelName[i].substring(topLevelName[i].indexOf('.') + 1);
      IPackageFragment cPackage = getRoot().createPackageFragment(packName, true, null);
      cu[i] = createCUfromTestFile(cPackage, className);
    }

    ISourceRange selection =
        TextRangeUtil.getSelection(cu[0], startLine, startColumn, endLine, endColumn);
    try {
      IntroduceIndirectionRefactoring ref =
          new IntroduceIndirectionRefactoring(cu[0], selection.getOffset(), selection.getLength());
      ref.setEnableUpdateReferences(updateReferences);
      if (qTypeName != null) ref.setIntermediaryClassName(qTypeName);
      if (newName != null) ref.setIntermediaryMethodName(newName);

      boolean failed = false;
      RefactoringStatus status = performRefactoringWithStatus(ref);
      if (status.hasFatalError()) {
        assertTrue(
            "Failed but shouldn't: " + status.getMessageMatchingSeverity(RefactoringStatus.FATAL),
            shouldFail);
        failed = true;
      } else assertFalse("Didn't fail although expected", shouldFail);

      if (!failed) {

        if (status.hasError())
          assertTrue(
              "Had errors but shouldn't: "
                  + status.getMessageMatchingSeverity(RefactoringStatus.ERROR),
              shouldError);
        else assertFalse("No error although expected", shouldError);

        if (status.hasWarning())
          assertTrue(
              "Had warnings but shouldn't: "
                  + status.getMessageMatchingSeverity(RefactoringStatus.WARNING),
              shouldWarn);
        else assertFalse("No warning although expected", shouldWarn);

        for (int i = 0; i < topLevelName.length; i++) {
          String className = topLevelName[i].substring(topLevelName[i].indexOf('.') + 1);
          assertEqualLines(
              "invalid output.",
              getFileContents(getOutputTestFileName(className)),
              cu[i].getSource());
        }
      }
    } finally {
      for (int i = 0; i < topLevelName.length; i++) JavaProjectHelper.delete(cu[i]);
    }
  }
 protected void tearDown() throws Exception {
   JavaProjectHelper.delete(fProject);
 }
 public void tearDown() throws Exception {
   JavaProjectHelper.delete(fJavaProject1);
   JavaProjectHelper.delete(fJavaProject2);
 }