28810678b2980a04db5a60c0ba7ff89e813b835e
[lttng-ust-java-tests.git] / src / test / java / org / lttng / ust / agent / utils / TestPrintRunner.java
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 RunListener listener = new TestPrintListener();
50
51 notifier.addListener(listener);
52 super.run(notifier);
53 notifier.removeListener(listener);
54 }
55
56 /**
57 * Listener that will print the class and test name to stdout.
58 */
59 public static class TestPrintListener extends RunListener {
60
61 @Override
62 public void testStarted(Description description) {
63 System.out.println("Running " + getTestName(description));
64 }
65
66 @Override
67 public void testAssumptionFailure(Failure failure) {
68 System.out.println("SKIPPING TEST: " + getTestName(failure.getDescription()));
69 System.out.println(failure.getMessage());
70 }
71
72 /**
73 * Get the className#methodName from a Description.
74 */
75 private static String getTestName(Description description) {
76 return description.getClassName() + '#' + description.getMethodName();
77 }
78 }
79
80 }
This page took 0.039487 seconds and 3 git commands to generate.