Esempio n. 1
0
 protected void assertConditionsWarning(RefactoringStatus conditions, int number) {
   if (number > 0) {
     assertTrue("Warning in Condition expected", conditions.hasWarning()); // $NON-NLS-1$
   }
   RefactoringStatusEntry[] entries = conditions.getEntries();
   int count = 0;
   for (RefactoringStatusEntry entry : entries) {
     if (entry.isWarning()) {
       ++count;
     }
   }
   assertEquals(number + " Warnings expected found " + count, count, number); // $NON-NLS-1$
 }
  /**
   * 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]);
    }
  }