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