예제 #1
0
  public void createScanner() throws TestFailException {
    jflexFiles.add((new File(testPath, testName + ".flex")).getPath());
    // invoke JFlex
    jflexResult = Exec.execJFlex(jflexCmdln, jflexFiles);
    // System.out.println(jflexResult);

    if (jflexResult.getSuccess()) {
      // Scanner generation successful
      if (Main.verbose) {
        System.out.println("Scanner generation successful");
      }

      if (expectJFlexFail) {
        System.out.println("JFlex failure expected!");
        throw new TestFailException();
      }

      // check JFlex output conformance
      File expected = new File(testPath, testName + "-flex.output");

      if (expected.exists()) {
        DiffStream check = new DiffStream();
        String diff;
        try {
          diff =
              check.diff(
                  jflexDiff, new StringReader(jflexResult.getOutput()), new FileReader(expected));
        } catch (FileNotFoundException e) {
          System.out.println("Error opening file " + expected);
          throw new TestFailException();
        }
        if (diff != null) {
          System.out.println("Test failed, unexpected jflex output: " + diff);
          System.out.println("JFlex output: " + jflexResult.getOutput());
          throw new TestFailException();
        }
      } else {
        System.out.println("Warning: no file for expected output [" + expected + "]");
      }

      // Compile Scanner
      String toCompile = new File(testPath, className + ".java").getPath();
      if (Main.verbose) {
        System.out.println("File to Compile: " + toCompile);
      }
      javacFiles.add(toCompile);
      javacResult = Exec.execJavac(testPath, Main.jflexTestVersion);

      // System.out.println(javacResult);
      if (Main.verbose) {
        System.out.println(
            "Compilation successful: "
                + javacResult.getSuccess()
                + " [expected: "
                + !expectJavacFail
                + "]");
      }
      if (javacResult.getSuccess() == expectJavacFail) {
        System.out.println("Compilation failed, messages:");
        System.out.println(javacResult.getOutput());
        throw new TestFailException();
      }
    } else {
      if (!expectJFlexFail) {
        System.out.println("Scanner generation failed!");
        System.out.println("JFlex output was:\n" + jflexResult.getOutput());
        throw new TestFailException();
      } else {
        // check JFlex output conformance
        File expected = new File(testPath, testName + "-flex.output");

        if (expected.exists()) {
          DiffStream check = new DiffStream();
          String diff;
          try {
            diff =
                check.diff(
                    jflexDiff, new StringReader(jflexResult.getOutput()), new FileReader(expected));
          } catch (FileNotFoundException e) {
            System.out.println("Error opening file " + expected);
            throw new TestFailException();
          }
          if (diff != null) {
            System.out.println("Test failed, unexpected jflex output: " + diff);
            System.out.println("JFlex output: " + jflexResult.getOutput());
            throw new TestFailException();
          }
        }
      }
    }
  }