001 /*
002 * Created on Dec 21, 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.ShortArrays;
023 import org.fest.util.ComparatorBasedComparisonStrategy;
024 import org.fest.util.VisibleForTesting;
025
026 /**
027 * Assertion methods for arrays of {@code short}s.
028 * <p>
029 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(short[])}</code>.
030 * </p>
031 *
032 * @author Yvonne Wang
033 * @author Alex Ruiz
034 * @author Joel Costigliola
035 * @author Mikhail Mazursky
036 */
037 public class ShortArrayAssert extends AbstractAssert<ShortArrayAssert, short[]> implements
038 EnumerableAssert<ShortArrayAssert, Short>, ArraySortedAssert<ShortArrayAssert, Short> {
039
040 @VisibleForTesting
041 ShortArrays arrays = ShortArrays.instance();
042
043 protected ShortArrayAssert(short[] actual) {
044 super(actual, ShortArrayAssert.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 ShortArrayAssert isNotEmpty() {
059 arrays.assertNotEmpty(info, actual);
060 return this;
061 }
062
063 /** {@inheritDoc} */
064 public ShortArrayAssert 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 ShortArrayAssert contains(short... 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 ShortArrayAssert containsOnly(short... 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 ShortArrayAssert containsSequence(short... 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 ShortArrayAssert contains(short 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 ShortArrayAssert doesNotContain(short... 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 ShortArrayAssert doesNotContain(short 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 ShortArrayAssert 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(short...)}</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 ShortArrayAssert startsWith(short... 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(short...)}</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 ShortArrayAssert endsWith(short... sequence) {
194 arrays.assertEndsWith(info, actual, sequence);
195 return this;
196 }
197
198 /** {@inheritDoc} */
199 public ShortArrayAssert isSorted() {
200 arrays.assertIsSorted(info, actual);
201 return this;
202 }
203
204 /** {@inheritDoc} */
205 public ShortArrayAssert isSortedAccordingTo(Comparator<? super Short> comparator) {
206 arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
207 return this;
208 }
209
210 /** {@inheritDoc} */
211 public ShortArrayAssert usingElementComparator(Comparator<? super Short> customComparator) {
212 this.arrays = new ShortArrays(new ComparatorBasedComparisonStrategy(customComparator));
213 return myself;
214 }
215
216 /** {@inheritDoc} */
217 public ShortArrayAssert usingDefaultElementComparator() {
218 this.arrays = ShortArrays.instance();
219 return myself;
220 }
221 }