protected boolean matchesSafely()

in future/src/main/java/com/spotify/hamcrest/future/ExceptionallyCompletedCompletionStage.java [44:75]


  protected boolean matchesSafely(
      final CompletionStage<?> stage, final Description mismatchDescription) {
    final CompletableFuture<?> future = stage.toCompletableFuture();
    if (future.isDone()) {
      if (future.isCancelled()) {
        mismatchDescription.appendText("a stage that was cancelled");
        return false;
      } else if (future.isCompletedExceptionally()) {
        try {
          future.getNow(null);
          throw new AssertionError(
              "This should never happen because the stage completed exceptionally.");
        } catch (CompletionException e) {
          if (matcher.matches(e.getCause())) {
            return true;
          } else {
            mismatchDescription.appendText("a stage completed exceptionally with ");
            matcher.describeMismatch(e.getCause(), mismatchDescription);
            return false;
          }
        }
      } else {
        mismatchDescription
            .appendText("a stage that completed to a value that was ")
            .appendValue(future.getNow(null));
        return false;
      }
    } else {
      mismatchDescription.appendText("a stage that was not completed");
      return false;
    }
  }