Serial: support for anonymous inner classes
[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 * @throws AssertException
115 * every time
116 */
117 public void fail(String reason) throws AssertException {
118 throw new AssertException("Failed!" + //
119 reason != null ? "\n" + reason : "");
120 }
121
122 /**
123 * Check that 2 {@link Object}s are equals.
124 *
125 * @param expected
126 * the expected value
127 * @param actual
128 * the actual value
129 *
130 * @throws AssertException
131 * in case they differ
132 */
133 public void assertEquals(Object expected, Object actual)
134 throws AssertException {
135 assertEquals(null, expected, actual);
136 }
137
138 /**
139 * Check that 2 {@link Object}s are equals.
140 *
141 * @param errorMessage
142 * the error message to display if they differ
143 * @param expected
144 * the expected value
145 * @param actual
146 * the actual value
147 *
148 * @throws AssertException
149 * in case they differ
150 */
151 public void assertEquals(String errorMessage, Object expected, Object actual)
152 throws AssertException {
153 if ((expected == null && actual != null)
154 || (expected != null && !expected.equals(actual))) {
155 if (errorMessage == null) {
156 throw new AssertException(generateAssertMessage(expected,
157 actual));
158 }
159
160 throw new AssertException(errorMessage, new AssertException(
161 generateAssertMessage(expected, actual)));
162 }
163 }
164
165 /**
166 * Check that 2 longs are equals.
167 *
168 * @param expected
169 * the expected value
170 * @param actual
171 * the actual value
172 *
173 * @throws AssertException
174 * in case they differ
175 */
176 public void assertEquals(long expected, long actual) throws AssertException {
177 assertEquals(Long.valueOf(expected), Long.valueOf(actual));
178 }
179
180 /**
181 * Check that 2 longs are equals.
182 *
183 * @param errorMessage
184 * the error message to display if they differ
185 * @param expected
186 * the expected value
187 * @param actual
188 * the actual value
189 *
190 * @throws AssertException
191 * in case they differ
192 */
193 public void assertEquals(String errorMessage, long expected, long actual)
194 throws AssertException {
195 assertEquals(errorMessage, Long.valueOf(expected), Long.valueOf(actual));
196 }
197
198 /**
199 * Check that 2 booleans are equals.
200 *
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(boolean expected, boolean actual)
210 throws AssertException {
211 assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual));
212 }
213
214 /**
215 * Check that 2 booleans are equals.
216 *
217 * @param errorMessage
218 * the error message to display if they differ
219 * @param expected
220 * the expected value
221 * @param actual
222 * the actual value
223 *
224 * @throws AssertException
225 * in case they differ
226 */
227 public void assertEquals(String errorMessage, boolean expected,
228 boolean actual) throws AssertException {
229 assertEquals(errorMessage, Boolean.valueOf(expected),
230 Boolean.valueOf(actual));
231 }
232
233 /**
234 * Check that 2 doubles are equals.
235 *
236 * @param expected
237 * the expected value
238 * @param actual
239 * the actual value
240 *
241 * @throws AssertException
242 * in case they differ
243 */
244 public void assertEquals(double expected, double actual)
245 throws AssertException {
246 assertEquals(Double.valueOf(expected), Double.valueOf(actual));
247 }
248
249 /**
250 * Check that 2 doubles are equals.
251 *
252 * @param errorMessage
253 * the error message to display if they differ
254 * @param expected
255 * the expected value
256 * @param actual
257 * the actual value
258 *
259 * @throws AssertException
260 * in case they differ
261 */
262 public void assertEquals(String errorMessage, double expected, double actual)
263 throws AssertException {
264 assertEquals(errorMessage, Double.valueOf(expected),
265 Double.valueOf(actual));
266 }
267
268 /**
269 * Check that 2 {@link List}s are equals.
270 *
271 * @param errorMessage
272 * the error message to display if they differ
273 * @param expected
274 * the expected value
275 * @param actual
276 * the actual value
277 *
278 * @throws AssertException
279 * in case they differ
280 */
281 public void assertEquals(List<?> expected, List<?> actual)
282 throws AssertException {
283 assertEquals("Assertion failed", expected, actual);
284 }
285
286 /**
287 * Check that 2 {@link List}s are equals.
288 *
289 * @param errorMessage
290 * the error message to display if they differ
291 * @param expected
292 * the expected value
293 * @param actual
294 * the actual value
295 * @param errorMessage
296 * the error message to display if they differ
297 *
298 * @throws AssertException
299 * in case they differ
300 */
301 public void assertEquals(String errorMessage, List<?> expected,
302 List<?> actual) throws AssertException {
303
304 if (expected.size() != actual.size()) {
305 assertEquals(errorMessage + ": not same number of items",
306 list(expected), list(actual));
307 }
308
309 int size = expected.size();
310 for (int i = 0; i < size; i++) {
311 assertEquals(errorMessage + ": item " + i
312 + " (0-based) is not correct", expected.get(i),
313 actual.get(i));
314 }
315 }
316
317 /**
318 * Check that given {@link Object} is not NULL.
319 *
320 * @param errorMessage
321 * the error message to display if it is NULL
322 * @param actual
323 * the actual value
324 *
325 * @throws AssertException
326 * in case they differ
327 */
328 public void assertNotNull(String errorMessage, Object actual)
329 throws AssertException {
330 if (actual == null) {
331 String defaultReason = String.format("" //
332 + "Assertion failed!%n" //
333 + "Object should not have been NULL");
334
335 if (errorMessage == null) {
336 throw new AssertException(defaultReason);
337 }
338
339 throw new AssertException(errorMessage, new AssertException(
340 defaultReason));
341 }
342 }
343
344 /**
345 * Generate the default assert message for 2 different values that were
346 * supposed to be equals.
347 *
348 * @param expected
349 * the expected value
350 * @param actual
351 * the actual value
352 *
353 * @return the message
354 */
355 public static String generateAssertMessage(Object expected, Object actual) {
356 return String.format("" //
357 + "Assertion failed!%n" //
358 + "Expected value: [%s]%n" //
359 + "Actual value: [%s]", expected, actual);
360 }
361
362 private static String list(List<?> items) {
363 StringBuilder builder = new StringBuilder();
364 for (Object item : items) {
365 if (builder.length() == 0) {
366 builder.append(items.size() + " item(s): ");
367 } else {
368 builder.append(", ");
369 }
370
371 builder.append("" + item);
372
373 if (builder.length() > 60) {
374 builder.setLength(57);
375 builder.append("...");
376 break;
377 }
378 }
379
380 return builder.toString();
381 }
382 }