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     */
039    public class ObjectArrayAssert extends AbstractAssert<ObjectArrayAssert, Object[]> implements
040        ObjectEnumerableAssert<ObjectArrayAssert>, IndexedObjectEnumerableAssert, ArraySortedAssert<ObjectArrayAssert, Object> {
041    
042      @VisibleForTesting
043      ObjectArrays arrays = ObjectArrays.instance();
044    
045      protected ObjectArrayAssert(Object[] actual) {
046        super(actual, ObjectArrayAssert.class);
047      }
048    
049      /** {@inheritDoc} */
050      public void isNullOrEmpty() {
051        arrays.assertNullOrEmpty(info, actual);
052      }
053    
054      /** {@inheritDoc} */
055      public void isEmpty() {
056        arrays.assertEmpty(info, actual);
057      }
058    
059      /** {@inheritDoc} */
060      public ObjectArrayAssert isNotEmpty() {
061        arrays.assertNotEmpty(info, actual);
062        return this;
063      }
064    
065      /** {@inheritDoc} */
066      public ObjectArrayAssert hasSize(int expected) {
067        arrays.assertHasSize(info, actual, expected);
068        return this;
069      }
070    
071      /** {@inheritDoc} */
072      public ObjectArrayAssert contains(Object... values) {
073        arrays.assertContains(info, actual, values);
074        return this;
075      }
076    
077      /** {@inheritDoc} */
078      public ObjectArrayAssert containsOnly(Object... values) {
079        arrays.assertContainsOnly(info, actual, values);
080        return this;
081      }
082    
083      /** {@inheritDoc} */
084      public ObjectArrayAssert containsSequence(Object... sequence) {
085        arrays.assertContainsSequence(info, actual, sequence);
086        return this;
087      }
088    
089      /** {@inheritDoc} */
090      public ObjectArrayAssert contains(Object value, Index index) {
091        arrays.assertContains(info, actual, value, index);
092        return this;
093      }
094    
095      /** {@inheritDoc} */
096      public ObjectArrayAssert doesNotContain(Object value, Index index) {
097        arrays.assertDoesNotContain(info, actual, value, index);
098        return this;
099      }
100    
101      /** {@inheritDoc} */
102      public ObjectArrayAssert doesNotContain(Object... values) {
103        arrays.assertDoesNotContain(info, actual, values);
104        return this;
105      }
106    
107      /** {@inheritDoc} */
108      public ObjectArrayAssert doesNotHaveDuplicates() {
109        arrays.assertDoesNotHaveDuplicates(info, actual);
110        return this;
111      }
112    
113      /** {@inheritDoc} */
114      public ObjectArrayAssert startsWith(Object... sequence) {
115        arrays.assertStartsWith(info, actual, sequence);
116        return this;
117      }
118    
119      /** {@inheritDoc} */
120      public ObjectArrayAssert endsWith(Object... sequence) {
121        arrays.assertEndsWith(info, actual, sequence);
122        return this;
123      }
124    
125      /** {@inheritDoc} */
126      public ObjectArrayAssert containsNull() {
127        arrays.assertContainsNull(info, actual);
128        return this;
129      }
130    
131      /** {@inheritDoc} */
132      public ObjectArrayAssert doesNotContainNull() {
133        arrays.assertDoesNotContainNull(info, actual);
134        return this;
135      }
136    
137      /** {@inheritDoc} */
138      public <E> ObjectArrayAssert are(Condition<E> condition) {
139              arrays.assertAre(info, actual, condition);
140              return myself;
141      }
142      
143      /** {@inheritDoc} */
144      public <E> ObjectArrayAssert areNot(Condition<E> condition) {
145              arrays.assertAreNot(info, actual, condition);
146              return myself;
147      } 
148     
149      /** {@inheritDoc} */
150      public <E> ObjectArrayAssert have(Condition<E> condition) {
151              arrays.assertHave(info, actual, condition);
152              return myself;
153      }  
154    
155      /** {@inheritDoc} */
156      public <E> ObjectArrayAssert doNotHave(Condition<E> condition) {
157              arrays.assertDoNotHave(info, actual, condition);
158              return myself;
159      }   
160      
161      /** {@inheritDoc} */
162      public <E> ObjectArrayAssert areAtLeast(int times, Condition<E> condition) {
163              arrays.assertAreAtLeast(info, actual, times, condition);
164              return myself;
165      }   
166      
167      /** {@inheritDoc} */
168      public <E> ObjectArrayAssert areNotAtLeast(int times, Condition<E> condition) {
169              arrays.assertAreNotAtLeast(info, actual, times, condition);
170              return myself;
171      }     
172      
173      /** {@inheritDoc} */
174      public <E> ObjectArrayAssert areAtMost(int times, Condition<E> condition) {
175              arrays.assertAreAtMost(info, actual, times, condition);
176              return myself;
177      }
178      
179      /** {@inheritDoc} */
180      public <E> ObjectArrayAssert areNotAtMost(int times, Condition<E> condition) {
181              arrays.assertAreNotAtMost(info, actual, times, condition);
182              return myself;
183      }     
184      
185      /** {@inheritDoc} */
186      public <E> ObjectArrayAssert areExactly(int times, Condition<E> condition) {
187              arrays.assertAreExactly(info, actual, times, condition);
188              return myself;
189      }
190      
191      /** {@inheritDoc} */
192      public <E> ObjectArrayAssert areNotExactly(int times, Condition<E> condition) {
193              arrays.assertAreNotExactly(info, actual, times, condition);
194              return myself;
195      }  
196      
197      /** {@inheritDoc} */
198      public <E> ObjectArrayAssert haveAtLeast(int times, Condition<E> condition) {
199              arrays.assertHaveAtLeast(info, actual, times, condition);
200              return myself;
201      }
202      
203      /** {@inheritDoc} */
204      public <E> ObjectArrayAssert doNotHaveAtLeast(int times, Condition<E> condition) {
205              arrays.assertDoNotHaveAtLeast(info, actual, times, condition);
206              return myself;
207      }  
208      
209      /** {@inheritDoc} */
210      public <E> ObjectArrayAssert haveAtMost(int times, Condition<E> condition) {
211              arrays.assertHaveAtMost(info, actual, times, condition);
212              return myself;
213      }
214      
215      /** {@inheritDoc} */
216      public <E> ObjectArrayAssert doNotHaveAtMost(int times, Condition<E> condition) {
217              arrays.assertDoNotHaveAtMost(info, actual, times, condition);
218              return myself;
219      }  
220      
221      /** {@inheritDoc} */
222      public <E> ObjectArrayAssert haveExactly(int times, Condition<E> condition) {
223              arrays.assertHaveExactly(info, actual, times, condition);
224              return myself;
225      }
226      
227      /** {@inheritDoc} */
228      public <E> ObjectArrayAssert doNotHaveExactly(int times, Condition<E> condition) {
229              arrays.assertDoNotHaveExactly(info, actual, times, condition);
230              return myself;
231      }  
232      
233      /** {@inheritDoc} */
234      public ObjectArrayAssert isSorted() {
235        arrays.assertIsSorted(info, actual);
236        return this;
237      }
238    
239      /** {@inheritDoc} */
240      public ObjectArrayAssert isSortedAccordingTo(Comparator<? extends Object> comparator) {
241        arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
242        return this;
243      }
244    
245      @Override
246      public ObjectArrayAssert usingComparator(Comparator<?> customComparator) {
247        super.usingComparator(customComparator);
248        this.arrays = new ObjectArrays(new ComparatorBasedComparisonStrategy(customComparator));
249        return myself;
250      }
251      
252      @Override
253      public ObjectArrayAssert usingDefaultComparator() {
254        super.usingDefaultComparator();
255        this.arrays = ObjectArrays.instance();
256        return myself;
257      }
258      
259      
260      
261    }