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