|
| 1 | +package com.nordstrom.automation.junit; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.ServiceLoader; |
| 5 | +import java.util.concurrent.Callable; |
| 6 | +import java.util.concurrent.ConcurrentHashMap; |
| 7 | + |
| 8 | +import net.bytebuddy.implementation.bind.annotation.RuntimeType; |
| 9 | +import net.bytebuddy.implementation.bind.annotation.SuperCall; |
| 10 | +import net.bytebuddy.implementation.bind.annotation.This; |
| 11 | + |
| 12 | +/** |
| 13 | + * This class declares the interceptor for the {@link org.junit.runners.BlockJUnit4ClassRunner#createTest |
| 14 | + * createTest} method. |
| 15 | + */ |
| 16 | +@SuppressWarnings("squid:S1118") |
| 17 | +public class CreateTest { |
| 18 | + |
| 19 | + private static final ServiceLoader<TestObjectWatcher> objectWatcherLoader; |
| 20 | + private static final Map<Object, Object> TARGET_TO_RUNNER = new ConcurrentHashMap<>(); |
| 21 | + private static final Map<Object, Object> RUNNER_TO_TARGET = new ConcurrentHashMap<>(); |
| 22 | + private static final ThreadLocal<Integer> COUNTER; |
| 23 | + private static final DepthGauge DEPTH; |
| 24 | + |
| 25 | + static { |
| 26 | + objectWatcherLoader = ServiceLoader.load(TestObjectWatcher.class); |
| 27 | + COUNTER = DepthGauge.getCounter(); |
| 28 | + DEPTH = new DepthGauge(COUNTER); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Interceptor for the {@link org.junit.runners.BlockJUnit4ClassRunner#createTest createTest} method. |
| 33 | + * |
| 34 | + * @param runner target {@link org.junit.runners.BlockJUnit4ClassRunner BlockJUnit4ClassRunner} object |
| 35 | + * @param proxy callable proxy for the intercepted method |
| 36 | + * @return {@code anything} - JUnit test class instance |
| 37 | + * @throws Exception {@code anything} (exception thrown by the intercepted method) |
| 38 | + */ |
| 39 | + @RuntimeType |
| 40 | + public static Object intercept(@This final Object runner, |
| 41 | + @SuperCall final Callable<?> proxy) throws Exception { |
| 42 | + |
| 43 | + Object testObj; |
| 44 | + try { |
| 45 | + DEPTH.increaseDepth(); |
| 46 | + testObj = LifecycleHooks.callProxy(proxy); |
| 47 | + } finally { |
| 48 | + DEPTH.decreaseDepth(); |
| 49 | + } |
| 50 | + |
| 51 | + TARGET_TO_RUNNER.put(testObj, runner); |
| 52 | + RUNNER_TO_TARGET.put(runner, testObj); |
| 53 | + LifecycleHooks.applyTimeout(testObj); |
| 54 | + |
| 55 | + if (DEPTH.atGroundLevel()) { |
| 56 | + synchronized(objectWatcherLoader) { |
| 57 | + for (TestObjectWatcher watcher : objectWatcherLoader) { |
| 58 | + watcher.testObjectCreated(testObj, runner); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return testObj; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the class runner associated with the specified instance. |
| 68 | + * |
| 69 | + * @param target instance of JUnit test class |
| 70 | + * @return {@link org.junit.runners.BlockJUnit4ClassRunner BlockJUnit4ClassRunner} for specified instance |
| 71 | + */ |
| 72 | + static Object getRunnerForTarget(Object target) { |
| 73 | + return TARGET_TO_RUNNER.get(target); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the JUnit test class instance for the specified class runner. |
| 78 | + * |
| 79 | + * @param runner JUnit class runner |
| 80 | + * @return JUnit test class instance for specified runner |
| 81 | + */ |
| 82 | + static Object getTargetForRunner(Object runner) { |
| 83 | + return RUNNER_TO_TARGET.get(runner); |
| 84 | + } |
| 85 | +} |
0 commit comments