private void failHelper1(
      int startLine,
      int startColumn,
      int endLine,
      int endColumn,
      boolean makeFinal,
      String className,
      int visibility,
      int expectedSeverity)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), false, true);
    ISourceRange selection =
        TextRangeUtil.getSelection(cu, startLine, startColumn, endLine, endColumn);
    ConvertAnonymousToNestedRefactoring ref =
        new ConvertAnonymousToNestedRefactoring(cu, selection.getOffset(), selection.getLength());

    RefactoringStatus preconditionResult = ref.checkInitialConditions(new NullProgressMonitor());
    if (preconditionResult.isOK()) preconditionResult = null;
    assertEquals("activation was supposed to be successful", null, preconditionResult);

    ref.setClassName(className);
    ref.setDeclareFinal(makeFinal);
    ref.setVisibility(visibility);

    if (preconditionResult == null)
      preconditionResult = ref.checkFinalConditions(new NullProgressMonitor());
    else preconditionResult.merge(ref.checkFinalConditions(new NullProgressMonitor()));
    if (preconditionResult.isOK()) preconditionResult = null;
    assertNotNull("precondition was supposed to fail", preconditionResult);

    assertEquals("incorrect severity:", expectedSeverity, preconditionResult.getSeverity());
  }
  private void failActivationHelper(
      int startLine, int startColumn, int endLine, int endColumn, int expectedSeverity)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), false, true);
    ISourceRange selection =
        TextRangeUtil.getSelection(cu, startLine, startColumn, endLine, endColumn);
    ConvertAnonymousToNestedRefactoring ref =
        new ConvertAnonymousToNestedRefactoring(cu, selection.getOffset(), selection.getLength());

    RefactoringStatus preconditionResult = ref.checkInitialConditions(new NullProgressMonitor());
    assertEquals(
        "activation was supposed to fail", expectedSeverity, preconditionResult.getSeverity());
  }
  /*
   * get selection from comment in source "//selection: line, col, line, col"
   * <br>relies on tabwidth == 4
   */
  protected ISourceRange getSelection(ICompilationUnit cu) throws Exception {
    String source = cu.getSource();
    String selection = "//selection:";
    int selStart = source.indexOf(selection);
    assertTrue(selStart != -1);

    int dataStart = selStart + selection.length();
    StringTokenizer tokenizer = new StringTokenizer(source.substring(dataStart), " ,\t\r\n");
    int line1 = Integer.parseInt(tokenizer.nextToken());
    int col1 = Integer.parseInt(tokenizer.nextToken());
    int line2 = Integer.parseInt(tokenizer.nextToken());
    int col2 = Integer.parseInt(tokenizer.nextToken());
    return TextRangeUtil.getSelection(cu, line1, col1, line2, col2);
  }
  private void helper1(
      int startLine,
      int startColumn,
      int endLine,
      int endColumn,
      boolean makeFinal,
      boolean makeStatic,
      String className,
      int visibility)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), true, true);
    ISourceRange selection =
        TextRangeUtil.getSelection(cu, startLine, startColumn, endLine, endColumn);
    ConvertAnonymousToNestedRefactoring ref =
        new ConvertAnonymousToNestedRefactoring(cu, selection.getOffset(), selection.getLength());

    RefactoringStatus preconditionResult = ref.checkInitialConditions(new NullProgressMonitor());
    if (preconditionResult.isOK()) preconditionResult = null;
    assertEquals("activation was supposed to be successful", null, preconditionResult);

    ref.setClassName(className);
    ref.setDeclareFinal(makeFinal);
    ref.setDeclareStatic(makeStatic);
    ref.setVisibility(visibility);

    if (preconditionResult == null)
      preconditionResult = ref.checkFinalConditions(new NullProgressMonitor());
    else preconditionResult.merge(ref.checkFinalConditions(new NullProgressMonitor()));
    if (preconditionResult.isOK()) preconditionResult = null;
    assertEquals("precondition was supposed to pass", null, preconditionResult);

    performChange(ref, false);

    IPackageFragment pack = (IPackageFragment) cu.getParent();
    String newCuName = getSimpleTestFileName(true, true);
    ICompilationUnit newcu = pack.getCompilationUnit(newCuName);
    assertTrue(newCuName + " does not exist", newcu.exists());
    assertEqualLines(getFileContents(getTestFileName(true, false)), newcu.getSource());
  }
  /**
   * 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]);
    }
  }