001    /*
002     * Created on Jul 15, 2010
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 @2010-2011 the original author or authors.
014     */
015    package org.fest.assertions.core;
016    
017    import java.util.Comparator;
018    
019    /**
020     * Base contract of all assertion objects: the minimum functionality that any assertion object should provide.
021     * @param <S> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/anMa4g"
022     *          target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
023     *          for more details.
024     * @param <A> the type of the "actual" value.
025     * 
026     * @author Yvonne Wang
027     * @author Alex Ruiz
028     * @author Nicolas François
029     */
030    public interface Assert<S, A> extends Descriptable<S>, ExtensionPoints<S, A> {
031    
032      /**
033       * Verifies that the actual value is equal to the given one.
034       * @param expected the given value to compare the actual value to.
035       * @return {@code this} assertion object.
036       * @throws AssertionError if the actual value is not equal to the given one.
037       */
038      S isEqualTo(A expected);
039    
040      /**
041       * Verifies that the actual value is not equal to the given one.
042       * @param other the given value to compare the actual value to.
043       * @return {@code this} assertion object.
044       * @throws AssertionError if the actual value is equal to the given one.
045       */
046      S isNotEqualTo(A other);
047    
048      /**
049       * Verifies that the actual value is {@code null}.
050       * @throws AssertionError if the actual value is not {@code null}.
051       */
052      void isNull();
053    
054      /**
055       * Verifies that the actual value is not {@code null}.
056       * @return {@code this} assertion object.
057       * @throws AssertionError if the actual value is {@code null}.
058       */
059      S isNotNull();
060    
061      /**
062       * Verifies that the actual value is the same as the given one.
063       * @param expected the given value to compare the actual value to.
064       * @return {@code this} assertion object.
065       * @throws AssertionError if the actual value is not the same as the given one.
066       */
067      S isSameAs(A expected);
068    
069      /**
070       * Verifies that the actual value is not the same as the given one.
071       * @param other the given value to compare the actual value to.
072       * @return {@code this} assertion object.
073       * @throws AssertionError if the actual value is the same as the given one.
074       */
075      S isNotSameAs(A other);
076    
077      /**
078       * Verifies that the actual value is present in the given array of values.
079       * @param values the given array to search the actual value in.
080       * @return {@code this} assertion object.
081       * @throws NullPointerException if the given array is {@code null}.
082       * @throws IllegalArgumentException if the given array is empty.
083       * @throws AssertionError if the actual value is not present in the given array.
084       */
085      S isIn(A... values);
086    
087      /**
088       * Verifies that the actual value is not present in the given array of values.
089       * @param values the given array to search the actual value in.
090       * @return {@code this} assertion object.
091       * @throws NullPointerException if the given array is {@code null}.
092       * @throws IllegalArgumentException if the given array is empty.
093       * @throws AssertionError if the actual value is present in the given array.
094       */
095      S isNotIn(A... values);
096    
097      /**
098       * Verifies that the actual value is present in the given values.
099       * @param values the given iterable to search the actual value in.
100       * @return {@code this} assertion object.
101       * @throws NullPointerException if the given collection is {@code null}.
102       * @throws IllegalArgumentException if the given collection is empty.
103       * @throws AssertionError if the actual value is not present in the given collection.
104       */
105      S isIn(Iterable<? extends A> values);
106    
107      /**
108       * Verifies that the actual value is not present in the given values.
109       * @param values the given iterable to search the actual value in.
110       * @return {@code this} assertion object.
111       * @throws NullPointerException if the given collection is {@code null}.
112       * @throws IllegalArgumentException if the given collection is empty.
113       * @throws AssertionError if the actual value is present in the given collection.
114       */
115      S isNotIn(Iterable<? extends A> values);
116    
117      /**
118       * Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.<br>
119       * Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default
120       * comparison strategy. </p> Example :
121       * 
122       * <pre>
123       * // compares invoices by payee 
124       * assertThat(invoiceList).usingComparator(invoicePayeeComparator).isEqualTo(expectedInvoiceList).
125       * 
126       * // compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator
127       * assertThat(invoiceList).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice)
128       * 
129       * // as assertThat(invoiceList) creates a new assertion, it uses standard comparison strategy (Invoice's equal method) to compare invoiceList elements to lowestInvoice.                                                      
130       * assertThat(invoiceList).contains(lowestInvoice).
131       * </pre>
132       * 
133       * Custom comparator is not parameterized with actual type A (ie. Comparator&lt;A&gt;) because if it was, we could not
134       * write the following code :
135       * 
136       * <pre>
137       * // frodo and sam are instances of Character (a Character having a Race)
138       * // raceComparator implements Comparator&lt;Character&gt; 
139       * // assertThat(frodo) returns an ObjectAssert and not a custom CharacterAssert implementing Assert&lt;CharacterAssert, Character&gt;  
140       * assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam); // won't compile !
141       * 
142       * The code does not compile because assertThat(frodo) returns an ObjectAssert, thus usingComparator expects a Comparator&lt;Object&gt; 
143       * and Comparator&lt;Character&gt; is not a Comparator&lt;Object&gt; as generics are not reified.
144       * 
145       * Note that, it would have worked if assertThat(frodo) returned a CharacterAssert implementing Assert&lt;CharacterAssert, Character&gt;. 
146       * </pre>
147       * 
148       * @param customComparator the comparator to use for incoming assertion checks.
149       * @throws NullPointerException if the given comparator is {@code null}.
150       * @return {@code this} assertion object.
151       */
152      S usingComparator(Comparator<?> customComparator);
153    
154      /**
155       * Revert to standard comparison for incoming assertion checks.<br>
156       * This method should be used to disable a custom comparison strategy set by calling
157       * {@link #usingComparator(Comparator)}.
158       * @return {@code this} assertion object.
159       */
160      S usingDefaultComparator();
161      
162      /**
163       * Throws <code>{@link UnsupportedOperationException}</code> if called. It is easy to accidentally call
164       * <code>{@link #equals(Object)}</code> instead of <code>isEqualTo</code>.
165       * @throws UnsupportedOperationException if this method is called.
166       */
167      @Override boolean equals(Object obj);
168    }