View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2015-2016 ForgeRock AS.
15   */
16  
17  package org.forgerock.util.test.assertj;
18  
19  import java.util.concurrent.ExecutionException;
20  
21  import org.assertj.core.api.AbstractAssert;
22  import org.assertj.core.api.AbstractThrowableAssert;
23  import org.assertj.core.api.Assertions;
24  import org.forgerock.util.promise.Promise;
25  
26  /**
27   * Assertion class for a promise. Allows verification of the value that was completed with.
28   * @param <T> The promised type.
29   * @param <A> The type of assert that this class is.
30   * @param <S> The type of assert that is returned from the succeeded method.
31   */
32  //@Checkstyle:ignoreFor 2
33  public abstract class AbstractAssertJPromiseAssert<T, A extends AbstractAssertJPromiseAssert<T, A, S>, S extends AbstractAssert<S, T>>
34          extends AbstractAssert<A, Promise<T, ?>> {
35  
36      /**
37       * Constructs a new assertion on promise.
38       * @param promise the actual promise to check
39       * @param type the type of assertion
40       */
41      protected AbstractAssertJPromiseAssert(Promise<T, ?> promise, Class<A> type) {
42          super(promise, type);
43      }
44  
45      /**
46       * Factory method for the succeeded assert class.
47       * @param actual The promised value.
48       * @return The {@link AbstractAssert} implementation.
49       */
50      protected abstract S createSucceededAssert(T actual);
51  
52      /**
53       * Asserts that the promise succeeded.
54       * @return An {@link AbstractAssert} for making assertions on the promise's completed value.
55       */
56      public final S succeeded() {
57          isNotNull();
58          if (!actual.isDone()) {
59              failWithMessage("Promise is not completed");
60          }
61          T result = null;
62          try {
63              result = actual.get();
64          } catch (InterruptedException e) {
65              failWithMessage("Promise was interrupted");
66          } catch (ExecutionException e) {
67              failWithMessage("Promise failed: <%s>", e.getCause());
68          }
69          return createSucceededAssert(result);
70      }
71  
72      /**
73       * Asserts that the promise failed.
74       * @return A {@link org.assertj.core.api.ThrowableAssert} for making
75       * assertions on the promise's failure cause.
76       */
77      public final AbstractThrowableAssert<?, ? extends Throwable> failedWithException() {
78          isNotNull();
79          try {
80              Object value = actual.get();
81              failWithMessage("Promise succeeded with value <%s>", value);
82          } catch (InterruptedException e) {
83              failWithMessage("Promise was interrupted");
84          } catch (ExecutionException e) {
85              return Assertions.assertThat(e.getCause());
86          }
87          throw new IllegalStateException("Shouldn't have reached here");
88      }
89  
90  }