diff --git a/embulk-junit5-engine/src/main/java/org/embulk/junit5/engine/EmbulkPluginTestEngine.java b/embulk-junit5-engine/src/main/java/org/embulk/junit5/engine/EmbulkPluginTestEngine.java index 50b0517..eba4e2c 100644 --- a/embulk-junit5-engine/src/main/java/org/embulk/junit5/engine/EmbulkPluginTestEngine.java +++ b/embulk-junit5-engine/src/main/java/org/embulk/junit5/engine/EmbulkPluginTestEngine.java @@ -19,6 +19,8 @@ import java.lang.reflect.Method; import java.util.Optional; import org.embulk.junit5.api.EmbulkPluginTest; +import org.junit.platform.commons.logging.Logger; +import org.junit.platform.commons.logging.LoggerFactory; import org.junit.platform.engine.EngineDiscoveryRequest; import org.junit.platform.engine.ExecutionRequest; import org.junit.platform.engine.TestDescriptor; @@ -29,8 +31,17 @@ import org.junit.platform.engine.support.hierarchical.OpenTest4JAwareThrowableCollector; import org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService; import org.junit.platform.engine.support.hierarchical.ThrowableCollector; +import org.junit.platform.launcher.LauncherDiscoveryRequest; public final class EmbulkPluginTestEngine extends HierarchicalTestEngine { + public EmbulkPluginTestEngine() { + super(); + final Class klass = this.getClass(); + this.klassLoader = klass.getClassLoader(); + logger.info(() -> "Initializing EmbulkPluginTestEngine@" + Integer.toHexString(this.hashCode())); + logger.info(() -> "EmbulkPluginTestEngine's ClassLoader: " + this.klassLoader.toString()); + } + /** * Returns {@code "embulk-junit5-engine"} as the engine ID. */ @@ -48,15 +59,14 @@ public final String getId() { */ @Override public TestDescriptor discover(final EngineDiscoveryRequest discoveryRequest, final UniqueId uniqueId) { - System.out.println("EngineDiscoveryRequest: " + discoveryRequest.toString()); - if (discoveryRequest instanceof org.junit.platform.launcher.LauncherDiscoveryRequest) { - final org.junit.platform.launcher.LauncherDiscoveryRequest launcherDiscoveryRequest = - (org.junit.platform.launcher.LauncherDiscoveryRequest) discoveryRequest; - System.out.println(" EngineFilters: " + launcherDiscoveryRequest.getEngineFilters()); - System.out.println(" PostDiscoveryFilters: " + launcherDiscoveryRequest.getPostDiscoveryFilters()); + logger.trace(() -> "EngineDiscoveryRequest: " + discoveryRequest.toString()); + if (discoveryRequest instanceof LauncherDiscoveryRequest) { + final LauncherDiscoveryRequest launcherDiscoveryRequest = (LauncherDiscoveryRequest) discoveryRequest; + logger.trace(() -> " EngineFilters: " + launcherDiscoveryRequest.getEngineFilters()); + logger.trace(() -> " PostDiscoveryFilters: " + launcherDiscoveryRequest.getPostDiscoveryFilters()); } - System.out.println(" ConfigurationParameters: " + discoveryRequest.getConfigurationParameters()); - System.out.println("UniqueId: " + uniqueId.toString()); + logger.trace(() -> " ConfigurationParameters: " + discoveryRequest.getConfigurationParameters()); + logger.trace(() -> "UniqueId: " + uniqueId.toString()); final EmbulkPluginTestEngineDescriptor engineDescriptor = new EmbulkPluginTestEngineDescriptor(uniqueId); @@ -72,12 +82,7 @@ public TestDescriptor discover(final EngineDiscoveryRequest discoveryRequest, fi // final Class testClass = classSelector.getJavaClass(); // Not to get the Java class directly! final String testClassName = classSelector.getClassName(); - final Class testClass; - try { - testClass = Class.forName(testClassName); // <= TODO: Load it under PluginClassLoader ! - } catch (final ClassNotFoundException ex) { - throw new RuntimeException(ex); - } + final Class testClass = findOrLoadClassFrom(this.klassLoader, testClassName);; final TestDescriptor classDescriptor = new ClassTestDescriptor(uniqueId.append("class", testClass.getName()), testClass); @@ -129,4 +134,22 @@ public final Optional getArtifactId() { protected EmbulkPluginTestEngineExecutionContext createExecutionContext​(final ExecutionRequest request) { return new EmbulkPluginTestEngineExecutionContext(); } + + private static Class findOrLoadClassFrom(final ClassLoader klassLoader, final String name) { + final Class foundClass = LoadedClassFinder.findFrom(klassLoader, name); + if (foundClass != null) { + logger.info(() -> "\"" + name + "\" has been already loaded: " + foundClass.toString()); + } + + logger.info(() -> "\"" + name + "\" has not been loaded."); + try { + return Class.forName(name); // <= TODO: Load it under PluginClassLoader ! + } catch (final ClassNotFoundException ex) { + throw new RuntimeException(ex); + } + } + + private static final Logger logger = LoggerFactory.getLogger(EmbulkPluginTestEngine.class); + + private final ClassLoader klassLoader; } diff --git a/embulk-junit5-engine/src/main/java/org/embulk/junit5/engine/LoadedClassFinder.java b/embulk-junit5-engine/src/main/java/org/embulk/junit5/engine/LoadedClassFinder.java new file mode 100644 index 0000000..e14f7de --- /dev/null +++ b/embulk-junit5-engine/src/main/java/org/embulk/junit5/engine/LoadedClassFinder.java @@ -0,0 +1,29 @@ +/* + * Copyright 2024 The Embulk project + * + * 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 org.embulk.junit5.engine; + +class LoadedClassFinder extends ClassLoader { + private LoadedClassFinder(final ClassLoader inner) { + this.inner = inner; + } + + static Class findFrom(final ClassLoader classLoader, final String name) { + return (new LoadedClassFinder(classLoader)).findLoadedClass(name); + } + + private final ClassLoader inner; +}