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

io.vertx.ext.unit.impl.TestSuiteReportImpl 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.Context;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.streams.ReadStream;
import io.vertx.ext.unit.report.TestSuiteReport;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.report.TestCaseReport;

import java.util.HashMap;
import java.util.Map;

/**
* @author Julien Viet
*/
class TestSuiteReportImpl implements TestSuiteReport {

  private final String name;
  private final long timeout;
  private final Handler before;
  private final Handler after;
  private final Handler beforeEach;
  private final Handler afterEach;
  private final TestCaseImpl[] tests;
  private Handler endHandler;
  private Handler exceptionHandler;
  private Handler handler;

  TestSuiteReportImpl(String name, long timeout, Handler before, Handler after,
                      Handler beforeEach, Handler afterEach,
                      TestCaseImpl[] tests) {
    this.name = name;
    this.timeout = timeout;
    this.before = before;
    this.after = after;
    this.beforeEach = beforeEach;
    this.afterEach = afterEach;
    this.tests = tests;
  }

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

  @Override
  public TestSuiteReport exceptionHandler(Handler handler) {
    exceptionHandler = handler;
    return this;
  }

  @Override
  public TestSuiteReport handler(Handler handler) {
    this.handler = handler;
    return this;
  }

  @Override
  public TestSuiteReport pause() {
    return this;
  }

  @Override
  public TestSuiteReport resume() {
    return this;
  }

  @Override
  public ReadStream fetch(long amount) {
    return this;
  }

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

  private Task buildTestCasesTasks(Map attributes, TestCaseImpl[] tests, int index, Task endTask) {
    if (tests.length > index) {
      TestCaseImpl test = tests[index];
      Task nextTask = buildTestCasesTasks(attributes, tests, index + 1, endTask);
      return (v, context) -> {
        TestCaseReportImpl testReport = new TestCaseReportImpl(test.name, timeout, test.repeat, new HashMap<>(attributes), beforeEach, test.handler, afterEach, exceptionHandler);
        if (handler != null) {
          handler.handle(testReport);
        }
        Task task = testReport.buildTask(nextTask);
        task.execute(null, context);
      };
    } else {
      if (after != null) {
        return new TestContextTask(new TestContextImpl(new HashMap<>(attributes), exceptionHandler), after, endTask, 0);
      } else {
        return endTask;
      }
    }
  }

  private Task buildTask() {
    Task endTask = (result, context) -> {
      if (result != null && result.failure != null && exceptionHandler != null) {
        exceptionHandler.handle(result.failure);
      }
      if (endHandler != null) {
        endHandler.handle(null);
      }
    };
    if (before != null) {
      HashMap attributes = new HashMap<>();
      return new TestContextTask(new TestContextImpl(attributes, exceptionHandler), before, result -> {
        if (result.failure == null) {
          Task runTask = buildTestCasesTasks(attributes, tests, 0, endTask);
          return (result_, context) -> runTask.execute(null, context);
        } else {
          return endTask;
        }
      }, 0);
    } else {
      return buildTestCasesTasks(new HashMap<>(), tests, 0, endTask);
    }
  }

  // For unit testing
  public void run(ExecutionContext context) {
    context.run(buildTask());
  }

  public void run(Boolean useEventLoop) {
    Context context = null;
    if (useEventLoop == null) {
      context = Vertx.currentContext();
    } else if (useEventLoop) {
      context = Vertx.currentContext();
      if (context == null) {
        throw new IllegalStateException("No event loop, your test should either provide a Vertx instance or " +
            "be executed in a Verticle");
      }
    }
    new ExecutionContext(context).run(buildTask());
  }

  public void run(Vertx vertx, Boolean useEventLoop) {
    Context context = Boolean.FALSE.equals(useEventLoop) ? null : vertx.getOrCreateContext();
    Task task = buildTask();
    new ExecutionContext(context).run(task);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy