All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.ext.unit.impl.TestCaseReportImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR3
Show newest version
package io.vertx.ext.unit.impl;

import io.vertx.core.Handler;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.report.TestResult;
import io.vertx.ext.unit.report.TestCaseReport;

import java.util.Map;
import java.util.function.Function;

/**
 * @author Julien Viet
 */
public class TestCaseReportImpl implements TestCaseReport {

  private final String name;
  private final long timeout;
  private final int repeat;
  private final Map attributes;
  private final Handler before;
  private final Handler test;
  private final Handler after;
  private final Handler unhandledFailureHandler;
  private volatile Handler completionHandler;

  public TestCaseReportImpl(String name,
                            long timeout,
                            int repeat,
                            Map attributes,
                            Handler before,
                            Handler test,
                            Handler after,
                            Handler unhandledFailureHandler) {

    this.attributes = attributes;
    this.timeout = timeout;
    this.repeat = repeat;
    this.name = name;
    this.before = before;
    this.test = test;
    this.after = after;
    this.unhandledFailureHandler = unhandledFailureHandler;
  }

  Task buildTask(Task nextTask) {
    // Build task assemblies for the test case
    Task task = (result, context) -> {
      if (completionHandler != null) {
        completionHandler.handle(new TestResultImpl(name, result.beginTime, result.duration(), result.failure));
      }
      nextTask.execute(null, context);
    };
    for (int count = 0;count < repeat;count++) {
      task = runTask(task);
    }
    return task;
  }

  private Task runTask(Task next) {
    TestContextImpl testContext = new TestContextImpl(attributes, unhandledFailureHandler);
    Task afterHandler;
    if (after != null) {
      afterHandler = new TestContextTask(testContext, after, next, timeout);
    } else {
      afterHandler = next;
    }
    Task testHandler = new TestContextTask(testContext, test, afterHandler, timeout);
    if (before != null) {
      Function> tmp = result -> {
        if (result.failure != null) {
          return next;
        } else {
          return testHandler;
        }
      };
      return new TestContextTask(testContext, before, tmp, timeout);
    } else {
      return testHandler;
    }
  }

  @Override
  public String name() {
    return name;
  }

  @Override
  public TestCaseReport endHandler(Handler handler) {
    completionHandler = handler;
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy