001 /*
002 * Created on May 21, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2009 the original author or authors.
015 */
016 package org.fest.assertions;
017
018 import static org.fest.assertions.Fail.*;
019 import static org.fest.assertions.Formatting.inBrackets;
020 import static org.fest.util.Strings.concat;
021
022 /**
023 * Understands a template for assertion methods.
024 * @param <T> the type of object implementations of this template can verify.
025 *
026 * @author Yvonne Wang
027 * @author Alex Ruiz
028 */
029 public abstract class GenericAssert<T> extends Assert {
030
031 protected final T actual;
032
033 /**
034 * Creates a new <code>{@link GenericAssert}</code>.
035 * @param actual the actual target to verify.
036 */
037 protected GenericAssert(T actual) {
038 this.actual = actual;
039 }
040
041 /**
042 * Asserts that the actual value (specified in the constructor of this class) is <code>null</code>.
043 * @throws AssertionError if the actual value is not <code>null</code>.
044 */
045 public final void isNull() {
046 failIfNotNull(customErrorMessage(), rawDescription(), actual);
047 }
048
049 /**
050 * Verifies that the actual value satisfies the given condition.
051 * @param condition the given condition.
052 * @return this assertion object.
053 * @throws NullPointerException if the given condition is <code>null</code>.
054 * @throws AssertionError if the actual value does not satisfy the given condition.
055 * @see #is(Condition)
056 */
057 protected abstract GenericAssert<T> satisfies(Condition<T> condition);
058
059 /**
060 * Verifies that the actual value does not satisfy the given condition.
061 * @param condition the given condition.
062 * @return this assertion object.
063 * @throws NullPointerException if the given condition is <code>null</code>.
064 * @throws AssertionError if the actual value does satisfies the given condition.
065 * @see #isNot(Condition)
066 */
067 protected abstract GenericAssert<T> doesNotSatisfy(Condition<T> condition);
068
069 /**
070 * Alias for <code>{@link #satisfies(Condition)}</code>.
071 * @param condition the given condition.
072 * @return this assertion object.
073 * @throws NullPointerException if the given condition is <code>null</code>.
074 * @throws AssertionError if the actual value does not satisfy the given condition.
075 * @since 1.2
076 */
077 protected abstract GenericAssert<T> is(Condition<T> condition);
078
079 /**
080 * Alias for <code>{@link #doesNotSatisfy(Condition)}</code>.
081 * @param condition the given condition.
082 * @return this assertion object.
083 * @throws NullPointerException if the given condition is <code>null</code>.
084 * @throws AssertionError if the actual value does satisfies the given condition.
085 * @since 1.2
086 */
087 protected abstract GenericAssert<T> isNot(Condition<T> condition);
088
089 /**
090 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
091 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
092 * failure will not show the provided description.
093 * <p>
094 * For example:
095 * <pre>
096 * assertThat(val).<strong>as</strong>("name").isEqualTo("Frodo");
097 * </pre>
098 * </p>
099 * @param description the description of the actual value.
100 * @return this assertion object.
101 */
102 protected abstract GenericAssert<T> as(String description);
103
104 /**
105 * Alias for <code>{@link #as(String)}</code>, since "as" is a keyword in
106 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
107 * method, otherwise any assertion failure will not show the provided description.
108 * <p>
109 * For example:
110 * <pre>
111 * assertThat(val).<strong>describedAs</strong>("name").isEqualTo("Frodo");
112 * </pre>
113 * </p>
114 * @param description the description of the actual value.
115 * @return this assertion object.
116 */
117 protected abstract GenericAssert<T> describedAs(String description);
118
119 /**
120 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
121 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
122 * failure will not show the provided description.
123 * <p>
124 * For example:
125 * <pre>
126 * assertThat(val).<strong>as</strong>(new BasicDescription("name")).isEqualTo("Frodo");
127 * </pre>
128 * </p>
129 * @param description the description of the actual value.
130 * @return this assertion object.
131 */
132 protected abstract GenericAssert<T> as(Description description);
133
134 /**
135 * Alias for <code>{@link #as(Description)}</code>, since "as" is a keyword in
136 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
137 * method, otherwise any assertion failure will not show the provided description.
138 * <p>
139 * For example:
140 * <pre>
141 * assertThat(val).<strong>describedAs</strong>(new BasicDescription("name")).isEqualTo("Frodo");
142 * </pre>
143 * </p>
144 * @param description the description of the actual value.
145 * @return this assertion object.
146 */
147 protected abstract GenericAssert<T> describedAs(Description description);
148
149 /**
150 * Verifies that the actual value is equal to the given one.
151 * @param expected the given value to compare the actual value to.
152 * @return this assertion object.
153 * @throws AssertionError if the actual value is not equal to the given one.
154 */
155 protected abstract GenericAssert<T> isEqualTo(T expected);
156
157 /**
158 * Verifies that the actual value is not equal to the given one.
159 * @param other the given value to compare the actual value to.
160 * @return this assertion object.
161 * @throws AssertionError if the actual value is equal to the given one.
162 */
163 protected abstract GenericAssert<T> isNotEqualTo(T other);
164
165 /**
166 * Verifies that the actual value is not <code>null</code>.
167 * @return this assertion object.
168 * @throws AssertionError if the actual value is <code>null</code>.
169 */
170 protected abstract GenericAssert<T> isNotNull();
171
172 /**
173 * Verifies that the actual value is the same as the given one.
174 * @param expected the given value to compare the actual value to.
175 * @return this assertion object.
176 * @throws AssertionError if the actual value is not the same as the given one.
177 */
178 protected abstract GenericAssert<T> isSameAs(T expected);
179
180 /**
181 * Verifies that the actual value is not the same as the given one.
182 * @param other the given value to compare the actual value to.
183 * @return this assertion object.
184 * @throws AssertionError if the actual value is the same as the given one.
185 */
186 protected abstract GenericAssert<T> isNotSameAs(T other);
187
188 /**
189 * Verifies that the actual value satisfies the given condition.
190 * @param condition the condition to check.
191 * @throws NullPointerException if the given condition is <code>null</code>.
192 * @throws AssertionError if the actual value does not satisfy the given condition.
193 */
194 protected final void assertSatisfies(Condition<T> condition) {
195 if (matches(condition)) return;
196 failIfCustomMessageIsSet();
197 fail(errorMessageIfConditionNotSatisfied(condition));
198 }
199
200 private String errorMessageIfConditionNotSatisfied(Condition<T> condition) {
201 String message = concat("actual value:", inBrackets(actual), " should satisfy condition");
202 return condition.addDescriptionTo(message);
203 }
204
205 /**
206 * Verifies that the actual value satisfies the given condition.
207 * @param condition the condition to check.
208 * @throws NullPointerException if the given condition is <code>null</code>.
209 * @throws AssertionError if the actual value does not satisfy the given condition.
210 */
211 protected final void assertIs(Condition<T> condition) {
212 if (matches(condition)) return;
213 failIfCustomMessageIsSet();
214 fail(errorMessageIfIsNot(condition));
215 }
216
217 private String errorMessageIfIsNot(Condition<T> condition) {
218 String message = concat("actual value:", inBrackets(actual), " should be");
219 return condition.addDescriptionTo(message);
220 }
221
222 /**
223 * Verifies that the actual value does not satisfy the given condition.
224 * @param condition the condition to check.
225 * @throws NullPointerException if the given condition is <code>null</code>.
226 * @throws AssertionError if the actual value satisfies the given condition.
227 */
228 protected final void assertDoesNotSatisfy(Condition<T> condition) {
229 if (!matches(condition)) return;
230 failIfCustomMessageIsSet();
231 fail(errorMessageIfConditionSatisfied(condition));
232 }
233
234 private String errorMessageIfConditionSatisfied(Condition<T> condition) {
235 String message = concat("actual value:", inBrackets(actual), " should not satisfy condition");
236 return condition.addDescriptionTo(message);
237 }
238
239 /**
240 * Verifies that the actual value does not satisfy the given condition.
241 * @param condition the condition to check.
242 * @throws NullPointerException if the given condition is <code>null</code>.
243 * @throws AssertionError if the actual value satisfies the given condition.
244 */
245 protected final void assertIsNot(Condition<T> condition) {
246 if (!matches(condition)) return;
247 failIfCustomMessageIsSet();
248 fail(errorMessageIfIs(condition));
249 }
250
251 private boolean matches(Condition<T> condition) {
252 validateIsNotNull(condition);
253 return condition.matches(actual);
254 }
255
256 private void validateIsNotNull(Condition<T> condition) {
257 if (condition == null) throw new NullPointerException("Condition to check should be null");
258 }
259
260 private String errorMessageIfIs(Condition<T> condition) {
261 String message = concat("actual value:", inBrackets(actual), " should not be");
262 return condition.addDescriptionTo(message);
263 }
264
265 /**
266 * Verifies that the actual value is equal to the given one.
267 * @param expected the value to compare the actual value to.
268 * @throws AssertionError if the actual value is not equal to the given one.
269 */
270 protected final void assertEqualTo(T expected) {
271 failIfNotEqual(customErrorMessage(), rawDescription(), actual, expected);
272 }
273
274 /**
275 * Verifies that the actual value is not equal to the given one.
276 * @param other the value to compare the actual value to.
277 * @throws AssertionError if the actual value is equal to the given one.
278 */
279 protected final void assertNotEqualTo(T other) {
280 failIfEqual(customErrorMessage(), rawDescription(), actual, other);
281 }
282
283 /**
284 * Verifies that the actual value is not <code>null</code>.
285 * @throws AssertionError if the actual value is <code>null</code>.
286 */
287 protected final void assertNotNull() {
288 failIfNull(customErrorMessage(), rawDescription(), actual);
289 }
290
291 /**
292 * Verifies that the actual value is the same as the given one.
293 * @param expected the value to compare the actual value to.
294 * @throws AssertionError if the actual value is not the same as the given one.
295 */
296 protected final void assertSameAs(T expected) {
297 failIfNotSame(customErrorMessage(), rawDescription(), actual, expected);
298 }
299
300 /**
301 * Verifies that the actual value is not the same as the given one.
302 * @param expected the value to compare the actual value to.
303 * @throws AssertionError if the actual value is the same as the given one.
304 */
305 protected final void assertNotSameAs(T expected) {
306 failIfSame(customErrorMessage(), rawDescription(), actual, expected);
307 }
308
309 /**
310 * Replaces the default message displayed in case of a failure with the given one.
311 * <p>
312 * For example, the following assertion:
313 * <pre>
314 * assertThat("Hello").isEqualTo("Bye");
315 * </pre>
316 * will fail with the default message "<em>expected:<'[Bye]'> but was:<'[Hello]'></em>."
317 * </p>
318 * <p>
319 * We can replace this message with our own:
320 * <pre>
321 * assertThat("Hello").overridingErrorMessage("'Hello' should be equal to 'Bye'").isEqualTo("Bye");
322 * </pre>
323 * in this case, the assertion will fail showing the message "<em>'Hello' should be equal to 'Bye'</em>".
324 * </p>
325 * @param message the given error message, which will replace the default one.
326 * @return this assertion.
327 * @since 1.2
328 */
329 protected abstract GenericAssert<T> overridingErrorMessage(String message);
330 }