001 /**
002 * Copyright (C) 2011 rwoo@gmx.de
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package com.googlecode.catchexception.throwable.apis;
017
018 import com.googlecode.catchexception.throwable.CatchThrowable;
019
020 import org.fest.assertions.api.Assertions;
021 import org.fest.assertions.api.ThrowableAssert;
022
023 /**
024 * Supports <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a>-like approach to catch and verify
025 * throwables (<i>given/when/then</i>).
026 * <p>
027 * EXAMPLE: <code><pre class="prettyprint lang-java">// given an empty list
028 List myList = new ArrayList();
029
030 // when we try to get the first element of the list
031 when(myList).get(1);
032
033 // then we expect an IndexOutOfBoundsThrowable
034 then(caughtThrowable())
035 .isInstanceOf(IndexOutOfBoundsThrowable.class)
036 .hasMessage("Index: 1, Size: 0")
037 .hasNoCause();
038
039 // then we expect an IndexOutOfBoundsThrowable (alternatively)
040 thenThrown(IndexOutOfBoundsThrowable.class);
041 </pre></code>
042 *
043 * @author rwoo
044 * @since 1.2.0
045 * @see com.googlecode.catchexception.throwable.apis.BDDCatchThrowable
046 * @deprecated As of release 1.3.0, replaced by {@link com.googlecode.catchexception.throwable.apis.BDDCatchThrowable()}
047 *
048 */
049 @Deprecated
050 public class CatchThrowableBdd {
051
052 /**
053 *
054 * @param <T>
055 * The type of the given <code>obj</code>.
056 *
057 * @param obj
058 * The instance that shall be proxied. Must not be <code>null</code>.
059 * @return Returns a proxy for the given object. The proxy catches throwables of the given type when a method on the
060 * proxy is called.
061 * @see com.googlecode.catchexception.throwable.CatchThrowable#catchThrowable(Object)
062 */
063 public static <T> T when(T obj) {
064 return CatchThrowable.catchThrowable(obj);
065 }
066
067 /**
068 * Throws an assertion if no throwable is thrown or if an throwable of an unexpected type is thrown.
069 * <p>
070 * EXAMPLE:
071 * <code><pre class="prettyprint lang-java">// given a list with nine members
072 List myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
073
074 // when we try to get the 500th member of the fellowship
075 when(myList).get(500);
076
077 // then we expect an IndexOutOfBoundsThrowable
078 thenThrown(IndexOutOfBoundsThrowable.class);
079 </pre></code>
080 *
081 * @param actualThrowableClazz
082 * the expected type of the caught throwable.
083 */
084 @SuppressWarnings("rawtypes")
085 public static void thenThrown(Class actualThrowableClazz) {
086 CatchThrowableUtils.thenThrown(actualThrowableClazz);
087 }
088
089 /**
090 * Enables <a href="https://github.com/alexruiz/fest-assert-2.x">FEST Fluent Assertions 2.x</a> about the caught
091 * throwable.
092 * <p>
093 * EXAMPLE: <code><pre class="prettyprint lang-java">// given an empty list
094 List myList = new ArrayList();
095
096 // when we try to get first element of the list
097 when(myList).get(1);
098
099 // then we expect an IndexOutOfBoundsThrowable
100 then(caughtThrowable())
101 .isInstanceOf(IndexOutOfBoundsThrowable.class)
102 .hasMessage("Index: 1, Size: 0")
103 .hasMessageStartingWith("Index: 1")
104 .hasMessageEndingWith("Size: 0")
105 .hasMessageContaining("Size")
106 .hasNoCause();
107 </pre></code>
108 *
109 * @param actualThrowable
110 * the value to be the target of the assertions methods.
111 * @return Returns the created assertion object.
112 * @see org.fest.assertions.api.Assertions#assertThat(Throwable)
113 */
114 public static ThrowableAssert then(Throwable actualThrowable) {
115 // delegate to FEST assertions
116 return Assertions.assertThat(actualThrowable);
117 }
118
119 }