Skip to content

Commit

Permalink
[#noissue] Cleanup PluginTest
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jan 2, 2025
1 parent 407ea8e commit f075fbd
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.tinylog.TaggedLogger;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -123,9 +123,9 @@ protected List<PluginForkedTestInstance> createTestCases(PluginForkedTestContext

private List<PluginForkedTestInstance> createSharedCasesWithDependencies(PluginForkedTestContext context) {
DependencyResolver resolver = getDependencyResolver(this.repositories);
List<String> sharedLibs = new ArrayList<>();
sharedLibs.add(context.getTestClassLocation());
sharedLibs.addAll(context.getSharedLibraries());
List<Path> sharedLibs = new ArrayList<>();
sharedLibs.add(context.getTestClassLocationPath());
sharedLibs.addAll(FileUtils.toPaths(context.getSharedLibraries()));
if (sharedDependencies != null) {
Map<String, List<Artifact>> dependencyMap = resolver.resolveDependencySets(sharedDependencies);
for (Map.Entry<String, List<Artifact>> artifactEntry : dependencyMap.entrySet()) {
Expand All @@ -152,7 +152,7 @@ private List<PluginForkedTestInstance> createSharedCasesWithDependencies(PluginF
final String testId = artifactEntry.getKey();
final List<Artifact> artifacts = artifactEntry.getValue();

List<String> libs = null;
List<Path> libs = null;
try {
libs = resolveArtifactsAndDependencies(resolver, artifacts);
} catch (DependencyResolutionException e) {
Expand All @@ -169,16 +169,16 @@ private List<PluginForkedTestInstance> createSharedCasesWithDependencies(PluginF
return cases;
}

private List<String> resolveArtifactsAndDependencies(DependencyResolver resolver, List<Artifact> artifacts) throws DependencyResolutionException {
final List<File> files = resolver.resolveArtifactsAndDependencies(artifacts);
private List<Path> resolveArtifactsAndDependencies(DependencyResolver resolver, List<Artifact> artifacts) throws DependencyResolutionException {
final List<Path> files = resolver.resolveArtifactsAndDependencies(artifacts);
return FileUtils.toAbsolutePath(files);
}

private DependencyResolver getDependencyResolver(String[] repositories) {
return RESOLVER_FACTORY.get(repositories);
}

private PluginForkedTestInstance newSharedProcessPluginTestCase(PluginForkedTestContext context, String testId, List<String> libs, SharedProcessManager sharedProcessManager) {
private PluginForkedTestInstance newSharedProcessPluginTestCase(PluginForkedTestContext context, String testId, List<Path> libs, SharedProcessManager sharedProcessManager) {
if (classLoding == ClassLoding.System) {
return new SharedPluginForkedTestInstance(context, testId, libs, true, sharedProcessManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.tinylog.TaggedLogger;

import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -108,9 +108,9 @@ public PluginSharedInstance createSharedInstance(PluginTestContext context) {
return null;
}

final List<String> libs = new ArrayList<>();
libs.add(context.getTestClassLocation().toString());
libs.addAll(context.getSharedLibList());
final List<Path> libs = new ArrayList<>();
libs.add(context.getTestClassLocation());
libs.addAll(FileUtils.toPaths(context.getSharedLibList()));

if (ArrayUtils.hasLength(sharedDependencies)) {
final DependencyResolver resolver = getDependencyResolver(repositories);
Expand Down Expand Up @@ -150,7 +150,8 @@ private List<PluginTestInstance> createCasesWithDependencies(PluginTestContext c

final String pluginsTest = "com.navercorp.pinpoint:pinpoint-plugins-test:" + VersionUtils.VERSION;
final Map<String, List<Artifact>> agentDependency = resolver.resolveDependencySets(pluginsTest);
final List<String> agentLibs = new ArrayList<>(context.getAgentLibList());
final List<Path> agentLibs = new ArrayList<>(16);
agentLibs.addAll(FileUtils.toPaths(context.getAgentLibList()));

for (Map.Entry<String, List<Artifact>> dependencyCase : agentDependency.entrySet()) {
try {
Expand All @@ -165,7 +166,7 @@ private List<PluginTestInstance> createCasesWithDependencies(PluginTestContext c
final Map<String, List<Artifact>> dependencyCases = resolver.resolveDependencySets(DEPENDENCY_VERSION_FILTER, dependencies);
for (Map.Entry<String, List<Artifact>> dependencyCase : dependencyCases.entrySet()) {
final String testId = dependencyCase.getKey();
final List<String> libs = new ArrayList<>();
final List<Path> libs = new ArrayList<>();
try {
final List<Artifact> artifactList = dependencyCase.getValue();
libs.addAll(resolveArtifactsAndDependencies(resolver, artifactList));
Expand All @@ -174,12 +175,7 @@ private List<PluginTestInstance> createCasesWithDependencies(PluginTestContext c
continue;
}

final List<File> fileList = new ArrayList<>();
for (String classPath : agentLibs) {
File file = new File(classPath);
fileList.add(file);
}
final URL[] agentUrls = URLUtils.fileToUrls(fileList);
final URL[] agentUrls = URLUtils.pathToUrls(agentLibs);

final Thread thread = Thread.currentThread();
final ClassLoader currentClassLoader = thread.getContextClassLoader();
Expand All @@ -198,8 +194,8 @@ private List<PluginTestInstance> createCasesWithDependencies(PluginTestContext c
return pluginTestInstanceList;
}

private List<String> resolveArtifactsAndDependencies(DependencyResolver resolver, List<Artifact> artifacts) throws DependencyResolutionException {
final List<File> files = resolver.resolveArtifactsAndDependencies(artifacts);
private List<Path> resolveArtifactsAndDependencies(DependencyResolver resolver, List<Artifact> artifacts) throws DependencyResolutionException {
final List<Path> files = resolver.resolveArtifactsAndDependencies(artifacts);
return FileUtils.toAbsolutePath(files);
}

Expand All @@ -210,7 +206,7 @@ private DependencyResolver getDependencyResolver(String[] repositories) {
private List<PluginTestInstance> createCasesWithJdkOnly(PluginTestContext context) throws ClassNotFoundException {
final PluginTestInstanceFactory pluginTestInstanceFactory = new PluginTestInstanceFactory(context);
ClassLoader contextClassLoader = ClassLoaderUtils.getContextClassLoader();
List<String> libs = Collections.emptyList();
List<Path> libs = Collections.emptyList();
List<String> transformIncludeList = Collections.emptyList();
final PluginTestInstance pluginTestInstance = pluginTestInstanceFactory.create(contextClassLoader, "", null, libs, transformIncludeList, classLoding);
final List<PluginTestInstance> pluginTestInstanceList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ public List<String> getRepositoryUrls() {
return repositoryUrls;
}

public String getTestClassLocation() {
public String getTestClassLocation() {
return testClassLocation.toString();
}

public Path getTestClassLocationPath() {
return testClassLocation;
}

public Path getAgentJar() {
return agentJar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
import com.navercorp.pinpoint.test.plugin.agent.PluginTestAgentStarter;
import com.navercorp.pinpoint.test.plugin.classloader.PluginAgentTestClassLoader;
import com.navercorp.pinpoint.test.plugin.classloader.PluginTestJunitTestClassLoader;
import com.navercorp.pinpoint.test.plugin.util.FileUtils;
import com.navercorp.pinpoint.test.plugin.util.URLUtils;
import org.junit.platform.commons.JUnitException;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -40,17 +41,14 @@ public PluginTestInstanceFactory(PluginTestContext context) {

public PluginTestInstance create(ClassLoader parentClassLoader, String testId,
PluginAgentTestClassLoader agentClassLoader,
List<String> libs,
List<Path> libs,
List<String> transformIncludeList,
ClassLoding classLoading) throws ClassNotFoundException {
final String id = testId + ":" + classLoading;
PluginTestInstanceCallback instanceContext = startAgent(context.getConfigFile().toString(), agentClassLoader);
final List<File> fileList = new ArrayList<>();
for (String classPath : getClassPath(libs, classLoading)) {
File file = new File(classPath);
fileList.add(file);
}
final URL[] urls = URLUtils.fileToUrls(fileList);

List<Path> classPath = getClassPath(libs, this.context);
final URL[] urls = URLUtils.pathToUrls(classPath);

PluginTestJunitTestClassLoader testClassLoader = new PluginTestJunitTestClassLoader(urls, parentClassLoader, instanceContext);
testClassLoader.setAgentClassLoader(agentClassLoader);
Expand All @@ -61,10 +59,11 @@ public PluginTestInstance create(ClassLoader parentClassLoader, String testId,
return new DefaultPluginTestInstance(id, testClassLoader, testClass, context.isManageTraceObject(), instanceContext);
}

List<String> getClassPath(List<String> libs, ClassLoding classLoading) {
final List<String> libList = new ArrayList<>(context.getJunitLibList());
List<Path> getClassPath(List<Path> libs, PluginTestContext context) {
final List<Path> libList = new ArrayList<>(16);
libList.addAll(FileUtils.toPaths(context.getJunitLibList()));
libList.addAll(libs);
libList.add(context.getTestClassLocation().toString());
libList.add(context.getTestClassLocation());
return libList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import com.navercorp.pinpoint.test.plugin.junit5.launcher.SharedPluginForkedTestLauncher;
import com.navercorp.pinpoint.test.plugin.shared.SharedProcessManager;
import com.navercorp.pinpoint.test.plugin.util.FileUtils;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -34,11 +36,11 @@ public class SharedPluginForkedTestInstance implements PluginForkedTestInstance

private final PluginForkedTestContext context;
private final String testId;
private final List<String> libs;
private final List<Path> libs;
private final boolean onSystemClassLoader;
private final SharedProcessManager processManager;

public SharedPluginForkedTestInstance(PluginForkedTestContext context, String testId, List<String> libs, boolean onSystemClassLoader, SharedProcessManager processManager) {
public SharedPluginForkedTestInstance(PluginForkedTestContext context, String testId, List<Path> libs, boolean onSystemClassLoader, SharedProcessManager processManager) {
this.context = context;
this.testId = testId + ":" + (onSystemClassLoader ? "system" : "child") + ":" + context.getJvmVersion();
this.libs = libs;
Expand All @@ -54,7 +56,7 @@ public String getTestId() {
public List<String> getClassPath() {
if (onSystemClassLoader) {
List<String> libs = new ArrayList<>(context.getRequiredLibraries());
libs.addAll(this.libs);
libs.addAll(FileUtils.toString(this.libs));
libs.add(context.getTestClassLocation());

return libs;
Expand Down Expand Up @@ -83,8 +85,8 @@ public List<String> getAppArgs() {
StringBuilder classPath = new StringBuilder();
classPath.append(CHILD_CLASS_PATH_PREFIX);

for (String lib : libs) {
classPath.append(lib);
for (Path lib : libs) {
classPath.append(lib.toString());
classPath.append(File.pathSeparatorChar);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.io.File;
import java.io.PrintStream;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -146,19 +148,19 @@ public SharedPluginForkedTestLauncher(String testClazzName, String testLocation,
this.out = out;
}

private List<TestInfo> newTestCaseInfo(List<TestParameter> testParameters, File testClazzLocation, String[] repositoryUrls, ClassLoader dependencyClassLoader) throws Exception {
private List<TestInfo> newTestCaseInfo(List<TestParameter> testParameters, Path testClazzLocation, String[] repositoryUrls, ClassLoader dependencyClassLoader) throws Exception {
ReflectionDependencyResolver dependencyResolver = new ReflectionDependencyResolver(dependencyClassLoader, repositoryUrls);
List<File> loggerDependencies = getLoggerDependencies(dependencyResolver, dependencyClassLoader);
List<Path> loggerDependencies = getLoggerDependencies(dependencyResolver, dependencyClassLoader);
logger.debug("loggerDependency:{}", loggerDependencies);

List<TestInfo> testInfos = new ArrayList<>();
for (TestParameter testParameter : testParameters) {
final List<File> testDependency = new ArrayList<>();
final List<Path> testDependency = new ArrayList<>();
testDependency.add(testClazzLocation);

testDependency.addAll(loggerDependencies);

List<File> testParameterDependency = getTestParameterDependency(dependencyClassLoader, dependencyResolver, testParameter);
List<Path> testParameterDependency = getTestParameterDependency(dependencyClassLoader, dependencyResolver, testParameter);
testDependency.addAll(testParameterDependency);

final TestInfo testInfo = new TestInfo(testParameter.getTestId(), testDependency, Arrays.asList(repositoryUrls));
Expand All @@ -167,40 +169,40 @@ private List<TestInfo> newTestCaseInfo(List<TestParameter> testParameters, File
return testInfos;
}

private List<File> getTestParameterDependency(ClassLoader mavenDependencyResolverClassLoader,
private List<Path> getTestParameterDependency(ClassLoader mavenDependencyResolverClassLoader,
ReflectionDependencyResolver dependencyResolver,
TestParameter testParameter) throws Exception {

final List<String> mavenDependencies = testParameter.getMavenDependencies();
List<File> testDependencyFileList = lookup(dependencyResolver, mavenDependencies, mavenDependencyResolverClassLoader);
List<Path> testDependencyFileList = lookup(dependencyResolver, mavenDependencies, mavenDependencyResolverClassLoader);
if (logger.isDebugEnabled()) {
logger.debug("@Dependency {}", mavenDependencies);
for (File file : testDependencyFileList) {
for (Path file : testDependencyFileList) {
logger.debug("-> {}", file);
}
}
return testDependencyFileList;
}

private List<File> getLoggerDependencies(ReflectionDependencyResolver dependencyResolver, ClassLoader mavenDependencyResolverClassLoader) throws Exception {
private List<Path> getLoggerDependencies(ReflectionDependencyResolver dependencyResolver, ClassLoader mavenDependencyResolverClassLoader) throws Exception {
if (!testLogger) {
return Collections.emptyList();
}
List<String> dependencyLib = PluginClassLoading.LOGGER_DEPENDENCY;
List<File> libFiles = lookup(dependencyResolver, dependencyLib, mavenDependencyResolverClassLoader);
List<Path> libFiles = lookup(dependencyResolver, dependencyLib, mavenDependencyResolverClassLoader);
if (logger.isDebugEnabled()) {
logger.debug("LoggerDependency {}", dependencyLib);
for (File libFile : libFiles) {
for (Path libFile : libFiles) {
logger.debug("-> {}", libFile);
}
}
return libFiles;
}

private List<File> lookup(final ReflectionDependencyResolver dependencyResolver, final List<String> dependencyLib, ClassLoader cl) throws Exception {
Callable<List<File>> callable = new ThreadContextCallable<>(new Callable<List<File>>() {
private List<Path> lookup(final ReflectionDependencyResolver dependencyResolver, final List<String> dependencyLib, ClassLoader cl) throws Exception {
Callable<List<Path>> callable = new ThreadContextCallable<>(new Callable<List<Path>>() {
@Override
public List<File> call() throws Exception {
public List<Path> call() throws Exception {
return dependencyResolver.lookup(dependencyLib);
}
}, cl);
Expand All @@ -225,8 +227,9 @@ private void logTestInformation() {

public void execute() throws Exception {
logTestInformation();
ClassLoader mavenDependencyResolverClassLoader = new ChildFirstClassLoader(URLUtils.fileToUrls(mavenDependencyResolverClassPaths));
File testClazzLocation = new File(testLocation);
URL[] classPath = URLUtils.fileToUrls(mavenDependencyResolverClassPaths);
ClassLoader mavenDependencyResolverClassLoader = new ChildFirstClassLoader(classPath);
Path testClazzLocation = Paths.get(testLocation);
List<TestInfo> testInfos = newTestCaseInfo(testParameters, testClazzLocation, repositoryUrls, mavenDependencyResolverClassLoader);

executes(testInfos);
Expand All @@ -240,7 +243,8 @@ private void executes(List<TestInfo> testInfos) {
SharedTestExecutor sharedTestExecutor = null;
SharedTestLifeCycleWrapper sharedTestLifeCycleWrapper = null;
if (sharedClazzName != null && sharedDependencyResolverClassPaths != null) {
final ClassLoader sharedClassLoader = new ChildFirstClassLoader(URLUtils.fileToUrls(sharedDependencyResolverClassPaths));
URL[] classPath = URLUtils.fileToUrls(sharedDependencyResolverClassPaths);
final ClassLoader sharedClassLoader = new ChildFirstClassLoader(classPath);
sharedTestExecutor = new SharedTestExecutor(sharedClazzName, sharedClassLoader);
sharedTestExecutor.startBefore(10, TimeUnit.MINUTES);
sharedTestLifeCycleWrapper = sharedTestExecutor.getSharedClassWrapper();
Expand All @@ -256,13 +260,13 @@ private void executes(List<TestInfo> testInfos) {
}

private ClassLoader createTestClassLoader(TestInfo testInfo) {
List<File> dependencyFileList = testInfo.getDependencyFileList();
List<Path> dependencyFileList = testInfo.getDependencyFileList();
if (logger.isDebugEnabled()) {
for (File dependency : dependencyFileList) {
for (Path dependency : dependencyFileList) {
logger.debug("testcase cl lib :{}", dependency);
}
}
URL[] urls = URLUtils.fileToUrls(dependencyFileList);
URL[] urls = URLUtils.pathToUrls(dependencyFileList);
return new ChildFirstClassLoader(urls, ProfilerClass.PINPOINT_PROFILER_CLASS);
}

Expand Down
Loading

0 comments on commit f075fbd

Please # to comment.