|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Incendo |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +package org.incendo.cloud.truth; |
| 25 | + |
| 26 | +import com.google.common.truth.Fact; |
| 27 | +import com.google.common.truth.FailureMetadata; |
| 28 | +import com.google.common.truth.OptionalSubject; |
| 29 | +import com.google.common.truth.Subject; |
| 30 | +import com.google.common.truth.ThrowableSubject; |
| 31 | +import java.util.Optional; |
| 32 | +import java.util.concurrent.CompletableFuture; |
| 33 | +import java.util.concurrent.CompletionException; |
| 34 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 35 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 36 | + |
| 37 | +import static com.google.common.truth.Truth.assertAbout; |
| 38 | + |
| 39 | +/** |
| 40 | + * Subject for assertions about completable futures. Futures are join()ed to get |
| 41 | + * results or exceptions, and {@link CompletionException}'s cause is unwrapped to |
| 42 | + * {@link #failure()}. Any other exceptions thrown by join() are unexpected failures. |
| 43 | + * This class also does not properly handle futures where null is a successful result. |
| 44 | + * |
| 45 | + * @param <T> future result type |
| 46 | + */ |
| 47 | +public final class CompletableFutureSubject<T> extends Subject { |
| 48 | + |
| 49 | + public static <T> @NonNull Factory<CompletableFutureSubject<T>, CompletableFuture<? extends @NonNull T>> completableFuture() { |
| 50 | + return CompletableFutureSubject::new; |
| 51 | + } |
| 52 | + |
| 53 | + public static <T> @NonNull CompletableFutureSubject<T> assertThat( |
| 54 | + final @Nullable CompletableFuture<? extends @NonNull T> actual |
| 55 | + ) { |
| 56 | + return assertAbout(CompletableFutureSubject.<T>completableFuture()).that(actual); |
| 57 | + } |
| 58 | + |
| 59 | + private final CompletableFuture<? extends @NonNull T> actual; |
| 60 | + |
| 61 | + private CompletableFutureSubject( |
| 62 | + final @NonNull FailureMetadata metadata, |
| 63 | + final @Nullable CompletableFuture<? extends @NonNull T> actual |
| 64 | + ) { |
| 65 | + super(metadata, actual); |
| 66 | + this.actual = actual; |
| 67 | + } |
| 68 | + |
| 69 | + public @NonNull OptionalSubject result() { |
| 70 | + if (this.actual == null) { |
| 71 | + this.failWithActual(Fact.simpleFact("expected future to not be null")); |
| 72 | + } |
| 73 | + try { |
| 74 | + final T join = this.actual.join(); |
| 75 | + if (join == null) { |
| 76 | + this.failWithActual(Fact.simpleFact("expected result to not be null")); |
| 77 | + } |
| 78 | + return this.check("join() result or null on failure").about(OptionalSubject.optionals()).that(Optional.of(join)); |
| 79 | + } catch (final CompletionException e) { |
| 80 | + return this.check("join() result or null on failure").about(OptionalSubject.optionals()).that(Optional.empty()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public @NonNull OptionalSubject failure() { |
| 85 | + if (this.actual == null) { |
| 86 | + this.failWithActual(Fact.simpleFact("expected future to not be null")); |
| 87 | + } |
| 88 | + try { |
| 89 | + this.actual.join(); |
| 90 | + return this.check("cause of CompletionException thrown by join()").about(OptionalSubject.optionals()).that(Optional.empty()); |
| 91 | + } catch (final CompletionException e) { |
| 92 | + return this.check("cause of CompletionException thrown by join()").about(OptionalSubject.optionals()).that(Optional.of(e.getCause())); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void hasResult() { |
| 97 | + this.result().isPresent(); |
| 98 | + this.failure().isEmpty(); |
| 99 | + } |
| 100 | + |
| 101 | + public void hasResult(final @NonNull T value) { |
| 102 | + this.result().hasValue(value); |
| 103 | + this.failure().isEmpty(); |
| 104 | + } |
| 105 | + |
| 106 | + public void hasFailure(final @NonNull Throwable throwable) { |
| 107 | + this.failure().hasValue(throwable); |
| 108 | + this.result().isEmpty(); |
| 109 | + } |
| 110 | + |
| 111 | + public @NonNull ThrowableSubject hasFailureThat() { |
| 112 | + this.result().isEmpty(); |
| 113 | + this.failure().isPresent(); |
| 114 | + try { |
| 115 | + this.actual.join(); |
| 116 | + this.failWithActual(Fact.simpleFact("expected future to throw on join()")); |
| 117 | + throw new IllegalStateException(); |
| 118 | + } catch (final CompletionException e) { |
| 119 | + return this.check("cause of CompletionException thrown by join()").that(e.getCause()); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments