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