001 /* 002 * Created on Jan 26, 2011 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 @2011 the original author or authors. 014 */ 015 package org.fest.assertions.internal; 016 017 import static org.fest.assertions.error.ShouldBeExactlyInstanceOf.shouldBeExactlyInstance; 018 import static org.fest.assertions.error.ShouldContainString.shouldContain; 019 import static org.fest.assertions.error.ShouldEndWith.shouldEndWith; 020 import static org.fest.assertions.error.ShouldHaveMessage.shouldHaveMessage; 021 import static org.fest.assertions.error.ShouldHaveNoCause.shouldHaveNoCause; 022 import static org.fest.assertions.error.ShouldStartWith.shouldStartWith; 023 import static org.fest.util.Objects.areEqual; 024 025 import org.fest.assertions.core.AssertionInfo; 026 import org.fest.util.VisibleForTesting; 027 028 /** 029 * Reusable assertions for <code>{@link Throwable}</code>s. 030 * 031 * @author Joel Costigliola 032 */ 033 public class Throwables { 034 035 private static final Throwables INSTANCE = new Throwables(); 036 037 /** 038 * Returns the singleton instance of this class. 039 * @return the singleton instance of this class. 040 */ 041 public static Throwables instance() { 042 return INSTANCE; 043 } 044 045 @VisibleForTesting Diff diff = new Diff(); 046 @VisibleForTesting Failures failures = Failures.instance(); 047 @VisibleForTesting Throwables() {} 048 049 /** 050 * Asserts that the actual {@code Throwable} type is an the same as the given type.<br> 051 * @param info contains information about the assertion. 052 * @param actual the given {@code Throwable}. 053 * @param type the type to check the actual {@code Throwable} against. 054 * @throws AssertionError if the actual {@code Throwable} is {@code null}. 055 * @throws AssertionError if the actual {@code Throwable} is not an instance of the given type. 056 * @throws NullPointerException if the given type is {@code null}. 057 */ 058 public void assertIsExactlyInstanceOf(AssertionInfo info, Throwable actual, Class<? extends Throwable> type) { 059 assertNotNull(info, actual); 060 if (type == null) throw new NullPointerException("The given type should not be null"); 061 Class<?> current = actual.getClass(); 062 if (type.equals(current)) return; 063 throw failures.failure(info, shouldBeExactlyInstance(actual, type)); 064 } 065 066 /** 067 * Asserts that the given actual {@code Throwable} message is equal to the given one. 068 * @param info contains information about the assertion. 069 * @param actual the given {@code Throwable}. 070 * @param message the expected message. 071 * @throws AssertionError if the actual {@code Throwable} is {@code null}. 072 * @throws AssertionError if the message of the actual {@code Throwable} is not equal to the given one. 073 */ 074 public void assertHasMessage(AssertionInfo info, Throwable actual, String message) { 075 assertNotNull(info, actual); 076 if (areEqual(actual.getMessage(), message)) return; 077 throw failures.failure(info, shouldHaveMessage(actual, message)); 078 } 079 080 /** 081 * Asserts that the actual {@code Throwable} does not have a cause. 082 * @param info contains information about the assertion. 083 * @param actual the given {@code Throwable}. 084 * @throws AssertionError if the actual {@code Throwable} is {@code null}. 085 * @throws AssertionError if the actual {@code Throwable} has a cause. 086 */ 087 public void assertHasNoCause(AssertionInfo info, Throwable actual) { 088 assertNotNull(info, actual); 089 Throwable actualCause = actual.getCause(); 090 if (actualCause == null) return; 091 throw failures.failure(info, shouldHaveNoCause(actual)); 092 } 093 094 /** 095 * Asserts that the message of the actual {@code Throwable} starts with the given description. 096 * @param info contains information about the assertion. 097 * @param actual the given {@code Throwable}. 098 * @param description the description expected to start the actual {@code Throwable}'s message. 099 * @throws AssertionError if the actual {@code Throwable} is {@code null}. 100 * @throws AssertionError if the message of the actual {@code Throwable} does not start with the given description. 101 */ 102 public void assertHasMessageStartingWith(AssertionInfo info, Throwable actual, String description) { 103 assertNotNull(info, actual); 104 // TODO unit test with null exception message 105 if (actual.getMessage() != null && actual.getMessage().startsWith(description)) return; 106 throw failures.failure(info, shouldStartWith(actual.getMessage(), description)); 107 } 108 109 /** 110 * Asserts that the message of the actual {@code Throwable} contains with the given description. 111 * @param info contains information about the assertion. 112 * @param actual the given {@code Throwable}. 113 * @param description the description expected to be contained in the actual {@code Throwable}'s message. 114 * @throws AssertionError if the actual {@code Throwable} is {@code null}. 115 * @throws AssertionError if the message of the actual {@code Throwable} does not contain the given description. 116 */ 117 public void assertHasMessageContaining(AssertionInfo info, Throwable actual, String description) { 118 assertNotNull(info, actual); 119 if (actual.getMessage() != null && actual.getMessage().contains(description)) return; 120 throw failures.failure(info, shouldContain(actual.getMessage(), description)); 121 } 122 123 /** 124 * Asserts that the message of the actual {@code Throwable} ends with the given description. 125 * @param info contains information about the assertion. 126 * @param actual the given {@code Throwable}. 127 * @param description the description expected to end the actual {@code Throwable}'s message. 128 * @throws AssertionError if the actual {@code Throwable} is {@code null}. 129 * @throws AssertionError if the message of the actual {@code Throwable} does not end with the given description. 130 */ 131 public void assertHasMessageEndingWith(AssertionInfo info, Throwable actual, String description) { 132 assertNotNull(info, actual); 133 if (actual.getMessage() != null && actual.getMessage().endsWith(description)) return; 134 throw failures.failure(info, shouldEndWith(actual.getMessage(), description)); 135 } 136 137 138 private static void assertNotNull(AssertionInfo info, Throwable actual) { 139 Objects.instance().assertNotNull(info, actual); 140 } 141 }