001 /* 002 * Created on Sep 30, 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.awt.image.BufferedImage; 018 import java.io.File; 019 import java.io.InputStream; 020 import java.math.BigDecimal; 021 import java.util.Date; 022 import java.util.List; 023 import java.util.Map; 024 025 import org.fest.assertions.data.MapEntry; 026 import org.fest.assertions.groups.Properties; 027 import org.fest.assertions.util.ImageReader; 028 029 /** 030 * Entry point for assertion methods for different data types. Each method in this class is a static factory for the 031 * type-specific assertion objects. The purpose of this class is to make test code more readable. 032 * <p> 033 * For example: 034 * 035 * <pre> 036 * int removed = employees.removeFired(); 037 * {@link Assertions#assertThat(int) assertThat}(removed).{@link IntegerAssert#isZero isZero}(); 038 * 039 * List<Employee> newEmployees = employees.hired(TODAY); 040 * {@link Assertions#assertThat(Iterable) assertThat}(newEmployees).{@link IterableAssert#hasSize(int) hasSize}(6); 041 * </pre> 042 * </p> 043 * 044 * @author Alex Ruiz 045 * @author Yvonne Wang 046 * @author David DIDIER 047 * @author Ted Young 048 * @author Joel Costigliola 049 * @author Matthieu Baechler 050 */ 051 public class Assertions { 052 053 /** 054 * Creates a new instance of <code>{@link BigDecimalAssert}</code>. 055 * @param actual the actual value. 056 * @return the created assertion object. 057 */ 058 public static BigDecimalAssert assertThat(BigDecimal actual) { 059 return new BigDecimalAssert(actual); 060 } 061 062 /** 063 * Creates a new instance of <code>{@link BooleanAssert}</code>. 064 * @param actual the actual value. 065 * @return the created assertion object. 066 */ 067 public static BooleanAssert assertThat(boolean actual) { 068 return new BooleanAssert(actual); 069 } 070 071 /** 072 * Creates a new instance of <code>{@link BooleanAssert}</code>. 073 * @param actual the actual value. 074 * @return the created assertion object. 075 */ 076 public static BooleanAssert assertThat(Boolean actual) { 077 return new BooleanAssert(actual); 078 } 079 080 /** 081 * Creates a new instance of <code>{@link BooleanArrayAssert}</code>. 082 * @param actual the actual value. 083 * @return the created assertion object. 084 */ 085 public static BooleanArrayAssert assertThat(boolean[] actual) { 086 return new BooleanArrayAssert(actual); 087 } 088 089 /** 090 * Creates a new instance of <code>{@link ImageAssert}</code>. To read an image from the file system use 091 * <code>{@link ImageReader#readImageFrom(String)}</code>. 092 * @param actual the actual value. 093 * @return the created assertion object. 094 */ 095 public static ImageAssert assertThat(BufferedImage actual) { 096 return new ImageAssert(actual); 097 } 098 099 /** 100 * Creates a new instance of <code>{@link ByteAssert}</code>. 101 * @param actual the actual value. 102 * @return the created assertion object. 103 */ 104 public static ByteAssert assertThat(byte actual) { 105 return new ByteAssert(actual); 106 } 107 108 /** 109 * Creates a new instance of <code>{@link ByteAssert}</code>. 110 * @param actual the actual value. 111 * @return the created assertion object. 112 */ 113 public static ByteAssert assertThat(Byte actual) { 114 return new ByteAssert(actual); 115 } 116 117 /** 118 * Creates a new instance of <code>{@link ByteArrayAssert}</code>. 119 * @param actual the actual value. 120 * @return the created assertion object. 121 */ 122 public static ByteArrayAssert assertThat(byte[] actual) { 123 return new ByteArrayAssert(actual); 124 } 125 126 /** 127 * Creates a new instance of <code>{@link CharacterAssert}</code>. 128 * @param actual the actual value. 129 * @return the created assertion object. 130 */ 131 public static CharacterAssert assertThat(char actual) { 132 return new CharacterAssert(actual); 133 } 134 135 /** 136 * Creates a new instance of <code>{@link CharArrayAssert}</code>. 137 * @param actual the actual value. 138 * @return the created assertion object. 139 */ 140 public static CharArrayAssert assertThat(char[] actual) { 141 return new CharArrayAssert(actual); 142 } 143 144 /** 145 * Creates a new instance of <code>{@link CharacterAssert}</code>. 146 * @param actual the actual value. 147 * @return the created assertion object. 148 */ 149 public static CharacterAssert assertThat(Character actual) { 150 return new CharacterAssert(actual); 151 } 152 153 /** 154 * Creates a new instance of <code>{@link IterableAssert}</code>. 155 * @param actual the actual value. 156 * @return the created assertion object. 157 */ 158 public static IterableAssert assertThat(Iterable<?> actual) { 159 return new IterableAssert(actual); 160 } 161 162 /** 163 * Creates a new instance of <code>{@link DoubleAssert}</code>. 164 * @param actual the actual value. 165 * @return the created assertion object. 166 */ 167 public static DoubleAssert assertThat(double actual) { 168 return new DoubleAssert(actual); 169 } 170 171 /** 172 * Creates a new instance of <code>{@link DoubleAssert}</code>. 173 * @param actual the actual value. 174 * @return the created assertion object. 175 */ 176 public static DoubleAssert assertThat(Double actual) { 177 return new DoubleAssert(actual); 178 } 179 180 /** 181 * Creates a new instance of <code>{@link DoubleArrayAssert}</code>. 182 * @param actual the actual value. 183 * @return the created assertion object. 184 */ 185 public static DoubleArrayAssert assertThat(double[] actual) { 186 return new DoubleArrayAssert(actual); 187 } 188 189 /** 190 * Creates a new instance of <code>{@link FileAssert}</code>. 191 * @param actual the actual value. 192 * @return the created assertion object. 193 */ 194 public static FileAssert assertThat(File actual) { 195 return new FileAssert(actual); 196 } 197 198 /** 199 * Creates a new instance of <code>{@link InputStreamAssert}</code>. 200 * @param actual the actual value. 201 * @return the created assertion object. 202 */ 203 public static InputStreamAssert assertThat(InputStream actual) { 204 return new InputStreamAssert(actual); 205 } 206 207 /** 208 * Creates a new instance of <code>{@link FloatAssert}</code>. 209 * @param actual the actual value. 210 * @return the created assertion object. 211 */ 212 public static FloatAssert assertThat(float actual) { 213 return new FloatAssert(actual); 214 } 215 216 /** 217 * Creates a new instance of <code>{@link FloatAssert}</code>. 218 * @param actual the actual value. 219 * @return the created assertion object. 220 */ 221 public static FloatAssert assertThat(Float actual) { 222 return new FloatAssert(actual); 223 } 224 225 /** 226 * Creates a new instance of <code>{@link FloatArrayAssert}</code>. 227 * @param actual the actual value. 228 * @return the created assertion object. 229 */ 230 public static FloatArrayAssert assertThat(float[] actual) { 231 return new FloatArrayAssert(actual); 232 } 233 234 /** 235 * Creates a new instance of <code>{@link IntegerAssert}</code>. 236 * @param actual the actual value. 237 * @return the created assertion object. 238 */ 239 public static IntegerAssert assertThat(int actual) { 240 return new IntegerAssert(actual); 241 } 242 243 /** 244 * Creates a new instance of <code>{@link IntArrayAssert}</code>. 245 * @param actual the actual value. 246 * @return the created assertion object. 247 */ 248 public static IntArrayAssert assertThat(int[] actual) { 249 return new IntArrayAssert(actual); 250 } 251 252 /** 253 * Creates a new instance of <code>{@link IntegerAssert}</code>. 254 * @param actual the actual value. 255 * @return the created assertion object. 256 */ 257 public static IntegerAssert assertThat(Integer actual) { 258 return new IntegerAssert(actual); 259 } 260 261 /** 262 * Creates a new instance of <code>{@link ListAssert}</code>. 263 * @param actual the actual value. 264 * @return the created assertion object. 265 */ 266 public static ListAssert assertThat(List<?> actual) { 267 return new ListAssert(actual); 268 } 269 270 /** 271 * Creates a new instance of <code>{@link LongAssert}</code>. 272 * @param actual the actual value. 273 * @return the created assertion object. 274 */ 275 public static LongAssert assertThat(long actual) { 276 return new LongAssert(actual); 277 } 278 279 /** 280 * Creates a new instance of <code>{@link LongAssert}</code>. 281 * @param actual the actual value. 282 * @return the created assertion object. 283 */ 284 public static LongAssert assertThat(Long actual) { 285 return new LongAssert(actual); 286 } 287 288 /** 289 * Creates a new instance of <code>{@link LongArrayAssert}</code>. 290 * @param actual the actual value. 291 * @return the created assertion object. 292 */ 293 public static LongArrayAssert assertThat(long[] actual) { 294 return new LongArrayAssert(actual); 295 } 296 297 /** 298 * Creates a new instance of <code>{@link ObjectAssert}</code>. 299 * @param actual the actual value. 300 * @return the created assertion object. 301 */ 302 public static ObjectAssert assertThat(Object actual) { 303 return new ObjectAssert(actual); 304 } 305 306 /** 307 * Creates a new instance of <code>{@link ObjectArrayAssert}</code>. 308 * @param actual the actual value. 309 * @return the created assertion object. 310 */ 311 public static ObjectArrayAssert assertThat(Object[] actual) { 312 return new ObjectArrayAssert(actual); 313 } 314 315 /** 316 * Creates a new instance of <code>{@link MapAssert}</code>. 317 * @param actual the actual value. 318 * @return the created assertion object. 319 */ 320 public static MapAssert assertThat(Map<?, ?> actual) { 321 return new MapAssert(actual); 322 } 323 324 /** 325 * Creates a new instance of <code>{@link ShortAssert}</code>. 326 * @param actual the actual value. 327 * @return the created assertion object. 328 */ 329 public static ShortAssert assertThat(short actual) { 330 return new ShortAssert(actual); 331 } 332 333 /** 334 * Creates a new instance of <code>{@link ShortAssert}</code>. 335 * @param actual the actual value. 336 * @return the created assertion object. 337 */ 338 public static ShortAssert assertThat(Short actual) { 339 return new ShortAssert(actual); 340 } 341 342 /** 343 * Creates a new instance of <code>{@link ShortArrayAssert}</code>. 344 * @param actual the actual value. 345 * @return the created assertion object. 346 */ 347 public static ShortArrayAssert assertThat(short[] actual) { 348 return new ShortArrayAssert(actual); 349 } 350 351 /** 352 * Creates a new instance of <code>{@link StringAssert}</code>. 353 * @param actual the actual value. 354 * @return the created assertion object. 355 */ 356 public static StringAssert assertThat(String actual) { 357 return new StringAssert(actual); 358 } 359 360 /** 361 * Creates a new instance of <code>{@link DateAssert}</code>. 362 * @param actual the actual value. 363 * @return the created assertion object. 364 */ 365 public static DateAssert assertThat(Date actual) { 366 return new DateAssert(actual); 367 } 368 369 /** 370 * Creates a new instance of <code>{@link ThrowableAssert}</code>. 371 * @param actual the actual value. 372 * @return the created assertion Throwable. 373 */ 374 public static ThrowableAssert assertThat(Throwable actual) { 375 return new ThrowableAssert(actual); 376 } 377 378 // ------------------------------------------------------------------------------------------------- 379 // fail methods : not assertions but here to have a single entry point to all Fest Assert features. 380 // ------------------------------------------------------------------------------------------------- 381 382 /** 383 * Only delegate to {@link Fail#setRemoveFestRelatedElementsFromStackTrace(boolean)} so that Assertions offers a full 384 * feature entry point to all Fest Assert features (but you can use Fail if you prefer). 385 */ 386 public static void setRemoveFestRelatedElementsFromStackTrace(boolean removeFestRelatedElementsFromStackTrace) { 387 Fail.setRemoveFestRelatedElementsFromStackTrace(removeFestRelatedElementsFromStackTrace); 388 } 389 390 /** 391 * Only delegate to {@link Fail#fail(String)} so that Assertions offers a full feature entry point to all Fest Assert 392 * features (but you can use Fail if you prefer). 393 */ 394 public static void fail(String failureMessage) { 395 Fail.fail(failureMessage); 396 } 397 398 /** 399 * Only delegate to {@link Fail#fail(String, Throwable)} so that Assertions offers a full feature entry point to all 400 * Fest Assert features (but you can use Fail if you prefer). 401 */ 402 public static void fail(String failureMessage, Throwable realCause) { 403 Fail.fail(failureMessage, realCause); 404 } 405 406 /** 407 * Only delegate to {@link Fail#failBecauseExceptionWasNotThrown(Class)} so that Assertions offers a full feature 408 * entry point to all Fest Assert features (but you can use Fail if you prefer). 409 */ 410 public static void failBecauseExceptionWasNotThrown(Class<? extends Exception> exceptionClass) { 411 Fail.failBecauseExceptionWasNotThrown(exceptionClass); 412 } 413 414 // ------------------------------------------------------------------------------------------------------ 415 // properties methods : not assertions but here to have a single entry point to all Fest Assert features. 416 // ------------------------------------------------------------------------------------------------------ 417 418 /** 419 * Only delegate to {@link Properties#extractProperty(String)} so that Assertions offers a full feature entry point to 420 * all Fest Assert features (but you can use Fail if you prefer). 421 * <p> 422 * Typical usage is to chain <code>extractProperty</code> with <code>from</code> method, see examples below : 423 * <pre> 424 * // extract simple property values having a java standard type (here String) 425 * assertThat(extractProperty("name").from(fellowshipOfTheRing)).contains("Boromir", "Gandalf", "Frodo", "Legolas") 426 * .doesNotContain("Sauron", "Elrond"); 427 * 428 * // extracting property works also with user's types (here Race) 429 * assertThat(extractProperty("race").from(fellowshipOfTheRing)).contains(HOBBIT, ELF).doesNotContain(ORC); 430 * 431 * // extract nested property on Race 432 * assertThat(extractProperty("race.name").from(fellowshipOfTheRing)).contains("Hobbit", "Elf").doesNotContain("Orc"); 433 * </pre> 434 */ 435 public static Properties extractProperty(String propertyName) { 436 return Properties.extractProperty(propertyName); 437 } 438 439 // ------------------------------------------------------------------------------------------------------ 440 // Map utility methods : not assertions but here to have a single entry point to all Fest Assert features. 441 // ------------------------------------------------------------------------------------------------------ 442 443 /** 444 * Only delegate to {@link MapEntry#entry(Object, Object)} so that Assertions offers a full feature entry point to 445 * all Fest Assert features (but you can use Fail if you prefer). 446 * <p> 447 * Typical usage is to call <code>entry</code> in MapAssert <code>contains</code> assertion, see examples below : 448 * <pre> 449 * assertThat(ringBearers).contains(entry(oneRing, frodo), entry(nenya, galadriel)); 450 * </pre> 451 */ 452 public static MapEntry entry(Object key, Object value) { 453 return MapEntry.entry(key, value); 454 } 455 456 /** Creates a new </code>{@link Assertions}</code>. */ 457 protected Assertions() {} 458 }