001    /*
002     * Created on Nov 3, 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.internal;
016    
017    import java.util.Comparator;
018    
019    import org.fest.assertions.core.ArraySortedAssert;
020    import org.fest.assertions.core.AssertionInfo;
021    import org.fest.assertions.core.Condition;
022    import org.fest.assertions.data.Index;
023    import org.fest.util.ComparisonStrategy;
024    import org.fest.util.StandardComparisonStrategy;
025    import org.fest.util.VisibleForTesting;
026    
027    /**
028     * Reusable assertions for arrays of objects.
029     * 
030     * @author Alex Ruiz
031     * @author Joel Costigliola
032     * @author Nicolas François
033     */
034    public class ObjectArrays {
035    
036      private static final ObjectArrays INSTANCE = new ObjectArrays();
037    
038      /**
039       * Returns the singleton instance of this class.
040       * @return the singleton instance of this class.
041       */
042      public static ObjectArrays instance() {
043        return INSTANCE;
044      }
045    
046      private Arrays arrays = Arrays.instance();
047    
048      @VisibleForTesting
049      ObjectArrays() {
050        this(StandardComparisonStrategy.instance());
051      }
052    
053      public ObjectArrays(ComparisonStrategy comparisonStrategy) {
054        this.arrays = new Arrays(comparisonStrategy);
055      }
056    
057      @VisibleForTesting
058      public Comparator<?> getComparator() {
059        return arrays.getComparator();
060      }
061    
062      @VisibleForTesting
063      Failures failures = Failures.instance();
064    
065      @VisibleForTesting
066      Conditions conditions = Conditions.instance();
067    
068      /**
069       * Asserts that the given array is {@code null} or empty.
070       * @param info contains information about the assertion.
071       * @param actual the given array.
072       * @throws AssertionError if the given array is not {@code null} *and* contains one or more elements.
073       */
074      public void assertNullOrEmpty(AssertionInfo info, Object[] actual) {
075        arrays.assertNullOrEmpty(info, failures, actual);
076      }
077    
078      /**
079       * Asserts that the given array is empty.
080       * @param info contains information about the assertion.
081       * @param actual the given array.
082       * @throws AssertionError if the given array is {@code null}.
083       * @throws AssertionError if the given array is not empty.
084       */
085      public void assertEmpty(AssertionInfo info, Object[] actual) {
086        arrays.assertEmpty(info, failures, actual);
087      }
088    
089      /**
090       * Asserts that the given array is not empty.
091       * @param info contains information about the assertion.
092       * @param actual the given array.
093       * @throws AssertionError if the given array is {@code null}.
094       * @throws AssertionError if the given array is empty.
095       */
096      public void assertNotEmpty(AssertionInfo info, Object[] actual) {
097        arrays.assertNotEmpty(info, failures, actual);
098      }
099    
100      /**
101       * Asserts that the number of elements in the given array is equal to the expected one.
102       * @param info contains information about the assertion.
103       * @param actual the given array.
104       * @param expectedSize the expected size of {@code actual}.
105       * @throws AssertionError if the given array is {@code null}.
106       * @throws AssertionError if the number of elements in the given array is different than the expected one.
107       */
108      public void assertHasSize(AssertionInfo info, Object[] actual, int expectedSize) {
109        arrays.assertHasSize(info, failures, actual, expectedSize);
110      }
111      
112      /**
113       * Assert that the actual array has the same size as the other {@code Iterable}.
114       * @param info contains information about the assertion.
115       * @param actual the given iterable.
116       * @param other the group to compare 
117       * @throws AssertionError if the actual group is {@code null}.
118       * @throws AssertionError if the other group is {@code null}.
119       * @throws AssertionError if the actual group does not have the same size.
120       */
121      public void assertHasSameSizeAs(AssertionInfo info, Object[] actual, Iterable<?> other) {
122        arrays.assertHasSameSizeAs(info, failures, actual, other);
123      }
124      
125      /**
126       * Assert that the actual array has the same size as the other array.
127       * @param info contains information about the assertion.
128       * @param actual the given array.
129       * @param other the group to compare 
130       * @throws AssertionError if the actual group is {@code null}.
131       * @throws AssertionError if the other group is {@code null}.
132       * @throws AssertionError if the actual group does not have the same size.
133       */
134      public void assertHasSameSizeAs(AssertionInfo info, Object[] actual, Object[] other) {
135        arrays.assertHasSameSizeAs(info, failures, actual, other);
136      }  
137    
138      /**
139       * Asserts that the given array contains the given values, in any order.
140       * @param info contains information about the assertion.
141       * @param actual the given array.
142       * @param values the values that are expected to be in the given array.
143       * @throws NullPointerException if the array of values is {@code null}.
144       * @throws IllegalArgumentException if the array of values is empty.
145       * @throws AssertionError if the given array is {@code null}.
146       * @throws AssertionError if the given array does not contain the given values.
147       */
148      public void assertContains(AssertionInfo info, Object[] actual, Object[] values) {
149        arrays.assertContains(info, failures, actual, values);
150      }
151    
152      /**
153       * Verifies that the given array contains the given object at the given index.
154       * @param info contains information about the assertion.
155       * @param actual the given array.
156       * @param value the object to look for.
157       * @param index the index where the object should be stored in the given array.
158       * @throws AssertionError if the given array is {@code null} or empty.
159       * @throws NullPointerException if the given {@code Index} is {@code null}.
160       * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of
161       *           the given array.
162       * @throws AssertionError if the given array does not contain the given object at the given index.
163       */
164      public void assertContains(AssertionInfo info, Object[] actual, Object value, Index index) {
165        arrays.assertContains(info, failures, actual, value, index);
166      }
167    
168      /**
169       * Verifies that the given array does not contain the given object at the given index.
170       * @param info contains information about the assertion.
171       * @param actual the given array.
172       * @param value the object to look for.
173       * @param index the index where the object should be stored in the given array.
174       * @throws AssertionError if the given array is {@code null}.
175       * @throws NullPointerException if the given {@code Index} is {@code null}.
176       * @throws AssertionError if the given array contains the given object at the given index.
177       */
178      public void assertDoesNotContain(AssertionInfo info, Object[] actual, Object value, Index index) {
179        arrays.assertDoesNotContain(info, failures, actual, value, index);
180      }
181    
182      /**
183       * Asserts that the given array contains only the given values and nothing else, in any order.
184       * @param info contains information about the assertion.
185       * @param actual the given array.
186       * @param values the values that are expected to be in the given array.
187       * @throws NullPointerException if the array of values is {@code null}.
188       * @throws IllegalArgumentException if the array of values is empty.
189       * @throws AssertionError if the given array is {@code null}.
190       * @throws AssertionError if the given array does not contain the given values or if the given array contains values
191       *           that are not in the given array.
192       */
193      public void assertContainsOnly(AssertionInfo info, Object[] actual, Object[] values) {
194        arrays.assertContainsOnly(info, failures, actual, values);
195      }
196    
197      /**
198       * Verifies that the given array contains the given sequence of objects, without any other objects between them.
199       * @param info contains information about the assertion.
200       * @param actual the given array.
201       * @param sequence the sequence of objects to look for.
202       * @throws AssertionError if the given array is {@code null}.
203       * @throws NullPointerException if the given sequence is {@code null}.
204       * @throws IllegalArgumentException if the given sequence is empty.
205       * @throws AssertionError if the given array does not contain the given sequence of objects.
206       */
207      public void assertContainsSequence(AssertionInfo info, Object[] actual, Object[] sequence) {
208        arrays.assertContainsSequence(info, failures, actual, sequence);
209      }
210    
211      /**
212       * Asserts that the given array does not contain the given values.
213       * @param info contains information about the assertion.
214       * @param actual the given array.
215       * @param values the values that are expected not to be in the given array.
216       * @throws NullPointerException if the array of values is {@code null}.
217       * @throws IllegalArgumentException if the array of values is empty.
218       * @throws AssertionError if the given array is {@code null}.
219       * @throws AssertionError if the given array contains any of given values.
220       */
221      public void assertDoesNotContain(AssertionInfo info, Object[] actual, Object[] values) {
222        arrays.assertDoesNotContain(info, failures, actual, values);
223      }
224    
225      /**
226       * Asserts that the given array does not have duplicate values.
227       * @param info contains information about the assertion.
228       * @param actual the given array.
229       * @throws NullPointerException if the array of values is {@code null}.
230       * @throws IllegalArgumentException if the array of values is empty.
231       * @throws AssertionError if the given array is {@code null}.
232       * @throws AssertionError if the given array contains duplicate values.
233       */
234      public void assertDoesNotHaveDuplicates(AssertionInfo info, Object[] actual) {
235        arrays.assertDoesNotHaveDuplicates(info, failures, actual);
236      }
237    
238      /**
239       * Verifies that the given array starts with the given sequence of objects, without any other objects between them.
240       * Similar to <code>{@link #assertContainsSequence(AssertionInfo, Object[], Object[])}</code>, but it also verifies
241       * that the first element in the sequence is also the first element of the given array.
242       * @param info contains information about the assertion.
243       * @param actual the given array.
244       * @param sequence the sequence of objects to look for.
245       * @throws NullPointerException if the given argument is {@code null}.
246       * @throws IllegalArgumentException if the given argument is an empty array.
247       * @throws AssertionError if the given array is {@code null}.
248       * @throws AssertionError if the given array does not start with the given sequence of objects.
249       */
250      public void assertStartsWith(AssertionInfo info, Object[] actual, Object[] sequence) {
251        arrays.assertStartsWith(info, failures, actual, sequence);
252      }
253    
254      /**
255       * Verifies that the given array ends with the given sequence of objects, without any other objects between them.
256       * Similar to <code>{@link #assertContainsSequence(AssertionInfo, Object[], Object[])}</code>, but it also verifies
257       * that the last element in the sequence is also the last element of the given array.
258       * @param info contains information about the assertion.
259       * @param actual the given array.
260       * @param sequence the sequence of objects to look for.
261       * @throws NullPointerException if the given argument is {@code null}.
262       * @throws IllegalArgumentException if the given argument is an empty array.
263       * @throws AssertionError if the given array is {@code null}.
264       * @throws AssertionError if the given array does not end with the given sequence of objects.
265       */
266      public void assertEndsWith(AssertionInfo info, Object[] actual, Object[] sequence) {
267        arrays.assertEndsWith(info, failures, actual, sequence);
268      }
269    
270      /**
271       * Asserts that the given array contains at least a null element.
272       * @param info contains information about the assertion.
273       * @param actual the given array.
274       * @throws AssertionError if the given array is {@code null}.
275       * @throws AssertionError if the given array does not contain a null element.
276       */
277      public void assertContainsNull(AssertionInfo info, Object[] actual) {
278        arrays.assertContainsNull(info, failures, actual);
279      }
280    
281      /**
282       * Asserts that the given array does not contain null elements.
283       * @param info contains information about the assertion.
284       * @param actual the given array.
285       * @throws AssertionError if the given array is {@code null}.
286       * @throws AssertionError if the given array contains a null element.
287       */
288      public void assertDoesNotContainNull(AssertionInfo info, Object[] actual) {
289        arrays.assertDoesNotContainNull(info, failures, actual);
290      }
291    
292      /**
293       * Assert that each element of given array satisfies the given condition.
294       * @param info contains information about the assertion.
295       * @param actual the given array.
296       * @param condition the given {@code Condition}.
297       * @throws NullPointerException if the given condition is {@code null}.
298       * @throws AssertionError if a element cannot be cast to E.
299       * @throws AssertionError if one or more element not satisfy the given condition.
300       */
301      public <E> void assertAre(AssertionInfo info, Object[] actual, Condition<E> condition) {
302        arrays.assertAre(info, failures, conditions, actual, condition);
303      }
304    
305      /**
306       * Assert that each element of given array not satisfies the given condition.
307       * @param info contains information about the assertion.
308       * @param actual the given array.
309       * @param condition the given {@code Condition}.
310       * @throws NullPointerException if the given condition is {@code null}.
311       * @throws AssertionError if a element cannot be cast to E.
312       * @throws AssertionError if one or more element satisfy the given condition.
313       */
314      public <E> void assertAreNot(AssertionInfo info, Object[] actual, Condition<E> condition) {
315        arrays.assertAreNot(info, failures, conditions, actual, condition);
316      }
317    
318      /**
319       * Assert that each element of given array satisfies the given condition.
320       * @param info contains information about the assertion.
321       * @param actual the given array.
322       * @param condition the given {@code Condition}.
323       * @throws NullPointerException if the given condition is {@code null}.
324       * @throws AssertionError if a element cannot be cast to E.
325       * @throws AssertionError if one or more element not satisfy the given condition.
326       */
327      public <E> void assertHave(AssertionInfo info, Object[] actual, Condition<E> condition) {
328        arrays.assertHave(info, failures, conditions, actual, condition);
329      }
330    
331      /**
332       * Assert that each element of given array not satisfies the given condition.
333       * @param info contains information about the assertion.
334       * @param actual the given array.
335       * @param condition the given {@code Condition}.
336       * @throws NullPointerException if the given condition is {@code null}.
337       * @throws AssertionError if a element cannot be cast to E.
338       * @throws AssertionError if one or more element satisfy the given condition.
339       */
340      public <E> void assertDoNotHave(AssertionInfo info, Object[] actual, Condition<E> condition) {
341        arrays.assertHaveNot(info, failures, conditions, actual, condition);
342      }
343    
344      /**
345       * Assert that there is <b>at least</b> <i>n</i> array elements satisfying the given condition.
346       * @param info contains information about the assertion.
347       * @param actual the given array.
348       * @param n the minimum number of times the condition should be verified.
349       * @param condition the given {@code Condition}.
350       * @throws NullPointerException if the given condition is {@code null}.
351       * @throws AssertionError if a element cannot be cast to E.
352       * @throws AssertionError if the number of elements satisfying the given condition is &lt; n.
353       */
354      public <E> void assertAreAtLeast(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
355        arrays.assertAreAtLeast(info, failures, conditions, actual, n, condition);
356      }
357    
358      /**
359       * Assert that there is <b>at least</b> <i>n</i> array elements <b>not</b> satisfying the given condition.
360       * @param info contains information about the assertion.
361       * @param actual the given array.
362       * @param n the number of times the condition should not be verified at least.
363       * @param condition the given {@code Condition}.
364       * @throws NullPointerException if the given condition is {@code null}.
365       * @throws AssertionError if a element cannot be cast to E.
366       * @throws AssertionError if the number of elements not satisfying the given condition is &lt; n.
367       */
368      public <E> void assertAreNotAtLeast(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
369        arrays.assertAreNotAtLeast(info, failures, conditions, actual, n, condition);
370      }
371    
372      /**
373       * Assert that there is <b>at most</b> <i>n</i> array elements satisfying the given condition.
374       * @param info contains information about the assertion.
375       * @param actual the given array.
376       * @param n the number of times the condition should be at most verified.
377       * @param condition the given {@code Condition}.
378       * @throws NullPointerException if the given condition is {@code null}.
379       * @throws AssertionError if a element cannot be cast to E.
380       * @throws AssertionError if the number of elements satisfying the given condition is &gt; n.
381       */
382      public <E> void assertAreAtMost(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
383        arrays.assertAreAtMost(info, failures, conditions, actual, n, condition);
384      }
385    
386      /**
387       * Verifies that there is <b>at most</b> <i>n</i> array elements <b>not</b> satisfying the given condition.
388       * @param info contains information about the assertion.
389       * @param actual the given array.
390       * @param n the number of times the condition should not be verified at most.
391       * @param condition the given {@code Condition}.
392       * @throws NullPointerException if the given condition is {@code null}.
393       * @throws AssertionError if a element cannot be cast to E.
394       * @throws AssertionError if the number of elements not satisfying the given condition is &gt; n.
395       */
396      public <E> void assertAreNotAtMost(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
397        arrays.assertAreNotAtMost(info, failures, conditions, actual, n, condition);
398      }
399    
400      /**
401       * Verifies that there is <b>exactly</b> <i>n</i> array elements satisfying the given condition.
402       * @param info contains information about the assertion.
403       * @param actual the given array.
404       * @param n the exact number of times the condition should be verified.
405       * @param condition the given {@code Condition}.
406       * @throws NullPointerException if the given condition is {@code null}.
407       * @throws AssertionError if a element cannot be cast to E.
408       * @throws AssertionError if the number of elements satisfying the given condition is &ne; n.
409       */
410      public <E> void assertAreExactly(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
411        arrays.assertAreExactly(info, failures, conditions, actual, n, condition);
412      }
413    
414      /**
415       * Verifies that there is <b>exactly</b> <i>n</i> elements in the actual {@code Iterable} <b>not</b> satisfying the
416       * given condition.
417       * @param info contains information about the assertion.
418       * @param actual the given array.
419       * @param n most times the condition should not be verify.
420       * @param condition the given {@code Condition}.
421       * @throws NullPointerException if the given condition is {@code null}.
422       * @throws AssertionError if a element cannot be cast to E.
423       * @throws AssertionError if the number of elements not satisfying the given condition is &ne; n.
424       */
425      public <E> void assertAreNotExactly(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
426        arrays.assertAreNotExactly(info, failures, conditions, actual, n, condition);
427      }
428    
429      /**
430       * An alias method of {@link #assertAreAtLeast(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
431       * (same logic, only error message differs).
432       */
433      public <E> void assertHaveAtLeast(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
434        arrays.assertHaveAtLeast(info, failures, conditions, actual, times, condition);
435      }
436    
437      /**
438       * An alias method of {@link #assertAreNotAtLeast(AssertionInfo, Object[], int, Condition)} to provide a richer fluent
439       * api (same logic, only error message differs).
440       */
441      public <E> void assertDoNotHaveAtLeast(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
442        arrays.assertDoNotHaveAtLeast(info, failures, conditions, actual, times, condition);
443      }
444    
445      /**
446       * An alias method of {@link #assertAreAtMost(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
447       * (same logic, only error message differs).
448       */
449      public <E> void assertHaveAtMost(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
450        arrays.assertHaveAtMost(info, failures, conditions, actual, times, condition);
451      }
452    
453      /**
454       * An alias method of {@link #assertAreNotAtMost(AssertionInfo, Object[], int, Condition)} to provide a richer fluent
455       * api (same logic, only error message differs).
456       */
457      public <E> void assertDoNotHaveAtMost(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
458        arrays.assertDoNotHaveAtMost(info, failures, conditions, actual, times, condition);
459      }
460    
461      /**
462       * An alias method of {@link #assertAreExactly(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
463       * (same logic, only error message differs).
464       */
465      public <E> void assertHaveExactly(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
466        arrays.assertHaveExactly(info, failures, conditions, actual, times, condition);
467      }
468    
469      /**
470       * An alias method of {@link #assertAreNotExactly(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
471       * (same logic, only error message differs).
472       */
473      public <E> void assertDoNotHaveExactly(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
474        arrays.assertDoNotHaveExactly(info, failures, conditions, actual, times, condition);
475      }
476    
477      /**
478       * Concrete implementation of {@link ArraySortedAssert#isSorted()}.
479       * 
480       * @param info contains information about the assertion.
481       * @param actual the given array.
482       */
483      public void assertIsSorted(AssertionInfo info, Object[] actual) {
484        arrays.assertIsSorted(info, failures, actual);
485      }
486    
487      /**
488       * Concrete implementation of {@link ArraySortedAssert#isSortedAccordingTo(Comparator)}.
489       * 
490       * @param info contains information about the assertion.
491       * @param actual the given array.
492       * @param comparator the {@link Comparator} used to compare array elements
493       */
494      public void assertIsSortedAccordingToComparator(AssertionInfo info, Object[] actual,
495          Comparator<? extends Object> comparator) {
496        Arrays.assertIsSortedAccordingToComparator(info, failures, actual, comparator);
497      }
498    
499    }