Skip to content

Commit

Permalink
[pinpoint-apm#11745] Apply Version templating to plugin-test module
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Nov 22, 2024
1 parent afa69fa commit f2d4ff5
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 102 deletions.
6 changes: 0 additions & 6 deletions agent-module/plugins-test-module/plugins-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,6 @@
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.navercorp.pinpoint.test.plugin;
public final class PluginEngineTemplate {
public static final String PROJECT_VERSION = "${project.version}";
public static final String LOG4J_VERSION = "${log4j2.version}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.navercorp.pinpoint.test.plugin;

import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.common.util.ArrayUtils;
import com.navercorp.pinpoint.test.plugin.classloader.PluginAgentTestClassLoader;
import com.navercorp.pinpoint.test.plugin.shared.PluginSharedInstance;
Expand All @@ -26,6 +25,7 @@
import com.navercorp.pinpoint.test.plugin.util.FileUtils;
import com.navercorp.pinpoint.test.plugin.util.TestLogger;
import com.navercorp.pinpoint.test.plugin.util.URLUtils;
import com.navercorp.pinpoint.test.plugin.util.VersionUtils;
import org.eclipse.aether.ConfigurationProperties;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.resolution.DependencyResolutionException;
Expand Down Expand Up @@ -145,7 +145,7 @@ private List<PluginTestInstance> createCasesWithDependencies(PluginTestContext c
final List<PluginTestInstance> pluginTestInstanceList = new ArrayList<>();
final DependencyResolver resolver = getDependencyResolver(repositories);

final String pluginsTest = "com.navercorp.pinpoint:pinpoint-plugins-test:" + Version.VERSION;
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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.navercorp.pinpoint.test.plugin;

import java.io.File;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.Objects;

class LibraryFilter {
Expand All @@ -41,10 +40,6 @@ public boolean filter(String cp) {
return false;
}

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

interface LibraryMatcher {
boolean include(String filePath);
}
Expand Down Expand Up @@ -97,9 +92,7 @@ private GlobMatcher(String[] paths) {

@Override
public boolean include(String filePath) {
Path path = new File(filePath).toPath();


Path path = Paths.get(filePath);
for (PathMatcher pathMatcher : pathMatchers) {
if (pathMatcher.matches(path)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.navercorp.pinpoint.test.plugin;

import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.test.plugin.util.StringUtils;
import com.navercorp.pinpoint.test.plugin.util.VersionUtils;

import java.io.File;
import java.nio.file.Paths;
Expand All @@ -9,6 +10,8 @@

public final class PluginClassLoading {

private static final String VERSION = VersionUtils.VERSION;

private static final String[] CLASS_PATHS_TO_CHECK_AS_CONTAINS = new String[]{
"junit", // JUnit
"opentest4",
Expand Down Expand Up @@ -54,7 +57,7 @@ public static String[] getContainsCheckSharedClassPath() {
}

private static final String[] CLASS_PATHS_TO_CHECK_AS_GLOB_MATCHES = new String[]{
"**" + File.separator + "pinpoint-*-plugin-" + Version.VERSION + ".jar", // required when executing test via mvn command
Paths.get("**", "pinpoint-*-plugin-" + VERSION + ".jar").toString()
};

public static String[] getGlobMatchesCheckClassPath() {
Expand Down Expand Up @@ -84,7 +87,7 @@ public static String[] getGlobMatchesCheckClassPath() {
"slf4j-tinylog"
};

private static final String LOG4J2_VERSION = Log4j2VersionTemplate.VERSION;
private static final String LOG4J2_VERSION = PluginEngineTemplate.LOG4J_VERSION;
private static final String[] LOGGER_DEPENDENCY_ID = new String[]{
"org.apache.logging.log4j:log4j-api:%s",
"org.apache.logging.log4j:log4j-core:%s",
Expand All @@ -103,13 +106,13 @@ public static List<String> format(String[] libs, String version) {
}

public static final String[] PLUGIN_GLOB_MATCHES = new String[]{
"**" + File.separator + "pinpoint-*-plugin-" + Version.VERSION + ".jar", // required when executing test via mvn command
"**" + File.separator + "naver-pinpoint-*-plugin-" + Version.VERSION + ".jar" // required when executing test via mvn command
Paths.get("**", "pinpoint-*-plugin-" + VERSION + ".jar").toString(), // required when executing test via mvn command
Paths.get("**", "naver-pinpoint-*-plugin-" + VERSION + ".jar").toString() // required when executing test via mvn command
};

public static final String[] PLUGIN_CONTAINS_MATCHES = new String[]{
File.separator + "pinpoint" + File.separator + "agent-module" + File.separator + "plugins" + File.separator,
File.separator + "pinpoint-naver" + File.separator + "agent-module" + File.separator + "naver-plugins" + File.separator
StringUtils.wrap(Paths.get("pinpoint", "agent-module", "plugins").toString(), File.separator),
StringUtils.wrap(Paths.get("pinpoint-naver", "agent-module", "naver-plugins").toString(), File.separator)
};

public static final String[] PLUGIN_IT_UTILS_CONTAINS_MATCHES = new String[]{
Expand Down Expand Up @@ -142,4 +145,4 @@ public static List<String> format(String[] libs, String version) {
Paths.get("testcase", "target").toString(),
Paths.get("testcase", "target", "classes").toString()
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public static <T> int getLength(final String string, final int nullValue) {
return string.length();
}

public static String wrap(final String str, final String wrapToken) {
if (isEmpty(str)) {
return str;
}
return wrapToken + str + wrapToken;
}

}
Original file line number Diff line number Diff line change
@@ -1,58 +1,9 @@
package com.navercorp.pinpoint.test.plugin.util;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class TestPluginVersion {
private static final String VERSION_PROPERTIES = "testplugin-version.properties";

private static final String VERSION_KEY = "PROJECT_VERSION";
private static final String NOT_COMPILED = "${project.version}";

private static final String VERSION;

static {
InputStream stream = TestPluginVersion.class.getClassLoader().getResourceAsStream(VERSION_PROPERTIES);
Properties properties = loadProperties(stream);

VERSION = readString(properties, VERSION_KEY);
}

private static String readString(Properties properties, String key) {
final String projectVersion = properties.getProperty(key);
if (projectVersion == null) {
throw new IllegalStateException(key + " key not found");
}
if (NOT_COMPILED.equals(projectVersion)) {
throw new IllegalStateException("Install pinpoint-test module( $test> mvn install)");
}
return projectVersion;
}

private static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignored) {
}
}
}

private static Properties loadProperties(InputStream stream) {
Properties properties = new Properties();
try {
properties.load(stream);
return properties;
} catch (IOException e) {
throw new RuntimeException("properties load failed", e);
} finally {
close(stream);
}
}

public static String getVersion() {
return VERSION;
return VersionUtils.VERSION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.navercorp.pinpoint.test.plugin.util;

import com.navercorp.pinpoint.test.plugin.PluginEngineTemplate;

public class VersionUtils {

public static String VERSION = PluginEngineTemplate.PROJECT_VERSION;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.navercorp.pinpoint.test.plugin;

import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.test.plugin.util.VersionUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -17,6 +17,8 @@

public class LibraryFinderTest {

private static final String VERSION = VersionUtils.VERSION;

@Test
public void filter() throws MalformedURLException {
LibraryFilter.LibraryMatcher matcher = LibraryFilter.createContainsMatcher(new String[]{"abc", "123"});
Expand All @@ -29,42 +31,42 @@ public void filter() throws MalformedURLException {
List<URL> list = Arrays.asList(url1, url2, url3, url4, url5);
Collections.shuffle(list);

Collection<Path> result = new ArrayDeque<>();
Collection<String> result = new ArrayDeque<>();
for (URL url : list) {
if (libraryFinder.filter(url)) {
result.add(Paths.get(url.getPath()));
String path = url.getPath();
if (libraryFinder.filter(path)) {
result.add(path);
}
}
Assertions.assertEquals(2, result.size());
}

@Test
public void globTest() throws MalformedURLException {
String jarPath = File.separator + "home"
+ File.separator + "pinpoint-mssql-jdbc-driver-plugin"
+ File.separator + Version.VERSION
+ File.separator + "pinpoint-mssql-jdbc-driver-plugin-" + Version.VERSION + ".jar";
String jarPath = File.separator + Paths.get("home",
"pinpoint-mssql-jdbc-driver-plugin",
VERSION,
"pinpoint-mssql-jdbc-driver-plugin-" + VERSION + ".jar"
);

String matchPath = "pinpoint-*-plugin-" + Version.VERSION + ".jar";
String matchPath = "pinpoint-*-plugin-" + VERSION + ".jar";
Path result = globMatches(jarPath, matchPath);
Assertions.assertNull(result);

matchPath = "*" + File.separator + "pinpoint-*-plugin-" + Version.VERSION + ".jar";
result = globMatches(jarPath, matchPath);
Assertions.assertNull(result);
String matchPath2 = Paths.get("*", "pinpoint-*-plugin-" + VERSION + ".jar").toString();
Path result2 = globMatches(jarPath, matchPath2);
Assertions.assertNull(result2);

matchPath = "**" + File.separator + "pinpoint-*-plugin-" + Version.VERSION + ".jar";
result = globMatches(jarPath, matchPath);
Assertions.assertEquals(Paths.get(jarPath), result);
String matchPath3 = Paths.get("**", "pinpoint-*-plugin-" + VERSION + ".jar").toString();
Path result3 = globMatches(jarPath, matchPath3);
Assertions.assertEquals(Paths.get(jarPath), result3);
}

private Path globMatches(String jarPath, String matchPath) throws MalformedURLException {
LibraryFilter.LibraryMatcher matcher = LibraryFilter.createGlobMatcher(new String[]{matchPath});
LibraryFilter libraryFinder = new LibraryFilter(matcher);

File file = new File(jarPath);
URL url = file.toURI().toURL();
if (libraryFinder.filter(url)) {
if (libraryFinder.filter(jarPath)) {
return Paths.get(jarPath);
}
return null;
Expand Down

0 comments on commit f2d4ff5

Please sign in to comment.