001 /* 002 * Created on Dec 23, 2007 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 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 @2007-2009 the original author or authors. 014 */ 015 package org.fest.assertions; 016 017 import static org.fest.assertions.Fail.failIfNotEqual; 018 import static org.fest.assertions.Formatting.inBrackets; 019 import static org.fest.util.Strings.concat; 020 021 /** 022 * Understands assertion methods for <code>{@link Throwable}</code>. To create a new instance of this class use the 023 * method <code>{@link Assertions#assertThat(Throwable)}</code>. 024 * 025 * @author David DIDIER 026 * @author Alex Ruiz 027 */ 028 public class ThrowableAssert extends GenericAssert<Throwable> { 029 030 // TODO remove dependency on ObjectAssert. 031 private final ObjectAssert objectAssert; 032 033 /** 034 * Creates a new <code>ThrowableAssert</code>. 035 * @param actual the target to verify. 036 */ 037 protected ThrowableAssert(Throwable actual) { 038 super(actual); 039 objectAssert = new ObjectAssert(actual); 040 } 041 042 /** {@inheritDoc} */ 043 public ThrowableAssert as(String description) { 044 objectAssert.as(description); 045 description(description); 046 return this; 047 } 048 049 /** {@inheritDoc} */ 050 public ThrowableAssert describedAs(String description) { 051 return as(description); 052 } 053 054 /** {@inheritDoc} */ 055 public ThrowableAssert as(Description description) { 056 objectAssert.as(description); 057 description(description); 058 return this; 059 } 060 061 /** {@inheritDoc} */ 062 public ThrowableAssert describedAs(Description description) { 063 return as(description); 064 } 065 066 /** 067 * Verifies that the actual <code>Throwable</code> is an instance of the given type. 068 * @param type the type to check the actual <code>Throwable</code> against. 069 * @return this assertion object. 070 * @throws AssertionError if the actual <code>Throwable</code> is <code>null</code>. 071 * @throws AssertionError if the actual <code>Throwable</code> is not an instance of the given type. 072 * @throws NullPointerException if the given type is <code>null</code>. 073 */ 074 public ThrowableAssert isInstanceOf(Class<? extends Throwable> type) { 075 objectAssert.isInstanceOf(type); 076 return this; 077 } 078 079 /** 080 * Verifies that the actual <code>Throwable</code> is an instance of the given type. In order for the assertion to 081 * pass, the type of the actual <code>Throwable</code> has to be exactly the same as the given type. 082 * @param type the type to check the actual <code>Throwable</code> against. 083 * @return this assertion object. 084 * @throws AssertionError if the actual <code>Throwable</code> is <code>null</code>. 085 * @throws AssertionError if the actual <code>Throwable</code> is not an instance of the given type. 086 * @throws NullPointerException if the given type is <code>null</code>. 087 */ 088 public ThrowableAssert isExactlyInstanceOf(Class<?> type) { 089 isNotNull(); 090 objectAssert.validateNotNull(type); 091 Class<?> current = actual.getClass(); 092 if (type.equals(current)) return this; 093 failIfCustomMessageIsSet(); 094 throw failure(concat("expected exactly the same type:", inBrackets(type), " but was:", inBrackets(current))); 095 } 096 097 /** 098 * Verifies that the message of the actual <code>Throwable</code> is equal to the given one. 099 * @param message the expected message. 100 * @return this assertion error. 101 * @throws AssertionError if the actual <code>Throwable</code> is <code>null</code>. 102 * @throws AssertionError if the message of the actual <code>Throwable</code> is not equal to the given one. 103 */ 104 public ThrowableAssert hasMessage(String message) { 105 isNotNull(); 106 failIfNotEqual(customErrorMessage(), rawDescription(), actual.getMessage(), message); 107 return this; 108 } 109 110 /** 111 * Verifies that the actual <code>Throwable</code> does not have a cause. 112 * @return this assertion object. 113 * @throws AssertionError if the actual <code>Throwable</code> is <code>null</code>. 114 * @throws AssertionError if the actual <code>Throwable</code> has a cause. 115 */ 116 public ThrowableAssert hasNoCause() { 117 isNotNull(); 118 Throwable actualCause = actual.getCause(); 119 if (actualCause == null) return this; 120 failIfCustomMessageIsSet(); 121 throw failure(concat("expected exception without cause, but cause was:", inBrackets(actualCause.getClass()))); 122 } 123 124 /** 125 * Verifies that the actual <code>Throwable</code> is equal to the given one. 126 * @param expected the given <code>Throwable</code> to compare the actual <code>Throwable</code> to. 127 * @return this assertion object. 128 * @throws AssertionError if the actual <code>Throwable</code> is not equal to the given one. 129 */ 130 public ThrowableAssert isEqualTo(Throwable expected) { 131 assertEqualTo(expected); 132 return this; 133 } 134 135 /** 136 * Verifies that the actual <code>Throwable</code> is not equal to the given one. 137 * @param other the given <code>Throwable</code> to compare the actual <code>Throwable</code> to. 138 * @return this assertion object. 139 * @throws AssertionError if the actual <code>Throwable</code> is equal to the given one. 140 */ 141 public ThrowableAssert isNotEqualTo(Throwable other) { 142 assertNotEqualTo(other); 143 return this; 144 } 145 146 /** 147 * Verifies that the actual <code>Throwable</code> is not <code>null</code>. 148 * 149 * @return this assertion object. 150 * 151 * @throws AssertionError if the actual <code>Throwable</code> is <code>null</code>. 152 */ 153 public ThrowableAssert isNotNull() { 154 assertNotNull(); 155 return this; 156 } 157 158 /** 159 * Verifies that the actual <code>Throwable</code> is not the same as the given one. 160 * @param other the given <code>Throwable</code> to compare the actual <code>Throwable</code> to. 161 * @return this assertion object. 162 * @throws AssertionError if the actual <code>Throwable</code> is the same as the given one. 163 */ 164 public ThrowableAssert isNotSameAs(Throwable other) { 165 assertNotSameAs(other); 166 return this; 167 } 168 169 /** 170 * Verifies that the actual <code>Throwable</code> is the same as the given one. 171 * @param expected the given <code>Throwable</code> to compare the actual <code>Throwable</code> to. 172 * @return this assertion object. 173 * @throws AssertionError if the actual <code>Throwable</code> is not the same as the given one. 174 */ 175 public ThrowableAssert isSameAs(Throwable expected) { 176 assertSameAs(expected); 177 return this; 178 } 179 180 /** 181 * Verifies that the actual <code>Throwable</code> satisfies the given condition. 182 * @param condition the given condition. 183 * @return this assertion object. 184 * @throws NullPointerException if the given condition is <code>null</code>. 185 * @throws AssertionError if the actual <code>Throwable</code> does not satisfy the given condition. 186 * @see #is(Condition) 187 */ 188 public ThrowableAssert satisfies(Condition<Throwable> condition) { 189 assertSatisfies(condition); 190 return this; 191 } 192 193 /** 194 * Verifies that the actual <code>Throwable</code> does not satisfy the given condition. 195 * @param condition the given condition. 196 * @return this assertion object. 197 * @throws NullPointerException if the given condition is <code>null</code>. 198 * @throws AssertionError if the actual <code>Throwable</code> satisfies the given condition. 199 * @see #isNot(Condition) 200 */ 201 public ThrowableAssert doesNotSatisfy(Condition<Throwable> condition) { 202 assertDoesNotSatisfy(condition); 203 return this; 204 } 205 206 207 /** 208 * Alias for <code>{@link #satisfies(Condition)}</code>. 209 * @param condition the given condition. 210 * @return this assertion object. 211 * @throws NullPointerException if the given condition is <code>null</code>. 212 * @throws AssertionError if the actual <code>Throwable</code> does not satisfy the given condition. 213 * @since 1.2 214 */ 215 public ThrowableAssert is(Condition<Throwable> condition) { 216 assertIs(condition); 217 return this; 218 } 219 220 /** 221 * Alias for <code>{@link #doesNotSatisfy(Condition)}</code>. 222 * @param condition the given condition. 223 * @return this assertion object. 224 * @throws NullPointerException if the given condition is <code>null</code>. 225 * @throws AssertionError if the actual <code>Throwable</code> satisfies the given condition. 226 * @since 1.2 227 */ 228 public ThrowableAssert isNot(Condition<Throwable> condition) { 229 assertIsNot(condition); 230 return this; 231 } 232 233 /** {@inheritDoc} */ 234 public ThrowableAssert overridingErrorMessage(String message) { 235 replaceDefaultErrorMessagesWith(message); 236 objectAssert.overridingErrorMessage(message); 237 return this; 238 } 239 }