Skip to content

Commit 16d2be4

Browse files
authored
Merge pull request #14 from Nordstrom/pr/frameworkmethod-to-testclass
Add more events and a new "watcher"
2 parents f8d9d8a + 19af1fb commit 16d2be4

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.nordstrom.tools</groupId>
44
<artifactId>junit-foundation</artifactId>
5-
<version>6.0.3-SNAPSHOT</version>
5+
<version>6.1.0-SNAPSHOT</version>
66
<packaging>jar</packaging>
77

88
<name>JUnit Foundation</name>

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class CreateTestClass {
2626
private static final ServiceLoader<TestClassWatcher> classWatcherLoader;
2727
private static final Logger LOGGER = LoggerFactory.getLogger(CreateTestClass.class);
2828
private static final Map<TestClass, Object> TESTCLASS_TO_RUNNER = new ConcurrentHashMap<>();
29+
private static final Map<Object, TestClass> METHOD_TO_TESTCLASS = new ConcurrentHashMap<>();
2930

3031
static {
3132
classWatcherLoader = ServiceLoader.load(TestClassWatcher.class);
@@ -45,6 +46,10 @@ public static TestClass intercept(@This final Object runner, @SuperCall final Ca
4546
TestClass testClass = (TestClass) proxy.call();
4647
TESTCLASS_TO_RUNNER.put(testClass, runner);
4748

49+
for (Object method : testClass.getAnnotatedMethods()) {
50+
METHOD_TO_TESTCLASS.put(method, testClass);
51+
}
52+
4853
for (TestClassWatcher watcher : classWatcherLoader) {
4954
watcher.testClassCreated(testClass, runner);
5055
}
@@ -118,4 +123,18 @@ static Object getRunnerFor(TestClass testClass) {
118123
}
119124
throw new IllegalArgumentException("No associated runner was found for specified test class");
120125
}
126+
127+
/**
128+
* Get the test class associated with the specified framework method.
129+
*
130+
* @param method {@code FrameworkMethod} object
131+
* @return {@link TestClass} object associated with the specified framework method
132+
*/
133+
static TestClass getTestClassWith(Object method) {
134+
TestClass testClass = METHOD_TO_TESTCLASS.get(method);
135+
if (testClass != null) {
136+
return testClass;
137+
}
138+
throw new IllegalArgumentException("No associated test class was found for specified framework method");
139+
}
121140
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ static synchronized JUnitConfig getConfig() {
126126
@SuppressWarnings("squid:S1118")
127127
public static class Run {
128128
private static final ServiceLoader<RunListener> runListenerLoader;
129+
private static final ServiceLoader<RunnerWatcher> runnerWatcherLoader;
129130
private static final Set<RunNotifier> NOTIFIERS = new HashSet<>();
130131
private static final Map<Object, Object> CHILD_TO_PARENT = new ConcurrentHashMap<>();
131132

132133
static {
133134
runListenerLoader = ServiceLoader.load(RunListener.class);
135+
runnerWatcherLoader = ServiceLoader.load(RunnerWatcher.class);
134136
}
135137

136138
/**
@@ -157,7 +159,15 @@ public static void intercept(@This final Object runner, @SuperCall final Callabl
157159
}
158160
}
159161

162+
for (RunnerWatcher watcher : runnerWatcherLoader) {
163+
watcher.runStarted(runner);
164+
}
165+
160166
proxy.call();
167+
168+
for (RunnerWatcher watcher : runnerWatcherLoader) {
169+
watcher.runFinished(runner);
170+
}
161171
}
162172

163173
/**
@@ -242,6 +252,16 @@ public static Object getRunnerFor(TestClass testClass) {
242252
return CreateTestClass.getRunnerFor(testClass);
243253
}
244254

255+
/**
256+
* Get the test class associated with the specified framework method.
257+
*
258+
* @param method {@code FrameworkMethod} object
259+
* @return {@link TestClass} object associated with the specified framework method
260+
*/
261+
public static TestClass getTestClassWith(Object method) {
262+
return CreateTestClass.getTestClassWith(method);
263+
}
264+
245265
/**
246266
* Get the parent runner that owns specified child runner.
247267
*
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.nordstrom.automation.junit;
2+
3+
public interface RunnerWatcher {
4+
5+
/**
6+
* Called when a test runner is about to run.
7+
*
8+
* @param runner JUnit test runner
9+
*/
10+
public void runStarted(Object runner);
11+
12+
/**
13+
* Called when a test runner has finished running.
14+
*
15+
* @param runner JUnit test runner
16+
*/
17+
public void runFinished(Object runner);
18+
19+
}

0 commit comments

Comments
 (0)