Esempio n. 1
0
  @Override
  public void runTest() throws Throwable {
    REF.IN_TESTS = true;

    IDocument document = new Document(data.source);
    ITextSelection selection =
        new TextSelection(
            document, data.sourceSelection.getOffset(), data.sourceSelection.getLength());
    RefactoringInfo info =
        new RefactoringInfo(
            document,
            selection,
            new IGrammarVersionProvider() {

              public int getGrammarVersion() throws MisconfigurationException {
                return IGrammarVersionProvider.GRAMMAR_PYTHON_VERSION_2_7;
              }
            });
    InlineLocalRefactoring refactoring = new InlineLocalRefactoring(info);

    NullProgressMonitor monitor = new NullProgressMonitor();
    RefactoringStatus result = refactoring.checkAllConditions(monitor);

    assertTrue(
        "Refactoring is not ok: " + result.getMessageMatchingSeverity(RefactoringStatus.WARNING),
        result.isOK());

    Change change = refactoring.createChange(monitor);
    change.perform(monitor);

    assertEquals(data.result, document.get());

    REF.IN_TESTS = false;
  }
Esempio n. 2
0
  /**
   * 執行Quick Fix變更
   *
   * @param activeEditor
   * @param document
   * @throws CoreException
   */
  private void performChange(IEditorPart activeEditor, IDocument document, ASTRewrite rewrite)
      throws CoreException {
    Change change = null;
    IRewriteTarget rewriteTarget = null;
    try {
      change = getChange(actRoot, rewrite);
      if (change != null) {
        if (document != null) {
          LinkedModeModel.closeAllModels(document);
        }
        if (activeEditor != null) {
          rewriteTarget = (IRewriteTarget) activeEditor.getAdapter(IRewriteTarget.class);
          if (rewriteTarget != null) {
            rewriteTarget.beginCompoundChange();
          }
        }

        change.initializeValidationData(new NullProgressMonitor());
        RefactoringStatus valid = change.isValid(new NullProgressMonitor());
        if (valid.hasFatalError()) {
          IStatus status =
              new Status(
                  IStatus.ERROR,
                  JavaPlugin.getPluginId(),
                  IStatus.ERROR,
                  valid.getMessageMatchingSeverity(RefactoringStatus.FATAL),
                  null);
          throw new CoreException(status);
        } else {
          IUndoManager manager = RefactoringCore.getUndoManager();
          manager.aboutToPerformChange(change);
          Change undoChange = change.perform(new NullProgressMonitor());
          manager.changePerformed(change, true);
          if (undoChange != null) {
            undoChange.initializeValidationData(new NullProgressMonitor());
            manager.addUndo("Quick Undo", undoChange);
          }
        }
      }
    } finally {
      if (rewriteTarget != null) {
        rewriteTarget.endCompoundChange();
      }

      if (change != null) {
        change.dispose();
      }
    }
  }
  /** Creates a refactoring (or returns a previously created refactoring). */
  public Refactoring createRefactoring() throws RefactoringException {
    if (refactoring != null) {
      return refactoring;
    }

    RefactoringStatus status = new RefactoringStatus();
    D descriptor = createDescriptor();
    try {
      refactoring = descriptor.createRefactoring(status);
    } catch (CoreException e) {
      throw new RefactoringException(e);
    }
    if (refactoring == null) {
      throw new RefactoringException(
          String.format(
              "The refactoring descriptor (%s) was unable to create a refactoring.",
              descriptor.getClass().getSimpleName()));
    }
    if (status.hasError()) {
      throw new RefactoringException(status.getMessageMatchingSeverity(RefactoringStatus.ERROR));
    }

    return refactoring;
  }
  /**
   * 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]);
    }
  }