5cff3630a725aa1e5fb67301ff1518875589a96c
[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 * Setup the test (called before the test is run).
43 *
44 * @throws Exception
45 * in case of error
46 */
47 @SuppressWarnings("unused")
48 public void setUp() throws Exception {
49 }
50
51 /**
52 * Tear-down the test (called when the test has been ran).
53 *
54 * @throws Exception
55 * in case of error
56 */
57 @SuppressWarnings("unused")
58 public void tearDown() throws Exception {
59 }
60
61 /**
62 * The test name.
63 *
64 * @return the name
65 */
66 public String getName() {
67 return name;
68 }
69
70 /**
71 * Actually do the test.
72 *
73 * @throws Exception
74 * in case of error
75 */
76 abstract public void test() throws Exception;
77
78 /**
79 * Force a failure.
80 *
81 * @throws AssertException
82 * every time
83 */
84 public void fail() throws AssertException {
85 fail(null);
86 }
87
88 /**
89 * Force a failure.
90 *
91 * @param reason
92 * the failure reason
93 * @throws AssertException
94 * every time
95 */
96 public void fail(String reason) throws AssertException {
97 throw new AssertException("Failed!" + //
98 reason != null ? "\n" + reason : "");
99 }
100
101 /**
102 * Check that 2 {@link Object}s are equals.
103 *
104 * @param expected
105 * the expected value
106 * @param actual
107 * the actual value
108 *
109 * @throws AssertException
110 * in case they differ
111 */
112 public void assertEquals(Object expected, Object actual)
113 throws AssertException {
114 assertEquals(null, expected, actual);
115 }
116
117 /**
118 * Check that 2 {@link Object}s are equals.
119 *
120 * @param errorMessage
121 * the error message to display if they differ
122 * @param expected
123 * the expected value
124 * @param actual
125 * the actual value
126 *
127 * @throws AssertException
128 * in case they differ
129 */
130 public void assertEquals(String errorMessage, Object expected, Object actual)
131 throws AssertException {
132 if ((expected == null && actual != null)
133 || (expected != null && !expected.equals(actual))) {
134 if (errorMessage == null) {
135 throw new AssertException(generateAssertMessage(expected,
136 actual));
137 }
138
139 throw new AssertException(errorMessage, new AssertException(
140 generateAssertMessage(expected, actual)));
141 }
142 }
143
144 /**
145 * Check that 2 longs are equals.
146 *
147 * @param expected
148 * the expected value
149 * @param actual
150 * the actual value
151 *
152 * @throws AssertException
153 * in case they differ
154 */
155 public void assertEquals(long expected, long actual) throws AssertException {
156 assertEquals(Long.valueOf(expected), Long.valueOf(actual));
157 }
158
159 /**
160 * Check that 2 longs are equals.
161 *
162 * @param errorMessage
163 * the error message to display if they differ
164 * @param expected
165 * the expected value
166 * @param actual
167 * the actual value
168 *
169 * @throws AssertException
170 * in case they differ
171 */
172 public void assertEquals(String errorMessage, long expected, long actual)
173 throws AssertException {
174 assertEquals(errorMessage, Long.valueOf(expected), Long.valueOf(actual));
175 }
176
177 /**
178 * Check that 2 booleans are equals.
179 *
180 * @param expected
181 * the expected value
182 * @param actual
183 * the actual value
184 *
185 * @throws AssertException
186 * in case they differ
187 */
188 public void assertEquals(boolean expected, boolean actual)
189 throws AssertException {
190 assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual));
191 }
192
193 /**
194 * Check that 2 booleans are equals.
195 *
196 * @param errorMessage
197 * the error message to display if they differ
198 * @param expected
199 * the expected value
200 * @param actual
201 * the actual value
202 *
203 * @throws AssertException
204 * in case they differ
205 */
206 public void assertEquals(String errorMessage, boolean expected,
207 boolean actual) throws AssertException {
208 assertEquals(errorMessage, Boolean.valueOf(expected),
209 Boolean.valueOf(actual));
210 }
211
212 /**
213 * Check that 2 doubles are equals.
214 *
215 * @param expected
216 * the expected value
217 * @param actual
218 * the actual value
219 *
220 * @throws AssertException
221 * in case they differ
222 */
223 public void assertEquals(double expected, double actual)
224 throws AssertException {
225 assertEquals(Double.valueOf(expected), Double.valueOf(actual));
226 }
227
228 /**
229 * Check that 2 doubles are equals.
230 *
231 * @param errorMessage
232 * the error message to display if they differ
233 * @param expected
234 * the expected value
235 * @param actual
236 * the actual value
237 *
238 * @throws AssertException
239 * in case they differ
240 */
241 public void assertEquals(String errorMessage, double expected, double actual)
242 throws AssertException {
243 assertEquals(errorMessage, Double.valueOf(expected),
244 Double.valueOf(actual));
245 }
246
247 /**
248 * Check that 2 {@link List}s are equals.
249 *
250 * @param errorMessage
251 * the error message to display if they differ
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(List<?> expected, List<?> actual)
261 throws AssertException {
262
263 assertEquals("The 2 lists don't contain the same number of items",
264 expected.size(), actual.size());
265
266 int size = expected.size();
267 for (int i = 0; i < size; i++) {
268 assertEquals("Line " + i + " (0-based) is not correct",
269 expected.get(i), actual.get(i));
270 }
271 }
272
273 /**
274 * Check that given {@link Object} is not NULL.
275 *
276 * @param errorMessage
277 * the error message to display if it is NULL
278 * @param actual
279 * the actual value
280 *
281 * @throws AssertException
282 * in case they differ
283 */
284 public void assertNotNull(String errorMessage, Object actual)
285 throws AssertException {
286 if (actual == null) {
287 String defaultReason = String.format("" //
288 + "Assertion failed!%n" //
289 + "Object should not have been NULL");
290
291 if (errorMessage == null) {
292 throw new AssertException(defaultReason);
293 }
294
295 throw new AssertException(errorMessage, new AssertException(
296 defaultReason));
297 }
298 }
299
300 /**
301 * Generate the default assert message for 2 different values that were
302 * supposed to be equals.
303 *
304 * @param expected
305 * the expected value
306 * @param actual
307 * the actual value
308 *
309 * @return the message
310 */
311 public static String generateAssertMessage(Object expected, Object actual) {
312 return String.format("" //
313 + "Assertion failed!%n" //
314 + "Expected value: [%s]%n" //
315 + "Actual value: [%s]", expected, actual);
316 }
317 }