From 6237e19a32628e8c43de93c1bdc952b09723d4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 20 Feb 2021 12:48:12 +0800 Subject: [PATCH] support modify default mock scope --- .../com/alibaba/testable/agent/PreMain.java | 4 ++++ .../agent/handler/MockClassHandler.java | 3 ++- .../testable/agent/util/GlobalConfig.java | 22 ++++++++++++++----- .../core/annotation/MockConstructor.java | 2 +- .../testable/core/annotation/MockMethod.java | 2 +- .../testable/core/model/MockScope.java | 9 +++++++- 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java index 38a690d..a8c171f 100755 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java @@ -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 diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java index 160cd1e..9f70d25 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java @@ -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; } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java b/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java index 3bb6ef6..654345d 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java @@ -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; } } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockConstructor.java b/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockConstructor.java index ee3d448..08526c4 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockConstructor.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockConstructor.java @@ -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; } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockMethod.java b/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockMethod.java index 824335a..571214c 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockMethod.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockMethod.java @@ -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; } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/model/MockScope.java b/testable-core/src/main/java/com/alibaba/testable/core/model/MockScope.java index 4cb5982..dad5456 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/model/MockScope.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/model/MockScope.java @@ -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; + } + } }