refactor invoke record logic to its own util

This commit is contained in:
金戟 2020-10-30 21:39:02 +08:00
parent 0b9a9c7ac6
commit 24e4b4f317
4 changed files with 46 additions and 34 deletions

View File

@ -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);
}

View File

@ -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));
}
}

View File

@ -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<String, Integer> INVOKE_RECORDS = new HashMap<String, Integer>();
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;
}
}

View File

@ -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<String, Integer> INVOKE_RECORDS = new HashMap<String, Integer>();
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)) {