Skip to content

Commit 1b67384

Browse files
authored
Merge pull request #39 from Nordstrom/pr/remove-duplicate-code
Retain AtomicTest object for entire execution of atomic test
2 parents 987954c + f4c39ba commit 1b67384

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

src/main/java/com/nordstrom/automation/junit/LifecycleHooks.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ public static TestClass getTestClassOf(Object runner) {
169169
return invoke(runner, "getTestClass");
170170
}
171171

172+
/**
173+
* Get the atomic test object for the specified class runner.
174+
*
175+
* @param runner JUnit class runner
176+
* @return {@link AtomicTest} object (may be {@code null})
177+
*/
178+
public static AtomicTest getAtomicTestOf(Object runner) {
179+
return RunAnnouncer.getAtomicTestOf(runner);
180+
}
181+
172182
/**
173183
* Get the description of the indicated child object from the runner for the specified test class instance.
174184
*
@@ -369,9 +379,6 @@ public static <T extends JUnitWatcher> Optional<T> getAttachedWatcher(Class<T> w
369379
watcher = RunAnnouncer.getAttachedWatcher(watcherType);
370380
if (watcher.isPresent()) return watcher;
371381

372-
watcher = RunAnnouncer.getAttachedWatcher(watcherType);
373-
if (watcher.isPresent()) return watcher;
374-
375382
return RunReflectiveCall.getAttachedWatcher(watcherType);
376383
}
377384

src/main/java/com/nordstrom/automation/junit/RunAnnouncer.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.nordstrom.automation.junit;
22

3+
import java.util.Map;
34
import java.util.Optional;
45
import java.util.ServiceLoader;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
58
import org.junit.internal.AssumptionViolatedException;
69
import org.junit.runner.Description;
710
import org.junit.runner.notification.Failure;
@@ -10,6 +13,7 @@
1013
public 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

Comments
 (0)