allow disable mocking

This commit is contained in:
金戟 2021-05-11 22:42:31 +08:00
parent 46288fa0e9
commit cd7b9cc922
6 changed files with 39 additions and 66 deletions

View File

@ -22,7 +22,7 @@ public class PreMain {
PropertiesParser.parseFile(ArgumentParser.configFilePath); PropertiesParser.parseFile(ArgumentParser.configFilePath);
GlobalConfig.setupLogRootPath(); GlobalConfig.setupLogRootPath();
LogUtil.info("TestableMock start at %s", System.getProperty(PROPERTY_USER_DIR)); LogUtil.info("TestableMock start at %s", System.getProperty(PROPERTY_USER_DIR));
if (GlobalConfig.shouldEnhanceThreadLocal()) { if (GlobalConfig.enhanceThreadLocal) {
// add transmittable thread local transformer // add transmittable thread local transformer
TtlAgent.premain(agentArgs, inst); TtlAgent.premain(agentArgs, inst);
} }

View File

@ -35,14 +35,14 @@ public class ArgumentParser {
} else if (k.equals(PKG_PREFIX)) { } else if (k.equals(PKG_PREFIX)) {
GlobalConfig.setPkgPrefixWhiteList(v); GlobalConfig.setPkgPrefixWhiteList(v);
} else if (k.equals(MOCK_SCOPE)) { } else if (k.equals(MOCK_SCOPE)) {
GlobalConfig.setDefaultMockScope(MockScope.of(v)); GlobalConfig.defaultMockScope = MockScope.of(v);
} else if (k.equals(CONFIG_FILE)) { } else if (k.equals(CONFIG_FILE)) {
configFilePath = v; configFilePath = v;
} }
} else { } else {
// parameter with single value // parameter with single value
if (a.equals(USE_THREAD_POOL)) { if (a.equals(USE_THREAD_POOL)) {
GlobalConfig.enableEnhanceThreadLocal(true); GlobalConfig.enhanceThreadLocal = true;
} }
} }
} }

View File

