001    /*
002     * Created on Dec 14, 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.EnumerableAssert;
021    import org.fest.assertions.data.Index;
022    import org.fest.assertions.internal.IntArrays;
023    import org.fest.util.ComparatorBasedComparisonStrategy;
024    import org.fest.util.VisibleForTesting;
025    
026    /**
027     * Assertion methods for arrays of {@code int}s.
028     * <p>
029     * To create an instance of this class, invoke <code>{@link Assertions#assertThat(int[])}</code>.
030     * </p>
031     * 
032     * @author Yvonne Wang
033     * @author Alex Ruiz
034     * @author Joel Costigliola
035     * @author Mikhail Mazursky
036     */
037    public class IntArrayAssert extends AbstractAssert<IntArrayAssert, int[]> implements EnumerableAssert<IntArrayAssert, Integer>,
038        ArraySortedAssert<IntArrayAssert, Integer> {
039    
040      @VisibleForTesting
041      IntArrays arrays = IntArrays.instance();
042    
043      protected IntArrayAssert(int[] actual) {
044        super(actual, IntArrayAssert.class);
045      }
046    
047      /** {@inheritDoc} */
048      public void isNullOrEmpty() {
049        arrays.assertNullOrEmpty(info, actual);
050      }
051    
052      /** {@inheritDoc} */
053      public void isEmpty() {
054        arrays.assertEmpty(info, actual);
055      }
056    
057      /** {@inheritDoc} */
058      public IntArrayAssert isNotEmpty() {
059        arrays.assertNotEmpty(info, actual);
060        return this;
061      }
062    
063      /** {@inheritDoc} */
064      public IntArrayAssert hasSize(int expected) {
065        arrays.assertHasSize(info, actual, expected);
066        return this;
067      }
068    
069      /**
070       * Verifies that the actual array contains the given values, in any order.
071       * @param values the given values.
072       * @return {@code this} assertion object.
073       * @throws NullPointerException if the given argument is {@code null}.
074       * @throws IllegalArgumentException if the given argument is an empty array.
075       * @throws AssertionError if the actual array is {@code null}.
076       * @throws AssertionError if the actual array does not contain the given values.
077       */
078      public IntArrayAssert contains(int... values) {
079        arrays.assertContains(info, actual, values);
080        return this;
081      }
082    
083      /**
084       * Verifies that the actual array contains only the given values and nothing else, in any order.
085       * @param values the given values.
086       * @return {@code this} assertion object.
087       * @throws NullPointerException if the given argument is {@code null}.
088       * @throws IllegalArgumentException if the given argument is an empty array.
089       * @throws AssertionError if the actual array is {@code null}.
090       * @throws AssertionError if the actual array does not contain the given values, i.e. the actual array contains some
091       *           or none of the given values, or the actual array contains more values than the given ones.
092       */
093      public IntArrayAssert containsOnly(int... values) {
094        arrays.assertContainsOnly(info, actual, values);
095        return this;
096      }
097    
098      /**
099       * Verifies that the actual array contains the given sequence, without any other values between them.
100       * @param sequence the sequence of values to look for.
101       * @return this assertion object.
102       * @throws AssertionError if the actual array is {@code null}.
103       * @throws AssertionError if the given array is {@code null}.
104       * @throws AssertionError if the actual array does not contain the given sequence.
105       */
106      public IntArrayAssert containsSequence(int... sequence) {
107        arrays.assertContainsSequence(info, actual, sequence);
108        return this;
109      }
110    
111      /**
112       * Verifies that the actual array contains the given value at the given index.
113       * @param value the value to look for.
114       * @param index the index where the value should be stored in the actual array.
115       * @return this assertion object.
116       * @throws AssertionError if the actual array is {@code null} or empty.
117       * @throws NullPointerException if the given {@code Index} is {@code null}.
118       * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of
119       *           the actual array.
120       * @throws AssertionError if the actual array does not contain the given value at the given index.
121       */
122      public IntArrayAssert contains(int value, Index index) {
123        arrays.assertContains(info, actual, value, index);
124        return this;
125      }
126    
127      /**
128       * Verifies that the actual array does not contain the given values.
129       * @param values the given values.
130       * @return {@code this} assertion object.
131       * @throws NullPointerException if the given argument is {@code null}.
132       * @throws IllegalArgumentException if the given argument is an empty array.
133       * @throws AssertionError if the actual array is {@code null}.
134       * @throws AssertionError if the actual array contains any of the given values.
135       */
136      public IntArrayAssert doesNotContain(int... values) {
137        arrays.assertDoesNotContain(info, actual, values);
138        return this;
139      }
140    
141      /**
142       * Verifies that the actual array does not contain the given value at the given index.
143       * @param value the value to look for.
144       * @param index the index where the value should be stored in the actual array.
145       * @return this assertion object.
146       * @throws AssertionError if the actual array is {@code null}.
147       * @throws NullPointerException if the given {@code Index} is {@code null}.
148       * @throws AssertionError if the actual array contains the given value at the given index.
149       */
150      public IntArrayAssert doesNotContain(int value, Index index) {
151        arrays.assertDoesNotContain(info, actual, value, index);
152        return this;
153      }
154    
155      /**
156       * Verifies that the actual array does not contain duplicates.
157       * @return {@code this} assertion object.
158       * @throws AssertionError if the actual array is {@code null}.
159       * @throws AssertionError if the actual array contains duplicates.
160       */
161      public IntArrayAssert doesNotHaveDuplicates() {
162        arrays.assertDoesNotHaveDuplicates(info, actual);
163        return this;
164      }
165    
166      /**
167       * Verifies that the actual array starts with the given sequence of values, without any other values between them.
168       * Similar to <code>{@link #containsSequence(int...)}</code>, but it also verifies that the first element in the
169       * sequence is also first element of the actual array.
170       * @param sequence the sequence of values to look for.
171       * @return this assertion object.
172       * @throws NullPointerException if the given argument is {@code null}.
173       * @throws IllegalArgumentException if the given argument is an empty array.
174       * @throws AssertionError if the actual array is {@code null}.
175       * @throws AssertionError if the actual array does not start with the given sequence.
176       */
177      public IntArrayAssert startsWith(int... sequence) {
178        arrays.assertStartsWith(info, actual, sequence);
179        return this;
180      }
181    
182      /**
183       * Verifies that the actual array ends with the given sequence of values, without any other values between them.
184       * Similar to <code>{@link #containsSequence(int...)}</code>, but it also verifies that the last element in the
185       * sequence is also last element of the actual array.
186       * @param sequence the sequence of values to look for.
187       * @return this assertion object.
188       * @throws NullPointerException if the given argument is {@code null}.
189       * @throws IllegalArgumentException if the given argument is an empty array.
190       * @throws AssertionError if the actual array is {@code null}.
191       * @throws AssertionError if the actual array does not end with the given sequence.
192       */
193      public IntArrayAssert endsWith(int... sequence) {
194        arrays.assertEndsWith(info, actual, sequence);
195        return this;
196      }
197    
198      /** {@inheritDoc} */
199      public IntArrayAssert isSorted() {
200        arrays.assertIsSorted(info, actual);
201        return this;
202      }
203    
204      /** {@inheritDoc} */
205      public IntArrayAssert isSortedAccordingTo(Comparator<? super Integer> comparator) {
206        arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
207        return this;
208      }
209    
210      /** {@inheritDoc} */
211      public IntArrayAssert usingElementComparator(Comparator<? super Integer> customComparator) {
212        this.arrays = new IntArrays(new ComparatorBasedComparisonStrategy(customComparator));
213        return myself;
214      }
215    
216      /** {@inheritDoc} */
217      public IntArrayAssert usingDefaultElementComparator() {
218        this.arrays = IntArrays.instance();
219        return myself;
220      }
221    }