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       * Asserts that the given array contains the given values, in any order.
114       * @param info contains information about the assertion.
115       * @param actual the given array.
116       * @param values the values that are expected to be in the given array.
117       * @throws NullPointerException if the array of values is {@code null}.
118       * @throws IllegalArgumentException if the array of values is empty.
119       * @throws AssertionError if the given array is {@code null}.
120       * @throws AssertionError if the given array does not contain the given values.
121       */
122      public void assertContains(AssertionInfo info, Object[] actual, Object[] values) {
123        arrays.assertContains(info, failures, actual, values);
124      }
125    
126      /**
127       * Verifies that the given array contains the given object at the given index.
128       * @param info contains information about the assertion.
129       * @param actual the given array.
130       * @param value the object to look for.
131       * @param index the index where the object should be stored in the given array.
132       * @throws AssertionError if the given array is {@code null} or empty.
133       * @throws NullPointerException if the given {@code Index} is {@code null}.
134       * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of
135       *           the given array.
136       * @throws AssertionError if the given array does not contain the given object at the given index.
137       */
138      public void assertContains(AssertionInfo info, Object[] actual, Object value, Index index) {
139        arrays.assertContains(info, failures, actual, value, index);
140      }
141    
142      /**
143       * Verifies that the given array does not contain the given object at the given index.
144       * @param info contains information about the assertion.
145       * @param actual the given array.
146       * @param value the object to look for.
147       * @param index the index where the object should be stored in the given array.
148       * @throws AssertionError if the given array is {@code null}.
149       * @throws NullPointerException if the given {@code Index} is {@code null}.
150       * @throws AssertionError if the given array contains the given object at the given index.
151       */
152      public void assertDoesNotContain(AssertionInfo info, Object[] actual, Object value, Index index) {
153        arrays.assertDoesNotContain(info, failures, actual, value, index);
154      }
155    
156      /**
157       * Asserts that the given array contains only the given values and nothing else, in any order.
158       * @param info contains information about the assertion.
159       * @param actual the given array.
160       * @param values the values that are expected to be in the given array.
161       * @throws NullPointerException if the array of values is {@code null}.
162       * @throws IllegalArgumentException if the array of values is empty.
163       * @throws AssertionError if the given array is {@code null}.
164       * @throws AssertionError if the given array does not contain the given values or if the given array contains values
165       *           that are not in the given array.
166       */
167      public void assertContainsOnly(AssertionInfo info, Object[] actual, Object[] values) {
168        arrays.assertContainsOnly(info, failures, actual, values);
169      }
170    
171      /**
172       * Verifies that the given array contains the given sequence of objects, without any other objects between them.
173       * @param info contains information about the assertion.
174       * @param actual the given array.
175       * @param sequence the sequence of objects to look for.
176       * @throws AssertionError if the given array is {@code null}.
177       * @throws NullPointerException if the given sequence is {@code null}.
178       * @throws IllegalArgumentException if the given sequence is empty.
179       * @throws AssertionError if the given array does not contain the given sequence of objects.
180       */
181      public void assertContainsSequence(AssertionInfo info, Object[] actual, Object[] sequence) {
182        arrays.assertContainsSequence(info, failures, actual, sequence);
183      }
184    
185      /**
186       * Asserts that the given array does not contain the given values.
187       * @param info contains information about the assertion.
188       * @param actual the given array.
189       * @param values the values that are expected not to be in the given array.
190       * @throws NullPointerException if the array of values is {@code null}.
191       * @throws IllegalArgumentException if the array of values is empty.
192       * @throws AssertionError if the given array is {@code null}.
193       * @throws AssertionError if the given array contains any of given values.
194       */
195      public void assertDoesNotContain(AssertionInfo info, Object[] actual, Object[] values) {
196        arrays.assertDoesNotContain(info, failures, actual, values);
197      }
198    
199      /**
200       * Asserts that the given array does not have duplicate values.
201       * @param info contains information about the assertion.
202       * @param actual the given array.
203       * @throws NullPointerException if the array of values is {@code null}.
204       * @throws IllegalArgumentException if the array of values is empty.
205       * @throws AssertionError if the given array is {@code null}.
206       * @throws AssertionError if the given array contains duplicate values.
207       */
208      public void assertDoesNotHaveDuplicates(AssertionInfo info, Object[] actual) {
209        arrays.assertDoesNotHaveDuplicates(info, failures, actual);
210      }
211    
212      /**
213       * Verifies that the given array starts with the given sequence of objects, without any other objects between them.
214       * Similar to <code>{@link #assertContainsSequence(AssertionInfo, Object[], Object[])}</code>, but it also verifies
215       * that the first element in the sequence is also the first element of the given array.
216       * @param info contains information about the assertion.
217       * @param actual the given array.
218       * @param sequence the sequence of objects to look for.
219       * @throws NullPointerException if the given argument is {@code null}.
220       * @throws IllegalArgumentException if the given argument is an empty array.
221       * @throws AssertionError if the given array is {@code null}.
222       * @throws AssertionError if the given array does not start with the given sequence of objects.
223       */
224      public void assertStartsWith(AssertionInfo info, Object[] actual, Object[] sequence) {
225        arrays.assertStartsWith(info, failures, actual, sequence);
226      }
227    
228      /**
229       * Verifies that the given array ends with the given sequence of objects, without any other objects between them.
230       * Similar to <code>{@link #assertContainsSequence(AssertionInfo, Object[], Object[])}</code>, but it also verifies
231       * that the last element in the sequence is also the last element of the given array.
232       * @param info contains information about the assertion.
233       * @param actual the given array.
234       * @param sequence the sequence of objects to look for.
235       * @throws NullPointerException if the given argument is {@code null}.
236       * @throws IllegalArgumentException if the given argument is an empty array.
237       * @throws AssertionError if the given array is {@code null}.
238       * @throws AssertionError if the given array does not end with the given sequence of objects.
239       */
240      public void assertEndsWith(AssertionInfo info, Object[] actual, Object[] sequence) {
241        arrays.assertEndsWith(info, failures, actual, sequence);
242      }
243    
244      /**
245       * Asserts that the given array contains at least a null element.
246       * @param info contains information about the assertion.
247       * @param actual the given array.
248       * @throws AssertionError if the given array is {@code null}.
249       * @throws AssertionError if the given array does not contain a null element.
250       */
251      public void assertContainsNull(AssertionInfo info, Object[] actual) {
252        arrays.assertContainsNull(info, failures, actual);
253      }
254    
255      /**
256       * Asserts that the given array does not contain null elements.
257       * @param info contains information about the assertion.
258       * @param actual the given array.
259       * @throws AssertionError if the given array is {@code null}.
260       * @throws AssertionError if the given array contains a null element.
261       */
262      public void assertDoesNotContainNull(AssertionInfo info, Object[] actual) {
263        arrays.assertDoesNotContainNull(info, failures, actual);
264      }
265    
266      /**
267       * Assert that each element of given array satisfies the given condition.
268       * @param info contains information about the assertion.
269       * @param actual the given array.
270       * @param condition the given {@code Condition}.
271       * @throws NullPointerException if the given condition is {@code null}.
272       * @throws AssertionError if a element cannot be cast to E.
273       * @throws AssertionError if one or more element not satisfy the given condition.
274       */
275      public <E> void assertAre(AssertionInfo info, Object[] actual, Condition<E> condition) {
276        arrays.assertAre(info, failures, conditions, actual, condition);
277      }
278    
279      /**
280       * Assert that each element of given array not satisfies the given condition.
281       * @param info contains information about the assertion.
282       * @param actual the given array.
283       * @param condition the given {@code Condition}.
284       * @throws NullPointerException if the given condition is {@code null}.
285       * @throws AssertionError if a element cannot be cast to E.
286       * @throws AssertionError if one or more element satisfy the given condition.
287       */
288      public <E> void assertAreNot(AssertionInfo info, Object[] actual, Condition<E> condition) {
289        arrays.assertAreNot(info, failures, conditions, actual, condition);
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 assertHave(AssertionInfo info, Object[] actual, Condition<E> condition) {
302        arrays.assertHave(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 assertDoNotHave(AssertionInfo info, Object[] actual, Condition<E> condition) {
315        arrays.assertHaveNot(info, failures, conditions, actual, condition);
316      }
317    
318      /**
319       * Assert that there is <b>at least</b> <i>n</i> array elements satisfying the given condition.
320       * @param info contains information about the assertion.
321       * @param actual the given array.
322       * @param n the minimum number of times the condition should be verified.
323       * @param condition the given {@code Condition}.
324       * @throws NullPointerException if the given condition is {@code null}.
325       * @throws AssertionError if a element cannot be cast to E.
326       * @throws AssertionError if the number of elements satisfying the given condition is &lt; n.
327       */
328      public <E> void assertAreAtLeast(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
329        arrays.assertAreAtLeast(info, failures, conditions, actual, n, condition);
330      }
331    
332      /**
333       * Assert that there is <b>at least</b> <i>n</i> array elements <b>not</b> satisfying the given condition.
334       * @param info contains information about the assertion.
335       * @param actual the given array.
336       * @param n the number of times the condition should not be verified at least.
337       * @param condition the given {@code Condition}.
338       * @throws NullPointerException if the given condition is {@code null}.
339       * @throws AssertionError if a element cannot be cast to E.
340       * @throws AssertionError if the number of elements not satisfying the given condition is &lt; n.
341       */
342      public <E> void assertAreNotAtLeast(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
343        arrays.assertAreNotAtLeast(info, failures, conditions, actual, n, condition);
344      }
345    
346      /**
347       * Assert that there is <b>at most</b> <i>n</i> array elements satisfying the given condition.
348       * @param info contains information about the assertion.
349       * @param actual the given array.
350       * @param n the number of times the condition should be at most verified.
351       * @param condition the given {@code Condition}.
352       * @throws NullPointerException if the given condition is {@code null}.
353       * @throws AssertionError if a element cannot be cast to E.
354       * @throws AssertionError if the number of elements satisfying the given condition is &gt; n.
355       */
356      public <E> void assertAreAtMost(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
357        arrays.assertAreAtMost(info, failures, conditions, actual, n, condition);
358      }
359    
360      /**
361       * Verifies that there is <b>at most</b> <i>n</i> array elements <b>not</b> satisfying the given condition.
362       * @param info contains information about the assertion.
363       * @param actual the given array.
364       * @param n the number of times the condition should not be verified at most.
365       * @param condition the given {@code Condition}.
366       * @throws NullPointerException if the given condition is {@code null}.
367       * @throws AssertionError if a element cannot be cast to E.
368       * @throws AssertionError if the number of elements not satisfying the given condition is &gt; n.
369       */
370      public <E> void assertAreNotAtMost(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
371        arrays.assertAreNotAtMost(info, failures, conditions, actual, n, condition);
372      }
373    
374      /**
375       * Verifies that there is <b>exactly</b> <i>n</i> array elements satisfying the given condition.
376       * @param info contains information about the assertion.
377       * @param actual the given array.
378       * @param n the exact number of times the condition should be verified.
379       * @param condition the given {@code Condition}.
380       * @throws NullPointerException if the given condition is {@code null}.
381       * @throws AssertionError if a element cannot be cast to E.
382       * @throws AssertionError if the number of elements satisfying the given condition is &ne; n.
383       */
384      public <E> void assertAreExactly(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
385        arrays.assertAreExactly(info, failures, conditions, actual, n, condition);
386      }
387    
388      /**
389       * Verifies that there is <b>exactly</b> <i>n</i> elements in the actual {@code Iterable} <b>not</b> satisfying the
390       * given condition.
391       * @param info contains information about the assertion.
392       * @param actual the given array.
393       * @param n most times the condition should not be verify.
394       * @param condition the given {@code Condition}.
395       * @throws NullPointerException if the given condition is {@code null}.
396       * @throws AssertionError if a element cannot be cast to E.
397       * @throws AssertionError if the number of elements not satisfying the given condition is &ne; n.
398       */
399      public <E> void assertAreNotExactly(AssertionInfo info, Object[] actual, int n, Condition<E> condition) {
400        arrays.assertAreNotExactly(info, failures, conditions, actual, n, condition);
401      }
402    
403      /**
404       * An alias method of {@link #assertAreAtLeast(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
405       * (same logic, only error message differs).
406       */
407      public <E> void assertHaveAtLeast(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
408        arrays.assertHaveAtLeast(info, failures, conditions, actual, times, condition);
409      }
410    
411      /**
412       * An alias method of {@link #assertAreNotAtLeast(AssertionInfo, Object[], int, Condition)} to provide a richer fluent
413       * api (same logic, only error message differs).
414       */
415      public <E> void assertDoNotHaveAtLeast(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
416        arrays.assertDoNotHaveAtLeast(info, failures, conditions, actual, times, condition);
417      }
418    
419      /**
420       * An alias method of {@link #assertAreAtMost(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
421       * (same logic, only error message differs).
422       */
423      public <E> void assertHaveAtMost(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
424        arrays.assertHaveAtMost(info, failures, conditions, actual, times, condition);
425      }
426    
427      /**
428       * An alias method of {@link #assertAreNotAtMost(AssertionInfo, Object[], int, Condition)} to provide a richer fluent
429       * api (same logic, only error message differs).
430       */
431      public <E> void assertDoNotHaveAtMost(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
432        arrays.assertDoNotHaveAtMost(info, failures, conditions, actual, times, condition);
433      }
434    
435      /**
436       * An alias method of {@link #assertAreExactly(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
437       * (same logic, only error message differs).
438       */
439      public <E> void assertHaveExactly(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
440        arrays.assertHaveExactly(info, failures, conditions, actual, times, condition);
441      }
442    
443      /**
444       * An alias method of {@link #assertAreNotExactly(AssertionInfo, Object[], int, Condition)} to provide a richer fluent api
445       * (same logic, only error message differs).
446       */
447      public <E> void assertDoNotHaveExactly(AssertionInfo info, Object[] actual, int times, Condition<E> condition) {
448        arrays.assertDoNotHaveExactly(info, failures, conditions, actual, times, condition);
449      }
450    
451      /**
452       * Concrete implementation of {@link ArraySortedAssert#isSorted()}.
453       * 
454       * @param info contains information about the assertion.
455       * @param actual the given array.
456       */
457      public void assertIsSorted(AssertionInfo info, Object[] actual) {
458        arrays.assertIsSorted(info, failures, actual);
459      }
460    
461      /**
462       * Concrete implementation of {@link ArraySortedAssert#isSortedAccordingTo(Comparator)}.
463       * 
464       * @param info contains information about the assertion.
465       * @param actual the given array.
466       * @param comparator the {@link Comparator} used to compare array elements
467       */
468      public void assertIsSortedAccordingToComparator(AssertionInfo info, Object[] actual,
469          Comparator<? extends Object> comparator) {
470        Arrays.assertIsSortedAccordingToComparator(info, failures, actual, comparator);
471      }
472    }