Base class for all assertions. @param the "self" type of this assertion class. Please read " Emulating 'self types' using Java Generics to simplify fluent API implementation " for more details. @param the type of t
| 43 | * @author Nicolas François |
| 44 | */ |
| 45 | public abstract class AbstractAssert<S extends AbstractAssert<S, A>, A> implements Assert<S, A> { |
| 46 | |
| 47 | @VisibleForTesting |
| 48 | Objects objects = Objects.instance(); |
| 49 | |
| 50 | @VisibleForTesting |
| 51 | Conditions conditions = Conditions.instance(); |
| 52 | |
| 53 | @VisibleForTesting |
| 54 | protected final WritableAssertionInfo info; |
| 55 | |
| 56 | // visibility is protected to allow us write custom assertions that need access to actual |
| 57 | @VisibleForTesting |
| 58 | protected final A actual; |
| 59 | protected final S myself; |
| 60 | |
| 61 | // we prefer not to use Class<? extends S> selfType because it would force inherited |
| 62 | // constructor to cast with a compiler warning |
| 63 | // let's keep compiler warning internal (when we can) and not expose them to our end users. |
| 64 | @SuppressWarnings("unchecked") |
| 65 | protected AbstractAssert(A actual, Class<?> selfType) { |
| 66 | myself = (S) selfType.cast(this); |
| 67 | this.actual = actual; |
| 68 | info = new WritableAssertionInfo(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Exposes the {@link WritableAssertionInfo} used in the current assertion for better extensibility.</br> When writing |
| 73 | * your own assertion class, you can use the returned {@link WritableAssertionInfo} to change the error message and |
| 74 | * still keep the description set by the assertion user. |
| 75 | * |
| 76 | * @return the {@link WritableAssertionInfo} used in the current assertion |
| 77 | */ |
| 78 | protected WritableAssertionInfo getWritableAssertionInfo() { |
| 79 | return info; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Utility method to ease writing custom assertions classes, you can use format specifiers in error message, they |
| 84 | * will be replaced by the given arguments. |
| 85 | * <p> |
| 86 | * Moreover, this method honors any description ({@link #as(String, Object...)} or overridden error message defined by |
| 87 | * the user ( {@link #overridingErrorMessage(String, Object...)}. |
| 88 | * <p> |
| 89 | * Example : |
| 90 | * |
| 91 | * <pre><code class='java'> |
| 92 | * public TolkienCharacterAssert hasName(String name) { |
| 93 | * // check that actual TolkienCharacter we want to make assertions on is not null. |
| 94 | * isNotNull(); |
| 95 | * |
| 96 | * // check condition |
| 97 | * if (!actual.getName().equals(name)) { |
| 98 | * failWithMessage("Expected character's name to be <%s> but was <%s>", name, actual.getName()); |
| 99 | * } |
| 100 | * |
| 101 | * // return the current assertion for method chaining |
| 102 | * return this; |