-
Notifications
You must be signed in to change notification settings - Fork 6
/
RunAnnouncer.java
123 lines (113 loc) · 4.57 KB
/
RunAnnouncer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.nordstrom.automation.junit;
import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class implements a notification-enhancing extension of the standard {@link RunListener} class. This run
* announcer is the source of notifications sent to attached implementations of the {@link RunWatcher} interface.
* Note that <b>RunAnnouncer</b> is attached
* <a href="https://github.com/sbabcoc/JUnit-Foundation#support-for-standard-junit-runlistener-providers">
* automatically</a> by <b>JUnit Foundation</b>; attaching this run listener through conventional methods (Maven
* or Gradle project configuration, {@code JUnitCore.addListener()}) is not only unnecessary, but will likely
* suppress <b>RunWatcher</b> notifications.
*/
@RunAnnouncer.ThreadSafe
public class RunAnnouncer extends RunListener implements JUnitWatcher {
private static final Logger LOGGER = LoggerFactory.getLogger(RunAnnouncer.class);
/**
* {@inheritDoc}
*/
@Override
public void testStarted(Description description) throws Exception {
LOGGER.debug("testStarted: {}", description);
AtomicTest atomicTest = ensureAtomicTestOf(description);
for (RunWatcher watcher : LifecycleHooks.getRunWatchers()) {
watcher.testStarted(atomicTest);
}
}
/**
* {@inheritDoc}
*/
@Override
public void testFinished(Description description) throws Exception {
LOGGER.debug("testFinished: {}", description);
AtomicTest atomicTest = ensureAtomicTestOf(description);
for (RunWatcher watcher : LifecycleHooks.getRunWatchers()) {
watcher.testFinished(atomicTest);
}
}
/**
* {@inheritDoc}
*/
@Override
public void testFailure(Failure failure) throws Exception {
LOGGER.debug("testFailure: {}", failure);
AtomicTest atomicTest = ensureAtomicTestOf(failure);
for (RunWatcher watcher : LifecycleHooks.getRunWatchers()) {
watcher.testFailure(atomicTest, failure.getException());
}
}
/**
* {@inheritDoc}
*/
@Override
public void testAssumptionFailure(Failure failure) {
LOGGER.debug("testAssumptionFailure: {}", failure);
AtomicTest atomicTest = ensureAtomicTestOf(failure);
for (RunWatcher watcher : LifecycleHooks.getRunWatchers()) {
watcher.testAssumptionFailure(atomicTest, (AssumptionViolatedException) failure.getException());
}
}
/**
* {@inheritDoc}
*/
@Override
public void testIgnored(Description description) throws Exception {
LOGGER.debug("testIgnored: {}", description);
AtomicTest atomicTest = ensureAtomicTestOf(description);
for (RunWatcher watcher : LifecycleHooks.getRunWatchers()) {
watcher.testIgnored(atomicTest);
}
}
/**
* Get the atomic test object for the specified method description.
* <p>
* <b>NOTE</b>: For ignored tests, this method returns an ephemeral object.
*
* @param description JUnit method description
* @return {@link AtomicTest} object
*/
private static AtomicTest ensureAtomicTestOf(Description description) {
// get atomic test for this description
AtomicTest atomicTest = EachTestNotifierInit.getAtomicTestOf(description);
// if none was found
if (atomicTest == null) {
// create ephemeral atomic test object
atomicTest = new AtomicTest(description);
}
return atomicTest;
}
/**
* Get the atomic test object for the specified failure.
* <p>
* <b>NOTE</b>: For suite failures, this method returns an ephemeral object.
*
* @param failure
* @return {@link AtomicTest} object
*/
private static AtomicTest ensureAtomicTestOf(Failure failure) {
// get atomic test for this description
AtomicTest atomicTest = EachTestNotifierInit.getAtomicTestOf(failure.getDescription());
// if none was found
if (atomicTest == null) {
// create ephemeral atomic test object
atomicTest = new AtomicTest(failure.getDescription());
// set the exception for this atomic test
atomicTest.setThrowable(failure.getException());
}
return atomicTest;
}
}