001 /*
002 * Created on Jul 26, 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.Condition;
021 import org.fest.assertions.core.IndexedObjectEnumerableAssert;
022 import org.fest.assertions.core.ObjectEnumerableAssert;
023 import org.fest.assertions.data.Index;
024 import org.fest.assertions.internal.ObjectArrays;
025 import org.fest.util.ComparatorBasedComparisonStrategy;
026 import org.fest.util.VisibleForTesting;
027
028 /**
029 * Assertion methods for arrays of objects.
030 * <p>
031 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Object[])}</code>.
032 * </p>
033 *
034 * @author Yvonne Wang
035 * @author Alex Ruiz
036 * @author Joel Costigliola
037 * @author Nicolas François
038 */
039 public class ObjectArrayAssert extends AbstractAssert<ObjectArrayAssert, Object[]> implements
040 ObjectEnumerableAssert<ObjectArrayAssert>, IndexedObjectEnumerableAssert, ArraySortedAssert<ObjectArrayAssert, Object> {
041
042 @VisibleForTesting
043 ObjectArrays arrays = ObjectArrays.instance();
044
045 protected ObjectArrayAssert(Object[] actual) {
046 super(actual, ObjectArrayAssert.class);
047 }
048
049 /** {@inheritDoc} */
050 public void isNullOrEmpty() {
051 arrays.assertNullOrEmpty(info, actual);
052 }
053
054 /** {@inheritDoc} */
055 public void isEmpty() {
056 arrays.assertEmpty(info, actual);
057 }
058
059 /** {@inheritDoc} */
060 public ObjectArrayAssert isNotEmpty() {
061 arrays.assertNotEmpty(info, actual);
062 return this;
063 }
064
065 /** {@inheritDoc} */
066 public ObjectArrayAssert hasSize(int expected) {
067 arrays.assertHasSize(info, actual, expected);
068 return this;
069 }
070
071 /** {@inheritDoc} */
072 public ObjectArrayAssert hasSameSizeAs(Object[] other) {
073 arrays.assertHasSameSizeAs(info, actual, other);
074 return this;
075 }
076
077 /** {@inheritDoc} */
078 public ObjectArrayAssert hasSameSizeAs(Iterable<?> other) {
079 arrays.assertHasSameSizeAs(info, actual, other);
080 return this;
081 }
082
083 /** {@inheritDoc} */
084 public ObjectArrayAssert contains(Object... values) {
085 arrays.assertContains(info, actual, values);
086 return this;
087 }
088
089 /** {@inheritDoc} */
090 public ObjectArrayAssert containsOnly(Object... values) {
091 arrays.assertContainsOnly(info, actual, values);
092 return this;
093 }
094
095 /** {@inheritDoc} */
096 public ObjectArrayAssert containsSequence(Object... sequence) {
097 arrays.assertContainsSequence(info, actual, sequence);
098 return this;
099 }
100
101 /** {@inheritDoc} */
102 public ObjectArrayAssert contains(Object value, Index index) {
103 arrays.assertContains(info, actual, value, index);
104 return this;
105 }
106
107 /** {@inheritDoc} */
108 public ObjectArrayAssert doesNotContain(Object value, Index index) {
109 arrays.assertDoesNotContain(info, actual, value, index);
110 return this;
111 }
112
113 /** {@inheritDoc} */
114 public ObjectArrayAssert doesNotContain(Object... values) {
115 arrays.assertDoesNotContain(info, actual, values);
116 return this;
117 }
118
119 /** {@inheritDoc} */
120 public ObjectArrayAssert doesNotHaveDuplicates() {
121 arrays.assertDoesNotHaveDuplicates(info, actual);
122 return this;
123 }
124
125 /** {@inheritDoc} */
126 public ObjectArrayAssert startsWith(Object... sequence) {
127 arrays.assertStartsWith(info, actual, sequence);
128 return this;
129 }
130
131 /** {@inheritDoc} */
132 public ObjectArrayAssert endsWith(Object... sequence) {
133 arrays.assertEndsWith(info, actual, sequence);
134 return this;
135 }
136
137 /** {@inheritDoc} */
138 public ObjectArrayAssert containsNull() {
139 arrays.assertContainsNull(info, actual);
140 return this;
141 }
142
143 /** {@inheritDoc} */
144 public ObjectArrayAssert doesNotContainNull() {
145 arrays.assertDoesNotContainNull(info, actual);
146 return this;
147 }
148
149 /** {@inheritDoc} */
150 public <E> ObjectArrayAssert are(Condition<E> condition) {
151 arrays.assertAre(info, actual, condition);
152 return myself;
153 }
154
155 /** {@inheritDoc} */
156 public <E> ObjectArrayAssert areNot(Condition<E> condition) {
157 arrays.assertAreNot(info, actual, condition);
158 return myself;
159 }
160
161 /** {@inheritDoc} */
162 public <E> ObjectArrayAssert have(Condition<E> condition) {
163 arrays.assertHave(info, actual, condition);
164 return myself;
165 }
166
167 /** {@inheritDoc} */
168 public <E> ObjectArrayAssert doNotHave(Condition<E> condition) {
169 arrays.assertDoNotHave(info, actual, condition);
170 return myself;
171 }
172
173 /** {@inheritDoc} */
174 public <E> ObjectArrayAssert areAtLeast(int times, Condition<E> condition) {
175 arrays.assertAreAtLeast(info, actual, times, condition);
176 return myself;
177 }
178
179 /** {@inheritDoc} */
180 public <E> ObjectArrayAssert areNotAtLeast(int times, Condition<E> condition) {
181 arrays.assertAreNotAtLeast(info, actual, times, condition);
182 return myself;
183 }
184
185 /** {@inheritDoc} */
186 public <E> ObjectArrayAssert areAtMost(int times, Condition<E> condition) {
187 arrays.assertAreAtMost(info, actual, times, condition);
188 return myself;
189 }
190
191 /** {@inheritDoc} */
192 public <E> ObjectArrayAssert areNotAtMost(int times, Condition<E> condition) {
193 arrays.assertAreNotAtMost(info, actual, times, condition);
194 return myself;
195 }
196
197 /** {@inheritDoc} */
198 public <E> ObjectArrayAssert areExactly(int times, Condition<E> condition) {
199 arrays.assertAreExactly(info, actual, times, condition);
200 return myself;
201 }
202
203 /** {@inheritDoc} */
204 public <E> ObjectArrayAssert areNotExactly(int times, Condition<E> condition) {
205 arrays.assertAreNotExactly(info, actual, times, condition);
206 return myself;
207 }
208
209 /** {@inheritDoc} */
210 public <E> ObjectArrayAssert haveAtLeast(int times, Condition<E> condition) {
211 arrays.assertHaveAtLeast(info, actual, times, condition);
212 return myself;
213 }
214
215 /** {@inheritDoc} */
216 public <E> ObjectArrayAssert doNotHaveAtLeast(int times, Condition<E> condition) {
217 arrays.assertDoNotHaveAtLeast(info, actual, times, condition);
218 return myself;
219 }
220
221 /** {@inheritDoc} */
222 public <E> ObjectArrayAssert haveAtMost(int times, Condition<E> condition) {
223 arrays.assertHaveAtMost(info, actual, times, condition);
224 return myself;
225 }
226
227 /** {@inheritDoc} */
228 public <E> ObjectArrayAssert doNotHaveAtMost(int times, Condition<E> condition) {
229 arrays.assertDoNotHaveAtMost(info, actual, times, condition);
230 return myself;
231 }
232
233 /** {@inheritDoc} */
234 public <E> ObjectArrayAssert haveExactly(int times, Condition<E> condition) {
235 arrays.assertHaveExactly(info, actual, times, condition);
236 return myself;
237 }
238
239 /** {@inheritDoc} */
240 public <E> ObjectArrayAssert doNotHaveExactly(int times, Condition<E> condition) {
241 arrays.assertDoNotHaveExactly(info, actual, times, condition);
242 return myself;
243 }
244
245 /** {@inheritDoc} */
246 public ObjectArrayAssert isSorted() {
247 arrays.assertIsSorted(info, actual);
248 return this;
249 }
250
251 /** {@inheritDoc} */
252 public ObjectArrayAssert isSortedAccordingTo(Comparator<? extends Object> comparator) {
253 arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
254 return this;
255 }
256
257 @Override
258 public ObjectArrayAssert usingComparator(Comparator<?> customComparator) {
259 super.usingComparator(customComparator);
260 this.arrays = new ObjectArrays(new ComparatorBasedComparisonStrategy(customComparator));
261 return myself;
262 }
263
264 @Override
265 public ObjectArrayAssert usingDefaultComparator() {
266 super.usingDefaultComparator();
267 this.arrays = ObjectArrays.instance();
268 return myself;
269 }
270
271
272
273 }