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

io.vertx.ext.unit.impl.TestCompletionImpl 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.TestCompletion;
import io.vertx.ext.unit.report.TestSuiteReport;
import io.vertx.ext.unit.report.Reporter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Julien Viet
 */
public class TestCompletionImpl extends CompletionImpl implements TestCompletion, Handler {

  private final AtomicReference report = new AtomicReference<>();
  private final List reporters = Collections.synchronizedList(new ArrayList<>());
  private final AtomicReference failure = new AtomicReference<>();

  public TestCompletionImpl(Reporter... reporters) {
    Collections.addAll(this.reporters, reporters);
  }

  public void addReporter(Reporter reporter) {
    reporters.add(reporter);
  }

  @Override
  public void handle(TestSuiteReport report) {
    Reporter[] reporters = this.reporters.toArray(new Reporter[this.reporters.size()]);
    Object[] reports = new Object[reporters.length];
    for (int i = 0;i < reporters.length;i++) {
      reports[i] = reporters[i].reportBeginTestSuite(report.name());
    }
    report.handler(testcase -> {
      for (int i = 0; i < reporters.length; i++) {
        reporters[i].reportBeginTestCase(reports[i], testcase.name());
      }
      testcase.endHandler(result -> {
        if (result.failed()) {
          failure.compareAndSet(null, result.failure().cause());
        }
        for (int i = 0; i < reporters.length; i++) {
          reporters[i].reportEndTestCase(reports[i], testcase.name(), result);
        }
      });
    });
    AtomicReference err = new AtomicReference<>();
    report.exceptionHandler(t -> {
      failure.compareAndSet(null, t);
      err.set(t);
      for (int i = 0; i < reporters.length; i++) {
        reporters[i].reportError(reports[i], t);
      }
    });
    report.endHandler(v -> {
      this.report.set(report);
      for (int i = 0; i < reporters.length; i++) {
        reporters[i].reportEndTestSuite(reports[i]);
      }
      if (failure.get() != null) {
        completable.completeExceptionally(failure.get());
      } else {
        completable.complete(null);
      }
    });
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy