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);
GlobalConfig.setupLogRootPath();
LogUtil.info("TestableMock start at %s", System.getProperty(PROPERTY_USER_DIR));
if (GlobalConfig.shouldEnhanceThreadLocal()) {
if (GlobalConfig.enhanceThreadLocal) {
// add transmittable thread local transformer
TtlAgent.premain(agentArgs, inst);
}

View File

@ -35,14 +35,14 @@ public class ArgumentParser {
} else if (k.equals(PKG_PREFIX)) {
GlobalConfig.setPkgPrefixWhiteList(v);
} else if (k.equals(MOCK_SCOPE)) {
GlobalConfig.setDefaultMockScope(MockScope.of(v));
GlobalConfig.defaultMockScope = MockScope.of(v);
} else if (k.equals(CONFIG_FILE)) {
configFilePath = v;
}
} else {
// parameter with single value
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 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 LOG_FILE = "log.file";
private static final String LOG_LEVEL = "log.level";
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 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 OMNI_INJECT_EXCLUDES = "omni.constructor.enhance.pkgPrefix.excludes";
private static final String ENABLE_THREAD_POOL = "thread.pool.enhance.enable";
public static void parseFile(String configFilePath) {
@ -55,6 +56,8 @@ public class PropertiesParser {
String v = pps.getProperty(k);
if (k.equals(DUMP_PATH)) {
GlobalConfig.setDumpPath(v);
} else if (k.equals(PKG_PREFIX_EXCLUDES)) {
GlobalConfig.setPkgPrefixBlackList(v);
} else if (k.equals(PKG_PREFIX_INCLUDES)) {
GlobalConfig.setPkgPrefixWhiteList(v);
} else if (k.equals(LOG_FILE)) {
@ -62,17 +65,17 @@ public class PropertiesParser {
} else if (k.equals(LOG_LEVEL)) {
GlobalConfig.setLogLevel(v);
} else if (k.equals(INNER_MOCK_CLASS_NAME)) {
GlobalConfig.setInnerMockClassName(v);
GlobalConfig.innerMockClassName = v;
} else if (k.startsWith(MOCK_PKG_MAPPING_PREFIX)) {
GlobalConfig.addMockPackageMapping(k.substring(MOCK_PKG_MAPPING_PREFIX.length()), v);
} else if (k.equals(DEFAULT_MOCK_SCOPE)) {
GlobalConfig.setDefaultMockScope(MockScope.of(v));
GlobalConfig.defaultMockScope = MockScope.of(v);
} else if (k.equals(ENABLE_OMNI_INJECT)) {
GlobalConfig.enableEnhanceOmniConstructor(Boolean.parseBoolean(v));
} else if (k.equals(OMNI_INJECT_EXCLUDES)) {
GlobalConfig.setOmniPkgPrefixBlackList(v);
GlobalConfig.enhanceOmniConstructor = Boolean.parseBoolean(v);
} else if (k.equals(ENABLE_MOCK_INJECT)) {
GlobalConfig.enhanceMock = Boolean.parseBoolean(v);
} 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) {
if (isMockMethodAnnotation(an) || isMockConstructorAnnotation(an)) {
MockScope scope = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_SCOPE,
GlobalConfig.getDefaultMockScope(), MockScope.class);
GlobalConfig.defaultMockScope, MockScope.class);
if (scope.equals(MockScope.GLOBAL)) {
return true;
}

View File

@ -51,20 +51,17 @@ public class TestableClassTransformer implements ClassFileTransformer {
// Ignore system class and reloaded class
return null;
}
byte[] bytes = shouldOmniEnhance(className) ? new OmniClassHandler().getBytes(classFileBuffer) : classFileBuffer;
ClassNode cn = ClassUtil.getClassNode(className);
if (cn != null) {
return transformMock(bytes, cn);
byte[] bytes = GlobalConfig.enhanceOmniConstructor ?
new OmniClassHandler().getBytes(classFileBuffer) : classFileBuffer;
if (GlobalConfig.enhanceMock) {
ClassNode cn = ClassUtil.getClassNode(className);
if (cn != null) {
return transformMock(bytes, cn);
}
}
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) {
String className = (GlobalConfig.getMockPackageMapping() == null) ? cn.name : mapPackage(cn.name);
try {
@ -167,10 +164,14 @@ public class TestableClassTransformer implements ClassFileTransformer {
if (null == className || className.contains(CGLIB_CLASS_PATTERN)) {
return true;
}
String[] pkgPrefixWhiteList = GlobalConfig.getPkgPrefixWhiteList();
if (pkgPrefixWhiteList != null) {
String[] blackList = GlobalConfig.getPkgPrefixBlackList();
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
return !isInPrefixList(className, pkgPrefixWhiteList);
return !isInPrefixList(className, whiteList);
}
return isInPrefixList(className, BLACKLIST_PREFIXES);
}
@ -282,7 +283,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
}
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 dumpPath = null;
private static String[] pkgPrefixWhiteList = null;
private static String[] omniPkgPrefixBlackList = null;
private static MockScope defaultMockScope = MockScope.GLOBAL;
private static boolean enhanceThreadLocal = false;
private static boolean enhanceOmniConstructor = false;
private static String innerMockClassName = "Mock";
private static String[] pkgPrefixBlackList = 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) {
if (level.equals(MUTE)) {
@ -72,20 +73,12 @@ public class GlobalConfig {
pkgPrefixWhiteList = parsePkgPrefixList(prefixes).toArray(new String[0]);
}
public static String[] getOmniPkgPrefixBlackList() {
return omniPkgPrefixBlackList;
public static String[] getPkgPrefixBlackList() {
return pkgPrefixBlackList;
}
public static void setOmniPkgPrefixBlackList(String prefixes) {
omniPkgPrefixBlackList = parsePkgPrefixList(prefixes).toArray(new String[0]);
}
public static MockScope getDefaultMockScope() {
return defaultMockScope;
}
public static void setDefaultMockScope(MockScope scope) {
defaultMockScope = scope;
public static void setPkgPrefixBlackList(String prefixes) {
pkgPrefixBlackList = parsePkgPrefixList(prefixes).toArray(new String[0]);
}
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) {
if (mockPkgMapping == null) {
mockPkgMapping = new HashMap<String, String>(5);