From 24e4b4f317733b8da70f0ca076ebb1c0a8861784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Fri, 30 Oct 2020 21:39:02 +0800 Subject: [PATCH] refactor invoke record logic to its own util --- .../agent/handler/TestClassHandler.java | 3 +- .../testable/core/tool/TestableTool.java | 5 ++- .../testable/core/util/InvokeRecordUtil.java | 41 +++++++++++++++++++ .../testable/core/util/TestableUtil.java | 31 -------------- 4 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java index 5332de9..0dc55a0 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java @@ -16,6 +16,7 @@ public class TestClassHandler extends BaseClassHandler { private static final String CLASS_TESTABLE_TOOL = "com/alibaba/testable/core/tool/TestableTool"; private static final String CLASS_TESTABLE_UTIL = "com/alibaba/testable/core/util/TestableUtil"; + private static final String CLASS_INVOKE_RECORD_UTIL = "com/alibaba/testable/core/util/InvokeRecordUtil"; private static final String FIELD_TEST_CASE = "TEST_CASE"; private static final String FIELD_SOURCE_METHOD = "SOURCE_METHOD"; private static final String METHOD_CURRENT_TEST_CASE_NAME = "currentTestCaseName"; @@ -95,7 +96,7 @@ public class TestClassHandler extends BaseClassHandler { } private void injectInvokeCounter(MethodNode mn) { - MethodInsnNode node = new MethodInsnNode(INVOKESTATIC, CLASS_TESTABLE_UTIL, METHOD_COUNT_MOCK_INVOKE, + MethodInsnNode node = new MethodInsnNode(INVOKESTATIC, CLASS_INVOKE_RECORD_UTIL, METHOD_COUNT_MOCK_INVOKE, SIGNATURE_INVOKE_COUNTER_METHOD, false); mn.instructions.insertBefore(mn.instructions.get(0), node); } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java index 5d8d6b7..8e46bf9 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java @@ -1,5 +1,6 @@ package com.alibaba.testable.core.tool; +import com.alibaba.testable.core.util.InvokeRecordUtil; import com.alibaba.testable.core.util.TestableUtil; /** @@ -27,9 +28,9 @@ public class TestableTool { * @param mockMethodName name of a mock method */ public static InvokeCounter verify(String mockMethodName) { - String testClass = Thread.currentThread().getStackTrace()[TestableUtil.INDEX_OF_TEST_CLASS].getClassName(); + String testClass = Thread.currentThread().getStackTrace()[InvokeRecordUtil.INDEX_OF_TEST_CLASS].getClassName(); String testCaseName = TestableUtil.currentTestCaseName(testClass); - return new InvokeCounter(TestableUtil.getInvokeCount(mockMethodName, testCaseName)); + return new InvokeCounter(InvokeRecordUtil.getInvokeCount(mockMethodName, testCaseName)); } } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java new file mode 100644 index 0000000..38e4e81 --- /dev/null +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java @@ -0,0 +1,41 @@ +package com.alibaba.testable.core.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author flin + */ +public class InvokeRecordUtil { + + private static final Map INVOKE_RECORDS = new HashMap(); + private final static String JOINER = "->"; + + /** + * [0]Thread -> [1]TestableUtil/TestableTool -> [2]TestClass + */ + public static final int INDEX_OF_TEST_CLASS = 2; + + /** + * Record mock method invoke event + */ + public static void countMockInvoke() { + StackTraceElement mockMethodTraceElement = Thread.currentThread().getStackTrace()[INDEX_OF_TEST_CLASS]; + String mockMethodName = mockMethodTraceElement.getMethodName(); + String testClass = mockMethodTraceElement.getClassName(); + String testCaseName = TestableUtil.currentTestCaseName(testClass); + String key = testCaseName + JOINER + mockMethodName; + int count = getInvokeCount(mockMethodName, testCaseName); + INVOKE_RECORDS.put(key, count + 1); + } + + public static int getInvokeCount(String mockMethodName, String testCaseName) { + String key = testCaseName + JOINER + mockMethodName; + Integer count = INVOKE_RECORDS.get(key); + if (count == null) { + count = 0; + } + return count; + } + +} diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java index 73a21a1..4142451 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java @@ -2,34 +2,12 @@ package com.alibaba.testable.core.util; import com.alibaba.testable.core.constant.ConstPool; -import java.util.HashMap; -import java.util.Map; /** * @author flin */ public class TestableUtil { - private static final Map INVOKE_RECORDS = new HashMap(); - private final static String JOINER = "->"; - /** - * [0]Thread -> [1]TestableUtil/TestableTool -> [2]TestClass - */ - public static final int INDEX_OF_TEST_CLASS = 2; - - /** - * Record mock method invoke event - */ - public static void countMockInvoke() { - StackTraceElement mockMethodTraceElement = Thread.currentThread().getStackTrace()[INDEX_OF_TEST_CLASS]; - String mockMethodName = mockMethodTraceElement.getMethodName(); - String testClass = mockMethodTraceElement.getClassName(); - String testCaseName = TestableUtil.currentTestCaseName(testClass); - String key = testCaseName + JOINER + mockMethodName; - int count = getInvokeCount(mockMethodName, testCaseName); - INVOKE_RECORDS.put(key, count + 1); - } - /** * Get the last visit method in source file * @param testClassRef usually `this` variable of the test class @@ -72,15 +50,6 @@ public class TestableUtil { return ""; } - public static int getInvokeCount(String mockMethodName, String testCaseName) { - String key = testCaseName + JOINER + mockMethodName; - Integer count = INVOKE_RECORDS.get(key); - if (count == null) { - count = 0; - } - return count; - } - private static String findLastMethodFromSourceClass(String sourceClassName, StackTraceElement[] stack) { for (StackTraceElement element : stack) { if (element.getClassName().equals(sourceClassName)) {