Commit | Line | Data |
---|---|---|
64c5b50c AM |
1 | /* |
2 | * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
19 | package org.lttng.ust.agent.utils; | |
20 | ||
21 | import org.junit.runner.Description; | |
22 | import org.junit.runner.notification.Failure; | |
23 | import org.junit.runner.notification.RunListener; | |
24 | import org.junit.runner.notification.RunNotifier; | |
25 | import org.junit.runners.BlockJUnit4ClassRunner; | |
26 | import org.junit.runners.model.InitializationError; | |
27 | ||
28 | /** | |
29 | * Test runner that will print the name of the test being run to stdout. | |
30 | * | |
31 | * Thanks to http://stackoverflow.com/a/27070843/4227853 for the tips. | |
32 | * | |
33 | * @author Alexandre Montplaisir | |
34 | */ | |
35 | public class TestPrintRunner extends BlockJUnit4ClassRunner { | |
36 | ||
37 | /** | |
38 | * Consructor | |
39 | * | |
40 | * @param klass | |
41 | * @throws InitializationError | |
42 | */ | |
43 | public TestPrintRunner(Class<?> klass) throws InitializationError { | |
44 | super(klass); | |
45 | } | |
46 | ||
47 | @Override | |
48 | public void run(RunNotifier notifier) { | |
49 | notifier.addListener(new TestPrintListener()); | |
50 | super.run(notifier); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Listener that will print the class and test name to stdout. | |
55 | */ | |
56 | public static class TestPrintListener extends RunListener { | |
57 | ||
58 | @Override | |
59 | public void testStarted(Description description) { | |
60 | System.out.println("Running " + getTestName(description)); | |
61 | } | |
62 | ||
63 | @Override | |
64 | public void testAssumptionFailure(Failure failure) { | |
65 | System.out.println("SKIPPING TEST: " + getTestName(failure.getDescription())); | |
66 | System.out.println(failure.getMessage()); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Get the className#methodName from a Description. | |
71 | */ | |
72 | private static String getTestName(Description description) { | |
73 | return description.getClassName() + '#' + description.getMethodName(); | |
74 | } | |
75 | } | |
76 | ||
77 | } |