001 /*
002 * Created on Nov 18, 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.Collection;
018 import java.util.Comparator;
019
020 import org.fest.assertions.core.Condition;
021 import org.fest.assertions.core.ObjectEnumerableAssert;
022 import org.fest.assertions.internal.Iterables;
023 import org.fest.util.ComparatorBasedComparisonStrategy;
024 import org.fest.util.VisibleForTesting;
025
026 /**
027 * Base class for implementations of <code>{@link ObjectEnumerableAssert}</code> whose actual value type is
028 * <code>{@link Collection}</code>.
029 * @param <S> the "self" type of this assertion class. Please read
030 * "<a href="http://bit.ly/anMa4g" target="_blank">Emulating 'self types' using Java Generics to simplify fluent
031 * API implementation</a>" for more details.
032 * @param <A> the type of the "actual" value.
033 *
034 * @author Yvonne Wang
035 * @author Alex Ruiz
036 * @author Mathieu Baechler
037 * @author Joel Costigliola
038 * @author Maciej Jaskowski
039 * @author Nicolas François
040 */
041 public abstract class AbstractIterableAssert<S, A extends Iterable<?> > extends AbstractAssert<S, A> implements
042 ObjectEnumerableAssert<S> {
043
044 @VisibleForTesting Iterables iterables = Iterables.instance();
045
046 protected AbstractIterableAssert(A actual, Class<S> selfType) {
047 super(actual, selfType);
048 }
049
050 /** {@inheritDoc} */
051 public final void isNullOrEmpty() {
052 iterables.assertNullOrEmpty(info, actual);
053 }
054
055 /** {@inheritDoc} */
056 public final void isEmpty() {
057 iterables.assertEmpty(info, actual);
058 }
059
060 /** {@inheritDoc} */
061 public final S isNotEmpty() {
062 iterables.assertNotEmpty(info, actual);
063 return myself;
064 }
065
066 /** {@inheritDoc} */
067 public final S hasSize(int expected) {
068 iterables.assertHasSize(info, actual, expected);
069 return myself;
070 }
071
072 /** {@inheritDoc} */
073 public final S contains(Object... values) {
074 iterables.assertContains(info, actual, values);
075 return myself;
076 }
077
078 /** {@inheritDoc} */
079 public final S containsOnly(Object... values) {
080 iterables.assertContainsOnly(info, actual, values);
081 return myself;
082 }
083
084 /**
085 * Verifies that all the elements of the actual {@code Iterable} are present in the given {@code Iterable}.
086 * @param values the {@code Iterable} that should contain all actual elements.
087 * @return this assertion object.
088 * @throws AssertionError if the actual {@code Iterable} is {@code null}.
089 * @throws NullPointerException if the given {@code Iterable} is {@code null}.
090 * @throws AssertionError if the actual {@code Iterable} is not subset of set {@code Iterable}.
091 */
092 public final S isSubsetOf(Iterable<?> values) {
093 iterables.assertIsSubsetOf(info, actual, values);
094 return myself;
095 }
096
097 /** {@inheritDoc} */
098 public final S containsSequence(Object... sequence) {
099 iterables.assertContainsSequence(info, actual, sequence);
100 return myself;
101 }
102
103 /** {@inheritDoc} */
104 public final S doesNotContain(Object... values) {
105 iterables.assertDoesNotContain(info, actual, values);
106 return myself;
107 }
108
109 /** {@inheritDoc} */
110 public final S doesNotHaveDuplicates() {
111 iterables.assertDoesNotHaveDuplicates(info, actual);
112 return myself;
113 }
114
115 /** {@inheritDoc} */
116 public final S startsWith(Object... sequence) {
117 iterables.assertStartsWith(info, actual, sequence);
118 return myself;
119 }
120
121 /** {@inheritDoc} */
122 public final S endsWith(Object... sequence) {
123 iterables.assertEndsWith(info, actual, sequence);
124 return myself;
125 }
126
127 /** {@inheritDoc} */
128 public S containsNull() {
129 iterables.assertContainsNull(info, actual);
130 return myself;
131 }
132
133 /** {@inheritDoc} */
134 public S doesNotContainNull() {
135 iterables.assertDoesNotContainNull(info, actual);
136 return myself;
137 }
138
139 /** {@inheritDoc} */
140 public <E> S are(Condition<E> condition) {
141 iterables.assertAre(info, actual, condition);
142 return myself;
143 }
144
145 /** {@inheritDoc} */
146 public <E> S areNot(Condition<E> condition) {
147 iterables.assertAreNot(info, actual, condition);
148 return myself;
149 }
150
151 /** {@inheritDoc} */
152 public <E> S have(Condition<E> condition) {
153 iterables.assertHave(info, actual, condition);
154 return myself;
155 }
156
157 /** {@inheritDoc} */
158 public <E> S doNotHave(Condition<E> condition) {
159 iterables.assertDoNotHave(info, actual, condition);
160 return myself;
161 }
162
163 /** {@inheritDoc} */
164 public <E> S areAtLeast(int times, Condition<E> condition) {
165 iterables.assertAreAtLeast(info, actual, times, condition);
166 return myself;
167 }
168
169 /** {@inheritDoc} */
170 public <E> S areNotAtLeast(int times, Condition<E> condition) {
171 iterables.assertAreNotAtLeast(info, actual, times, condition);
172 return myself;
173 }
174
175 /** {@inheritDoc} */
176 public <E> S areAtMost(int times, Condition<E> condition) {
177 iterables.assertAreAtMost(info, actual, times, condition);
178 return myself;
179 }
180
181 /** {@inheritDoc} */
182 public <E> S areNotAtMost(int times, Condition<E> condition) {
183 iterables.assertAreNotAtMost(info, actual, times, condition);
184 return myself;
185 }
186
187 /** {@inheritDoc} */
188 public <E> S areExactly(int times, Condition<E> condition) {
189 iterables.assertAreExactly(info, actual, times, condition);
190 return myself;
191 }
192
193 /** {@inheritDoc} */
194 public <E> S areNotExactly(int times, Condition<E> condition) {
195 iterables.assertAreNotExactly(info, actual, times, condition);
196 return myself;
197 }
198
199 /** {@inheritDoc} */
200 public <E> S haveAtLeast(int times, Condition<E> condition) {
201 iterables.assertHaveAtLeast(info, actual, times, condition);
202 return myself;
203 }
204
205 /** {@inheritDoc} */
206 public <E> S doNotHaveAtLeast(int times, Condition<E> condition) {
207 iterables.assertDoNotHaveAtLeast(info, actual, times, condition);
208 return myself;
209 }
210
211 /** {@inheritDoc} */
212 public <E> S haveAtMost(int times, Condition<E> condition) {
213 iterables.assertHaveAtMost(info, actual, times, condition);
214 return myself;
215 }
216
217 /** {@inheritDoc} */
218 public <E> S doNotHaveAtMost(int times, Condition<E> condition) {
219 iterables.assertDoNotHaveAtMost(info, actual, times, condition);
220 return myself;
221 }
222
223 /** {@inheritDoc} */
224 public <E> S haveExactly(int times, Condition<E> condition) {
225 iterables.assertHaveExactly(info, actual, times, condition);
226 return myself;
227 }
228 /** {@inheritDoc} */
229 public <E> S doNotHaveExactly(int times, Condition<E> condition) {
230 iterables.assertDoNotHaveExactly(info, actual, times, condition);
231 return myself;
232 }
233
234
235 @Override
236 public S usingComparator(Comparator<?> customComparator) {
237 super.usingComparator(customComparator);
238 this.iterables = new Iterables(new ComparatorBasedComparisonStrategy(customComparator));
239 return myself;
240 }
241
242 @Override
243 public S usingDefaultComparator() {
244 super.usingDefaultComparator();
245 this.iterables = Iterables.instance();
246 return myself;
247 }
248 }