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