mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-09 20:00:21 +08:00
fix test case mark and add unnullable map
This commit is contained in:
parent
affa56d057
commit
19f28c32db
@ -1,6 +1,8 @@
|
||||
package com.alibaba.testable.agent.handler;
|
||||
|
||||
import com.alibaba.testable.agent.util.ClassUtil;
|
||||
import com.alibaba.testable.core.util.InvokeRecordUtil;
|
||||
import com.alibaba.testable.core.util.TestableUtil;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
/**
|
||||
@ -25,6 +27,8 @@ abstract public class BaseClassWithContextHandler extends BaseClassHandler {
|
||||
private static final String CLASS_MAP = "java/util/Map";
|
||||
private static final String METHOD_MAP_GET = "get";
|
||||
private static final String SIGNATURE_MAP_GET = "(Ljava/lang/Object;)Ljava/lang/Object;";
|
||||
private static final String CLASS_BASE_CLASS_WITH_CONTEXT_HANDLER
|
||||
= "com/alibaba/testable/agent/handler/BaseClassWithContextHandler";
|
||||
|
||||
protected AbstractInsnNode[] handleTestableUtil(ClassNode cn, MethodNode mn, AbstractInsnNode[] instructions, int i) {
|
||||
if (instructions[i].getOpcode() == GETSTATIC) {
|
||||
@ -54,8 +58,7 @@ abstract public class BaseClassWithContextHandler extends BaseClassHandler {
|
||||
SIGNATURE_CURRENT_SOURCE_METHOD_NAME, false));
|
||||
} else if (FIELD_MOCK_CONTEXT.equals(fieldName)) {
|
||||
il.add(new FieldInsnNode(GETSTATIC, CLASS_MOCK_CONTEXT, FIELD_PARAMETERS, SIGNATURE_PARAMETERS));
|
||||
il.add(new VarInsnNode(ALOAD, 0));
|
||||
il.add(new MethodInsnNode(INVOKESPECIAL, cn.name, METHOD_GET_TEST_CASE_MARK,
|
||||
il.add(new MethodInsnNode(INVOKESTATIC, CLASS_BASE_CLASS_WITH_CONTEXT_HANDLER, METHOD_GET_TEST_CASE_MARK,
|
||||
SIGNATURE_GET_TEST_CASE_MARK, false));
|
||||
il.add(new MethodInsnNode(INVOKEINTERFACE, CLASS_MAP, METHOD_MAP_GET,
|
||||
SIGNATURE_MAP_GET, true));
|
||||
@ -67,10 +70,12 @@ abstract public class BaseClassWithContextHandler extends BaseClassHandler {
|
||||
return mn.instructions.toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* get mark compose by TestClass and TestCase
|
||||
* @return test case mark
|
||||
*/
|
||||
abstract protected String getTestCaseMark();
|
||||
public static String getTestCaseMark() {
|
||||
String clazz = Thread.currentThread().getStackTrace()[InvokeRecordUtil.INDEX_OF_TEST_CLASS].getClassName();
|
||||
// TODO: temporary used
|
||||
String testClass = clazz.endsWith("Mock") ? clazz.substring(0, clazz.length() - 5) : clazz;
|
||||
String testCaseName = TestableUtil.currentTestCaseName(testClass);
|
||||
return testClass + "::" + testCaseName;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -42,15 +42,6 @@ public class MockClassHandler extends BaseClassWithContextHandler {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestCaseMark() {
|
||||
String mockClass = Thread.currentThread().getStackTrace()[InvokeRecordUtil.INDEX_OF_TEST_CLASS].getClassName();
|
||||
// TODO: temporary used
|
||||
String testClass = mockClass.substring(0, mockClass.length() - 5);
|
||||
String testCaseName = TestableUtil.currentTestCaseName(testClass);
|
||||
return testClass + "::" + testCaseName;
|
||||
}
|
||||
|
||||
private void handleInstruction(ClassNode cn, MethodNode mn) {
|
||||
AbstractInsnNode[] instructions = mn.instructions.toArray();
|
||||
for (int i = 0; i < instructions.length; i++) {
|
||||
|
@ -26,13 +26,6 @@ public class TestClassHandler extends BaseClassWithContextHandler {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestCaseMark() {
|
||||
String testClass = Thread.currentThread().getStackTrace()[InvokeRecordUtil.INDEX_OF_TEST_CLASS].getClassName();
|
||||
String testCaseName = TestableUtil.currentTestCaseName(testClass);
|
||||
return testClass + "::" + testCaseName;
|
||||
}
|
||||
|
||||
private void handleInstruction(ClassNode cn, MethodNode mn) {
|
||||
AbstractInsnNode[] instructions = mn.instructions.toArray();
|
||||
for (int i = 0; i < instructions.length; i++) {
|
||||
|
@ -11,6 +11,6 @@ public class MockContext {
|
||||
/**
|
||||
* Mock method name → ( key → value )
|
||||
*/
|
||||
public static final Map<String, Map<String, Object>> parameters = new HashMap<String, Map<String, Object>>();
|
||||
public static final Map<String, HashMap<String, Object>> parameters = UnnullableMap.of(new HashMap<String, Object>());
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package com.alibaba.testable.agent.model;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author flin
|
||||
*/
|
||||
public class UnnullableMap<K, V> extends HashMap<K, V> {
|
||||
|
||||
V defaultValue;
|
||||
|
||||
private UnnullableMap(V defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public static <K, V> UnnullableMap<K, V> of(V defaultValue) {
|
||||
return new UnnullableMap<K, V>(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
V value = super.get(key);
|
||||
if (value == null) {
|
||||
if (defaultValue instanceof Cloneable) {
|
||||
value = PrivateAccessor.invoke(defaultValue, "clone");
|
||||
} else {
|
||||
value = defaultValue;
|
||||
}
|
||||
super.put((K)key, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
@ -30,7 +30,9 @@ public class InvokeRecordUtil {
|
||||
public static void recordMockInvoke(Object[] args, boolean isConstructor, boolean isTargetClassInParameter) {
|
||||
StackTraceElement mockMethodTraceElement = Thread.currentThread().getStackTrace()[INDEX_OF_TEST_CLASS];
|
||||
String mockMethodName = mockMethodTraceElement.getMethodName();
|
||||
String testClass = mockMethodTraceElement.getClassName();
|
||||
String mockClass = mockMethodTraceElement.getClassName();
|
||||
// TODO: temporary used
|
||||
String testClass = mockClass.substring(0, mockClass.length() - 5);
|
||||
String testCaseName = TestableUtil.currentTestCaseName(testClass);
|
||||
String identify = getInvokeIdentify(mockMethodName, testClass, testCaseName);
|
||||
List<Object[]> records = getInvokeRecord(identify);
|
||||
|
Loading…
Reference in New Issue
Block a user