@ -18,14 +18,15 @@ public class PropertiesParser {
private static final String DEFAULT_CONFIG_FILE = "src/test/resources/testable.properties"; private static final String DEFAULT_CONFIG_FILE = "src/test/resources/testable.properties";
private static final String DUMP_PATH = "dump.path"; private static final String DUMP_PATH = "dump.path";
private static final String PKG_PREFIX_EXCLUDES = "enhance.pkgPrefix.excludes";
private static final String PKG_PREFIX_INCLUDES = "enhance.pkgPrefix.includes"; private static final String PKG_PREFIX_INCLUDES = "enhance.pkgPrefix.includes";
private static final String LOG_FILE = "log.file"; private static final String LOG_FILE = "log.file";
private static final String LOG_LEVEL = "log.level"; private static final String LOG_LEVEL = "log.level";
private static final String INNER_MOCK_CLASS_NAME = "mock.innerClass.name"; private static final String INNER_MOCK_CLASS_NAME = "mock.innerClass.name";
private static final String MOCK_PKG_MAPPING_PREFIX = "mock.package.mapping."; private static final String MOCK_PKG_MAPPING_PREFIX = "mock.package.mapping.";
private static final String DEFAULT_MOCK_SCOPE = "mock.scope.default"; private static final String DEFAULT_MOCK_SCOPE = "mock.scope.default";
private static final String ENABLE_MOCK_INJECT = "mock.enhance.enable";
private static final String ENABLE_OMNI_INJECT = "omni.constructor.enhance.enable"; private static final String ENABLE_OMNI_INJECT = "omni.constructor.enhance.enable";
private static final String OMNI_INJECT_EXCLUDES = "omni.constructor.enhance.pkgPrefix.excludes";
private static final String ENABLE_THREAD_POOL = "thread.pool.enhance.enable"; private static final String ENABLE_THREAD_POOL = "thread.pool.enhance.enable";
public static void parseFile(String configFilePath) { public static void parseFile(String configFilePath) {
@ -55,6 +56,8 @@ public class PropertiesParser {
String v = pps.getProperty(k); String v = pps.getProperty(k);
if (k.equals(DUMP_PATH)) { if (k.equals(DUMP_PATH)) {
GlobalConfig.setDumpPath(v); GlobalConfig.setDumpPath(v);
} else if (k.equals(PKG_PREFIX_EXCLUDES)) {
GlobalConfig.setPkgPrefixBlackList(v);
} else if (k.equals(PKG_PREFIX_INCLUDES)) { } else if (k.equals(PKG_PREFIX_INCLUDES)) {
GlobalConfig.setPkgPrefixWhiteList(v); GlobalConfig.setPkgPrefixWhiteList(v);
} else if (k.equals(LOG_FILE)) { } else if (k.equals(LOG_FILE)) {
@ -62,17 +65,17 @@ public class PropertiesParser {
} else if (k.equals(LOG_LEVEL)) { } else if (k.equals(LOG_LEVEL)) {
GlobalConfig.setLogLevel(v); GlobalConfig.setLogLevel(v);
} else if (k.equals(INNER_MOCK_CLASS_NAME)) { } else if (k.equals(INNER_MOCK_CLASS_NAME)) {
GlobalConfig.setInnerMockClassName(v); GlobalConfig.innerMockClassName = v;
} else if (k.startsWith(MOCK_PKG_MAPPING_PREFIX)) { } else if (k.startsWith(MOCK_PKG_MAPPING_PREFIX)) {
GlobalConfig.addMockPackageMapping(k.substring(MOCK_PKG_MAPPING_PREFIX.length()), v); GlobalConfig.addMockPackageMapping(k.substring(MOCK_PKG_MAPPING_PREFIX.length()), v);
} else if (k.equals(DEFAULT_MOCK_SCOPE)) { } else if (k.equals(DEFAULT_MOCK_SCOPE)) {
GlobalConfig.setDefaultMockScope(MockScope.of(v)); GlobalConfig.defaultMockScope = MockScope.of(v);
} else if (k.equals(ENABLE_OMNI_INJECT)) { } else if (k.equals(ENABLE_OMNI_INJECT)) {
GlobalConfig.enableEnhanceOmniConstructor(Boolean.parseBoolean(v)); GlobalConfig.enhanceOmniConstructor = Boolean.parseBoolean(v);
} else if (k.equals(OMNI_INJECT_EXCLUDES)) { } else if (k.equals(ENABLE_MOCK_INJECT)) {
GlobalConfig.setOmniPkgPrefixBlackList(v); GlobalConfig.enhanceMock = Boolean.parseBoolean(v);
} else if (k.equals(ENABLE_THREAD_POOL)) { } else if (k.equals(ENABLE_THREAD_POOL)) {
GlobalConfig.enableEnhanceThreadLocal(Boolean.parseBoolean(v)); GlobalConfig.enhanceThreadLocal = Boolean.parseBoolean(v);
} }
} }
} }

View File

@ -225,7 +225,7 @@ public class MockClassHandler extends BaseClassWithContextHandler {
for (AnnotationNode an : mn.visibleAnnotations) { for (AnnotationNode an : mn.visibleAnnotations) {
if (isMockMethodAnnotation(an) || isMockConstructorAnnotation(an)) { if (isMockMethodAnnotation(an) || isMockConstructorAnnotation(an)) {
MockScope scope = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_SCOPE, MockScope scope = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_SCOPE,
GlobalConfig.getDefaultMockScope(), MockScope.class); GlobalConfig.defaultMockScope, MockScope.class);
if (scope.equals(MockScope.GLOBAL)) { if (scope.equals(MockScope.GLOBAL)) {
return true; return true;
} }

View File

@ -51,20 +51,17 @@ public class TestableClassTransformer implements ClassFileTransformer {
// Ignore system class and reloaded class // Ignore system class and reloaded class
return null; return null;
} }
byte[] bytes = shouldOmniEnhance(className) ? new OmniClassHandler().getBytes(classFileBuffer) : classFileBuffer; byte[] bytes = GlobalConfig.enhanceOmniConstructor ?
ClassNode cn = ClassUtil.getClassNode(className); new OmniClassHandler().getBytes(classFileBuffer) : classFileBuffer;
if (cn != null) { if (GlobalConfig.enhanceMock) {
return transformMock(bytes, cn); ClassNode cn = ClassUtil.getClassNode(className);
if (cn != null) {
return transformMock(bytes, cn);
}
} }
return bytes; return bytes;
} }
private boolean shouldOmniEnhance(String className) {
String[] blackList = GlobalConfig.getOmniPkgPrefixBlackList();
return GlobalConfig.shouldEnhanceOmniConstructor() &&
(blackList == null || !isInPrefixList(className, blackList));
}
private byte[] transformMock(byte[] bytes, ClassNode cn) { private byte[] transformMock(byte[] bytes, ClassNode cn) {
String className = (GlobalConfig.getMockPackageMapping() == null) ? cn.name : mapPackage(cn.name); String className = (GlobalConfig.getMockPackageMapping() == null) ? cn.name : mapPackage(cn.name);
try { try {
@ -167,10 +164,14 @@ public class TestableClassTransformer implements ClassFileTransformer {
if (null == className || className.contains(CGLIB_CLASS_PATTERN)) { if (null == className || className.contains(CGLIB_CLASS_PATTERN)) {
return true; return true;
} }
String[] pkgPrefixWhiteList = GlobalConfig.getPkgPrefixWhiteList(); String[] blackList = GlobalConfig.getPkgPrefixBlackList();
if (pkgPrefixWhiteList != null) { if (blackList != null && isInPrefixList(className, blackList)) {
return true;
}
String[] whiteList = GlobalConfig.getPkgPrefixWhiteList();
if (whiteList != null) {
// Only consider package in provided list as non-system class // Only consider package in provided list as non-system class
return !isInPrefixList(className, pkgPrefixWhiteList); return !isInPrefixList(className, whiteList);
} }
return isInPrefixList(className, BLACKLIST_PREFIXES); return isInPrefixList(className, BLACKLIST_PREFIXES);
} }
@ -282,7 +283,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
} }
private String getInnerMockClassName(String className) { private String getInnerMockClassName(String className) {
return className + DOLLAR + GlobalConfig.getInnerMockClassName(); return className + DOLLAR + GlobalConfig.innerMockClassName;
} }
} }

