support modify default mock scope

This commit is contained in:
金戟 2021-02-20 12:48:12 +08:00
parent df73dac54f
commit 6237e19a32
6 changed files with 32 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package com.alibaba.testable.agent;
import com.alibaba.testable.agent.transformer.TestableClassTransformer;
import com.alibaba.testable.agent.util.GlobalConfig;
import com.alibaba.testable.core.model.MockScope;
import com.alibaba.ttl.threadpool.agent.TtlAgent;
import java.lang.instrument.Instrumentation;
@ -17,6 +18,7 @@ public class PreMain {
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 MOCK_SCOPE = "mockScope";
private static final String EQUAL = "=";
private static boolean enhanceThreadLocal = false;
@ -46,6 +48,8 @@ public class PreMain {
GlobalConfig.setDumpPath(v);
} else if (k.equals(PKG_PREFIX)) {
GlobalConfig.setPkgPrefix(v);
} else if (k.equals(MOCK_SCOPE)) {
GlobalConfig.setDefaultMockScope(MockScope.of(v));
}
} else {
// parameter with single value

View File

@ -5,6 +5,7 @@ import com.alibaba.testable.agent.constant.ConstPool;
import com.alibaba.testable.agent.tool.ImmutablePair;
import com.alibaba.testable.agent.util.AnnotationUtil;
import com.alibaba.testable.agent.util.ClassUtil;
import com.alibaba.testable.agent.util.GlobalConfig;
import com.alibaba.testable.agent.util.MethodUtil;
import com.alibaba.testable.core.model.MockScope;
import org.objectweb.asm.Label;
@ -209,7 +210,7 @@ public class MockClassHandler extends BaseClassWithContextHandler {
if (ClassUtil.toByteCodeClassName(ConstPool.MOCK_METHOD).equals(an.desc) ||
ClassUtil.toByteCodeClassName(ConstPool.MOCK_CONSTRUCTOR).equals(an.desc)) {
MockScope scope = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_SCOPE,
MockScope.ASSOCIATED, MockScope.class);
GlobalConfig.getDefaultMockScope(), MockScope.class);
if (scope.equals(MockScope.GLOBAL)) {
return true;
}

View File

@ -1,5 +1,6 @@
package com.alibaba.testable.agent.util;
import com.alibaba.testable.core.model.MockScope;
import com.alibaba.testable.core.util.LogUtil;
/**
@ -13,6 +14,7 @@ public class GlobalConfig {
private static String dumpPath = null;
private static String pkgPrefix = null;
private static MockScope defaultMockScope = MockScope.GLOBAL;
public static boolean setLogLevel(String level) {
if (level.equals(MUTE)) {
@ -28,19 +30,27 @@ public class GlobalConfig {
return false;
}
public static void setDumpPath(String path) {
dumpPath = path;
}
public static String getDumpPath() {
return dumpPath;
}
public static void setDumpPath(String path) {
dumpPath = path;
}
public static String getPkgPrefix() {
return pkgPrefix;
}
public static void setPkgPrefix(String pkgPrefix) {
GlobalConfig.pkgPrefix = pkgPrefix;
public static void setPkgPrefix(String prefix) {
pkgPrefix = prefix;
}
public static MockScope getDefaultMockScope() {
return defaultMockScope;
}
public static void setDefaultMockScope(MockScope scope) {
defaultMockScope = scope;
}
}

View File

@ -18,6 +18,6 @@ public @interface MockConstructor {
* specify the effective scope of the mock method
* @return global or associated
*/
MockScope scope() default MockScope.ASSOCIATED;
MockScope scope() default MockScope.GLOBAL;
}

View File

@ -31,6 +31,6 @@ public @interface MockMethod {
* specify the effective scope of the mock method
* @return global or associated
*/
MockScope scope() default MockScope.ASSOCIATED;
MockScope scope() default MockScope.GLOBAL;
}

View File

@ -10,6 +10,13 @@ public enum MockScope {
/**
* Mock method available for any test cases
*/
GLOBAL
GLOBAL;
public static MockScope of(String scope) {
try {
return valueOf(scope);
} catch (IllegalArgumentException e) {
return GLOBAL;
}
}
}