85a65d7a8cc83e716d499ea201efd481295d1867
[nikiroo-utils.git] / src / be / nikiroo / utils / test / TestCase.java
1 package be.nikiroo.utils.test;
2
3 import java.util.List;
4
5 /**
6 * A {@link TestCase} that can be run with {@link TestLauncher}.
7 *
8 * @author niki
9 */
10 abstract public class TestCase {
11 /**
12 * The type of {@link Exception} used to signal a failed assertion or a
13 * force-fail.
14 *
15 * @author niki
16 */
17 class AssertException extends Exception {
18 private static final long serialVersionUID = 1L;
19
20 public AssertException(String reason, Exception source) {
21 super(reason, source);
22 }
23
24 public AssertException(String reason) {
25 super(reason);
26 }
27 }
28
29 private String name;
30
31 /**
32 * Create a new {@link TestCase}.
33 *
34 * @param name
35 * the test name
36 */
37 public TestCase(String name) {
38 this.name = name;
39 }
40
41 /**
42 * This constructor can be used if you require a no-param constructor. In
43 * this case, you are allowed to set the name manually via
44 * {@link TestCase#setName}.
45 */
46 protected TestCase() {
47 this("no name");
48 }
49
50 /**
51 * Setup the test (called before the test is run).
52 *
53 * @throws Exception
54 * in case of error
55 */
56 public void setUp() throws Exception {
57 }
58
59 /**
60 * Tear-down the test (called when the test has been ran).
61 *
62 * @throws Exception
63 * in case of error
64 */
65 public void tearDown() throws Exception {
66 }
67
68 /**
69 * The test name.
70 *
71 * @return the name
72 */
73 public String getName() {
74 return name;
75 }
76
77 /**
78 * The test name.
79 *
80 * @param name
81 * the new name (internal use only)
82 *
83 * @return this (so we can chain and so we can initialize it in a member
84 * variable if this is an anonymous inner class)
85 */
86 protected TestCase setName(String name) {
87 this.name = name;
88 return this;
89 }
90
91 /**
92 * Actually do the test.
93 *
94 * @throws Exception
95 * in case of error
96 */
97 abstract public void test() throws Exception;
98
99 /**
100 * Force a failure.
101 *
102 * @throws AssertException
103 * every time
104 */
105 public void fail() throws AssertException {
106 fail(null);
107 }
108
109 /**
110 * Force a failure.
111 *
112 * @param reason
113 * the failure reason
114 *
115 * @throws AssertException
116 * every time
117 */
118 public void fail(String reason) throws AssertException {
119 fail(reason, null);
120 }
121
122 /**
123 * Force a failure.
124 *
125 * @param reason
126 * the failure reason
127 * @param e
128 * the exception that caused the failure (can be NULL)
129 *
130 * @throws AssertException
131 * every time
132 */
133 public void fail(String reason, Exception e) throws AssertException {
134 throw new AssertException("Failed!" + //
135 reason != null ? "\n" + reason : "", e);
136 }
137
138 /**
139 * Check that 2 {@link Object}s are equals.
140 *
141 * @param expected
142 * the expected value
143 * @param actual
144 * the actual value
145 *
146 * @throws AssertException
147 * in case they differ
148 */
149 public void assertEquals(Object expected, Object actual)
150 throws AssertException {
151 assertEquals(null, expected, actual);
152 }
153
154 /**
155 * Check that 2 {@link Object}s are equals.
156 *
157 * @param errorMessage
158 * the error message to display if they differ
159 * @param expected
160 * the expected value
161 * @param actual
162 * the actual value
163 *
164 * @throws AssertException
165 * in case they differ
166 */
167 public void assertEquals(String errorMessage, Object expected, Object actual)
168 throws AssertException {
169 if ((expected == null && actual != null)
170 || (expected != null && !expected.equals(actual))) {
171 if (errorMessage == null) {
172 throw new AssertException(generateAssertMessage(expected,
173 actual));
174 }
175
176 throw new AssertException(errorMessage, new AssertException(
177 generateAssertMessage(expected, actual)));
178 }
179 }
180
181 /**
182 * Check that 2 longs are equals.
183 *
184 * @param expected
185 * the expected value
186 * @param actual
187 * the actual value
188 *
189 * @throws AssertException
190 * in case they differ
191 */
192 public void assertEquals(long expected, long actual) throws AssertException {
193 assertEquals(Long.valueOf(expected), Long.valueOf(actual));
194 }
195
196 /**
197 * Check that 2 longs are equals.
198 *
199 * @param errorMessage
200 * the error message to display if they differ
201 * @param expected
202 * the expected value
203 * @param actual
204 * the actual value
205 *
206 * @throws AssertException
207 * in case they differ
208 */
209 public void assertEquals(String errorMessage, long expected, long actual)
210 throws AssertException {
211 assertEquals(errorMessage, Long.valueOf(expected), Long.valueOf(actual));
212 }
213
214 /**
215 * Check that 2 booleans are equals.
216 *
217 * @param expected
218 * the expected value
219 * @param actual
220 * the actual value
221 *
222 * @throws AssertException
223 * in case they differ
224 */
225 public void assertEquals(boolean expected, boolean actual)
226 throws AssertException {
227 assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual));
228 }
229
230 /**
231 * Check that 2 booleans are equals.
232 *
233 * @param errorMessage
234 * the error message to display if they differ
235 * @param expected
236 * the expected value
237 * @param actual
238 * the actual value
239 *
240 * @throws AssertException
241 * in case they differ
242 */
243 public void assertEquals(String errorMessage, boolean expected,
244 boolean actual) throws AssertException {
245 assertEquals(errorMessage, Boolean.valueOf(expected),
246 Boolean.valueOf(actual));
247 }
248
249 /**
250 * Check that 2 doubles are equals.
251 *
252 * @param expected
253 * the expected value
254 * @param actual
255 * the actual value
256 *
257 * @throws AssertException
258 * in case they differ
259 */
260 public void assertEquals(double expected, double actual)
261 throws AssertException {
262 assertEquals(Double.valueOf(expected), Double.valueOf(actual));
263 }
264
265 /**
266 * Check that 2 doubles are equals.
267 *
268 * @param errorMessage
269 * the error message to display if they differ
270 * @param expected
271 * the expected value
272 * @param actual
273 * the actual value
274 *
275 * @throws AssertException
276 * in case they differ
277 */
278 public void assertEquals(String errorMessage, double expected, double actual)
279 throws AssertException {
280 assertEquals(errorMessage, Double.valueOf(expected),
281 Double.valueOf(actual));
282 }
283
284 /**
285 * Check that 2 {@link List}s are equals.
286 *
287 * @param errorMessage
288 * the error message to display if they differ
289 * @param expected
290 * the expected value
291 * @param actual
292 * the actual value
293 *
294 * @throws AssertException
295 * in case they differ
296 */
297 public void assertEquals(List<?> expected, List<?> actual)
298 throws AssertException {
299 assertEquals("Assertion failed", expected, actual);
300 }
301
302 /**
303 * Check that 2 {@link List}s are equals.
304 *
305 * @param errorMessage
306 * the error message to display if they differ
307 * @param expected
308 * the expected value
309 * @param actual
310 * the actual value
311 * @param errorMessage
312 * the error message to display if they differ
313 *
314 * @throws AssertException
315 * in case they differ
316 */
317 public void assertEquals(String errorMessage, List<?> expected,
318 List<?> actual) throws AssertException {
319
320 if (expected.size() != actual.size()) {
321 assertEquals(errorMessage + ": not same number of items",
322 list(expected), list(actual));
323 }
324
325 int size = expected.size();
326 for (int i = 0; i < size; i++) {
327 assertEquals(errorMessage + ": item " + i
328 + " (0-based) is not correct", expected.get(i),
329 actual.get(i));
330 }
331 }
332
333 /**
334 * Check that given {@link Object} is not NULL.
335 *
336 * @param errorMessage
337 * the error message to display if it is NULL
338 * @param actual
339 * the actual value
340 *
341 * @throws AssertException
342 * in case they differ
343 */
344 public void assertNotNull(String errorMessage, Object actual)
345 throws AssertException {
346 if (actual == null) {
347 String defaultReason = String.format("" //
348 + "Assertion failed!%n" //
349 + "Object should not have been NULL");
350
351 if (errorMessage == null) {
352 throw new AssertException(defaultReason);
353 }
354
355 throw new AssertException(errorMessage, new AssertException(
356 defaultReason));
357 }
358 }
359
360 /**
361 * Generate the default assert message for 2 different values that were
362 * supposed to be equals.
363 *
364 * @param expected
365 * the expected value
366 * @param actual
367 * the actual value
368 *
369 * @return the message
370 */
371 public static String generateAssertMessage(Object expected, Object actual) {
372 return String.format("" //
373 + "Assertion failed!%n" //
374 + "Expected value: [%s]%n" //
375 + "Actual value: [%s]", expected, actual);
376 }
377
378 private static String list(List<?> items) {
379 StringBuilder builder = new StringBuilder();
380 for (Object item : items) {
381 if (builder.length() == 0) {
382 builder.append(items.size() + " item(s): ");
383 } else {
384 builder.append(", ");
385 }
386
387 builder.append("" + item);
388
389 if (builder.length() > 60) {
390 builder.setLength(57);
391 builder.append("...");
392 break;
393 }
394 }
395
396 return builder.toString();
397 }
398 }