mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-24 11:20:26 +08:00
support specify package prefix of class to be mocked
This commit is contained in:
parent
126c7257d8
commit
7bbdf82a41
@ -14,6 +14,7 @@ public class PreMain {
|
||||
private static final String AND = "&";
|
||||
private static final String LOG_LEVEL = "logLevel";
|
||||
private static final String DUMP_PATH = "dumpPath";
|
||||
private static final String PKG_PREFIX = "pkgPrefix";
|
||||
private static final String EQUAL = "=";
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation inst) {
|
||||
@ -34,6 +35,8 @@ public class PreMain {
|
||||
GlobalConfig.setLogLevel(v);
|
||||
} else if (k.equals(DUMP_PATH)) {
|
||||
GlobalConfig.setDumpPath(v);
|
||||
} else if (k.equals(PKG_PREFIX)) {
|
||||
GlobalConfig.setPkgPrefix(v);
|
||||
}
|
||||
} else {
|
||||
GlobalConfig.setLogLevel(a);
|
||||
|
@ -36,6 +36,7 @@ import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassNa
|
||||
public class TestableClassTransformer implements ClassFileTransformer {
|
||||
|
||||
private static final String FIELD_DIAGNOSE = "diagnose";
|
||||
private static final String COMMA = ",";
|
||||
|
||||
/**
|
||||
* Just avoid spend time to scan those surely non-user classes
|
||||
@ -105,14 +106,25 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
if (null == className) {
|
||||
return true;
|
||||
}
|
||||
for (String prefix : WHITELIST_PREFIXES) {
|
||||
if (className.startsWith(prefix)) {
|
||||
return false;
|
||||
String whitePrefix = GlobalConfig.getPkgPrefix();
|
||||
if (whitePrefix != null) {
|
||||
for (String prefix : whitePrefix.split(COMMA)) {
|
||||
if (className.startsWith(prefix)) {
|
||||
// Only consider package in provided list as non-system class
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (String prefix : BLACKLIST_PREFIXES) {
|
||||
if (className.startsWith(prefix)) {
|
||||
return true;
|
||||
return true;
|
||||
} else {
|
||||
for (String prefix : WHITELIST_PREFIXES) {
|
||||
if (className.startsWith(prefix)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (String prefix : BLACKLIST_PREFIXES) {
|
||||
if (className.startsWith(prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -12,6 +12,7 @@ public class GlobalConfig {
|
||||
private static final String VERBOSE = "verbose";
|
||||
|
||||
private static String dumpPath = null;
|
||||
private static String pkgPrefix = null;
|
||||
|
||||
public static boolean setLogLevel(String level) {
|
||||
if (level.equals(MUTE)) {
|
||||
@ -35,4 +36,11 @@ public class GlobalConfig {
|
||||
return dumpPath;
|
||||
}
|
||||
|
||||
public static String getPkgPrefix() {
|
||||
return pkgPrefix;
|
||||
}
|
||||
|
||||
public static void setPkgPrefix(String pkgPrefix) {
|
||||
GlobalConfig.pkgPrefix = pkgPrefix;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.alibaba.testable;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
@ -45,6 +46,12 @@ public class TestableMojo extends AbstractMojo
|
||||
@Parameter
|
||||
private String dumpPath;
|
||||
|
||||
/**
|
||||
* Package prefixes of class need to be mocked (comma split)
|
||||
*/
|
||||
@Parameter
|
||||
private String pkgPrefix;
|
||||
|
||||
/**
|
||||
* Name of the Testable Agent artifact.
|
||||
*/
|
||||
@ -71,13 +78,17 @@ public class TestableMojo extends AbstractMojo
|
||||
getLog().error("failed to fetch project properties");
|
||||
return;
|
||||
}
|
||||
|
||||
String extraArgs = "";
|
||||
if (logLevel != null && !logLevel.isEmpty()) {
|
||||
if (!Strings.isNullOrEmpty(logLevel)) {
|
||||
extraArgs += "&logLevel=" + logLevel;
|
||||
}
|
||||
if (dumpPath != null && !dumpPath.isEmpty()) {
|
||||
if (!Strings.isNullOrEmpty(dumpPath)) {
|
||||
extraArgs += "&dumpPath=" + dumpPath;
|
||||
}
|
||||
if (!Strings.isNullOrEmpty(pkgPrefix)) {
|
||||
extraArgs += "&pkgPrefix=" + pkgPrefix;
|
||||
}
|
||||
final String oldArgs = projectProperties.getProperty(testArgsPropertyKey);
|
||||
String newArgs = (oldArgs == null) ? getAgentJarArgs().trim() : (oldArgs + getAgentJarArgs());
|
||||
if (!extraArgs.isEmpty()) {
|
||||
@ -91,6 +102,7 @@ public class TestableMojo extends AbstractMojo
|
||||
final Artifact testableAgentArtifact = pluginArtifactMap.get(AGENT_ARTIFACT_NAME);
|
||||
if (testableAgentArtifact == null) {
|
||||
getLog().error("failed to find testable agent jar");
|
||||
return "";
|
||||
}
|
||||
return " -javaagent:" + testableAgentArtifact.getFile().getAbsolutePath();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user