Skip to content

Commit

Permalink
[pinpoint-apm#9414] Refer classpath for querying available libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
acafela authored and smilu97 committed Nov 16, 2022
1 parent a016b52 commit 5170033
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package com.navercorp.pinpoint.test.plugin;

import com.navercorp.pinpoint.common.annotations.VisibleForTesting;
import com.navercorp.pinpoint.common.util.SystemProperty;
import com.navercorp.pinpoint.test.plugin.util.ArrayUtils;
import com.navercorp.pinpoint.test.plugin.util.CodeSourceUtils;
import com.navercorp.pinpoint.test.plugin.util.StringUtils;
import com.navercorp.pinpoint.test.plugin.util.TestLogger;
import com.navercorp.pinpoint.test.plugin.util.TestPluginVersion;

import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.runner.Runner;
Expand All @@ -34,14 +35,13 @@
import java.io.File;
import java.lang.management.ManagementFactory;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public abstract class AbstractPinpointPluginTestSuite extends Suite {

Expand Down Expand Up @@ -92,21 +92,16 @@ public AbstractPinpointPluginTestSuite(Class<?> testClass) throws Initialization
Repository repository = testClass.getAnnotation(Repository.class);
this.repositoryUrls = getRepository(repository);

List<ClassLoaderLib> classLoaderLibs = collectLib(getClass().getClassLoader());
List<String> classPaths = normalizeClassPaths(getClassPathList());
if (logger.isDebugEnabled()) {
for (ClassLoaderLib classLoaderLib : classLoaderLibs) {
logger.debug("classLoader:{}", classLoaderLib.getClassLoader());
for (URL lib : classLoaderLib.getLibs()) {
logger.debug("-> {}", classLoaderLib.getClassLoader(), lib);
}
}
logger.debug("classPaths: {}", classPaths);
}

final LibraryFilter requiredLibraryFilter = new LibraryFilter(
LibraryFilter.createContainsMatcher(PluginClassLoading.getContainsCheckClassPath()),
LibraryFilter.createGlobMatcher(PluginClassLoading.getGlobMatchesCheckClassPath()));

this.requiredLibraries = filterLib(classLoaderLibs, requiredLibraryFilter);
this.requiredLibraries = filterLibs(classPaths, requiredLibraryFilter);
if (logger.isDebugEnabled()) {
for (String requiredLibrary : requiredLibraries) {
logger.debug("requiredLibraries :{}", requiredLibrary);
Expand All @@ -116,7 +111,7 @@ public AbstractPinpointPluginTestSuite(Class<?> testClass) throws Initialization
final LibraryFilter mavenDependencyLibraryFilter = new LibraryFilter(
LibraryFilter.createContainsMatcher(PluginClassLoading.MAVEN_DEPENDENCY_CLASS_PATHS));

this.mavenDependencyLibraries = filterLib(classLoaderLibs, mavenDependencyLibraryFilter);
this.mavenDependencyLibraries = filterLibs(classPaths, mavenDependencyLibraryFilter);
if (logger.isDebugEnabled()) {
for (String mavenDependencyLibrary : mavenDependencyLibraries) {
logger.debug("mavenDependencyLibraries :{}", mavenDependencyLibrary);
Expand Down Expand Up @@ -187,54 +182,36 @@ private String resolveTestClassLocation(Class<?> testClass) {
return toPathString(testClassLocation);
}

private static class ClassLoaderLib {
private final ClassLoader cl;
private final List<URL> libs;

public ClassLoaderLib(ClassLoader cl, List<URL> libs) {
this.cl = cl;
this.libs = libs;
}

public ClassLoader getClassLoader() {
return cl;
}

public List<URL> getLibs() {
return libs;
private List<String> filterLibs(List<String> classPaths, LibraryFilter classPathFilter) {
final TreeSet<String> result = new TreeSet<>(Comparator.naturalOrder());
for (String classPath: classPaths) {
if (classPathFilter.filter(classPath)) {
result.add(classPath);
}
}
return new ArrayList<>(result);
}

private List<String> filterLib(List<ClassLoaderLib> classLoaderLibs, LibraryFilter classPathFilter) {
Set<String> result = new HashSet<>();
for (ClassLoaderLib classLoaderLib : classLoaderLibs) {
List<URL> libs = classLoaderLib.getLibs();
for (URL lib : libs) {
if (classPathFilter.filter(lib)) {
final String filterLibs = toPathString(lib);
if (filterLibs != null) {
result.add(filterLibs);
}
}
}
private List<String> getClassPathList() {
String classPath = SystemProperty.INSTANCE.getProperty("java.class.path");
if (StringUtils.isEmpty(classPath)) {
return Collections.emptyList();
}
List<String> libs = new ArrayList<>(result);
libs.sort(Comparator.naturalOrder());
return libs;
String[] paths = classPath.split(":");
return Arrays.asList(paths);
}

private List<ClassLoaderLib> collectLib(ClassLoader cl) {
List<ClassLoaderLib> libs = new ArrayList<>();
while (cl != null) {
if (cl instanceof URLClassLoader) {
URLClassLoader ucl = ((URLClassLoader) cl);
URL[] urLs = ucl.getURLs();
libs.add(new ClassLoaderLib(cl, Arrays.asList(urLs)));
}
@VisibleForTesting
static String normalizeClassPath(String classPath) {
return Paths.get(classPath).toAbsolutePath().normalize().toString();
}

cl = cl.getParent();
private List<String> normalizeClassPaths(List<String> classPaths) {
final List<String> result = new ArrayList<>(classPaths.size());
for (final String cp: classPaths) {
result.add(normalizeClassPath(cp));
}
return libs;
return result;
}

private static String toPathString(URL url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ public LibraryFilter(LibraryMatcher... matchers) {
this.matchers = Objects.requireNonNull(matchers, "matchers");
}

public boolean filter(URL url) {
public boolean filter(String cp) {
for (LibraryMatcher matcher : matchers) {
if (matcher.include(url.getFile())) {
if (matcher.include(cp)) {
return true;
}
}
return false;
}

public boolean filter(URL url) {
return filter(url.getFile());
}

interface LibraryMatcher {
boolean include(String filePath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 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 org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author youngjin.kim2
*/
@ExtendWith(MockitoExtension.class)
public class AbstractPinpointPluginTestSuiteTest {

@Test
public void normalizeShouldWork() {
final String result = AbstractPinpointPluginTestSuite.normalizeClassPath("./foo/bar/../zoo");
final String expect = System.getProperty("user.dir") + "/foo/zoo";
assertThat(result).isEqualTo(expect);
}

}

0 comments on commit 5170033

Please sign in to comment.