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