Commit | Line | Data |
---|---|---|
80383c14 NR |
1 | package be.nikiroo.utils.test; |
2 | ||
86057589 NR |
3 | import java.io.PrintWriter; |
4 | import java.io.StringWriter; | |
80383c14 NR |
5 | import java.util.ArrayList; |
6 | import java.util.List; | |
7 | ||
8 | /** | |
9 | * A {@link TestLauncher} starts a series of {@link TestCase}s and displays the | |
10 | * result to the user. | |
11 | * | |
12 | * @author niki | |
13 | */ | |
14 | public class TestLauncher { | |
15 | /** | |
16 | * {@link Exception} happening during the setup process. | |
17 | * | |
18 | * @author niki | |
19 | */ | |
20 | private class SetupException extends Exception { | |
21 | private static final long serialVersionUID = 1L; | |
22 | ||
e9ca6bb8 | 23 | public SetupException(Throwable e) { |
80383c14 NR |
24 | super(e); |
25 | } | |
26 | } | |
27 | ||
28 | /** | |
29 | * {@link Exception} happening during the tear-down process. | |
30 | * | |
31 | * @author niki | |
32 | */ | |
33 | private class TearDownException extends Exception { | |
34 | private static final long serialVersionUID = 1L; | |
35 | ||
e9ca6bb8 | 36 | public TearDownException(Throwable e) { |
80383c14 NR |
37 | super(e); |
38 | } | |
39 | } | |
40 | ||
41 | private List<TestLauncher> series; | |
42 | private List<TestCase> tests; | |
43 | private int columns; | |
44 | private String okString; | |
45 | private String koString; | |
46 | private String name; | |
47 | private boolean cont; | |
48 | ||
49 | protected int executed; | |
50 | protected int total; | |
51 | ||
86057589 NR |
52 | private int currentSeries = 0; |
53 | ||
80383c14 NR |
54 | /** |
55 | * Create a new {@link TestLauncher} with default parameters. | |
56 | * | |
57 | * @param name | |
58 | * the test suite name | |
59 | * @param args | |
60 | * the arguments to configure the number of columns and the ok/ko | |
61 | * {@link String}s | |
62 | */ | |
63 | public TestLauncher(String name, String[] args) { | |
64 | this.name = name; | |
65 | ||
66 | int cols = 80; | |
67 | if (args != null && args.length >= 1) { | |
68 | try { | |
69 | cols = Integer.parseInt(args[0]); | |
70 | } catch (NumberFormatException e) { | |
71 | System.err.println("Test configuration: given number " | |
72 | + "of columns is not parseable: " + args[0]); | |
73 | } | |
74 | } | |
75 | ||
76 | setColumns(cols); | |
77 | ||
78 | String okString = "[ ok ]"; | |
79 | String koString = "[ !! ]"; | |
80 | if (args != null && args.length >= 3) { | |
81 | okString = args[1]; | |
82 | koString = args[2]; | |
83 | } | |
84 | ||
85 | setOkString(okString); | |
86 | setKoString(koString); | |
87 | ||
88 | series = new ArrayList<TestLauncher>(); | |
89 | tests = new ArrayList<TestCase>(); | |
90 | cont = true; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Called before actually starting the tests themselves. | |
95 | * | |
96 | * @throws Exception | |
97 | * in case of error | |
98 | */ | |
79ce1a49 | 99 | @SuppressWarnings("unused") |
80383c14 NR |
100 | protected void start() throws Exception { |
101 | } | |
102 | ||
103 | /** | |
104 | * Called when the tests are passed (or failed to do so). | |
105 | * | |
106 | * @throws Exception | |
107 | * in case of error | |
108 | */ | |
79ce1a49 | 109 | @SuppressWarnings("unused") |
80383c14 NR |
110 | protected void stop() throws Exception { |
111 | } | |
112 | ||
113 | protected void addTest(TestCase test) { | |
114 | tests.add(test); | |
115 | } | |
116 | ||
117 | protected void addSeries(TestLauncher series) { | |
118 | this.series.add(series); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Launch the series of {@link TestCase}s and the {@link TestCase}s. | |
123 | * | |
124 | * @return the number of errors | |
125 | */ | |
126 | public int launch() { | |
127 | return launch(0); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Launch the series of {@link TestCase}s and the {@link TestCase}s. | |
132 | * | |
133 | * @param depth | |
134 | * the level at which is the launcher (0 = main launcher) | |
135 | * | |
136 | * @return the number of errors | |
137 | */ | |
138 | public int launch(int depth) { | |
139 | int errors = 0; | |
140 | executed = 0; | |
141 | total = tests.size(); | |
142 | ||
143 | print(depth); | |
144 | ||
145 | try { | |
146 | start(); | |
147 | ||
148 | errors += launchTests(depth); | |
149 | if (tests.size() > 0 && depth == 0) { | |
150 | System.out.println(""); | |
151 | } | |
152 | ||
86057589 | 153 | currentSeries = 0; |
80383c14 NR |
154 | for (TestLauncher serie : series) { |
155 | errors += serie.launch(depth + 1); | |
156 | executed += serie.executed; | |
157 | total += serie.total; | |
86057589 | 158 | currentSeries++; |
80383c14 NR |
159 | } |
160 | } catch (Exception e) { | |
161 | print(depth, "__start"); | |
162 | print(depth, e); | |
163 | } finally { | |
164 | try { | |
165 | stop(); | |
166 | } catch (Exception e) { | |
167 | print(depth, "__stop"); | |
168 | print(depth, e); | |
169 | } | |
170 | } | |
171 | ||
172 | print(depth, executed, errors, total); | |
173 | ||
174 | return errors; | |
175 | } | |
176 | ||
177 | /** | |
178 | * Launch the {@link TestCase}s. | |
179 | * | |
180 | * @param depth | |
181 | * the level at which is the launcher (0 = main launcher) | |
182 | * | |
183 | * @return the number of errors | |
184 | */ | |
185 | protected int launchTests(int depth) { | |
186 | int errors = 0; | |
187 | for (TestCase test : tests) { | |
188 | print(depth, test.getName()); | |
189 | ||
e9ca6bb8 | 190 | Throwable ex = null; |
80383c14 NR |
191 | try { |
192 | try { | |
193 | test.setUp(); | |
e9ca6bb8 | 194 | } catch (Throwable e) { |
80383c14 NR |
195 | throw new SetupException(e); |
196 | } | |
197 | test.test(); | |
198 | try { | |
199 | test.tearDown(); | |
e9ca6bb8 | 200 | } catch (Throwable e) { |
80383c14 NR |
201 | throw new TearDownException(e); |
202 | } | |
e9ca6bb8 | 203 | } catch (Throwable e) { |
80383c14 NR |
204 | ex = e; |
205 | } | |
206 | ||
207 | if (ex != null) { | |
208 | errors++; | |
209 | } | |
210 | ||
211 | print(depth, ex); | |
212 | ||
213 | executed++; | |
214 | ||
215 | if (ex != null && !cont) { | |
216 | break; | |
217 | } | |
218 | } | |
219 | ||
220 | return errors; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Specify a custom number of columns to use for the display of messages. | |
225 | * | |
226 | * @param columns | |
227 | * the number of columns | |
228 | */ | |
229 | public void setColumns(int columns) { | |
230 | this.columns = columns; | |
231 | } | |
232 | ||
233 | /** | |
234 | * Continue to run the tests when an error is detected. | |
235 | * | |
236 | * @param cont | |
237 | * yes or no | |
238 | */ | |
239 | public void setContinueAfterFail(boolean cont) { | |
240 | this.cont = cont; | |
241 | } | |
242 | ||
243 | /** | |
244 | * Set a custom "[ ok ]" {@link String} when a test passed. | |
245 | * | |
246 | * @param okString | |
247 | * the {@link String} to display at the end of a success | |
248 | */ | |
249 | public void setOkString(String okString) { | |
250 | this.okString = okString; | |
251 | } | |
252 | ||
253 | /** | |
254 | * Set a custom "[ !! ]" {@link String} when a test failed. | |
255 | * | |
256 | * @param koString | |
257 | * the {@link String} to display at the end of a failure | |
258 | */ | |
259 | public void setKoString(String koString) { | |
260 | this.koString = koString; | |
261 | } | |
262 | ||
263 | /** | |
264 | * Print the test suite header. | |
265 | * | |
266 | * @param depth | |
267 | * the level at which is the launcher (0 = main launcher) | |
268 | */ | |
269 | protected void print(int depth) { | |
270 | if (depth == 0) { | |
271 | System.out.println("[ Test suite: " + name + " ]"); | |
272 | System.out.println(""); | |
273 | } else { | |
86057589 | 274 | System.out.println(prefix(depth, false) + name + ":"); |
80383c14 NR |
275 | } |
276 | } | |
277 | ||
278 | /** | |
279 | * Print the name of the {@link TestCase} we will start immediately after. | |
280 | * | |
281 | * @param depth | |
282 | * the level at which is the launcher (0 = main launcher) | |
db31c358 NR |
283 | * @param name |
284 | * the {@link TestCase} name | |
80383c14 NR |
285 | */ |
286 | protected void print(int depth, String name) { | |
86057589 NR |
287 | name = prefix(depth, false) |
288 | + (name == null ? "" : name).replace("\t", " "); | |
80383c14 | 289 | |
0988831f NR |
290 | StringBuilder dots = new StringBuilder(); |
291 | while ((name.length() + dots.length()) < columns - 11) { | |
292 | dots.append('.'); | |
80383c14 NR |
293 | } |
294 | ||
0988831f | 295 | System.out.print(name + dots.toString()); |
80383c14 NR |
296 | } |
297 | ||
298 | /** | |
299 | * Print the result of the {@link TestCase} we just ran. | |
300 | * | |
301 | * @param depth | |
302 | * the level at which is the launcher (0 = main launcher) | |
303 | * @param error | |
304 | * the {@link Exception} it ran into if any | |
305 | */ | |
e9ca6bb8 | 306 | private void print(int depth, Throwable error) { |
80383c14 NR |
307 | if (error != null) { |
308 | System.out.println(" " + koString); | |
16d59378 NR |
309 | StringWriter sw = new StringWriter(); |
310 | PrintWriter pw = new PrintWriter(sw); | |
311 | error.printStackTrace(pw); | |
312 | String lines = sw.toString(); | |
86057589 NR |
313 | for (String line : lines.split("\n")) { |
314 | System.out.println(prefix(depth, false) + "\t\t" + line); | |
80383c14 NR |
315 | } |
316 | } else { | |
317 | System.out.println(" " + okString); | |
318 | } | |
319 | } | |
320 | ||
321 | /** | |
322 | * Print the total result for this test suite. | |
323 | * | |
324 | * @param depth | |
325 | * the level at which is the launcher (0 = main launcher) | |
326 | * @param executed | |
327 | * the number of tests actually ran | |
328 | * @param errors | |
329 | * the number of errors encountered | |
330 | * @param total | |
331 | * the total number of tests in the suite | |
332 | */ | |
333 | private void print(int depth, int executed, int errors, int total) { | |
334 | int ok = executed - errors; | |
335 | int pc = (int) ((100.0 * ok) / executed); | |
336 | if (pc == 0 && ok > 0) { | |
337 | pc = 1; | |
338 | } | |
339 | int pcTotal = (int) ((100.0 * ok) / total); | |
340 | if (pcTotal == 0 && ok > 0) { | |
341 | pcTotal = 1; | |
342 | } | |
343 | ||
344 | String resume = "Tests passed: " + ok + "/" + executed + " (" + pc | |
345 | + "%) on a total of " + total + " (" + pcTotal + "% total)"; | |
346 | if (depth == 0) { | |
347 | System.out.println(resume); | |
348 | } else { | |
86057589 NR |
349 | String arrow = "┗▶ "; |
350 | System.out.println(prefix(depth, currentSeries == 0) + arrow | |
351 | + resume); | |
352 | System.out.println(prefix(depth, currentSeries == 0)); | |
80383c14 NR |
353 | } |
354 | } | |
355 | ||
356 | private int last = -1; | |
357 | ||
358 | /** | |
359 | * Return the prefix to print before the current line. | |
360 | * | |
361 | * @param depth | |
362 | * the current depth | |
86057589 NR |
363 | * @param first |
364 | * this line is the first of its tabulation level | |
80383c14 NR |
365 | * |
366 | * @return the prefix | |
367 | */ | |
86057589 | 368 | private String prefix(int depth, boolean first) { |
80383c14 NR |
369 | String space = tabs(depth - 1); |
370 | ||
371 | String line = ""; | |
372 | if (depth > 0) { | |
373 | if (depth > 1) { | |
86057589 | 374 | if (depth != last && first) { |
80383c14 NR |
375 | line = "╻"; // first line |
376 | } else { | |
377 | line = "┃"; // continuation | |
378 | } | |
379 | } | |
86057589 NR |
380 | |
381 | space += line + tabs(1); | |
80383c14 NR |
382 | } |
383 | ||
384 | last = depth; | |
385 | return space; | |
386 | } | |
387 | ||
388 | /** | |
389 | * Return the given number of space-converted tabs in a {@link String}. | |
390 | * | |
391 | * @param depth | |
392 | * the number of tabs to return | |
393 | * | |
394 | * @return the string | |
395 | */ | |
396 | private String tabs(int depth) { | |
80383c14 NR |
397 | StringBuilder builder = new StringBuilder(); |
398 | for (int i = 0; i < depth; i++) { | |
399 | builder.append(" "); | |
400 | } | |
401 | return builder.toString(); | |
402 | } | |
403 | } |