11package com .nordstrom .automation .junit ;
22
3+ import java .util .Map ;
34import java .util .Optional ;
45import java .util .ServiceLoader ;
6+ import java .util .concurrent .ConcurrentHashMap ;
7+
58import org .junit .internal .AssumptionViolatedException ;
69import org .junit .runner .Description ;
710import org .junit .runner .notification .Failure ;
1013public class RunAnnouncer extends RunListener {
1114
1215 private static final ServiceLoader <RunWatcher > runWatcherLoader ;
16+ private static final Map <Object , AtomicTest > RUNNER_TO_ATOMICTEST = new ConcurrentHashMap <>();
1317
1418 static {
1519 runWatcherLoader = ServiceLoader .load (RunWatcher .class );
@@ -20,7 +24,7 @@ public class RunAnnouncer extends RunListener {
2024 */
2125 @ Override
2226 public void testStarted (Description description ) throws Exception {
23- AtomicTest atomicTest = getAtomicTest (description );
27+ AtomicTest atomicTest = newAtomicTest (description );
2428 synchronized (runWatcherLoader ) {
2529 for (RunWatcher watcher : runWatcherLoader ) {
2630 watcher .testStarted (atomicTest );
@@ -33,7 +37,7 @@ public void testStarted(Description description) throws Exception {
3337 */
3438 @ Override
3539 public void testFinished (Description description ) throws Exception {
36- AtomicTest atomicTest = getAtomicTest ( description );
40+ AtomicTest atomicTest = getAtomicTestOf ( Run . getThreadRunner () );
3741 synchronized (runWatcherLoader ) {
3842 for (RunWatcher watcher : runWatcherLoader ) {
3943 watcher .testFinished (atomicTest );
@@ -46,7 +50,7 @@ public void testFinished(Description description) throws Exception {
4650 */
4751 @ Override
4852 public void testFailure (Failure failure ) throws Exception {
49- AtomicTest atomicTest = getAtomicTest (failure );
53+ AtomicTest atomicTest = setTestFailure (failure );
5054 synchronized (runWatcherLoader ) {
5155 for (RunWatcher watcher : runWatcherLoader ) {
5256 watcher .testFailure (atomicTest , failure .getException ());
@@ -59,7 +63,7 @@ public void testFailure(Failure failure) throws Exception {
5963 */
6064 @ Override
6165 public void testAssumptionFailure (Failure failure ) {
62- AtomicTest atomicTest = getAtomicTest (failure );
66+ AtomicTest atomicTest = setTestFailure (failure );
6367 synchronized (runWatcherLoader ) {
6468 for (RunWatcher watcher : runWatcherLoader ) {
6569 watcher .testAssumptionFailure (atomicTest , (AssumptionViolatedException ) failure .getException ());
@@ -72,7 +76,7 @@ public void testAssumptionFailure(Failure failure) {
7276 */
7377 @ Override
7478 public void testIgnored (Description description ) throws Exception {
75- AtomicTest atomicTest = getAtomicTest (description );
79+ AtomicTest atomicTest = newAtomicTest (description );
7680 synchronized (runWatcherLoader ) {
7781 for (RunWatcher watcher : runWatcherLoader ) {
7882 watcher .testIgnored (atomicTest );
@@ -81,17 +85,36 @@ public void testIgnored(Description description) throws Exception {
8185 }
8286
8387 /**
84- * Get the atomic test object from the specified description.
88+ * Create new atomic test object for the specified description.
8589 *
8690 * @param description {@link Description} object
8791 * @return {@link AtomicTest} object
8892 */
89- static AtomicTest getAtomicTest (Description description ) {
90- return new AtomicTest (Run .getThreadRunner (), description );
93+ private static AtomicTest newAtomicTest (Description description ) {
94+ Object runner = Run .getThreadRunner ();
95+ AtomicTest atomicTest = new AtomicTest (runner , description );
96+ RUNNER_TO_ATOMICTEST .put (runner , atomicTest );
97+ return atomicTest ;
9198 }
9299
93- static AtomicTest getAtomicTest (Failure failure ) {
94- AtomicTest atomicTest = getAtomicTest (failure .getDescription ());
100+ /**
101+ * Get the atomic test object for the specified class runner.
102+ *
103+ * @param runner JUnit class runner
104+ * @return {@link AtomicTest} object (may be {@code null})
105+ */
106+ static AtomicTest getAtomicTestOf (Object runner ) {
107+ return RUNNER_TO_ATOMICTEST .get (runner );
108+ }
109+
110+ /**
111+ * Store the specified failure in the active atomic test.
112+ *
113+ * @param failure {@link Failure} object
114+ * @return {@link AtomicTest} object
115+ */
116+ private static AtomicTest setTestFailure (Failure failure ) {
117+ AtomicTest atomicTest = getAtomicTestOf (Run .getThreadRunner ());
95118 atomicTest .setThrowable (failure .getException ());
96119 return atomicTest ;
97120 }
0 commit comments