Beispiel #1
0
 /** Do not use. Testing purposes only. */
 public Result run(Runner runner) {
   Result result = new Result();
   RunListener listener = result.createListener();
   fNotifier.addFirstListener(listener);
   try {
     fNotifier.fireTestRunStarted(runner.getDescription());
     runner.run(fNotifier);
     fNotifier.fireTestRunFinished(result);
   } finally {
     removeListener(listener);
   }
   return result;
 }
  // Mostly taken from Junit4Provider.java
  public RunResult invokeJunit4(Class<?> clazz, String requestedTestMethod)
      throws TestSetFailedException {

    final ReporterFactory reporterFactory = providerParameters.getReporterFactory();

    RunListener reporter = reporterFactory.createReporter();

    ConsoleOutputCapture.startCapture((ConsoleOutputReceiver) reporter);

    JUnit4RunListener jUnit4TestSetReporter = new JUnit4RunListener(reporter);

    Result result = new Result();
    RunNotifier listeners = getRunNotifier(jUnit4TestSetReporter, result, customRunListeners);

    listeners.fireTestRunStarted(Description.createTestDescription(clazz, requestedTestMethod));

    final ReportEntry report =
        new SimpleReportEntry(
            getClass().getName() + "." + requestedTestMethod,
            clazz.getName() + "." + requestedTestMethod);
    reporter.testSetStarting(report);
    try {
      for (final Method method : clazz.getMethods()) {
        if (method.getParameterTypes().length == 0
            && requestedTestMethod.equals(method.getName())) {
          Request.method(clazz, method.getName()).getRunner().run(listeners);
        }
      }
    } catch (Throwable e) {
      reporter.testError(
          SimpleReportEntry.withException(
              report.getSourceName(),
              report.getName(),
              new PojoStackTraceWriter(report.getSourceName(), report.getName(), e)));
    } finally {
      reporter.testSetCompleted(report);
    }
    listeners.fireTestRunFinished(result);
    JUnit4RunListener.rethrowAnyTestMechanismFailures(result);

    closeRunNotifier(jUnit4TestSetReporter, customRunListeners);
    return reporterFactory.close();
  }
  @Override
  public void evaluate() throws Throwable {
    ProcessBuilder builder = new ProcessBuilder();

    builder.command(buildCommand());
    builder.redirectErrorStream(true);
    builder.directory(testCase.getWorkingDirectory());

    System.out.println("Working directory: " + SystemProperty.INSTANCE.getProperty("user.dir"));
    System.out.println("Command: " + builder.command());

    Description parentDescription = runner.getDescription();

    final Process process = builder.start();

    try {
      Scanner scanner = testCase.startTest(process);

      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();

        if (line.startsWith(JUNIT_OUTPUT_DELIMITER)) {
          System.out.println(line);
          String[] tokens = line.split(JUNIT_OUTPUT_DELIMITER_REGEXP);
          String event = tokens[1];

          if ("testRunStarted".equals(event)) {
            notifier.fireTestRunStarted(parentDescription);
          } else if ("testRunFinished".equals(event)) {
            notifier.fireTestRunFinished(result);
          } else if ("testStarted".equals(event)) {
            Description ofTest = findDescription(parentDescription, tokens[2]);
            notifier.fireTestStarted(ofTest);
          } else if ("testFinished".equals(event)) {
            Description ofTest = findDescription(parentDescription, tokens[2]);
            notifier.fireTestFinished(ofTest);
          } else if ("testFailure".equals(event)) {
            List<String> stackTrace =
                tokens.length > 5
                    ? Arrays.asList(tokens).subList(5, tokens.length - 1)
                    : Collections.<String>emptyList();
            Failure failure =
                toFailure(parentDescription, tokens[2], tokens[3], tokens[4], stackTrace);
            notifier.fireTestFailure(failure);
          } else if ("testAssumptionFailure".equals(event)) {
            List<String> stackTrace =
                tokens.length > 5
                    ? Arrays.asList(tokens).subList(5, tokens.length - 1)
                    : Collections.<String>emptyList();
            Failure failure =
                toFailure(parentDescription, tokens[2], tokens[3], tokens[4], stackTrace);
            notifier.fireTestAssumptionFailed(failure);
          } else if ("testIgnored".equals(event)) {
            Description ofTest = findDescription(parentDescription, tokens[2]);
            notifier.fireTestIgnored(ofTest);
          }
        } else {
          System.out.println(line);
        }
      }
    } catch (Throwable t) {
      System.err.println("Failed to execute test");
      t.printStackTrace();

      throw t;
    } finally {
      try {
        testCase.endTest(process);
      } finally {

        Timer timer = new Timer();
        timer.schedule(
            new TimerTask() {

              @Override
              public void run() {
                process.destroy();
              }
            },
            10 * 1000);

        try {
          process.waitFor();
        } catch (InterruptedException e) {
          // ignore
        }

        timer.cancel();
      }
    }
  }