Skip to content

Commit 90ef0ae

Browse files
authored
Get real invoked method description; enhance ArtifactParams feature (#66)
1 parent a2ffc02 commit 90ef0ae

File tree

7 files changed

+80
-26
lines changed

7 files changed

+80
-26
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
*/
1010
public interface ArtifactParams extends Params {
1111

12+
/**
13+
* Get the {@link AtomIdentity} test watcher for the current test class instance.
14+
*
15+
* @return {@code AtomIdentity} test watcher
16+
*/
17+
AtomIdentity getAtomIdentity();
18+
1219
/**
1320
* Get the JUnit method description object for the current test class instance.
1421
*

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44

5+
import org.junit.internal.runners.model.ReflectiveCallable;
56
import org.junit.rules.TestWatcher;
67
import org.junit.runner.Description;
78

@@ -17,6 +18,7 @@ public class AtomIdentity extends TestWatcher implements ArtifactParams {
1718

1819
private final Object instance;
1920
private Description description;
21+
private ReflectiveCallable callable;
2022

2123
public AtomIdentity(Object instance) {
2224
this.instance = instance;
@@ -39,6 +41,32 @@ public Object getInstance() {
3941
return instance;
4042
}
4143

44+
/**
45+
* Set the {@link ReflectiveCallable} object for the current test class instance.
46+
*
47+
* @param callable {@link ReflectiveCallable} object
48+
*/
49+
public void setCallable(ReflectiveCallable callable) {
50+
this.callable = callable;
51+
}
52+
53+
/**
54+
* Get the {@link ReflectiveCallable} object for the current test class instance.
55+
*
56+
* @return {@link ReflectiveCallable} object
57+
*/
58+
public ReflectiveCallable getCallable() {
59+
return callable;
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
@Override
66+
public AtomIdentity getAtomIdentity() {
67+
return this;
68+
}
69+
4270
/**
4371
* {@inheritDoc}
4472
*/
@@ -57,5 +85,5 @@ public Optional<Map<String, Object>> getParameters() {
5785
}
5886
return Optional.absent();
5987
}
60-
88+
6189
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public AtomicTest(Object runner, T identity) {
3333
this.description = invoke(runner, "describeChild", identity);
3434
this.particles = invoke(runner, "getChildren");
3535
}
36+
37+
public AtomicTest(AtomicTest<T> original, Description description) {
38+
this.runner = original.runner;
39+
this.identity = original.identity;
40+
this.description = description;
41+
this.particles = original.particles;
42+
}
3643

3744
/**
3845
* Get the runner for this atomic test.

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,8 @@ public class CreateTest {
1919

2020
private static final Map<Object, Object> TARGET_TO_RUNNER = new ConcurrentHashMap<>();
2121
private static final Map<Object, Object> RUNNER_TO_TARGET = new ConcurrentHashMap<>();
22-
private static final ThreadLocal<DepthGauge> DEPTH;
2322
private static final Logger LOGGER = LoggerFactory.getLogger(CreateTest.class);
2423

25-
static {
26-
DEPTH = new ThreadLocal<DepthGauge>() {
27-
@Override
28-
protected DepthGauge initialValue() {
29-
return new DepthGauge();
30-
}
31-
};
32-
}
33-
3424
/**
3525
* Interceptor for the {@link org.junit.runners.BlockJUnit4ClassRunner#createTest createTest} method.
3626
*
@@ -43,26 +33,19 @@ protected DepthGauge initialValue() {
4333
public static Object intercept(@This final Object runner,
4434
@SuperCall final Callable<?> proxy) throws Exception {
4535

46-
Object target;
47-
try {
48-
DEPTH.get().increaseDepth();
49-
target = LifecycleHooks.callProxy(proxy);
50-
} finally {
51-
DEPTH.get().decreaseDepth();
52-
}
53-
54-
TARGET_TO_RUNNER.put(target, runner);
55-
RUNNER_TO_TARGET.put(runner, target);
36+
Object target = LifecycleHooks.callProxy(proxy);
37+
// apply parameter-based global timeout
38+
TimeoutUtils.applyTestTimeout(runner, target);
5639

57-
if (DEPTH.get().atGroundLevel()) {
40+
if (null == TARGET_TO_RUNNER.put(target, runner)) {
5841
LOGGER.debug("testObjectCreated: {}", target);
42+
RUNNER_TO_TARGET.put(runner, target);
43+
5944
for (TestObjectWatcher watcher : LifecycleHooks.getObjectWatchers()) {
6045
watcher.testObjectCreated(target, runner);
6146
}
6247
}
6348

64-
// apply parameter-based global timeout
65-
TimeoutUtils.applyTestTimeout(runner, target);
6649
return target;
6750
}
6851

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class RunAnnouncer extends RunListener {
3131
@SuppressWarnings({"rawtypes", "unchecked"})
3232
public void testStarted(Description description) throws Exception {
3333
LOGGER.debug("testStarted: {}", description);
34-
AtomicTest<?> atomicTest = getAtomicTestOf(description);
34+
AtomicTest<?> atomicTest = newAtomicTest(description);
3535
for (RunWatcher watcher : LifecycleHooks.getRunWatchers()) {
3636
if (isSupported(watcher, atomicTest)) {
3737
watcher.testStarted(atomicTest);
@@ -110,7 +110,25 @@ public void testIgnored(Description description) throws Exception {
110110
static <T> AtomicTest<T> newAtomicTest(Object runner, T identity) {
111111
AtomicTest<T> atomicTest = new AtomicTest<>(runner, identity);
112112
RUNNER_TO_ATOMICTEST.put(runner, atomicTest);
113-
RUNNER_TO_ATOMICTEST.put(atomicTest.getDescription(), atomicTest);
113+
return atomicTest;
114+
}
115+
116+
/**
117+
* Create new atomic test object for the specified description.
118+
*
119+
* @param <T> type of children associated with the specified runner
120+
* @param description description of the test that is about to be run
121+
* @return {@link AtomicTest} object (may be {@code null})
122+
*/
123+
static <T> AtomicTest<T> newAtomicTest(Description description) {
124+
AtomicTest<T> atomicTest = null;
125+
AtomicTest<T> original = getAtomicTestOf(LifecycleHooks.getThreadRunner());
126+
127+
if (original != null) {
128+
atomicTest = new AtomicTest<>(original, description);
129+
RUNNER_TO_ATOMICTEST.put(description, atomicTest);
130+
}
131+
114132
return atomicTest;
115133
}
116134

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ public static Object intercept(@This final ReflectiveCallable callable, @SuperCa
5757
throws Exception {
5858

5959
Object child = null;
60+
Object target = null;
6061

6162
try {
6263
child = getFieldValue(callable, "this$0");
64+
target = getFieldValue(callable, "val$target");
6365
} catch (IllegalAccessException | NoSuchFieldException | SecurityException | IllegalArgumentException e) {
6466
// handled below
6567
}
@@ -68,6 +70,10 @@ public static Object intercept(@This final ReflectiveCallable callable, @SuperCa
6870
if (runner == null) {
6971
runner = Run.getThreadRunner();
7072
}
73+
74+
if (ArtifactParams.class.isInstance(target)) {
75+
((ArtifactParams) target).getAtomIdentity().setCallable(callable);
76+
}
7177

7278
Object result = null;
7379
Throwable thrown = null;

src/test/java/com/nordstrom/automation/junit/TestBase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public abstract class TestBase implements ArtifactParams {
1212
@Rule
1313
public final UnitTestCapture watcher = new UnitTestCapture(this);
1414

15+
@Override
16+
public AtomIdentity getAtomIdentity() {
17+
return watcher;
18+
}
19+
1520
@Override
1621
public Description getDescription() {
1722
return watcher.getDescription();

0 commit comments

Comments
 (0)