|
| 1 | +package com.nordstrom.automation.junit; |
| 2 | + |
| 3 | +import static com.nordstrom.automation.junit.LifecycleHooks.getFieldValue; |
| 4 | +import static com.nordstrom.automation.junit.LifecycleHooks.setFieldValue; |
| 5 | + |
| 6 | +import java.util.Map; |
| 7 | +import java.util.ServiceLoader; |
| 8 | +import java.util.concurrent.Callable; |
| 9 | +import java.util.concurrent.ConcurrentHashMap; |
| 10 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 11 | + |
| 12 | +import org.junit.runners.model.RunnerScheduler; |
| 13 | +import org.junit.runners.model.TestClass; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import net.bytebuddy.implementation.bind.annotation.SuperCall; |
| 18 | +import net.bytebuddy.implementation.bind.annotation.This; |
| 19 | + |
| 20 | +/** |
| 21 | + * This class declares the interceptor for the {@link org.junit.runners.ParentRunner#createTestClass |
| 22 | + * createTestClass} method. |
| 23 | + */ |
| 24 | +@SuppressWarnings("squid:S1118") |
| 25 | +public class CreateTestClass { |
| 26 | + private static final ServiceLoader<TestClassWatcher> classWatcherLoader; |
| 27 | + private static final Logger LOGGER = LoggerFactory.getLogger(CreateTestClass.class); |
| 28 | + private static final Map<TestClass, Object> TESTCLASS_TO_RUNNER = new ConcurrentHashMap<>(); |
| 29 | + |
| 30 | + static { |
| 31 | + classWatcherLoader = ServiceLoader.load(TestClassWatcher.class); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Interceptor for the {@link org.junit.runners.ParentRunner#createTestClass createTestClass} method. |
| 36 | + * |
| 37 | + * @param runner underlying test runner |
| 38 | + * @param proxy callable proxy for the intercepted method |
| 39 | + * @return new {@link TestClass} object |
| 40 | + * @throws Exception {@code anything} (exception thrown by the intercepted method) |
| 41 | + */ |
| 42 | + public static TestClass intercept(@This final Object runner, @SuperCall final Callable<?> proxy) |
| 43 | + throws Exception { |
| 44 | + |
| 45 | + TestClass testClass = (TestClass) proxy.call(); |
| 46 | + TESTCLASS_TO_RUNNER.put(testClass, runner); |
| 47 | + |
| 48 | + for (TestClassWatcher watcher : classWatcherLoader) { |
| 49 | + watcher.testClassCreated(testClass, runner); |
| 50 | + } |
| 51 | + |
| 52 | + attachRunnerScheduler(testClass, runner); |
| 53 | + return testClass; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Attach lifecycle-reporting runner scheduler to the specified parent runner. |
| 58 | + * |
| 59 | + * @param testClass {@link TestClass} object that was just created |
| 60 | + * @param runner {@link ParentRunner} for the specified test class |
| 61 | + */ |
| 62 | + private static void attachRunnerScheduler(final TestClass testClass, final Object runner) { |
| 63 | + try { |
| 64 | + RunnerScheduler scheduler = getFieldValue(runner, "scheduler"); |
| 65 | + setFieldValue(runner, "scheduler", createRunnerScheduler(testClass, scheduler)); |
| 66 | + } catch (IllegalAccessException | NoSuchFieldException | SecurityException | IllegalArgumentException e) { |
| 67 | + LOGGER.warn("Unable to attach notifying runner scheduler", e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Create notifying runner scheduler, which forwards to the previous scheduler if specified. |
| 73 | + * |
| 74 | + * @param testClass {@link TestClass} object that was just created |
| 75 | + * @param scheduler runner scheduler that's currently attached to the specified runner (may be {@code null}) |
| 76 | + * @return new notifying runner scheduler |
| 77 | + */ |
| 78 | + private static RunnerScheduler createRunnerScheduler(final TestClass testClass, final RunnerScheduler scheduler) { |
| 79 | + return new RunnerScheduler() { |
| 80 | + private AtomicBoolean scheduled = new AtomicBoolean(false); |
| 81 | + |
| 82 | + public void schedule(Runnable childStatement) { |
| 83 | + if (scheduled.compareAndSet(false, true)) { |
| 84 | + for (TestClassWatcher watcher : classWatcherLoader) { |
| 85 | + watcher.testClassStarted(testClass); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + RunReflectiveCall.fireTestStarted(testClass, childStatement); |
| 90 | + |
| 91 | + if (scheduler != null) { |
| 92 | + scheduler.schedule(childStatement); |
| 93 | + } else { |
| 94 | + childStatement.run(); |
| 95 | + } |
| 96 | + |
| 97 | + RunReflectiveCall.fireTestFinished(testClass); |
| 98 | + } |
| 99 | + |
| 100 | + public void finished() { |
| 101 | + for (TestClassWatcher watcher : classWatcherLoader) { |
| 102 | + watcher.testClassFinished(testClass); |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get the parent runner associate with the specified test class. |
| 110 | + * |
| 111 | + * @param testClass {@link TestClass} object |
| 112 | + * @return {@code ParentRunner} object associated with the specified test class |
| 113 | + */ |
| 114 | + static Object getRunnerFor(TestClass testClass) { |
| 115 | + Object runner = TESTCLASS_TO_RUNNER.get(testClass); |
| 116 | + if (runner != null) { |
| 117 | + return runner; |
| 118 | + } |
| 119 | + throw new IllegalArgumentException("No associated runner was found for specified test class"); |
| 120 | + } |
| 121 | +} |
0 commit comments