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