mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-02-04 08:40:20 +08:00
refactor invoke record logic to its own util
This commit is contained in:
parent
0b9a9c7ac6
commit
24e4b4f317
@ -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_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_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_TEST_CASE = "TEST_CASE";
|
||||||
private static final String FIELD_SOURCE_METHOD = "SOURCE_METHOD";
|
private static final String FIELD_SOURCE_METHOD = "SOURCE_METHOD";
|
||||||
private static final String METHOD_CURRENT_TEST_CASE_NAME = "currentTestCaseName";
|
private static final String METHOD_CURRENT_TEST_CASE_NAME = "currentTestCaseName";
|
||||||
@ -95,7 +96,7 @@ public class TestClassHandler extends BaseClassHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void injectInvokeCounter(MethodNode mn) {
|
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);
|
SIGNATURE_INVOKE_COUNTER_METHOD, false);
|
||||||
mn.instructions.insertBefore(mn.instructions.get(0), node);
|
mn.instructions.insertBefore(mn.instructions.get(0), node);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.alibaba.testable.core.tool;
|
package com.alibaba.testable.core.tool;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.util.InvokeRecordUtil;
|
||||||
import com.alibaba.testable.core.util.TestableUtil;
|
import com.alibaba.testable.core.util.TestableUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,9 +28,9 @@ public class TestableTool {
|
|||||||
* @param mockMethodName name of a mock method
|
* @param mockMethodName name of a mock method
|
||||||
*/
|
*/
|
||||||
public static InvokeCounter verify(String mockMethodName) {
|
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);
|
String testCaseName = TestableUtil.currentTestCaseName(testClass);
|
||||||
return new InvokeCounter(TestableUtil.getInvokeCount(mockMethodName, testCaseName));
|
return new InvokeCounter(InvokeRecordUtil.getInvokeCount(mockMethodName, testCaseName));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,34 +2,12 @@ package com.alibaba.testable.core.util;
|
|||||||
|
|
||||||
import com.alibaba.testable.core.constant.ConstPool;
|
import com.alibaba.testable.core.constant.ConstPool;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author flin
|
* @author flin
|
||||||
*/
|
*/
|
||||||
public class TestableUtil {
|
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
|
* Get the last visit method in source file
|
||||||
* @param testClassRef usually `this` variable of the test class
|
* @param testClassRef usually `this` variable of the test class
|
||||||
@ -72,15 +50,6 @@ public class TestableUtil {
|
|||||||
return "";
|
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) {
|
private static String findLastMethodFromSourceClass(String sourceClassName, StackTraceElement[] stack) {
|
||||||
for (StackTraceElement element : stack) {
|
for (StackTraceElement element : stack) {
|
||||||
if (element.getClassName().equals(sourceClassName)) {
|
if (element.getClassName().equals(sourceClassName)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user