001    /*
002     * Created on Jul 26, 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.api;
016    
017    import java.util.Comparator;
018    
019    import org.fest.assertions.core.ArraySortedAssert;
020    import org.fest.assertions.core.Condition;
021    import org.fest.assertions.core.IndexedObjectEnumerableAssert;
022    import org.fest.assertions.core.ObjectEnumerableAssert;
023    import org.fest.assertions.data.Index;
024    import org.fest.assertions.internal.ObjectArrays;
025    import org.fest.util.ComparatorBasedComparisonStrategy;
026    import org.fest.util.VisibleForTesting;
027    
028    /**
029     * Assertion methods for arrays of objects.
030     * <p>
031     * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Object[])}</code>.
032     * </p>
033     * 
034     * @author Yvonne Wang
035     * @author Alex Ruiz
036     * @author Joel Costigliola
037     * @author Nicolas François
038     * @author Mikhail Mazursky
039     */
040    public class ObjectArrayAssert<T> extends AbstractAssert<ObjectArrayAssert<T>, T[]> implements
041        ObjectEnumerableAssert<ObjectArrayAssert<T>, T>, IndexedObjectEnumerableAssert<T>, ArraySortedAssert<ObjectArrayAssert<T>, T> {
042    
043      @VisibleForTesting
044      ObjectArrays arrays = ObjectArrays.instance();
045    
046      protected ObjectArrayAssert(T[] actual) {
047        super(actual, ObjectArrayAssert.class);
048      }
049    
050      /** {@inheritDoc} */
051      public void isNullOrEmpty() {
052        arrays.assertNullOrEmpty(info, actual);
053      }
054    
055      /** {@inheritDoc} */
056      public void isEmpty() {
057        arrays.assertEmpty(info, actual);
058      }
059    
060      /** {@inheritDoc} */
061      public ObjectArrayAssert<T> isNotEmpty() {
062        arrays.assertNotEmpty(info, actual);
063        return this;
064      }
065    
066      /** {@inheritDoc} */
067      public ObjectArrayAssert<T> hasSize(int expected) {
068        arrays.assertHasSize(info, actual, expected);
069        return this;
070      }
071      
072      /** {@inheritDoc} */
073      public ObjectArrayAssert<T> hasSameSizeAs(Object[] other) {
074        arrays.assertHasSameSizeAs(info, actual, other);
075        return this;
076      }
077      
078      /** {@inheritDoc} */
079      public ObjectArrayAssert<T> hasSameSizeAs(Iterable<?> other) {
080        arrays.assertHasSameSizeAs(info, actual, other);
081        return this;
082      }  
083    
084      /** {@inheritDoc} */
085      public ObjectArrayAssert<T> contains(Object... values) {
086        arrays.assertContains(info, actual, values);
087        return this;
088      }
089    
090      /** {@inheritDoc} */
091      public ObjectArrayAssert<T> containsOnly(Object... values) {
092        arrays.assertContainsOnly(info, actual, values);
093        return this;
094      }
095    
096      /** {@inheritDoc} */
097      public ObjectArrayAssert<T> containsSequence(Object... sequence) {
098        arrays.assertContainsSequence(info, actual, sequence);
099        return this;
100      }
101    
102      /** {@inheritDoc} */
103      public ObjectArrayAssert<T> contains(Object value, Index index) {
104        arrays.assertContains(info, actual, value, index);
105        return this;
106      }
107    
108      /** {@inheritDoc} */
109      public ObjectArrayAssert<T> doesNotContain(Object value, Index index) {
110        arrays.assertDoesNotContain(info, actual, value, index);
111        return this;
112      }
113    
114      /** {@inheritDoc} */
115      public ObjectArrayAssert<T> doesNotContain(Object... values) {
116        arrays.assertDoesNotContain(info, actual, values);
117        return this;
118      }
119    
120      /** {@inheritDoc} */
121      public ObjectArrayAssert<T> doesNotHaveDuplicates() {
122        arrays.assertDoesNotHaveDuplicates(info, actual);
123        return this;
124      }
125    
126      /** {@inheritDoc} */
127      public ObjectArrayAssert<T> startsWith(Object... sequence) {
128        arrays.assertStartsWith(info, actual, sequence);
129        return this;
130      }
131    
132      /** {@inheritDoc} */
133      public ObjectArrayAssert<T> endsWith(Object... sequence) {
134        arrays.assertEndsWith(info, actual, sequence);
135        return this;
136      }
137    
138      /** {@inheritDoc} */
139      public ObjectArrayAssert<T> containsNull() {
140        arrays.assertContainsNull(info, actual);
141        return this;
142      }
143    
144      /** {@inheritDoc} */
145      public ObjectArrayAssert<T> doesNotContainNull() {
146        arrays.assertDoesNotContainNull(info, actual);
147        return this;
148      }
149    
150      /** {@inheritDoc} */
151      public <E> ObjectArrayAssert<T> are(Condition<E> condition) {
152              arrays.assertAre(info, actual, condition);
153              return myself;
154      }
155      
156      /** {@inheritDoc} */
157      public <E> ObjectArrayAssert<T> areNot(Condition<E> condition) {
158              arrays.assertAreNot(info, actual, condition);
159              return myself;
160      } 
161     
162      /** {@inheritDoc} */
163      public <E> ObjectArrayAssert<T> have(Condition<E> condition) {
164              arrays.assertHave(info, actual, condition);
165              return myself;
166      }  
167    
168      /** {@inheritDoc} */
169      public <E> ObjectArrayAssert<T> doNotHave(Condition<E> condition) {
170              arrays.assertDoNotHave(info, actual, condition);
171              return myself;
172      }   
173      
174      /** {@inheritDoc} */
175      public <E> ObjectArrayAssert<T> areAtLeast(int times, Condition<E> condition) {
176              arrays.assertAreAtLeast(info, actual, times, condition);
177              return myself;
178      }   
179      
180      /** {@inheritDoc} */
181      public <E> ObjectArrayAssert<T> areNotAtLeast(int times, Condition<E> condition) {
182              arrays.assertAreNotAtLeast(info, actual, times, condition);
183              return myself;
184      }     
185      
186      /** {@inheritDoc} */
187      public <E> ObjectArrayAssert<T> areAtMost(int times, Condition<E> condition) {
188              arrays.assertAreAtMost(info, actual, times, condition);
189              return myself;
190      }
191      
192      /** {@inheritDoc} */
193      public <E> ObjectArrayAssert<T> areNotAtMost(int times, Condition<E> condition) {
194              arrays.assertAreNotAtMost(info, actual, times, condition);
195              return myself;
196      }     
197      
198      /** {@inheritDoc} */
199      public <E> ObjectArrayAssert<T> areExactly(int times, Condition<E> condition) {
200              arrays.assertAreExactly(info, actual, times, condition);
201              return myself;
202      }
203      
204      /** {@inheritDoc} */
205      public <E> ObjectArrayAssert<T> areNotExactly(int times, Condition<E> condition) {
206              arrays.assertAreNotExactly(info, actual, times, condition);
207              return myself;
208      }  
209      
210      /** {@inheritDoc} */
211      public <E> ObjectArrayAssert<T> haveAtLeast(int times, Condition<E> condition) {
212              arrays.assertHaveAtLeast(info, actual, times, condition);
213              return myself;
214      }
215      
216      /** {@inheritDoc} */
217      public <E> ObjectArrayAssert<T> doNotHaveAtLeast(int times, Condition<E> condition) {
218              arrays.assertDoNotHaveAtLeast(info, actual, times, condition);
219              return myself;
220      }  
221      
222      /** {@inheritDoc} */
223      public <E> ObjectArrayAssert<T> haveAtMost(int times, Condition<E> condition) {
224              arrays.assertHaveAtMost(info, actual, times, condition);
225              return myself;
226      }
227      
228      /** {@inheritDoc} */
229      public <E> ObjectArrayAssert<T> doNotHaveAtMost(int times, Condition<E> condition) {
230              arrays.assertDoNotHaveAtMost(info, actual, times, condition);
231              return myself;
232      }  
233      
234      /** {@inheritDoc} */
235      public <E> ObjectArrayAssert<T> haveExactly(int times, Condition<E> condition) {
236              arrays.assertHaveExactly(info, actual, times, condition);
237              return myself;
238      }
239      
240      /** {@inheritDoc} */
241      public <E> ObjectArrayAssert<T> doNotHaveExactly(int times, Condition<E> condition) {
242              arrays.assertDoNotHaveExactly(info, actual, times, condition);
243              return myself;
244      }  
245      
246      /** {@inheritDoc} */
247      public ObjectArrayAssert<T> isSorted() {
248        arrays.assertIsSorted(info, actual);
249        return this;
250      }
251    
252      /** {@inheritDoc} */
253      public ObjectArrayAssert<T> isSortedAccordingTo(Comparator<? super T> comparator) {
254        arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
255        return this;
256      }
257      
258      /** {@inheritDoc} */
259      public ObjectArrayAssert<T> containsAll(Iterable<? extends T> iterable) {
260        arrays.assertContainsAll(info, actual, iterable);
261        return this;
262      }
263    
264      public ObjectArrayAssert<T> usingElementComparator(Comparator<? super T> customComparator) {
265        this.arrays = new ObjectArrays(new ComparatorBasedComparisonStrategy(customComparator));
266        return myself;
267      }
268    
269      public ObjectArrayAssert<T> usingDefaultElementComparator() {
270        this.arrays = ObjectArrays.instance();
271        return myself;
272      }
273    
274    }