View File

@ -32,12 +32,13 @@ public class GlobalConfig {
private static String logFile = null; private static String logFile = null;
private static String dumpPath = null; private static String dumpPath = null;
private static String[] pkgPrefixWhiteList = null; private static String[] pkgPrefixWhiteList = null;
private static String[] omniPkgPrefixBlackList = null; private static String[] pkgPrefixBlackList = null;
private static MockScope defaultMockScope = MockScope.GLOBAL;
private static boolean enhanceThreadLocal = false;
private static boolean enhanceOmniConstructor = false;
private static String innerMockClassName = "Mock";
private static Map<String, String> mockPkgMapping = null; private static Map<String, String> mockPkgMapping = null;
public static MockScope defaultMockScope = MockScope.GLOBAL;
public static boolean enhanceMock = true;
public static boolean enhanceOmniConstructor = false;
public static boolean enhanceThreadLocal = false;
public static String innerMockClassName = "Mock";
public static void setLogLevel(String level) { public static void setLogLevel(String level) {
if (level.equals(MUTE)) { if (level.equals(MUTE)) {
@ -72,20 +73,12 @@ public class GlobalConfig {
pkgPrefixWhiteList = parsePkgPrefixList(prefixes).toArray(new String[0]); pkgPrefixWhiteList = parsePkgPrefixList(prefixes).toArray(new String[0]);
} }
public static String[] getOmniPkgPrefixBlackList() { public static String[] getPkgPrefixBlackList() {
return omniPkgPrefixBlackList; return pkgPrefixBlackList;
} }
public static void setOmniPkgPrefixBlackList(String prefixes) { public static void setPkgPrefixBlackList(String prefixes) {
omniPkgPrefixBlackList = parsePkgPrefixList(prefixes).toArray(new String[0]); pkgPrefixBlackList = parsePkgPrefixList(prefixes).toArray(new String[0]);
}
public static MockScope getDefaultMockScope() {
return defaultMockScope;
}
public static void setDefaultMockScope(MockScope scope) {
defaultMockScope = scope;
} }
public static void setupLogRootPath() { public static void setupLogRootPath() {
@ -117,30 +110,6 @@ public class GlobalConfig {
} }
} }
public static void enableEnhanceThreadLocal(boolean enabled) {
enhanceThreadLocal = enabled;
}
public static boolean shouldEnhanceThreadLocal() {
return enhanceThreadLocal;
}
public static void enableEnhanceOmniConstructor(boolean enabled) {
enhanceOmniConstructor = enabled;
}
public static boolean shouldEnhanceOmniConstructor() {
return enhanceOmniConstructor;
}
public static void setInnerMockClassName(String name) {
innerMockClassName = name;
}
public static String getInnerMockClassName() {
return innerMockClassName;
}
public static void addMockPackageMapping(String originPkg, String mockClassPkg) { public static void addMockPackageMapping(String originPkg, String mockClassPkg) {
if (mockPkgMapping == null) { if (mockPkgMapping == null) {
mockPkgMapping = new HashMap<String, String>(5); mockPkgMapping = new HashMap<String, String>(5);