Commit | Line | Data |
---|---|---|
80383c14 NR |
1 | package be.nikiroo.utils.test; |
2 | ||
620f7329 NR |
3 | import java.util.List; |
4 | ||
80383c14 NR |
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 | ||
c37cc690 NR |
20 | public AssertException(String reason, Exception source) { |
21 | super(reason, source); | |
22 | } | |
23 | ||
80383c14 NR |
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 | public void setUp() throws Exception { | |
48 | } | |
49 | ||
50 | /** | |
51 | * Tear-down the test (called when the test has been ran). | |
52 | * | |
53 | * @throws Exception | |
54 | * in case of error | |
55 | */ | |
56 | public void tearDown() throws Exception { | |
57 | } | |
58 | ||
59 | /** | |
60 | * The test name. | |
61 | * | |
62 | * @return the name | |
63 | */ | |
64 | public String getName() { | |
65 | return name; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Actually do the test. | |
70 | * | |
71 | * @throws Exception | |
72 | * in case of error | |
73 | */ | |
74 | abstract public void test() throws Exception; | |
75 | ||
76 | /** | |
77 | * Force a failure. | |
78 | * | |
79 | * @throws AssertException | |
80 | * every time | |
81 | */ | |
82 | public void fail() throws AssertException { | |
83 | fail(null); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Force a failure. | |
88 | * | |
89 | * @param reason | |
90 | * the failure reason | |
91 | * @throws AssertException | |
92 | * every time | |
93 | */ | |
94 | public void fail(String reason) throws AssertException { | |
95 | throw new AssertException("Failed!" + // | |
96 | reason != null ? "\n" + reason : ""); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Check that 2 {@link Object}s are equals. | |
101 | * | |
102 | * @param expected | |
103 | * the expected value | |
104 | * @param actual | |
105 | * the actual value | |
106 | * | |
107 | * @throws AssertException | |
108 | * in case they differ | |
109 | */ | |
110 | public void assertEquals(Object expected, Object actual) | |
111 | throws AssertException { | |
112 | assertEquals(null, expected, actual); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Check that 2 {@link Object}s are equals. | |
117 | * | |
db31c358 NR |
118 | * @param errorMessage |
119 | * the error message to display if they differ | |
80383c14 NR |
120 | * @param expected |
121 | * the expected value | |
122 | * @param actual | |
123 | * the actual value | |
124 | * | |
125 | * @throws AssertException | |
126 | * in case they differ | |
127 | */ | |
128 | public void assertEquals(String errorMessage, Object expected, Object actual) | |
129 | throws AssertException { | |
80383c14 NR |
130 | if ((expected == null && actual != null) |
131 | || (expected != null && !expected.equals(actual))) { | |
c37cc690 NR |
132 | if (errorMessage == null) { |
133 | throw new AssertException(generateAssertMessage(expected, | |
134 | actual)); | |
c37cc690 | 135 | } |
cd0c27d2 NR |
136 | |
137 | throw new AssertException(errorMessage, new AssertException( | |
138 | generateAssertMessage(expected, actual))); | |
80383c14 NR |
139 | } |
140 | } | |
141 | ||
142 | /** | |
620f7329 | 143 | * Check that 2 longs are equals. |
80383c14 NR |
144 | * |
145 | * @param expected | |
146 | * the expected value | |
147 | * @param actual | |
148 | * the actual value | |
149 | * | |
150 | * @throws AssertException | |
151 | * in case they differ | |
152 | */ | |
153 | public void assertEquals(long expected, long actual) throws AssertException { | |
0988831f | 154 | assertEquals(Long.valueOf(expected), Long.valueOf(actual)); |
80383c14 NR |
155 | } |
156 | ||
157 | /** | |
620f7329 | 158 | * Check that 2 longs are equals. |
80383c14 | 159 | * |
db31c358 NR |
160 | * @param errorMessage |
161 | * the error message to display if they differ | |
80383c14 NR |
162 | * @param expected |
163 | * the expected value | |
164 | * @param actual | |
165 | * the actual value | |
166 | * | |
167 | * @throws AssertException | |
168 | * in case they differ | |
169 | */ | |
170 | public void assertEquals(String errorMessage, long expected, long actual) | |
171 | throws AssertException { | |
0988831f | 172 | assertEquals(errorMessage, Long.valueOf(expected), Long.valueOf(actual)); |
80383c14 NR |
173 | } |
174 | ||
175 | /** | |
620f7329 | 176 | * Check that 2 booleans are equals. |
80383c14 NR |
177 | * |
178 | * @param expected | |
179 | * the expected value | |
180 | * @param actual | |
181 | * the actual value | |
182 | * | |
183 | * @throws AssertException | |
184 | * in case they differ | |
185 | */ | |
186 | public void assertEquals(boolean expected, boolean actual) | |
187 | throws AssertException { | |
0988831f | 188 | assertEquals(Boolean.valueOf(expected), Boolean.valueOf(actual)); |
80383c14 NR |
189 | } |
190 | ||
191 | /** | |
620f7329 | 192 | * Check that 2 booleans are equals. |
80383c14 | 193 | * |
db31c358 NR |
194 | * @param errorMessage |
195 | * the error message to display if they differ | |
80383c14 NR |
196 | * @param expected |
197 | * the expected value | |
198 | * @param actual | |
199 | * the actual value | |
200 | * | |
201 | * @throws AssertException | |
202 | * in case they differ | |
203 | */ | |
204 | public void assertEquals(String errorMessage, boolean expected, | |
205 | boolean actual) throws AssertException { | |
0988831f NR |
206 | assertEquals(errorMessage, Boolean.valueOf(expected), |
207 | Boolean.valueOf(actual)); | |
80383c14 NR |
208 | } |
209 | ||
210 | /** | |
620f7329 | 211 | * Check that 2 doubles are equals. |
80383c14 NR |
212 | * |
213 | * @param expected | |
214 | * the expected value | |
215 | * @param actual | |
216 | * the actual value | |
217 | * | |
218 | * @throws AssertException | |
219 | * in case they differ | |
220 | */ | |
221 | public void assertEquals(double expected, double actual) | |
222 | throws AssertException { | |
0988831f | 223 | assertEquals(Double.valueOf(expected), Double.valueOf(actual)); |
80383c14 NR |
224 | } |
225 | ||
226 | /** | |
620f7329 | 227 | * Check that 2 doubles are equals. |
80383c14 | 228 | * |
db31c358 NR |
229 | * @param errorMessage |
230 | * the error message to display if they differ | |
80383c14 NR |
231 | * @param expected |
232 | * the expected value | |
233 | * @param actual | |
234 | * the actual value | |
235 | * | |
236 | * @throws AssertException | |
237 | * in case they differ | |
238 | */ | |
239 | public void assertEquals(String errorMessage, double expected, double actual) | |
240 | throws AssertException { | |
0988831f NR |
241 | assertEquals(errorMessage, Double.valueOf(expected), |
242 | Double.valueOf(actual)); | |
80383c14 | 243 | } |
86057589 | 244 | |
620f7329 NR |
245 | /** |
246 | * Check that 2 {@link List}s are equals. | |
247 | * | |
248 | * @param errorMessage | |
249 | * the error message to display if they differ | |
250 | * @param expected | |
251 | * the expected value | |
252 | * @param actual | |
253 | * the actual value | |
254 | * | |
255 | * @throws AssertException | |
256 | * in case they differ | |
257 | */ | |
258 | public void assertEquals(List<?> expected, List<?> actual) | |
259 | throws AssertException { | |
534332ff NR |
260 | assertEquals("Assertion failed", expected, actual); |
261 | } | |
262 | ||
263 | /** | |
264 | * Check that 2 {@link List}s are equals. | |
265 | * | |
266 | * @param errorMessage | |
267 | * the error message to display if they differ | |
268 | * @param expected | |
269 | * the expected value | |
270 | * @param actual | |
271 | * the actual value | |
272 | * @param errorMessage | |
273 | * the error message to display if they differ | |
274 | * | |
275 | * @throws AssertException | |
276 | * in case they differ | |
277 | */ | |
278 | public void assertEquals(String errorMessage, List<?> expected, | |
279 | List<?> actual) throws AssertException { | |
620f7329 | 280 | |
534332ff NR |
281 | if (expected.size() != actual.size()) { |
282 | assertEquals(errorMessage + ": not same number of items", | |
283 | list(expected), list(actual)); | |
284 | } | |
620f7329 NR |
285 | |
286 | int size = expected.size(); | |
287 | for (int i = 0; i < size; i++) { | |
534332ff NR |
288 | assertEquals(errorMessage + ": item " + i |
289 | + " (0-based) is not correct", expected.get(i), | |
290 | actual.get(i)); | |
620f7329 NR |
291 | } |
292 | } | |
293 | ||
16d59378 NR |
294 | /** |
295 | * Check that given {@link Object} is not NULL. | |
296 | * | |
db31c358 NR |
297 | * @param errorMessage |
298 | * the error message to display if it is NULL | |
16d59378 NR |
299 | * @param actual |
300 | * the actual value | |
301 | * | |
302 | * @throws AssertException | |
303 | * in case they differ | |
304 | */ | |
305 | public void assertNotNull(String errorMessage, Object actual) | |
306 | throws AssertException { | |
307 | if (actual == null) { | |
c37cc690 | 308 | String defaultReason = String.format("" // |
0988831f NR |
309 | + "Assertion failed!%n" // |
310 | + "Object should not have been NULL"); | |
c37cc690 | 311 | |
16d59378 | 312 | if (errorMessage == null) { |
c37cc690 | 313 | throw new AssertException(defaultReason); |
16d59378 | 314 | } |
cd0c27d2 NR |
315 | |
316 | throw new AssertException(errorMessage, new AssertException( | |
317 | defaultReason)); | |
16d59378 NR |
318 | } |
319 | } | |
320 | ||
86057589 NR |
321 | /** |
322 | * Generate the default assert message for 2 different values that were | |
323 | * supposed to be equals. | |
324 | * | |
325 | * @param expected | |
326 | * the expected value | |
327 | * @param actual | |
328 | * the actual value | |
329 | * | |
330 | * @return the message | |
331 | */ | |
332 | public static String generateAssertMessage(Object expected, Object actual) { | |
333 | return String.format("" // | |
0988831f NR |
334 | + "Assertion failed!%n" // |
335 | + "Expected value: [%s]%n" // | |
86057589 NR |
336 | + "Actual value: [%s]", expected, actual); |
337 | } | |
534332ff NR |
338 | |
339 | private static String list(List<?> items) { | |
340 | StringBuilder builder = new StringBuilder(); | |
341 | for (Object item : items) { | |
342 | if (builder.length() == 0) { | |
343 | builder.append(items.size() + " item(s): "); | |
344 | } else { | |
345 | builder.append(", "); | |
346 | } | |
347 | ||
348 | builder.append("" + item); | |
349 | ||
350 | if (builder.length() > 60) { | |
351 | builder.setLength(57); | |
352 | builder.append("..."); | |
353 | break; | |
354 | } | |
355 | } | |
356 | ||
357 | return builder.toString(); | |
358 | } | |
80383c14 | 359 | } |