From ea4c6c5b3e68175c0ec893904ce189f3048b1af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sun, 16 May 2021 23:16:51 +0800 Subject: [PATCH] allow disable mock target checking --- .../src/test/resources/testable.properties | 1 + .../testable/agent/config/PropertiesParser.java | 5 ++++- .../testable/agent/transformer/MockClassParser.java | 13 +++++++------ .../alibaba/testable/agent/util/GlobalConfig.java | 1 + 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/demo/java-demo/src/test/resources/testable.properties b/demo/java-demo/src/test/resources/testable.properties index afe9e97..638a7d3 100644 --- a/demo/java-demo/src/test/resources/testable.properties +++ b/demo/java-demo/src/test/resources/testable.properties @@ -1 +1,2 @@ omni.constructor.enhance.enable = true +mock.target.checking.enable = true diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java b/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java index fb204ee..4bcf95b 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/config/PropertiesParser.java @@ -22,10 +22,11 @@ public class PropertiesParser { 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 ENABLE_MOCK_INJECT = "mock.enhance.enable"; 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_MOCK_TARGET_CHECK = "mock.target.checking.enable"; private static final String ENABLE_OMNI_INJECT = "omni.constructor.enhance.enable"; private static final String ENABLE_THREAD_POOL = "thread.pool.enhance.enable"; @@ -74,6 +75,8 @@ public class PropertiesParser { GlobalConfig.enhanceOmniConstructor = Boolean.parseBoolean(v); } else if (k.equals(ENABLE_MOCK_INJECT)) { GlobalConfig.enhanceMock = Boolean.parseBoolean(v); + } else if (k.equals(ENABLE_MOCK_TARGET_CHECK)) { + GlobalConfig.checkMockTargetExistence = Boolean.parseBoolean(v); } else if (k.equals(ENABLE_THREAD_POOL)) { GlobalConfig.enhanceThreadLocal = Boolean.parseBoolean(v); } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java index 993cd1b..62850eb 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java @@ -3,10 +3,7 @@ package com.alibaba.testable.agent.transformer; import com.alibaba.testable.agent.constant.ConstPool; import com.alibaba.testable.agent.model.MethodInfo; 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.DiagnoseUtil; -import com.alibaba.testable.agent.util.MethodUtil; +import com.alibaba.testable.agent.util.*; import com.alibaba.testable.core.exception.TargetNotExistException; import com.alibaba.testable.core.util.LogUtil; import org.objectweb.asm.Type; @@ -85,11 +82,15 @@ public class MockClassParser { for (AnnotationNode an : mn.visibleAnnotations) { String fullClassName = toJavaStyleClassName(an.desc); if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) { - checkTargetConstructorExists(mn); + if (GlobalConfig.checkMockTargetExistence) { + checkTargetConstructorExists(mn); + } methodInfos.add(new MethodInfo(ClassUtil.getSourceClassName(cn.name), CONSTRUCTOR, mn.desc, cn.name, mn.name, mn.desc, isStatic(mn))); } else if (fullClassName.equals(ConstPool.MOCK_METHOD) && isValidMockMethod(mn, an)) { - checkTargetMethodExists(mn, an); + if (GlobalConfig.checkMockTargetExistence) { + checkTargetMethodExists(mn, an); + } String targetMethod = AnnotationUtil.getAnnotationParameter( an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class); MethodInfo mi = getMethodInfo(cn, mn, an, targetMethod); 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 357db6c..fff125e 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 @@ -38,6 +38,7 @@ public class GlobalConfig { public static boolean enhanceMock = true; public static boolean enhanceOmniConstructor = false; public static boolean enhanceThreadLocal = false; + public static boolean checkMockTargetExistence = false; public static String innerMockClassName = "Mock"; public static void setLogLevel(String level) {