001 /* 002 * Created on Dec 27, 2006 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with 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 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2006-2009 the original author or authors. 015 */ 016 package org.fest.assertions; 017 018 import java.awt.image.BufferedImage; 019 import java.io.File; 020 import java.math.BigDecimal; 021 import java.util.*; 022 023 /** 024 * Understands an entry point for assertion methods for different data types. Each method in this class is a static 025 * factory for the type-specific assertion objects. The purpose of this class is to make test code more readable. 026 * <p> 027 * For example: 028 * <pre> 029 * int removed = employees.removeFired(); 030 * {@link org.fest.assertions.Assertions#assertThat(int) assertThat}(removed).{@link org.fest.assertions.IntAssert#isZero isZero}(); 031 * 032 * List<Employee> newEmployees = employees.hired(TODAY); 033 * {@link org.fest.assertions.Assertions#assertThat(java.util.Collection) assertThat}(newEmployees).{@link org.fest.assertions.CollectionAssert#hasSize(int) hasSize}(6); 034 * </pre> 035 * </p> 036 * 037 * @author Alex Ruiz 038 * @author Yvonne Wang 039 * @author David DIDIER 040 * @author Ted Young 041 */ 042 public class Assertions { 043 044 private static <T> Collection<T> asCollection(Iterator<T> iterator) { 045 List<T> list = new ArrayList<T>(); 046 while (iterator.hasNext()) list.add(iterator.next()); 047 return list; 048 } 049 050 /** 051 * Creates a new instance of <code>{@link BigDecimalAssert}</code>. 052 * @param actual the value to be the target of the assertions methods. 053 * @return the created assertion object. 054 */ 055 public static BigDecimalAssert assertThat(BigDecimal actual) { 056 return new BigDecimalAssert(actual); 057 } 058 059 /** 060 * Creates a new instance of <code>{@link BooleanAssert}</code>. 061 * @param actual the value to be the target of the assertions methods. 062 * @return the created assertion object. 063 */ 064 public static BooleanAssert assertThat(boolean actual) { 065 return new BooleanAssert(actual); 066 } 067 068 /** 069 * Creates a new instance of <code>{@link BooleanAssert}</code>. 070 * @param actual the value to be the target of the assertions methods. 071 * @return the created assertion object. 072 */ 073 public static BooleanAssert assertThat(Boolean actual) { 074 return new BooleanAssert(actual); 075 } 076 077 /** 078 * Creates a new instance of <code>{@link BooleanArrayAssert}</code>. 079 * @param actual the value to be the target of the assertions methods. 080 * @return the created assertion object. 081 */ 082 public static BooleanArrayAssert assertThat(boolean[] actual) { 083 return new BooleanArrayAssert(actual); 084 } 085 086 /** 087 * Creates a new instance of <code>{@link ImageAssert}</code>. 088 * @param actual the value to be the target of the assertions methods. 089 * @return the created assertion object. 090 */ 091 public static ImageAssert assertThat(BufferedImage actual) { 092 return new ImageAssert(actual); 093 } 094 095 /** 096 * Creates a new instance of <code>{@link ByteAssert}</code>. 097 * @param actual the value to be the target of the assertions methods. 098 * @return the created assertion object. 099 */ 100 public static ByteAssert assertThat(byte actual) { 101 return new ByteAssert(actual); 102 } 103 104 /** 105 * Creates a new instance of <code>{@link ByteAssert}</code>. 106 * @param actual the value to be the target of the assertions methods. 107 * @return the created assertion object. 108 */ 109 public static ByteAssert assertThat(Byte actual) { 110 return new ByteAssert(actual); 111 } 112 113 /** 114 * Creates a new instance of <code>{@link ByteArrayAssert}</code>. 115 * @param actual the value to be the target of the assertions methods. 116 * @return the created assertion object. 117 */ 118 public static ByteArrayAssert assertThat(byte[] actual) { 119 return new ByteArrayAssert(actual); 120 } 121 122 /** 123 * Creates a new instance of <code>{@link CharAssert}</code>. 124 * @param actual the value to be the target of the assertions methods. 125 * @return the created assertion object. 126 */ 127 public static CharAssert assertThat(char actual) { 128 return new CharAssert(actual); 129 } 130 131 /** 132 * Creates a new instance of <code>{@link CharAssert}</code>. 133 * @param actual the value to be the target of the assertions methods. 134 * @return the created assertion object. 135 */ 136 public static CharAssert assertThat(Character actual) { 137 return new CharAssert(actual); 138 } 139 140 /** 141 * Creates a new instance of <code>{@link CharArrayAssert}</code>. 142 * @param actual the value to be the target of the assertions methods. 143 * @return the created assertion object. 144 */ 145 public static CharArrayAssert assertThat(char[] actual) { 146 return new CharArrayAssert(actual); 147 } 148 149 /** 150 * Creates a new instance of <code>{@link CollectionAssert}</code>. 151 * @param actual the value to be the target of the assertions methods. 152 * @return the created assertion object. 153 */ 154 public static CollectionAssert assertThat(Collection<?> actual) { 155 return new CollectionAssert(actual); 156 } 157 158 /** 159 * Creates a new instance of <code>{@link ListAssert}</code>. 160 * @param actual the value to be the target of the assertions methods. 161 * @return the created assertion object. 162 * @since 1.1 163 */ 164 public static ListAssert assertThat(List<?> actual) { 165 return new ListAssert(actual); 166 } 167 168 /** 169 * Creates a new instance of <code>{@link DoubleAssert}</code>. 170 * @param actual the value to be the target of the assertions methods. 171 * @return the created assertion object. 172 */ 173 public static DoubleAssert assertThat(double actual) { 174 return new DoubleAssert(actual); 175 } 176 177 /** 178 * Creates a new instance of <code>{@link DoubleAssert}</code>. 179 * @param actual the value to be the target of the assertions methods. 180 * @return the created assertion object. 181 */ 182 public static DoubleAssert assertThat(Double actual) { 183 return new DoubleAssert(actual); 184 } 185 186 /** 187 * Creates a new instance of <code>{@link DoubleArrayAssert}</code>. 188 * @param actual the value to be the target of the assertions methods. 189 * @return the created assertion object. 190 */ 191 public static DoubleArrayAssert assertThat(double[] actual) { 192 return new DoubleArrayAssert(actual); 193 } 194 195 /** 196 * Creates a new instance of <code>{@link FileAssert}</code>. 197 * @param actual the value to be the target of the assertions methods. 198 * @return the created assertion object. 199 */ 200 public static FileAssert assertThat(File actual) { 201 return new FileAssert(actual); 202 } 203 204 /** 205 * Creates a new instance of <code>{@link FloatAssert}</code>. 206 * @param actual the value to be the target of the assertions methods. 207 * @return the created assertion object. 208 */ 209 public static FloatAssert assertThat(float actual) { 210 return new FloatAssert(actual); 211 } 212 213 /** 214 * Creates a new instance of <code>{@link FloatAssert}</code>. 215 * @param actual the value to be the target of the assertions methods. 216 * @return the created assertion object. 217 */ 218 public static FloatAssert assertThat(Float actual) { 219 return new FloatAssert(actual); 220 } 221 222 /** 223 * Creates a new instance of <code>{@link FloatArrayAssert}</code>. 224 * @param actual the value to be the target of the assertions methods. 225 * @return the created assertion object. 226 */ 227 public static FloatArrayAssert assertThat(float[] actual) { 228 return new FloatArrayAssert(actual); 229 } 230 231 /** 232 * Creates a new instance of <code>{@link IntAssert}</code>. 233 * @param actual the value to be the target of the assertions methods. 234 * @return the created assertion object. 235 */ 236 public static IntAssert assertThat(int actual) { 237 return new IntAssert(actual); 238 } 239 240 /** 241 * Creates a new instance of <code>{@link IntAssert}</code>. 242 * @param actual the value to be the target of the assertions methods. 243 * @return the created assertion object. 244 */ 245 public static IntAssert assertThat(Integer actual) { 246 return new IntAssert(actual); 247 } 248 249 /** 250 * Creates a new instance of <code>{@link IntArrayAssert}</code>. 251 * @param actual the value to be the target of the assertions methods. 252 * @return the created assertion object. 253 */ 254 public static IntArrayAssert assertThat(int[] actual) { 255 return new IntArrayAssert(actual); 256 } 257 258 /** 259 * Creates a new instance of <code>{@link CollectionAssert}</code>. 260 * @param actual the value an <code>Iterator</code> that which contents will be added to a new <code>Collection</code>. 261 * @return the created assertion object. 262 */ 263 public static CollectionAssert assertThat(Iterator<?> actual) { 264 return assertThat(asCollection(actual)); 265 } 266 267 /** 268 * Creates a new instance of <code>{@link LongAssert}</code>. 269 * @param actual the value to be the target of the assertions methods. 270 * @return the created assertion object. 271 */ 272 public static LongAssert assertThat(long actual) { 273 return new LongAssert(actual); 274 } 275 276 /** 277 * Creates a new instance of <code>{@link LongAssert}</code>. 278 * @param actual the value to be the target of the assertions methods. 279 * @return the created assertion object. 280 */ 281 public static LongAssert assertThat(Long actual) { 282 return new LongAssert(actual); 283 } 284 285 /** 286 * Creates a new instance of <code>{@link LongArrayAssert}</code>. 287 * @param actual the value to be the target of the assertions methods. 288 * @return the created assertion object. 289 */ 290 public static LongArrayAssert assertThat(long[] actual) { 291 return new LongArrayAssert(actual); 292 } 293 294 /** 295 * Creates a new instance of <code>{@link MapAssert}</code>. 296 * @param actual the value to be the target of the assertions methods. 297 * @return the created assertion object. 298 */ 299 public static MapAssert assertThat(Map<?, ?> actual) { 300 return new MapAssert(actual); 301 } 302 303 /** 304 * Creates a new instance of <code>{@link ObjectAssert}</code>. 305 * @param actual the value to be the target of the assertions methods. 306 * @return the created assertion object. 307 */ 308 public static ObjectAssert assertThat(Object actual) { 309 return new ObjectAssert(actual); 310 } 311 312 /** 313 * Creates a new instance of <code>{@link ObjectArrayAssert}</code>. 314 * @param actual the value to be the target of the assertions methods. 315 * @return the created assertion object. 316 */ 317 public static ObjectArrayAssert assertThat(Object[] actual) { 318 return new ObjectArrayAssert(actual); 319 } 320 321 /** 322 * Creates a new instance of <code>{@link ShortAssert}</code>. 323 * @param actual the value to be the target of the assertions methods. 324 * @return the created assertion object. 325 */ 326 public static ShortAssert assertThat(short actual) { 327 return new ShortAssert(actual); 328 } 329 330 /** 331 * Creates a new instance of <code>{@link ShortAssert}</code>. 332 * @param actual the value to be the target of the assertions methods. 333 * @return the created assertion object. 334 */ 335 public static ShortAssert assertThat(Short actual) { 336 return new ShortAssert(actual); 337 } 338 339 /** 340 * Creates a new instance of <code>{@link ShortArrayAssert}</code>. 341 * @param actual the value to be the target of the assertions methods. 342 * @return the created assertion object. 343 */ 344 public static ShortArrayAssert assertThat(short[] actual) { 345 return new ShortArrayAssert(actual); 346 } 347 348 /** 349 * Creates a new instance of <code>{@link StringAssert}</code>. 350 * @param actual the value to be the target of the assertions methods. 351 * @return the created assertion object. 352 */ 353 public static StringAssert assertThat(String actual) { 354 return new StringAssert(actual); 355 } 356 357 /** 358 * Returns the given assertion. This method improves code readability by surrounding the given assertion with "<code>assertThat</code>". 359 * <p> 360 * For example, let's assume we have the following custom assertion class: 361 * 362 * <pre> 363 * public class ServerSocketAssertion implements AssertExtension { 364 * private final ServerSocket socket; 365 * 366 * public ServerSocketAssertion(ServerSocket socket) { 367 * this.socket = socket; 368 * } 369 * 370 * public ServerSocketAssert isConnectedTo(int port) { 371 * assertThat(socket.isBound()).isTrue(); 372 * assertThat(socket.getLocalPort()).isEqualTo(port); 373 * assertThat(socket.isClosed()).isFalse(); 374 * return this; 375 * } 376 * } 377 * </pre> 378 * </p> 379 * <p> 380 * We can wrap that assertion with "<code>assertThat</code>" to improve test code readability. 381 * <pre> 382 * ServerSocketAssertion socket = new ServerSocketAssertion(server.getSocket()); 383 * assertThat(socket).isConnectedTo(2000); 384 * </pre> 385 * </p> 386 * 387 * @param <T> the generic type of the user-defined assertion. 388 * @param assertion the assertion to return. 389 * @return the given assertion. 390 */ 391 public static <T extends AssertExtension> T assertThat(T assertion) { 392 return assertion; 393 } 394 395 /** 396 * Creates a new instance of <code>{@link ThrowableAssert}</code>. 397 * @param actual the value to be the target of the assertions methods. 398 * @return the created assertion object. 399 */ 400 public static ThrowableAssert assertThat(Throwable actual) { 401 return new ThrowableAssert(actual); 402 } 403 404 /** 405 * This constructor is protected to make it possible to subclass this class. Since all its methods are static, there 406 * is no point on creating a new instance of it. 407 */ 408 protected Assertions() {} 409 }