Skip to content

Commit 862e4e0

Browse files
authored
Merge pull request #38 from Nordstrom/pr/add-watcher-finder
Pr/add watcher finder
2 parents 5a05d7a + baf49d9 commit 862e4e0

File tree

11 files changed

+143
-13
lines changed

11 files changed

+143
-13
lines changed

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

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

33
import java.util.Map;
4+
import java.util.Optional;
45
import java.util.ServiceLoader;
56
import java.util.concurrent.Callable;
67
import java.util.concurrent.ConcurrentHashMap;
@@ -79,4 +80,25 @@ static Object getRunnerForTarget(Object target) {
7980
static Object getTargetForRunner(Object runner) {
8081
return RUNNER_TO_TARGET.get(runner);
8182
}
83+
84+
/**
85+
* Get reference to an instance of the specified watcher type.
86+
*
87+
* @param <T> watcher type
88+
* @param watcherType watcher type
89+
* @return optional watcher instance
90+
*/
91+
@SuppressWarnings("unchecked")
92+
static <T extends JUnitWatcher> Optional<T> getAttachedWatcher(Class<T> watcherType) {
93+
if (TestObjectWatcher.class.isAssignableFrom(watcherType)) {
94+
synchronized(objectWatcherLoader) {
95+
for (TestObjectWatcher watcher : objectWatcherLoader) {
96+
if (watcher.getClass() == watcherType) {
97+
return Optional.of((T) watcher);
98+
}
99+
}
100+
}
101+
}
102+
return Optional.empty();
103+
}
82104
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.nordstrom.automation.junit;
2+
3+
/**
4+
* <b>JUnitWatcher</b> is a marker interface for JUnit test execution event watchers.
5+
*/
6+
public interface JUnitWatcher {
7+
8+
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import java.lang.reflect.InvocationTargetException;
99
import java.lang.reflect.Method;
1010
import java.util.Arrays;
11+
import java.util.Optional;
1112
import java.util.ServiceLoader;
1213
import java.util.concurrent.Callable;
1314

1415
import org.junit.Test;
1516
import org.junit.runner.Description;
17+
import org.junit.runner.notification.RunListener;
1618
import org.junit.runners.model.TestClass;
1719
import com.nordstrom.automation.junit.JUnitConfig.JUnitSettings;
1820
import com.nordstrom.common.base.UncheckedThrow;
@@ -349,4 +351,38 @@ static Object callProxy(final Callable<?> proxy) throws Exception {
349351
throw UncheckedThrow.throwUnchecked(e.getCause());
350352
}
351353
}
354+
355+
/**
356+
* Get reference to an instance of the specified watcher type.
357+
*
358+
* @param <T> watcher type
359+
* @param watcherType watcher type
360+
* @return optional watcher instance
361+
*/
362+
public static <T extends JUnitWatcher> Optional<T> getAttachedWatcher(Class<T> watcherType) {
363+
Optional<T> watcher = CreateTest.getAttachedWatcher(watcherType);
364+
if (watcher.isPresent()) return watcher;
365+
366+
watcher = Run.getAttachedWatcher(watcherType);
367+
if (watcher.isPresent()) return watcher;
368+
369+
watcher = RunAnnouncer.getAttachedWatcher(watcherType);
370+
if (watcher.isPresent()) return watcher;
371+
372+
watcher = RunAnnouncer.getAttachedWatcher(watcherType);
373+
if (watcher.isPresent()) return watcher;
374+
375+
return RunReflectiveCall.getAttachedWatcher(watcherType);
376+
}
377+
378+
/**
379+
* Get reference to an instance of the specified listener type.
380+
*
381+
* @param <T> listener type
382+
* @param listenerType listener type
383+
* @return optional listener instance
384+
*/
385+
public static <T extends RunListener> Optional<T> getAttachedListener(Class<T> listenerType) {
386+
return Run.getAttachedListener(listenerType);
387+
}
352388
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* This interface defines the methods implemented by JUnit method watchers.
77
*/
8-
public interface MethodWatcher {
8+
public interface MethodWatcher extends JUnitWatcher {
99

1010
/**
1111
* Invoked before each test or configuration method is invoked

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Deque;
55
import java.util.List;
66
import java.util.Map;
7+
import java.util.Optional;
78
import java.util.ServiceLoader;
89
import java.util.Set;
910
import java.util.concurrent.Callable;
@@ -160,4 +161,44 @@ static boolean fireRunFinished(Object runner) {
160161
}
161162
return false;
162163
}
164+
165+
/**
166+
* Get reference to an instance of the specified watcher type.
167+
*
168+
* @param <T> watcher type
169+
* @param watcherType watcher type
170+
* @return optional watcher instance
171+
*/
172+
@SuppressWarnings("unchecked")
173+
static <T extends JUnitWatcher> Optional<T> getAttachedWatcher(Class<T> watcherType) {
174+
if (RunnerWatcher.class.isAssignableFrom(watcherType)) {
175+
synchronized(runnerWatcherLoader) {
176+
for (RunnerWatcher watcher : runnerWatcherLoader) {
177+
if (watcher.getClass() == watcherType) {
178+
return Optional.of((T) watcher);
179+
}
180+
}
181+
}
182+
}
183+
return Optional.empty();
184+
}
185+
186+
/**
187+
* Get reference to an instance of the specified listener type.
188+
*
189+
* @param <T> listener type
190+
* @param listenerType listener type
191+
* @return optional listener instance
192+
*/
193+
@SuppressWarnings("unchecked")
194+
static <T extends RunListener> Optional<T> getAttachedListener(Class<T> listenerType) {
195+
synchronized(runListenerLoader) {
196+
for (RunListener listener : runListenerLoader) {
197+
if (listener.getClass() == listenerType) {
198+
return Optional.of((T) listener);
199+
}
200+
}
201+
}
202+
return Optional.empty();
203+
}
163204
}

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

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

3+
import java.util.Optional;
34
import java.util.ServiceLoader;
45
import org.junit.internal.AssumptionViolatedException;
56
import org.junit.runner.Description;
@@ -94,4 +95,25 @@ static AtomicTest getAtomicTest(Failure failure) {
9495
atomicTest.setThrowable(failure.getException());
9596
return atomicTest;
9697
}
98+
99+
/**
100+
* Get reference to an instance of the specified watcher type.
101+
*
102+
* @param <T> watcher type
103+
* @param watcherType watcher type
104+
* @return optional watcher instance
105+
*/
106+
@SuppressWarnings("unchecked")
107+
static <T extends JUnitWatcher> Optional<T> getAttachedWatcher(Class<T> watcherType) {
108+
if (RunWatcher.class.isAssignableFrom(watcherType)) {
109+
synchronized(runWatcherLoader) {
110+
for (RunWatcher watcher : runWatcherLoader) {
111+
if (watcher.getClass() == watcherType) {
112+
return Optional.of((T) watcher);
113+
}
114+
}
115+
}
116+
}
117+
return Optional.empty();
118+
}
97119
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.util.HashMap;
66
import java.util.Map;
7-
import java.util.Objects;
87
import java.util.Optional;
98
import java.util.ServiceLoader;
109
import java.util.concurrent.Callable;
@@ -100,16 +99,18 @@ public static Object intercept(@This final Object callable, @SuperCall final Cal
10099
/**
101100
* Get reference to an instance of the specified watcher type.
102101
*
102+
* @param <T> watcher type
103103
* @param watcherType watcher type
104104
* @return optional watcher instance
105105
*/
106-
public static Optional<MethodWatcher> getAttachedWatcher(
107-
Class<? extends MethodWatcher> watcherType) {
108-
Objects.requireNonNull(watcherType, "[watcherType] must be non-null");
109-
synchronized(methodWatcherLoader) {
110-
for (MethodWatcher watcher : methodWatcherLoader) {
111-
if (watcher.getClass() == watcherType) {
112-
return Optional.of(watcher);
106+
@SuppressWarnings("unchecked")
107+
static <T extends JUnitWatcher> Optional<T> getAttachedWatcher(Class<T> watcherType) {
108+
if (MethodWatcher.class.isAssignableFrom(watcherType)) {
109+
synchronized(methodWatcherLoader) {
110+
for (MethodWatcher watcher : methodWatcherLoader) {
111+
if (watcher.getClass() == watcherType) {
112+
return Optional.of((T) watcher);
113+
}
113114
}
114115
}
115116
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* This interface defines the methods implemented by JUnit run watchers.
77
*/
8-
public interface RunWatcher {
8+
public interface RunWatcher extends JUnitWatcher {
99

1010
/**
1111
* Called when an atomic test is about to be started.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.nordstrom.automation.junit;
22

3-
public interface RunnerWatcher {
3+
public interface RunnerWatcher extends JUnitWatcher {
44

55
/**
66
* Called when a test runner is about to run.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This interface defines the methods implemented by JUnit test object watchers. These watchers are registered via a
55
* ServiceLoader provider configuration file.
66
*/
7-
public interface TestObjectWatcher {
7+
public interface TestObjectWatcher extends JUnitWatcher {
88

99
/**
1010
* Invoked when a test class instance gets created.

0 commit comments

Comments
 (0)