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-2011 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 * 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 /**
045 * Creates a new instance of <code>{@link BigDecimalAssert}</code>.
046 * @param actual the value to be the target of the assertions methods.
047 * @return the created assertion object.
048 */
049 public static BigDecimalAssert assertThat(BigDecimal actual) {
050 return new BigDecimalAssert(actual);
051 }
052
053 /**
054 * Creates a new instance of <code>{@link BooleanAssert}</code>.
055 * @param actual the value to be the target of the assertions methods.
056 * @return the created assertion object.
057 */
058 public static BooleanAssert assertThat(boolean actual) {
059 return new BooleanAssert(actual);
060 }
061
062 /**
063 * Creates a new instance of <code>{@link BooleanAssert}</code>.
064 * @param actual the value to be the target of the assertions methods.
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 BooleanArrayAssert}</code>.
073 * @param actual the value to be the target of the assertions methods.
074 * @return the created assertion object.
075 */
076 public static BooleanArrayAssert assertThat(boolean[] actual) {
077 return new BooleanArrayAssert(actual);
078 }
079
080 /**
081 * Creates a new instance of <code>{@link ImageAssert}</code>.
082 * @param actual the value to be the target of the assertions methods.
083 * @return the created assertion object.
084 */
085 public static ImageAssert assertThat(BufferedImage actual) {
086 return new ImageAssert(actual);
087 }
088
089 /**
090 * Creates a new instance of <code>{@link ByteAssert}</code>.
091 * @param actual the value to be the target of the assertions methods.
092 * @return the created assertion object.
093 */
094 public static ByteAssert assertThat(byte actual) {
095 return new ByteAssert(actual);
096 }
097
098 /**
099 * Creates a new instance of <code>{@link ByteAssert}</code>.
100 * @param actual the value to be the target of the assertions methods.
101 * @return the created assertion object.
102 */
103 public static ByteAssert assertThat(Byte actual) {
104 return new ByteAssert(actual);
105 }
106
107 /**
108 * Creates a new instance of <code>{@link ByteArrayAssert}</code>.
109 * @param actual the value to be the target of the assertions methods.
110 * @return the created assertion object.
111 */
112 public static ByteArrayAssert assertThat(byte[] actual) {
113 return new ByteArrayAssert(actual);
114 }
115
116 /**
117 * Creates a new instance of <code>{@link CharAssert}</code>.
118 * @param actual the value to be the target of the assertions methods.
119 * @return the created assertion object.
120 */
121 public static CharAssert assertThat(char actual) {
122 return new CharAssert(actual);
123 }
124
125 /**
126 * Creates a new instance of <code>{@link CharAssert}</code>.
127 * @param actual the value to be the target of the assertions methods.
128 * @return the created assertion object.
129 */
130 public static CharAssert assertThat(Character actual) {
131 return new CharAssert(actual);
132 }
133
134 /**
135 * Creates a new instance of <code>{@link CharArrayAssert}</code>.
136 * @param actual the value to be the target of the assertions methods.
137 * @return the created assertion object.
138 */
139 public static CharArrayAssert assertThat(char[] actual) {
140 return new CharArrayAssert(actual);
141 }
142
143 /**
144 * Creates a new instance of <code>{@link CollectionAssert}</code>.
145 * @param actual the value to be the target of the assertions methods.
146 * @return the created assertion object.
147 */
148 public static CollectionAssert assertThat(Collection<?> actual) {
149 return new CollectionAssert(actual);
150 }
151
152 /**
153 * Creates a new instance of <code>{@link ListAssert}</code>.
154 * @param actual the value to be the target of the assertions methods.
155 * @return the created assertion object.
156 * @since 1.1
157 */
158 public static ListAssert assertThat(List<?> actual) {
159 return new ListAssert(actual);
160 }
161
162 /**
163 * Creates a new instance of <code>{@link DoubleAssert}</code>.
164 * @param actual the value to be the target of the assertions methods.
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 value to be the target of the assertions methods.
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 value to be the target of the assertions methods.
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 value to be the target of the assertions methods.
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 FloatAssert}</code>.
200 * @param actual the value to be the target of the assertions methods.
201 * @return the created assertion object.
202 */
203 public static FloatAssert assertThat(float actual) {
204 return new FloatAssert(actual);
205 }
206
207 /**
208 * Creates a new instance of <code>{@link FloatAssert}</code>.
209 * @param actual the value to be the target of the assertions methods.
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 FloatArrayAssert}</code>.
218 * @param actual the value to be the target of the assertions methods.
219 * @return the created assertion object.
220 */
221 public static FloatArrayAssert assertThat(float[] actual) {
222 return new FloatArrayAssert(actual);
223 }
224
225 /**
226 * Creates a new instance of <code>{@link IntAssert}</code>.
227 * @param actual the value to be the target of the assertions methods.
228 * @return the created assertion object.
229 */
230 public static IntAssert assertThat(int actual) {
231 return new IntAssert(actual);
232 }
233
234 /**
235 * Creates a new instance of <code>{@link IntAssert}</code>.
236 * @param actual the value to be the target of the assertions methods.
237 * @return the created assertion object.
238 */
239 public static IntAssert assertThat(Integer actual) {
240 return new IntAssert(actual);
241 }
242
243 /**
244 * Creates a new instance of <code>{@link IntArrayAssert}</code>.
245 * @param actual the value to be the target of the assertions methods.
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 IteratorAssert}</code>.
254 * @param actual an {@code Iterable} whose contents will be added to a new {@code Collection}.
255 * @return the created assertion object.
256 */
257 public static IteratorAssert assertThat(Iterable<?> actual) {
258 Iterator<?> iterator = actual == null ? null : actual.iterator();
259 return assertThat(iterator);
260 }
261
262 /**
263 * Creates a new instance of <code>{@link IteratorAssert}</code>.
264 * @param actual an {@code Iterator} whose contents will be added to a new {@code Collection}.
265 * @return the created assertion object.
266 */
267 public static IteratorAssert assertThat(Iterator<?> actual) {
268 return new IteratorAssert(actual);
269 }
270
271 /**
272 * Creates a new instance of <code>{@link LongAssert}</code>.
273 * @param actual the value to be the target of the assertions methods.
274 * @return the created assertion object.
275 */
276 public static LongAssert assertThat(long actual) {
277 return new LongAssert(actual);
278 }
279
280 /**
281 * Creates a new instance of <code>{@link LongAssert}</code>.
282 * @param actual the value to be the target of the assertions methods.
283 * @return the created assertion object.
284 */
285 public static LongAssert assertThat(Long actual) {
286 return new LongAssert(actual);
287 }
288
289 /**
290 * Creates a new instance of <code>{@link LongArrayAssert}</code>.
291 * @param actual the value to be the target of the assertions methods.
292 * @return the created assertion object.
293 */
294 public static LongArrayAssert assertThat(long[] actual) {
295 return new LongArrayAssert(actual);
296 }
297
298 /**
299 * Creates a new instance of <code>{@link MapAssert}</code>.
300 * @param actual the value to be the target of the assertions methods.
301 * @return the created assertion object.
302 */
303 public static MapAssert assertThat(Map<?, ?> actual) {
304 return new MapAssert(actual);
305 }
306
307 /**
308 * Creates a new instance of <code>{@link ObjectAssert}</code>.
309 * @param actual the value to be the target of the assertions methods.
310 * @return the created assertion object.
311 */
312 public static ObjectAssert assertThat(Object actual) {
313 return new ObjectAssert(actual);
314 }
315
316 /**
317 * Creates a new instance of <code>{@link ObjectArrayAssert}</code>.
318 * @param actual the value to be the target of the assertions methods.
319 * @return the created assertion object.
320 */
321 public static ObjectArrayAssert assertThat(Object[] actual) {
322 return new ObjectArrayAssert(actual);
323 }
324
325 /**
326 * Creates a new instance of <code>{@link ShortAssert}</code>.
327 * @param actual the value to be the target of the assertions methods.
328 * @return the created assertion object.
329 */
330 public static ShortAssert assertThat(short actual) {
331 return new ShortAssert(actual);
332 }
333
334 /**
335 * Creates a new instance of <code>{@link ShortAssert}</code>.
336 * @param actual the value to be the target of the assertions methods.
337 * @return the created assertion object.
338 */
339 public static ShortAssert assertThat(Short actual) {
340 return new ShortAssert(actual);
341 }
342
343 /**
344 * Creates a new instance of <code>{@link ShortArrayAssert}</code>.
345 * @param actual the value to be the target of the assertions methods.
346 * @return the created assertion object.
347 */
348 public static ShortArrayAssert assertThat(short[] actual) {
349 return new ShortArrayAssert(actual);
350 }
351
352 /**
353 * Creates a new instance of <code>{@link StringAssert}</code>.
354 * @param actual the value to be the target of the assertions methods.
355 * @return the created assertion object.
356 */
357 public static StringAssert assertThat(String actual) {
358 return new StringAssert(actual);
359 }
360
361 /**
362 * Returns the given assertion. This method improves code readability by surrounding the given assertion with "<code>assertThat</code>".
363 * <p>
364 * For example, let's assume we have the following custom assertion class:
365 *
366 * <pre>
367 * public class ServerSocketAssertion implements AssertExtension {
368 * private final ServerSocket socket;
369 *
370 * public ServerSocketAssertion(ServerSocket socket) {
371 * this.socket = socket;
372 * }
373 *
374 * public ServerSocketAssert isConnectedTo(int port) {
375 * assertThat(socket.isBound()).isTrue();
376 * assertThat(socket.getLocalPort()).isEqualTo(port);
377 * assertThat(socket.isClosed()).isFalse();
378 * return this;
379 * }
380 * }
381 * </pre>
382 * </p>
383 * <p>
384 * We can wrap that assertion with "<code>assertThat</code>" to improve test code readability.
385 * <pre>
386 * ServerSocketAssertion socket = new ServerSocketAssertion(server.getSocket());
387 * assertThat(socket).isConnectedTo(2000);
388 * </pre>
389 * </p>
390 *
391 * @param <T> the generic type of the user-defined assertion.
392 * @param assertion the assertion to return.
393 * @return the given assertion.
394 */
395 public static <T extends AssertExtension> T assertThat(T assertion) {
396 return assertion;
397 }
398
399 /**
400 * Creates a new instance of <code>{@link ThrowableAssert}</code>.
401 * @param actual the value to be the target of the assertions methods.
402 * @return the created assertion object.
403 */
404 public static ThrowableAssert assertThat(Throwable actual) {
405 return new ThrowableAssert(actual);
406 }
407
408 /**
409 * This constructor is protected to make it possible to subclass this class. Since all its methods are static, there
410 * is no point on creating a new instance of it.
411 */
412 protected Assertions() {}
413 }