|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.testsupport.runner.classpath; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.net.URLClassLoader; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.extension.Extension; |
| 25 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 26 | +import org.junit.jupiter.api.extension.InvocationInterceptor; |
| 27 | +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; |
| 28 | +import org.junit.platform.engine.discovery.DiscoverySelectors; |
| 29 | +import org.junit.platform.launcher.Launcher; |
| 30 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 31 | +import org.junit.platform.launcher.TestPlan; |
| 32 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 33 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 34 | +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 35 | +import org.junit.platform.launcher.listeners.TestExecutionSummary; |
| 36 | + |
| 37 | +import org.springframework.util.CollectionUtils; |
| 38 | +import org.springframework.util.ReflectionUtils; |
| 39 | + |
| 40 | +/** |
| 41 | + * A custom {@link Extension} that runs tests using a modified class path. Entries are |
| 42 | + * excluded from the class path using {@link ClassPathExclusions @ClassPathExclusions} and |
| 43 | + * overridden using {@link ClassPathOverrides @ClassPathOverrides} on the test class. A |
| 44 | + * class loader is created with the customized class path and is used both to load the |
| 45 | + * test class and as the thread context class loader while the test is being run. |
| 46 | + * |
| 47 | + * @author Christoph Dreis |
| 48 | + * @since 2.2.0 |
| 49 | + */ |
| 50 | +public class ModifiedClassPathExtension implements InvocationInterceptor { |
| 51 | + |
| 52 | + @Override |
| 53 | + public void interceptBeforeAllMethod(Invocation<Void> invocation, |
| 54 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 55 | + interceptInvocation(invocation, extensionContext); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void interceptBeforeEachMethod(Invocation<Void> invocation, |
| 60 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 61 | + interceptInvocation(invocation, extensionContext); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void interceptAfterEachMethod(Invocation<Void> invocation, |
| 66 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 67 | + interceptInvocation(invocation, extensionContext); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void interceptAfterAllMethod(Invocation<Void> invocation, |
| 72 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 73 | + interceptInvocation(invocation, extensionContext); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, |
| 78 | + ExtensionContext extensionContext) throws Throwable { |
| 79 | + if (isModifiedClassPathClassLoader(extensionContext)) { |
| 80 | + invocation.proceed(); |
| 81 | + return; |
| 82 | + } |
| 83 | + ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); |
| 84 | + URLClassLoader classLoader = ModifiedClassPathClassLoaderFactory |
| 85 | + .createTestClassLoader(extensionContext.getRequiredTestClass()); |
| 86 | + Thread.currentThread().setContextClassLoader(classLoader); |
| 87 | + try { |
| 88 | + fakeInvocation(invocation); |
| 89 | + TestExecutionSummary summary = launchTests(invocationContext, extensionContext, classLoader); |
| 90 | + if (!CollectionUtils.isEmpty(summary.getFailures())) { |
| 91 | + throw summary.getFailures().get(0).getException(); |
| 92 | + } |
| 93 | + } |
| 94 | + catch (Exception ex) { |
| 95 | + throw ex; |
| 96 | + } |
| 97 | + finally { |
| 98 | + Thread.currentThread().setContextClassLoader(originalClassLoader); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private TestExecutionSummary launchTests(ReflectiveInvocationContext<Method> invocationContext, |
| 103 | + ExtensionContext extensionContext, URLClassLoader classLoader) throws ClassNotFoundException { |
| 104 | + Class<?> testClass = classLoader.loadClass(extensionContext.getRequiredTestClass().getName()); |
| 105 | + Method method = ReflectionUtils.findMethod(testClass, invocationContext.getExecutable().getName()); |
| 106 | + LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() |
| 107 | + .selectors(DiscoverySelectors.selectMethod(testClass, method)).build(); |
| 108 | + Launcher launcher = LauncherFactory.create(); |
| 109 | + TestPlan testPlan = launcher.discover(request); |
| 110 | + SummaryGeneratingListener listener = new SummaryGeneratingListener(); |
| 111 | + launcher.registerTestExecutionListeners(listener); |
| 112 | + launcher.execute(testPlan); |
| 113 | + return listener.getSummary(); |
| 114 | + } |
| 115 | + |
| 116 | + private boolean isModifiedClassPathClassLoader(ExtensionContext extensionContext) { |
| 117 | + return extensionContext.getRequiredTestClass().getClassLoader().getClass().getName() |
| 118 | + .equals(ModifiedClassPathClassLoader.class.getName()); |
| 119 | + } |
| 120 | + |
| 121 | + private void interceptInvocation(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable { |
| 122 | + if (isModifiedClassPathClassLoader(extensionContext)) { |
| 123 | + invocation.proceed(); |
| 124 | + } |
| 125 | + else { |
| 126 | + fakeInvocation(invocation); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void fakeInvocation(Invocation invocation) { |
| 131 | + try { |
| 132 | + Field field = ReflectionUtils.findField(invocation.getClass(), "invoked"); |
| 133 | + ReflectionUtils.makeAccessible(field); |
| 134 | + ReflectionUtils.setField(field, invocation, new AtomicBoolean(true)); |
| 135 | + } |
| 136 | + catch (Throwable ignore) { |
| 137 | + |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments