|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | +package org.springframework.data.jpa.util; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 21 | +import org.junit.jupiter.api.extension.InvocationInterceptor; |
| 22 | +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; |
| 23 | +import org.junit.platform.engine.discovery.DiscoverySelectors; |
| 24 | +import org.junit.platform.launcher.Launcher; |
| 25 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 26 | +import org.junit.platform.launcher.TestPlan; |
| 27 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 28 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 29 | +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 30 | +import org.junit.platform.launcher.listeners.TestExecutionSummary; |
| 31 | +import org.springframework.util.CollectionUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Simplified version of <a href= |
| 35 | + * "https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java">ModifiedClassPathExtension</a>. |
| 36 | + * |
| 37 | + * @author Christoph Strobl |
| 38 | + */ |
| 39 | +class ClassPathExclusionsExtension implements InvocationInterceptor { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void interceptBeforeAllMethod(Invocation<Void> invocation, |
| 43 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 44 | + intercept(invocation, extensionContext); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void interceptBeforeEachMethod(Invocation<Void> invocation, |
| 49 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 50 | + intercept(invocation, extensionContext); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void interceptAfterEachMethod(Invocation<Void> invocation, |
| 55 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 56 | + intercept(invocation, extensionContext); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void interceptAfterAllMethod(Invocation<Void> invocation, |
| 61 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 62 | + intercept(invocation, extensionContext); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, |
| 67 | + ExtensionContext extensionContext) throws Throwable { |
| 68 | + interceptMethod(invocation, invocationContext, extensionContext); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void interceptTestTemplateMethod(Invocation<Void> invocation, |
| 73 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 74 | + interceptMethod(invocation, invocationContext, extensionContext); |
| 75 | + } |
| 76 | + |
| 77 | + private void interceptMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, |
| 78 | + ExtensionContext extensionContext) throws Throwable { |
| 79 | + |
| 80 | + if (isModifiedClassPathClassLoader(extensionContext)) { |
| 81 | + invocation.proceed(); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + Class<?> testClass = extensionContext.getRequiredTestClass(); |
| 86 | + Method testMethod = invocationContext.getExecutable(); |
| 87 | + PackageExcludingClassLoader modifiedClassLoader = PackageExcludingClassLoader.get(testClass, testMethod); |
| 88 | + if (modifiedClassLoader == null) { |
| 89 | + invocation.proceed(); |
| 90 | + return; |
| 91 | + } |
| 92 | + invocation.skip(); |
| 93 | + ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); |
| 94 | + Thread.currentThread().setContextClassLoader(modifiedClassLoader); |
| 95 | + try { |
| 96 | + runTest(extensionContext.getUniqueId()); |
| 97 | + } finally { |
| 98 | + Thread.currentThread().setContextClassLoader(originalClassLoader); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void runTest(String testId) throws Throwable { |
| 103 | + |
| 104 | + LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() |
| 105 | + .selectors(DiscoverySelectors.selectUniqueId(testId)).build(); |
| 106 | + Launcher launcher = LauncherFactory.create(); |
| 107 | + TestPlan testPlan = launcher.discover(request); |
| 108 | + SummaryGeneratingListener listener = new SummaryGeneratingListener(); |
| 109 | + launcher.registerTestExecutionListeners(listener); |
| 110 | + launcher.execute(testPlan); |
| 111 | + TestExecutionSummary summary = listener.getSummary(); |
| 112 | + if (!CollectionUtils.isEmpty(summary.getFailures())) { |
| 113 | + throw summary.getFailures().get(0).getException(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void intercept(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable { |
| 118 | + if (isModifiedClassPathClassLoader(extensionContext)) { |
| 119 | + invocation.proceed(); |
| 120 | + return; |
| 121 | + } |
| 122 | + invocation.skip(); |
| 123 | + } |
| 124 | + |
| 125 | + private boolean isModifiedClassPathClassLoader(ExtensionContext extensionContext) { |
| 126 | + Class<?> testClass = extensionContext.getRequiredTestClass(); |
| 127 | + ClassLoader classLoader = testClass.getClassLoader(); |
| 128 | + return classLoader.getClass().getName().equals(PackageExcludingClassLoader.class.getName()); |
| 129 | + } |
| 130 | +} |
0 commit comments