001 /*
002 * Created on Jan 28, 2011
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this Throwable except in compliance with
005 * 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 is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2011 the original author or authors.
014 */
015 package org.fest.assertions.api;
016
017 import org.fest.assertions.internal.*;
018 import org.fest.util.VisibleForTesting;
019
020 /**
021 * Assertion methods for <code>{@link Throwable}</code>s.
022 * <p>
023 * To create a new instance of this class, invoke <code>{@link Assertions#assertThat(Throwable)}</code>.
024 * </p>
025 *
026 * @author David DIDIER
027 * @author Alex Ruiz
028 * @author Joel Costigliola
029 */
030 public class ThrowableAssert extends AbstractAssert<ThrowableAssert, Throwable> {
031
032 @VisibleForTesting Throwables throwables = Throwables.instance();
033
034 protected ThrowableAssert(Throwable actual) {
035 super(actual, ThrowableAssert.class);
036 }
037
038 /**
039 * Verifies that the actual {@code Throwable} is an instance of the given type.
040 * @param type the type to check the actual {@code Throwable} against.
041 * @return this assertion object.
042 * @throws AssertionError if the actual {@code Throwable} is {@code null}.
043 * @throws AssertionError if the actual {@code Throwable} is not an instance of the given type.
044 * @throws NullPointerException if the given type is {@code null}.
045 */
046 public ThrowableAssert isInstanceOf(Class<? extends Throwable> type) {
047 Objects.instance().assertIsInstanceOf(info, actual, type);
048 return this;
049 }
050
051 /**
052 * Verifies that the actual {@code Throwable} is an instance of the given type. In order for the assertion to
053 * pass, the type of the actual {@code Throwable} has to be exactly the same as the given type.
054 * @param type the type to check the actual {@code Throwable} against.
055 * @return this assertion object.
056 * @throws AssertionError if the actual {@code Throwable} is {@code null}.
057 * @throws AssertionError if the actual {@code Throwable} is not an instance of the given type.
058 * @throws NullPointerException if the given type is {@code null}.
059 */
060 public ThrowableAssert isExactlyInstanceOf(Class<? extends Throwable> type) {
061 throwables.assertIsExactlyInstanceOf(info, actual, type);
062 return this;
063 }
064
065 /**
066 * Verifies that the message of the actual {@code Throwable} is equal to the given one.
067 * @param message the expected message.
068 * @return this assertion object.
069 * @throws AssertionError if the actual {@code Throwable} is {@code null}.
070 * @throws AssertionError if the message of the actual {@code Throwable} is not equal to the given one.
071 */
072 public ThrowableAssert hasMessage(String message) {
073 throwables.assertHasMessage(info, actual, message);
074 return this;
075 }
076
077 /**
078 * Verifies that the actual {@code Throwable} does not have a cause.
079 * @return this assertion object.
080 * @throws AssertionError if the actual {@code Throwable} is {@code null}.
081 * @throws AssertionError if the actual {@code Throwable} has a cause.
082 */
083 public ThrowableAssert hasNoCause() {
084 throwables.assertHasNoCause(info, actual);
085 return this;
086 }
087
088 /**
089 * Verifies that the message of the actual {@code Throwable} starts with the given description.
090 * @param description the description expected to start the actual {@code Throwable}'s message.
091 * @return this assertion object.
092 * @throws AssertionError if the actual {@code Throwable} is {@code null}.
093 * @throws AssertionError if the message of the actual {@code Throwable} does not start with the given description.
094 */
095 public ThrowableAssert hasMessageStartingWith(String description) {
096 throwables.assertHasMessageStartingWith(info, actual, description);
097 return this;
098 }
099
100 /**
101 * Verifies that the message of the actual {@code Throwable} contains with the given description.
102 * @param description the description expected to be contained in the actual {@code Throwable}'s message.
103 * @return this assertion object.
104 * @throws AssertionError if the actual {@code Throwable} is {@code null}.
105 * @throws AssertionError if the message of the actual {@code Throwable} does not contain the given description.
106 */
107 public ThrowableAssert hasMessageContaining(String description) {
108 throwables.assertHasMessageContaining(info, actual, description);
109 return this;
110 }
111
112 /**
113 * Verifies that the message of the actual {@code Throwable} ends with the given description.
114 * @param description the description expected to end the actual {@code Throwable}'s message.
115 * @return this assertion object.
116 * @throws AssertionError if the actual {@code Throwable} is {@code null}.
117 * @throws AssertionError if the message of the actual {@code Throwable} does not end with the given description.
118 */
119 public ThrowableAssert hasMessageEndingWith(String description) {
120 throwables.assertHasMessageEndingWith(info, actual, description);
121 return this;
122 }
123
124 }