Exemple #1
0
  public void writeProblemLog(Appendable out) throws IOException {
    out.append("----------\n");
    if (problems != null) {
      int errorCount = 0;
      for (CompilationResult r : problems) {
        for (CategorizedProblem p : r.getAllProblems()) {
          out.append(Integer.toString(++errorCount));
          out.append(". ");
          if (p.isError()) {
            out.append("ERROR in ");
          } else if (p.isWarning()) {
            out.append("WARNING in ");
          } else {
            out.append("Problem in ");
          }
          out.append(new String(p.getOriginatingFileName()));

          String errorReportSource =
              ((DefaultProblem) p).errorReportSource(r.compilationUnit.getContents());

          out.append(errorReportSource);
          out.append("\n");
          out.append(p.getMessage());
          out.append("\n----------\n");
        }
      }
    }
  }
  public void checkParse(char[] source, String expectedSyntaxErrorDiagnosis, String testName) {

    /* using regular parser in DIET mode */
    Parser parser =
        new Parser(
            new ProblemReporter(
                DefaultErrorHandlingPolicies.proceedWithAllProblems(),
                new CompilerOptions(getCompilerOptions()),
                new DefaultProblemFactory(Locale.getDefault())),
            optimizeStringLiterals);
    ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null);
    CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);

    parser.parse(sourceUnit, compilationResult);

    StringBuffer buffer = new StringBuffer(100);
    if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
      CategorizedProblem[] problems = compilationResult.getAllProblems();
      int count = problems.length;
      int problemCount = 0;
      char[] unitSource = compilationResult.compilationUnit.getContents();
      for (int i = 0; i < count; i++) {
        if (problems[i] != null) {
          if (problemCount == 0) buffer.append("----------\n");
          problemCount++;
          buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
          buffer.append(
              " in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
          try {
            buffer.append(((DefaultProblem) problems[i]).errorReportSource(unitSource));
            buffer.append("\n");
            buffer.append(problems[i].getMessage());
            buffer.append("\n");
          } catch (Exception e) {
          }
          buffer.append("----------\n");
        }
      }
    }
    String computedSyntaxErrorDiagnosis = buffer.toString();
    // System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
    assertEquals(
        "Invalid syntax error diagnosis" + testName,
        Util.convertToIndependantLineDelimiter(expectedSyntaxErrorDiagnosis),
        Util.convertToIndependantLineDelimiter(computedSyntaxErrorDiagnosis));
  }