From 08eeb982f770c841cca06414d560ead82bf620e2 Mon Sep 17 00:00:00 2001 From: emeroad Date: Tue, 19 Nov 2024 14:21:53 +0900 Subject: [PATCH] [#11705] Refactor PluginTestEngine --- .../test/plugin/AfterPinpointPluginTest.java | 27 --- .../test/plugin/BeforePinpointPluginTest.java | 27 --- .../plugin/DefaultPluginTestInstance.java | 49 +++-- .../test/plugin/PinpointPluginTestUtils.java | 25 --- .../test/plugin/PluginTestInstance.java | 7 +- .../plugin/PluginTestInstanceFactory.java | 7 +- .../plugin/agent/PluginTestAgentStarter.java | 4 +- .../agent/classloader/MockInstrumentor.java | 4 +- .../PluginForkedTestUnitTestDescriptor.java | 4 +- .../PluginTestClassTestDescriptor.java | 1 - .../PluginTestUnitTestDescriptor.java | 8 +- .../junit5/engine/PluginTestEngine.java | 182 +----------------- .../PluginForkedTestDescriptorBuilder.java | 54 ++++++ .../PluginJunitTestDescriptorBuilder.java | 42 ++++ .../discovery/PluginPostDiscoveryFilter.java | 9 +- .../PluginTestDescriptorBuilder.java | 58 ++++++ .../discovery/TestDescriptorBuilder.java | 10 + .../discovery/TestDescriptorRegistry.java | 19 ++ .../engine/discovery/TestDescriptorUtils.java | 83 ++++++++ .../predicates/IsTestClassWithJunitAgent.java | 30 --- .../IsTestClassWithPinpointAgent.java | 30 --- .../IsTestClassWithPluginForkedTest.java | 30 --- .../predicates/IsTestClassWithPluginTest.java | 30 --- .../test/plugin/util/CallExecutable.java | 6 + .../test/plugin/util/RunExecutable.java | 6 + .../plugin/util/ThreadContextExecutor.java | 15 +- 26 files changed, 344 insertions(+), 423 deletions(-) delete mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/AfterPinpointPluginTest.java delete mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/BeforePinpointPluginTest.java delete mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PinpointPluginTestUtils.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginForkedTestDescriptorBuilder.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginJunitTestDescriptorBuilder.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginTestDescriptorBuilder.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorBuilder.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorRegistry.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorUtils.java delete mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithJunitAgent.java delete mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPinpointAgent.java delete mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginForkedTest.java delete mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginTest.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/CallExecutable.java create mode 100644 agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/RunExecutable.java diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/AfterPinpointPluginTest.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/AfterPinpointPluginTest.java deleted file mode 100644 index 6205994c4352..000000000000 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/AfterPinpointPluginTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2014 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.test.plugin; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface AfterPinpointPluginTest { -} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/BeforePinpointPluginTest.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/BeforePinpointPluginTest.java deleted file mode 100644 index 312ec8a2f8c5..000000000000 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/BeforePinpointPluginTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2014 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.test.plugin; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface BeforePinpointPluginTest { -} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/DefaultPluginTestInstance.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/DefaultPluginTestInstance.java index fe68209da438..a6cd525d6425 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/DefaultPluginTestInstance.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/DefaultPluginTestInstance.java @@ -18,6 +18,9 @@ import com.navercorp.pinpoint.test.plugin.classloader.PluginTestClassLoader; import com.navercorp.pinpoint.test.plugin.shared.ThreadFactory; +import com.navercorp.pinpoint.test.plugin.util.CallExecutable; +import com.navercorp.pinpoint.test.plugin.util.RunExecutable; +import org.junit.platform.commons.JUnitException; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -29,12 +32,12 @@ public class DefaultPluginTestInstance implements PluginTestInstance { - private String id; + private final String id; private PluginTestClassLoader classLoader; - private Class testClass; - private boolean manageTraceObject; + private final Class testClass; + private final boolean manageTraceObject; private PluginTestInstanceCallback callback; - private ExecutorService executorService; + private final ExecutorService executorService; public DefaultPluginTestInstance(String id, PluginTestClassLoader classLoader, Class testClass, boolean manageTraceObject, PluginTestInstanceCallback callback) { this.id = id; @@ -63,10 +66,10 @@ public Class getTestClass() { return this.testClass; } - public T execute(final Callable callable, boolean verify) { + public T execute(final CallExecutable callable, boolean verify) { Callable task = new Callable() { @Override - public T call() throws Exception { + public T call() { try { callback.before(verify, manageTraceObject); return callable.call(); @@ -77,17 +80,38 @@ public T call() throws Exception { }; Future future = this.executorService.submit(task); + return await(future); + } + + public void execute(final RunExecutable runnable, boolean verify) { + Runnable task = new Runnable() { + @Override + public void run() { + try { + callback.before(verify, manageTraceObject); + runnable.run(); + } finally { + callback.after(verify, manageTraceObject); + } + } + }; + + Future future = this.executorService.submit(task); + await(future); + } + + private T await(Future future) { try { - return future.get(30l, TimeUnit.SECONDS); + return future.get(30L, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new RuntimeException(e); + throw new JUnitException(this.id + " failed", e); } catch (ExecutionException e) { - throw new RuntimeException(e); + throw new JUnitException(this.id + " failed", e); } catch (TimeoutException e) { // testcase interrupt future.cancel(true); - throw new RuntimeException(e); + throw new JUnitException(this.id + " failed", e); } } @@ -102,10 +126,11 @@ public void clear() { this.classLoader = null; } if (this.executorService != null) { - this.executorService.shutdownNow(); + this.executorService.shutdown(); try { - if (!this.executorService.awaitTermination(10, TimeUnit.SECONDS)) { + if (!this.executorService.awaitTermination(10L, TimeUnit.SECONDS)) { System.err.println("ExecutorService did not terminate in the specified time"); + this.executorService.shutdownNow(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PinpointPluginTestUtils.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PinpointPluginTestUtils.java deleted file mode 100644 index 7b38e4c09ef3..000000000000 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PinpointPluginTestUtils.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.navercorp.pinpoint.test.plugin; - -import java.lang.reflect.Method; - -/** - * @author Taejin Koo - */ -public final class PinpointPluginTestUtils { - - public static String getTestDescribe(Method method) { - if (method == null) { - return "Method null"; - } - - StringBuilder describe = new StringBuilder("Method "); - describe.append(method.getName()); - - describe.append('('); - describe.append(method.getDeclaringClass().getName()); - describe.append(')'); - - return describe.toString(); - } - -} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstance.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstance.java index 2f022f6b5307..9aa573289d14 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstance.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstance.java @@ -16,7 +16,8 @@ package com.navercorp.pinpoint.test.plugin; -import java.util.concurrent.Callable; +import com.navercorp.pinpoint.test.plugin.util.CallExecutable; +import com.navercorp.pinpoint.test.plugin.util.RunExecutable; public interface PluginTestInstance { String getTestId(); @@ -25,7 +26,9 @@ public interface PluginTestInstance { Class getTestClass(); - T execute(final Callable callable, boolean verify); + T execute(final CallExecutable callable, boolean verify); + + void execute(final RunExecutable runnable, boolean verify); void clear(); } diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstanceFactory.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstanceFactory.java index 0de33a0197fe..3de01c428f7d 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstanceFactory.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/PluginTestInstanceFactory.java @@ -20,6 +20,7 @@ import com.navercorp.pinpoint.test.plugin.classloader.PluginAgentTestClassLoader; import com.navercorp.pinpoint.test.plugin.classloader.PluginTestJunitTestClassLoader; import com.navercorp.pinpoint.test.plugin.util.URLUtils; +import org.junit.platform.commons.JUnitException; import java.io.File; import java.lang.reflect.Constructor; @@ -70,11 +71,11 @@ List getClassPath(List libs, ClassLoding classLoading) { PluginTestInstanceCallback startAgent(String configFile, ClassLoader classLoader) { try { Class testClass = classLoader.loadClass(PluginTestAgentStarter.class.getName()); - Constructor constructor = testClass.getConstructor(String.class, ClassLoader.class); + Constructor constructor = testClass.getConstructor(String.class); Method method = testClass.getDeclaredMethod("getCallback"); - return (PluginTestInstanceCallback) method.invoke(constructor.newInstance(configFile, classLoader)); + return (PluginTestInstanceCallback) method.invoke(constructor.newInstance(configFile)); } catch (Exception e) { - throw new RuntimeException("agent configFile=" + configFile + ", classLoader=" + classLoader, e); + throw new JUnitException("agent configFile=" + configFile + ", classLoader=" + classLoader, e); } } } diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/PluginTestAgentStarter.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/PluginTestAgentStarter.java index e0f081b82876..c5cd862514b3 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/PluginTestAgentStarter.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/PluginTestAgentStarter.java @@ -33,10 +33,10 @@ public class PluginTestAgentStarter { private DefaultApplicationContext applicationContext; - public PluginTestAgentStarter(String configFile, ClassLoader classLoader) { + public PluginTestAgentStarter(String configFile) { final com.navercorp.pinpoint.profiler.test.MockApplicationContextFactory factory = new MockApplicationContextFactory(); this.applicationContext = factory.build(configFile); - this.mockInstrumentor = new MockInstrumentor(classLoader, applicationContext.getClassFileTransformer()); + this.mockInstrumentor = new MockInstrumentor(applicationContext.getClassFileTransformer()); this.interceptorRegistryBinder = applicationContext.getInterceptorRegistryBinder(); this.pluginTestVerifier = new PluginVerifierExternalAdaptor(applicationContext); } diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/classloader/MockInstrumentor.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/classloader/MockInstrumentor.java index f8e5c817f267..4646ac980bab 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/classloader/MockInstrumentor.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/agent/classloader/MockInstrumentor.java @@ -29,11 +29,9 @@ */ public class MockInstrumentor { private final Logger logger = LogManager.getLogger(this.getClass()); - private final ClassLoader loader; private final ClassFileTransformer dispatcher; - public MockInstrumentor(ClassLoader loader, ClassFileTransformer defaultTransformer) { - this.loader = loader; + public MockInstrumentor(ClassFileTransformer defaultTransformer) { this.dispatcher = Objects.requireNonNull(defaultTransformer, "defaultTransformer"); } diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginForkedTestUnitTestDescriptor.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginForkedTestUnitTestDescriptor.java index efb74a056622..43e83e0264cf 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginForkedTestUnitTestDescriptor.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginForkedTestUnitTestDescriptor.java @@ -71,14 +71,14 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte MutableExtensionRegistry extensionRegistry = context.getExtensionRegistry(); JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(this.getUniqueId(), configuration); - ExtensionContext classExtensionContext = ExtensionContextFactory.jupiterEngineContext( + ExtensionContext jupiterExtensionContext = ExtensionContextFactory.jupiterEngineContext( context.getExecutionListener(), engineDescriptor, context.getConfiguration(), ec -> new DefaultExecutableInvoker(ec, extensionRegistry)); // @formatter:off return context.extend() .withThrowableCollector(throwableCollector) - .withExtensionContext(classExtensionContext) + .withExtensionContext(jupiterExtensionContext) .build(); // @formatter:on } diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestClassTestDescriptor.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestClassTestDescriptor.java index 1f1203400bd6..e460b7748ed6 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestClassTestDescriptor.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestClassTestDescriptor.java @@ -45,7 +45,6 @@ public JupiterEngineExecutionContext before(JupiterEngineExecutionContext contex public void after(JupiterEngineExecutionContext context) { this.pluginTestInstance.execute(() -> { super.after(context); - return null; }, false); } diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestUnitTestDescriptor.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestUnitTestDescriptor.java index db08ebdeea35..eaa3e13cfa62 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestUnitTestDescriptor.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/descriptor/PluginTestUnitTestDescriptor.java @@ -62,9 +62,7 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte public JupiterEngineExecutionContext before(JupiterEngineExecutionContext context) { ThrowableCollector throwableCollector = context.getThrowableCollector(); if (sharedInstance != null) { - throwableCollector.execute(() -> { - sharedInstance.before(); - }); + throwableCollector.execute(sharedInstance::before); } if (throwableCollector.isNotEmpty()) { @@ -82,9 +80,7 @@ public void after(JupiterEngineExecutionContext context) { Throwable previousThrowable = throwableCollector.getThrowable(); if (sharedInstance != null) { - throwableCollector.execute(() -> { - sharedInstance.after(); - }); + throwableCollector.execute(sharedInstance::after); } // If the previous Throwable was not null when this method was called, diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/PluginTestEngine.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/PluginTestEngine.java index 70d127a54e6e..98010b067b36 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/PluginTestEngine.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/PluginTestEngine.java @@ -16,39 +16,17 @@ package com.navercorp.pinpoint.test.plugin.junit5.engine; -import com.navercorp.pinpoint.profiler.test.junit5.TestClassWrapper; -import com.navercorp.pinpoint.profiler.test.junit5.TestContext; -import com.navercorp.pinpoint.test.plugin.DefaultPluginForkedTestSuite; -import com.navercorp.pinpoint.test.plugin.DefaultPluginTestSuite; -import com.navercorp.pinpoint.test.plugin.PluginForkedTestInstance; -import com.navercorp.pinpoint.test.plugin.PluginTestInstance; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestClassTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestDependencyTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestMethodTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestUnitTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginJunitTestClassTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginJunitTestMethodTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestClassTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestDependencyTestDescriptor; import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestMethodTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestUnitTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates.IsTestClassWithJunitAgent; -import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates.IsTestClassWithPinpointAgent; -import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates.IsTestClassWithPluginForkedTest; -import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates.IsTestClassWithPluginTest; -import com.navercorp.pinpoint.test.plugin.shared.PluginSharedInstance; +import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.TestDescriptorBuilder; +import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.TestDescriptorRegistry; import org.junit.jupiter.engine.config.CachingJupiterConfiguration; import org.junit.jupiter.engine.config.DefaultJupiterConfiguration; import org.junit.jupiter.engine.config.JupiterConfiguration; import org.junit.jupiter.engine.descriptor.ClassTestDescriptor; import org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor; -import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor; import org.junit.jupiter.engine.discovery.DiscoverySelectorResolver; import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; import org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory; -import org.junit.platform.commons.util.ClassUtils; -import org.junit.platform.commons.util.ReflectionUtils; import org.junit.platform.engine.EngineDiscoveryRequest; import org.junit.platform.engine.ExecutionRequest; import org.junit.platform.engine.TestDescriptor; @@ -56,16 +34,12 @@ import org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine; import org.junit.platform.engine.support.hierarchical.ThrowableCollector; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Optional; public class PluginTestEngine extends HierarchicalTestEngine { - private static final IsTestClassWithPinpointAgent isTestClassWithPinpointAgent = new IsTestClassWithPinpointAgent(); - private static final IsTestClassWithPluginTest isTestClassWithPluginTest = new IsTestClassWithPluginTest(); - private static final IsTestClassWithPluginForkedTest isTestClassWithPluginForkedTest = new IsTestClassWithPluginForkedTest(); - private static final IsTestClassWithJunitAgent isTestClassWithJunitAgent = new IsTestClassWithJunitAgent(); + private final TestDescriptorRegistry registry = new TestDescriptorRegistry(); @Override public String getId() { @@ -99,15 +73,7 @@ public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId for (TestDescriptor testDescriptor : engineDescriptor.getChildren()) { if (testDescriptor instanceof ClassTestDescriptor) { final Class testClass = ((ClassTestDescriptor) testDescriptor).getTestClass(); - TestDescriptor pluginTestDescriptor = null; - if (isTestClassWithPluginTest.test(testClass)) { - pluginTestDescriptor = addPluginTestDescriptor(testDescriptor, configuration); - } else if (isTestClassWithPluginForkedTest.test(testClass)) { - pluginTestDescriptor = addPluginForkedTestDescriptor(testDescriptor, configuration); - } else if (isTestClassWithJunitAgent.test(testClass)) { - pluginTestDescriptor = addPluginJunitTestDescriptor(testDescriptor, configuration); - } - + TestDescriptor pluginTestDescriptor = getDescriptor(testDescriptor, testClass, configuration); if (pluginTestDescriptor != null) { pluginTestDescriptorList.add(pluginTestDescriptor); removedTestDescriptorList.add(testDescriptor); @@ -130,146 +96,14 @@ public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId return engineDescriptor; } - TestDescriptor addPluginTestDescriptor(TestDescriptor testDescriptor, JupiterConfiguration configuration) { - final DefaultPluginTestSuite testSuite = new DefaultPluginTestSuite(((ClassTestDescriptor) testDescriptor).getTestClass()); - final PluginSharedInstance sharedInstance = testSuite.getPluginSharedInstance(); - final List testInstanceList = testSuite.getPluginTestInstanceList(); - - // Unit - final PluginTestUnitTestDescriptor pluginTestUnitTestDescriptor = new PluginTestUnitTestDescriptor(testDescriptor.getUniqueId(), ((ClassTestDescriptor) testDescriptor).getTestClass(), configuration, sharedInstance); - for (PluginTestInstance pluginTestInstance : testInstanceList) { - final String testId = pluginTestInstance.getTestId(); - // Dependency - final PluginTestDependencyTestDescriptor pluginTestDependencyTestDescriptor = toPluginTestDependencyTestDescriptor(configuration, pluginTestUnitTestDescriptor, testId); - if (pluginTestDependencyTestDescriptor != null) { - pluginTestUnitTestDescriptor.addChild(pluginTestDependencyTestDescriptor); - // Class - final PluginTestClassTestDescriptor pluginTestClassTestDescriptor = toPluginTestClassTestDescriptor(configuration, pluginTestDependencyTestDescriptor, pluginTestInstance); - if (pluginTestClassTestDescriptor != null) { - pluginTestDependencyTestDescriptor.addChild(pluginTestClassTestDescriptor); - for (TestDescriptor descriptor : testDescriptor.getChildren()) { - if (descriptor instanceof TestMethodTestDescriptor) { - final Method method = ((TestMethodTestDescriptor) descriptor).getTestMethod(); - final PluginTestMethodTestDescriptor pluginTestMethodTestDescriptor = toPluginTestMethodTestDescriptor(configuration, pluginTestClassTestDescriptor, pluginTestInstance, method); - if (pluginTestMethodTestDescriptor != null) { - pluginTestClassTestDescriptor.addChild(pluginTestMethodTestDescriptor); - } - } - } - } - } - } - return pluginTestUnitTestDescriptor; - } - - TestDescriptor addPluginForkedTestDescriptor(TestDescriptor testDescriptor, JupiterConfiguration configuration) { - final DefaultPluginForkedTestSuite testSuite = new DefaultPluginForkedTestSuite(((ClassTestDescriptor) testDescriptor).getTestClass()); - final List testInstanceList = testSuite.getPluginTestInstanceList(); - - // Unit - final PluginForkedTestUnitTestDescriptor pluginTestUnitTestDescriptor = new PluginForkedTestUnitTestDescriptor(testDescriptor.getUniqueId(), ((ClassTestDescriptor) testDescriptor).getTestClass(), configuration, testInstanceList); - for (PluginForkedTestInstance pluginTestInstance : testInstanceList) { - final String testId = pluginTestInstance.getTestId(); - // Dependency - final PluginForkedTestDependencyTestDescriptor pluginTestDependencyTestDescriptor = toPluginForkedTestDependencyTestDescriptor(configuration, pluginTestUnitTestDescriptor, testId); - if (pluginTestDependencyTestDescriptor != null) { - pluginTestUnitTestDescriptor.addChild(pluginTestDependencyTestDescriptor); - // Class - final PluginForkedTestClassTestDescriptor pluginTestClassTestDescriptor = toPluginForkedTestClassTestDescriptor(configuration, pluginTestDependencyTestDescriptor); - if (pluginTestClassTestDescriptor != null) { - pluginTestDependencyTestDescriptor.addChild(pluginTestClassTestDescriptor); - for (TestDescriptor descriptor : testDescriptor.getChildren()) { - if (descriptor instanceof TestMethodTestDescriptor) { - final Method method = ((TestMethodTestDescriptor) descriptor).getTestMethod(); - final PluginForkedTestMethodTestDescriptor pluginTestMethodTestDescriptor = toPluginForkedTestMethodTestDescriptor(configuration, pluginTestClassTestDescriptor, pluginTestInstance, method); - pluginTestClassTestDescriptor.addChild(pluginTestMethodTestDescriptor); - } - } - } - } - } - return pluginTestUnitTestDescriptor; - } - - TestDescriptor addPluginJunitTestDescriptor(TestDescriptor testDescriptor, JupiterConfiguration configuration) { - final TestContext testContext = new TestContext(new TestClassWrapper(((ClassTestDescriptor) testDescriptor).getTestClass())); - final Class testClass = testContext.createTestClass(); - - // Class - final PluginJunitTestClassTestDescriptor pluginTestClassTestDescriptor = new PluginJunitTestClassTestDescriptor(testDescriptor.getUniqueId(), testClass, configuration, testContext); - //switchTestDescriptor(testDescriptor, pluginTestClassTestDescriptor); - for (TestDescriptor descriptor : testDescriptor.getChildren()) { - if (descriptor instanceof TestMethodTestDescriptor) { - final Method method = ((TestMethodTestDescriptor) descriptor).getTestMethod(); - final Method testMethod = ReflectionUtils.findMethod(testClass, method.getName(), method.getParameterTypes()).orElseThrow(() -> new IllegalStateException("not found method")); - - final PluginJunitTestMethodTestDescriptor pluginTestMethodTestDescriptor = new PluginJunitTestMethodTestDescriptor(descriptor.getUniqueId(), testClass, testMethod, configuration, testContext); - pluginTestClassTestDescriptor.addChild(pluginTestMethodTestDescriptor); - } - } - return pluginTestClassTestDescriptor; - } - - private static PluginTestDependencyTestDescriptor toPluginTestDependencyTestDescriptor(JupiterConfiguration configuration, PluginTestUnitTestDescriptor parentTestDescriptor, String testId) { - try { - return new PluginTestDependencyTestDescriptor(parentTestDescriptor.getUniqueId().append("dependency", testId), parentTestDescriptor.getTestClass(), configuration, testId); - } catch (Throwable t) { - System.out.println(t.getMessage()); + private TestDescriptor getDescriptor(TestDescriptor testDescriptor, Class testClass, JupiterConfiguration configuration) { + TestDescriptorBuilder builder = registry.getDescriptor(testClass); + if (builder == null) { return null; } + return builder.build(testDescriptor, configuration); } - private static PluginForkedTestDependencyTestDescriptor toPluginForkedTestDependencyTestDescriptor(JupiterConfiguration configuration, PluginForkedTestUnitTestDescriptor parentTestDescriptor, String testId) { - try { - return new PluginForkedTestDependencyTestDescriptor(parentTestDescriptor.getUniqueId().append("dependency", testId), parentTestDescriptor.getTestClass(), configuration, testId); - } catch (Throwable t) { - System.out.println(t.getMessage()); - return null; - } - } - - private static PluginTestClassTestDescriptor toPluginTestClassTestDescriptor(JupiterConfiguration configuration, PluginTestDependencyTestDescriptor parentTestDescriptor, PluginTestInstance pluginTestInstance) { - try { - final Class testClass = pluginTestInstance.getTestClass(); - return new PluginTestClassTestDescriptor(parentTestDescriptor.getUniqueId().append(ClassTestDescriptor.SEGMENT_TYPE, testClass.getName()), testClass, configuration, pluginTestInstance); - } catch (Throwable t) { - System.out.println(t.getMessage()); - return null; - } - } - - private static PluginForkedTestClassTestDescriptor toPluginForkedTestClassTestDescriptor(JupiterConfiguration configuration, PluginForkedTestDependencyTestDescriptor parentTestDescriptor) { - try { - final Class testClass = parentTestDescriptor.getTestClass(); - return new PluginForkedTestClassTestDescriptor(parentTestDescriptor.getUniqueId().append(ClassTestDescriptor.SEGMENT_TYPE, testClass.getName()), testClass, configuration); - } catch (Throwable t) { - System.out.println(t.getMessage()); - return null; - } - } - - private static PluginTestMethodTestDescriptor toPluginTestMethodTestDescriptor(JupiterConfiguration configuration, PluginTestClassTestDescriptor parentTestDescriptor, PluginTestInstance pluginTestInstance, Method method) { - try { - final Class testClass = pluginTestInstance.getTestClass(); - final Method testMethod = ReflectionUtils.findMethod(testClass, method.getName(), method.getParameterTypes()).orElseThrow(() -> new IllegalStateException("not found method")); - return new PluginTestMethodTestDescriptor(parentTestDescriptor.getUniqueId().append(TestMethodTestDescriptor.SEGMENT_TYPE, testMethod.getName()), testClass, testMethod, configuration, pluginTestInstance); - } catch (Throwable t) { - System.out.println(t.getMessage()); - return null; - } - } - - private static PluginForkedTestMethodTestDescriptor toPluginForkedTestMethodTestDescriptor(JupiterConfiguration configuration, PluginForkedTestClassTestDescriptor parentTestDescriptor, PluginForkedTestInstance pluginTestInstance, Method method) { - try { - final Class testClass = parentTestDescriptor.getTestClass(); - final Method testMethod = ReflectionUtils.findMethod(testClass, method.getName(), method.getParameterTypes()).orElseThrow(() -> new IllegalStateException("not found method")); - String methodId = String.format("%s(%s)", method.getName(), ClassUtils.nullSafeToString(method.getParameterTypes())); - return new PluginForkedTestMethodTestDescriptor(parentTestDescriptor.getUniqueId().append(TestMethodTestDescriptor.SEGMENT_TYPE, methodId), testClass, testMethod, configuration, pluginTestInstance); - } catch (Throwable t) { - System.out.println(t.getMessage()); - return null; - } - } @Override public JupiterEngineExecutionContext createExecutionContext(ExecutionRequest request) { diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginForkedTestDescriptorBuilder.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginForkedTestDescriptorBuilder.java new file mode 100644 index 000000000000..009b01c5a5e9 --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginForkedTestDescriptorBuilder.java @@ -0,0 +1,54 @@ +package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery; + +import com.navercorp.pinpoint.test.plugin.DefaultPluginForkedTestSuite; +import com.navercorp.pinpoint.test.plugin.PluginForkedTest; +import com.navercorp.pinpoint.test.plugin.PluginForkedTestInstance; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestClassTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestDependencyTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestMethodTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestUnitTestDescriptor; +import org.junit.jupiter.engine.config.JupiterConfiguration; +import org.junit.jupiter.engine.descriptor.ClassTestDescriptor; +import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor; +import org.junit.platform.commons.util.AnnotationUtils; +import org.junit.platform.engine.TestDescriptor; + +import java.lang.reflect.Method; +import java.util.List; + +public class PluginForkedTestDescriptorBuilder implements TestDescriptorBuilder { + + @Override + public boolean test(Class candidate) { + return AnnotationUtils.isAnnotated(candidate, PluginForkedTest.class); + } + + public TestDescriptor build(TestDescriptor testDescriptor, JupiterConfiguration configuration) { + final DefaultPluginForkedTestSuite testSuite = new DefaultPluginForkedTestSuite(((ClassTestDescriptor) testDescriptor).getTestClass()); + final List testInstanceList = testSuite.getPluginTestInstanceList(); + + // Unit + final PluginForkedTestUnitTestDescriptor pluginTestUnitTestDescriptor = new PluginForkedTestUnitTestDescriptor(testDescriptor.getUniqueId(), ((ClassTestDescriptor) testDescriptor).getTestClass(), configuration, testInstanceList); + for (PluginForkedTestInstance pluginTestInstance : testInstanceList) { + final String testId = pluginTestInstance.getTestId(); + // Dependency + final PluginForkedTestDependencyTestDescriptor pluginTestDependencyTestDescriptor = TestDescriptorUtils.toPluginForkedTestDependencyTestDescriptor(configuration, pluginTestUnitTestDescriptor, testId); + if (pluginTestDependencyTestDescriptor != null) { + pluginTestUnitTestDescriptor.addChild(pluginTestDependencyTestDescriptor); + // Class + final PluginForkedTestClassTestDescriptor pluginTestClassTestDescriptor = TestDescriptorUtils.toPluginForkedTestClassTestDescriptor(configuration, pluginTestDependencyTestDescriptor); + if (pluginTestClassTestDescriptor != null) { + pluginTestDependencyTestDescriptor.addChild(pluginTestClassTestDescriptor); + for (TestDescriptor descriptor : testDescriptor.getChildren()) { + if (descriptor instanceof TestMethodTestDescriptor) { + final Method method = ((TestMethodTestDescriptor) descriptor).getTestMethod(); + final PluginForkedTestMethodTestDescriptor pluginTestMethodTestDescriptor = TestDescriptorUtils.toPluginForkedTestMethodTestDescriptor(configuration, pluginTestClassTestDescriptor, pluginTestInstance, method); + pluginTestClassTestDescriptor.addChild(pluginTestMethodTestDescriptor); + } + } + } + } + } + return pluginTestUnitTestDescriptor; + } +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginJunitTestDescriptorBuilder.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginJunitTestDescriptorBuilder.java new file mode 100644 index 000000000000..37864c8a98c3 --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginJunitTestDescriptorBuilder.java @@ -0,0 +1,42 @@ +package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery; + +import com.navercorp.pinpoint.profiler.test.junit5.JunitAgentConfigPath; +import com.navercorp.pinpoint.profiler.test.junit5.TestClassWrapper; +import com.navercorp.pinpoint.profiler.test.junit5.TestContext; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginJunitTestClassTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginJunitTestMethodTestDescriptor; +import org.junit.jupiter.engine.config.JupiterConfiguration; +import org.junit.jupiter.engine.descriptor.ClassTestDescriptor; +import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor; +import org.junit.platform.commons.util.AnnotationUtils; +import org.junit.platform.commons.util.ReflectionUtils; +import org.junit.platform.engine.TestDescriptor; + +import java.lang.reflect.Method; + +public class PluginJunitTestDescriptorBuilder implements TestDescriptorBuilder { + + @Override + public boolean test(Class candidate) { + return AnnotationUtils.isAnnotated(candidate, JunitAgentConfigPath.class); + } + + public TestDescriptor build(TestDescriptor testDescriptor, JupiterConfiguration configuration) { + final TestContext testContext = new TestContext(new TestClassWrapper(((ClassTestDescriptor) testDescriptor).getTestClass())); + final Class testClass = testContext.createTestClass(); + + // Class + final PluginJunitTestClassTestDescriptor pluginTestClassTestDescriptor = new PluginJunitTestClassTestDescriptor(testDescriptor.getUniqueId(), testClass, configuration, testContext); + //switchTestDescriptor(testDescriptor, pluginTestClassTestDescriptor); + for (TestDescriptor descriptor : testDescriptor.getChildren()) { + if (descriptor instanceof TestMethodTestDescriptor) { + final Method method = ((TestMethodTestDescriptor) descriptor).getTestMethod(); + final Method testMethod = ReflectionUtils.findMethod(testClass, method.getName(), method.getParameterTypes()).orElseThrow(() -> new IllegalStateException("not found method")); + + final PluginJunitTestMethodTestDescriptor pluginTestMethodTestDescriptor = new PluginJunitTestMethodTestDescriptor(descriptor.getUniqueId(), testClass, testMethod, configuration, testContext); + pluginTestClassTestDescriptor.addChild(pluginTestMethodTestDescriptor); + } + } + return pluginTestClassTestDescriptor; + } +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginPostDiscoveryFilter.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginPostDiscoveryFilter.java index 7acd17bb23b8..109966988339 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginPostDiscoveryFilter.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginPostDiscoveryFilter.java @@ -23,9 +23,6 @@ import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestClassTestDescriptor; import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestDescriptor; import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestMethodTestDescriptor; -import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates.IsTestClassWithJunitAgent; -import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates.IsTestClassWithPluginForkedTest; -import com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates.IsTestClassWithPluginTest; import org.junit.platform.engine.FilterResult; import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestSource; @@ -34,9 +31,7 @@ import org.junit.platform.launcher.PostDiscoveryFilter; public class PluginPostDiscoveryFilter implements PostDiscoveryFilter { - private static final IsTestClassWithPluginTest isTestClassWithPluginTest = new IsTestClassWithPluginTest(); - private static final IsTestClassWithPluginForkedTest isTestClassWithPluginForkedTest = new IsTestClassWithPluginForkedTest(); - private static final IsTestClassWithJunitAgent isTestClassWithJunitAgent = new IsTestClassWithJunitAgent(); + private final TestDescriptorRegistry registry = new TestDescriptorRegistry(); @Override public FilterResult apply(TestDescriptor testDescriptor) { @@ -97,6 +92,6 @@ boolean hasPinpointAgent(TestDescriptor testDescriptor) { } boolean hasPinpointAgent(Class javaClass) { - return isTestClassWithPluginTest.test(javaClass) || isTestClassWithPluginForkedTest.test(javaClass) || isTestClassWithJunitAgent.test(javaClass); + return registry.getDescriptor(javaClass) != null; } } \ No newline at end of file diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginTestDescriptorBuilder.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginTestDescriptorBuilder.java new file mode 100644 index 000000000000..d5bc09de99ec --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/PluginTestDescriptorBuilder.java @@ -0,0 +1,58 @@ +package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery; + +import com.navercorp.pinpoint.test.plugin.DefaultPluginTestSuite; +import com.navercorp.pinpoint.test.plugin.PluginTest; +import com.navercorp.pinpoint.test.plugin.PluginTestInstance; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestClassTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestDependencyTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestMethodTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestUnitTestDescriptor; +import com.navercorp.pinpoint.test.plugin.shared.PluginSharedInstance; +import org.junit.jupiter.engine.config.JupiterConfiguration; +import org.junit.jupiter.engine.descriptor.ClassTestDescriptor; +import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor; +import org.junit.platform.commons.util.AnnotationUtils; +import org.junit.platform.engine.TestDescriptor; + +import java.lang.reflect.Method; +import java.util.List; + +public class PluginTestDescriptorBuilder implements TestDescriptorBuilder { + + @Override + public boolean test(Class candidate) { + return AnnotationUtils.isAnnotated(candidate, PluginTest.class); + } + + public TestDescriptor build(TestDescriptor testDescriptor, JupiterConfiguration configuration) { + final DefaultPluginTestSuite testSuite = new DefaultPluginTestSuite(((ClassTestDescriptor) testDescriptor).getTestClass()); + final PluginSharedInstance sharedInstance = testSuite.getPluginSharedInstance(); + final List testInstanceList = testSuite.getPluginTestInstanceList(); + + // Unit + final PluginTestUnitTestDescriptor pluginTestUnitTestDescriptor = new PluginTestUnitTestDescriptor(testDescriptor.getUniqueId(), ((ClassTestDescriptor) testDescriptor).getTestClass(), configuration, sharedInstance); + for (PluginTestInstance pluginTestInstance : testInstanceList) { + final String testId = pluginTestInstance.getTestId(); + // Dependency + final PluginTestDependencyTestDescriptor pluginTestDependencyTestDescriptor = TestDescriptorUtils.toPluginTestDependencyTestDescriptor(configuration, pluginTestUnitTestDescriptor, testId); + if (pluginTestDependencyTestDescriptor != null) { + pluginTestUnitTestDescriptor.addChild(pluginTestDependencyTestDescriptor); + // Class + final PluginTestClassTestDescriptor pluginTestClassTestDescriptor = TestDescriptorUtils.toPluginTestClassTestDescriptor(configuration, pluginTestDependencyTestDescriptor, pluginTestInstance); + if (pluginTestClassTestDescriptor != null) { + pluginTestDependencyTestDescriptor.addChild(pluginTestClassTestDescriptor); + for (TestDescriptor descriptor : testDescriptor.getChildren()) { + if (descriptor instanceof TestMethodTestDescriptor) { + final Method method = ((TestMethodTestDescriptor) descriptor).getTestMethod(); + final PluginTestMethodTestDescriptor pluginTestMethodTestDescriptor = TestDescriptorUtils.toPluginTestMethodTestDescriptor(configuration, pluginTestClassTestDescriptor, pluginTestInstance, method); + if (pluginTestMethodTestDescriptor != null) { + pluginTestClassTestDescriptor.addChild(pluginTestMethodTestDescriptor); + } + } + } + } + } + } + return pluginTestUnitTestDescriptor; + } +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorBuilder.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorBuilder.java new file mode 100644 index 000000000000..d816d93f48a0 --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorBuilder.java @@ -0,0 +1,10 @@ +package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery; + +import org.junit.jupiter.engine.config.JupiterConfiguration; +import org.junit.platform.engine.TestDescriptor; + +import java.util.function.Predicate; + +public interface TestDescriptorBuilder extends Predicate> { + TestDescriptor build(TestDescriptor testDescriptor, JupiterConfiguration configuration); +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorRegistry.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorRegistry.java new file mode 100644 index 000000000000..6cbdb43ff988 --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorRegistry.java @@ -0,0 +1,19 @@ +package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery; + +public class TestDescriptorRegistry { + + private static final TestDescriptorBuilder[] builders = new TestDescriptorBuilder[] { + new PluginTestDescriptorBuilder(), + new PluginForkedTestDescriptorBuilder(), + new PluginJunitTestDescriptorBuilder(), + }; + + public TestDescriptorBuilder getDescriptor(Class testClass) { + for (TestDescriptorBuilder builder : builders) { + if (builder.test(testClass)) { + return builder; + } + } + return null; + } +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorUtils.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorUtils.java new file mode 100644 index 000000000000..ea291195f58f --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/TestDescriptorUtils.java @@ -0,0 +1,83 @@ +package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery; + +import com.navercorp.pinpoint.test.plugin.PluginForkedTestInstance; +import com.navercorp.pinpoint.test.plugin.PluginTestInstance; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestClassTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestDependencyTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestMethodTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginForkedTestUnitTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestClassTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestDependencyTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestMethodTestDescriptor; +import com.navercorp.pinpoint.test.plugin.junit5.descriptor.PluginTestUnitTestDescriptor; +import org.junit.jupiter.engine.config.JupiterConfiguration; +import org.junit.jupiter.engine.descriptor.ClassTestDescriptor; +import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor; +import org.junit.platform.commons.util.ClassUtils; +import org.junit.platform.commons.util.ReflectionUtils; + +import java.lang.reflect.Method; + +public class TestDescriptorUtils { + + public static PluginTestDependencyTestDescriptor toPluginTestDependencyTestDescriptor(JupiterConfiguration configuration, PluginTestUnitTestDescriptor parentTestDescriptor, String testId) { + try { + return new PluginTestDependencyTestDescriptor(parentTestDescriptor.getUniqueId().append("dependency", testId), parentTestDescriptor.getTestClass(), configuration, testId); + } catch (Throwable t) { + System.out.println(t.getMessage()); + return null; + } + } + + public static PluginForkedTestDependencyTestDescriptor toPluginForkedTestDependencyTestDescriptor(JupiterConfiguration configuration, PluginForkedTestUnitTestDescriptor parentTestDescriptor, String testId) { + try { + return new PluginForkedTestDependencyTestDescriptor(parentTestDescriptor.getUniqueId().append("dependency", testId), parentTestDescriptor.getTestClass(), configuration, testId); + } catch (Throwable t) { + System.out.println(t.getMessage()); + return null; + } + } + + public static PluginTestClassTestDescriptor toPluginTestClassTestDescriptor(JupiterConfiguration configuration, PluginTestDependencyTestDescriptor parentTestDescriptor, PluginTestInstance pluginTestInstance) { + try { + final Class testClass = pluginTestInstance.getTestClass(); + return new PluginTestClassTestDescriptor(parentTestDescriptor.getUniqueId().append(ClassTestDescriptor.SEGMENT_TYPE, testClass.getName()), testClass, configuration, pluginTestInstance); + } catch (Throwable t) { + System.out.println(t.getMessage()); + return null; + } + } + + public static PluginForkedTestClassTestDescriptor toPluginForkedTestClassTestDescriptor(JupiterConfiguration configuration, PluginForkedTestDependencyTestDescriptor parentTestDescriptor) { + try { + final Class testClass = parentTestDescriptor.getTestClass(); + return new PluginForkedTestClassTestDescriptor(parentTestDescriptor.getUniqueId().append(ClassTestDescriptor.SEGMENT_TYPE, testClass.getName()), testClass, configuration); + } catch (Throwable t) { + System.out.println(t.getMessage()); + return null; + } + } + + public static PluginTestMethodTestDescriptor toPluginTestMethodTestDescriptor(JupiterConfiguration configuration, PluginTestClassTestDescriptor parentTestDescriptor, PluginTestInstance pluginTestInstance, Method method) { + try { + final Class testClass = pluginTestInstance.getTestClass(); + final Method testMethod = ReflectionUtils.findMethod(testClass, method.getName(), method.getParameterTypes()).orElseThrow(() -> new IllegalStateException("not found method")); + return new PluginTestMethodTestDescriptor(parentTestDescriptor.getUniqueId().append(TestMethodTestDescriptor.SEGMENT_TYPE, testMethod.getName()), testClass, testMethod, configuration, pluginTestInstance); + } catch (Throwable t) { + System.out.println(t.getMessage()); + return null; + } + } + + public static PluginForkedTestMethodTestDescriptor toPluginForkedTestMethodTestDescriptor(JupiterConfiguration configuration, PluginForkedTestClassTestDescriptor parentTestDescriptor, PluginForkedTestInstance pluginTestInstance, Method method) { + try { + final Class testClass = parentTestDescriptor.getTestClass(); + final Method testMethod = ReflectionUtils.findMethod(testClass, method.getName(), method.getParameterTypes()).orElseThrow(() -> new IllegalStateException("not found method")); + String methodId = String.format("%s(%s)", method.getName(), ClassUtils.nullSafeToString(method.getParameterTypes())); + return new PluginForkedTestMethodTestDescriptor(parentTestDescriptor.getUniqueId().append(TestMethodTestDescriptor.SEGMENT_TYPE, methodId), testClass, testMethod, configuration, pluginTestInstance); + } catch (Throwable t) { + System.out.println(t.getMessage()); + return null; + } + } +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithJunitAgent.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithJunitAgent.java deleted file mode 100644 index f67028d3b26d..000000000000 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithJunitAgent.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2023 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates; - - -import com.navercorp.pinpoint.profiler.test.junit5.JunitAgentConfigPath; -import org.junit.platform.commons.util.AnnotationUtils; - -import java.util.function.Predicate; - -public class IsTestClassWithJunitAgent implements Predicate> { - @Override - public boolean test(Class candidate) { - return AnnotationUtils.isAnnotated(candidate, JunitAgentConfigPath.class); - } -} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPinpointAgent.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPinpointAgent.java deleted file mode 100644 index 9925b6a1e676..000000000000 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPinpointAgent.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2023 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates; - - -import com.navercorp.pinpoint.test.plugin.PinpointAgent; -import org.junit.platform.commons.util.AnnotationUtils; - -import java.util.function.Predicate; - -public class IsTestClassWithPinpointAgent implements Predicate> { - @Override - public boolean test(Class candidate) { - return AnnotationUtils.isAnnotated(candidate, PinpointAgent.class); - } -} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginForkedTest.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginForkedTest.java deleted file mode 100644 index 7fcfd9aec81a..000000000000 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginForkedTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2023 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates; - - -import com.navercorp.pinpoint.test.plugin.PluginForkedTest; -import org.junit.platform.commons.util.AnnotationUtils; - -import java.util.function.Predicate; - -public class IsTestClassWithPluginForkedTest implements Predicate> { - @Override - public boolean test(Class candidate) { - return AnnotationUtils.isAnnotated(candidate, PluginForkedTest.class); - } -} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginTest.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginTest.java deleted file mode 100644 index 48f89aa2828d..000000000000 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/junit5/engine/discovery/predicates/IsTestClassWithPluginTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2023 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.test.plugin.junit5.engine.discovery.predicates; - - -import com.navercorp.pinpoint.test.plugin.PluginTest; -import org.junit.platform.commons.util.AnnotationUtils; - -import java.util.function.Predicate; - -public class IsTestClassWithPluginTest implements Predicate> { - @Override - public boolean test(Class candidate) { - return AnnotationUtils.isAnnotated(candidate, PluginTest.class); - } -} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/CallExecutable.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/CallExecutable.java new file mode 100644 index 000000000000..fd2bf2063b06 --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/CallExecutable.java @@ -0,0 +1,6 @@ +package com.navercorp.pinpoint.test.plugin.util; + +@FunctionalInterface +public interface CallExecutable { + V call(); +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/RunExecutable.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/RunExecutable.java new file mode 100644 index 000000000000..f3cab68ad63f --- /dev/null +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/RunExecutable.java @@ -0,0 +1,6 @@ +package com.navercorp.pinpoint.test.plugin.util; + +@FunctionalInterface +public interface RunExecutable { + void run(); +} diff --git a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/ThreadContextExecutor.java b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/ThreadContextExecutor.java index fc3feda03b90..748b079e8454 100644 --- a/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/ThreadContextExecutor.java +++ b/agent-module/plugins-test-module/plugins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/ThreadContextExecutor.java @@ -15,21 +15,21 @@ public ClassLoader getClassLoader() { return context; } - public void execute(Executable executable) { + public void execute(RunExecutable executable) { Objects.requireNonNull(executable, "executable"); final Thread thread = Thread.currentThread(); final ClassLoader before = thread.getContextClassLoader(); thread.setContextClassLoader(context); try { - executable.execute(); + executable.run(); } finally { thread.setContextClassLoader(before); } } - public V call(Callable callable) { + public V call(CallExecutable callable) { Objects.requireNonNull(callable, "callable"); final Thread thread = Thread.currentThread(); @@ -42,13 +42,4 @@ public V call(Callable callable) { } } - @FunctionalInterface - public interface Executable { - void execute(); - } - - @FunctionalInterface - public interface Callable { - V call(); - } }