remove old mock context holder

This commit is contained in:
金戟 2021-02-13 17:47:19 +08:00
parent 374a4d5442
commit 8814276d63
4 changed files with 21 additions and 67 deletions

View File

@ -1,6 +1,5 @@
package com.alibaba.testable.agent.handler;
import com.alibaba.testable.core.util.MockContextUtil;
import org.objectweb.asm.tree.*;
/**
@ -10,29 +9,26 @@ abstract public class BaseClassWithContextHandler 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_MOCK_CONTEXT_HOLDER = "com/alibaba/testable/agent/model/MockContextHolder";
private static final String CLASS_MOCK_CONTEXT_UTIL = "com/alibaba/testable/core/util/MockContextUtil";
private static final String FIELD_SOURCE_METHOD = "SOURCE_METHOD";
private static final String FIELD_MOCK_CONTEXT = "MOCK_CONTEXT";
private static final String FIELD_PARAMETERS = "parameters";
private static final String METHOD_PARAMETERS = "parameters";
private static final String METHOD_CURRENT_SOURCE_METHOD_NAME = "currentSourceMethodName";
private static final String METHOD_GET_TEST_CASE_MARK = "getTestCaseMark";
private static final String SIGNATURE_CURRENT_SOURCE_METHOD_NAME = "()Ljava/lang/String;";
private static final String SIGNATURE_GET_TEST_CASE_MARK = "()Ljava/lang/String;";
private static final String SIGNATURE_PARAMETERS = "Ljava/util/Map;";
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";
private static final String SIGNATURE_PARAMETERS = "()Ljava/util/Map;";
protected AbstractInsnNode[] handleTestableUtil(ClassNode cn, MethodNode mn, AbstractInsnNode[] instructions, int i) {
protected void handleTestableUtil(MethodNode mn) {
AbstractInsnNode[] instructions = mn.instructions.toArray();
// Note: instructions.length will change when instructions updated
for (int i = 0; i < instructions.length; i++) {
if (instructions[i].getOpcode() == GETSTATIC) {
FieldInsnNode fieldInsnNode = (FieldInsnNode)instructions[i];
if (isTestableUtilField(fieldInsnNode)) {
instructions = replaceTestableUtilField(cn, mn, instructions, fieldInsnNode.name, i);
replaceTestableUtilField(mn, instructions, fieldInsnNode.name, i);
instructions = mn.instructions.toArray();
}
}
}
return instructions;
}
private boolean isTestableUtilField(FieldInsnNode fieldInsnNode) {
@ -40,30 +36,19 @@ abstract public class BaseClassWithContextHandler extends BaseClassHandler {
(fieldInsnNode.name.equals(FIELD_SOURCE_METHOD) || fieldInsnNode.name.equals(FIELD_MOCK_CONTEXT));
}
private AbstractInsnNode[] replaceTestableUtilField(ClassNode cn, MethodNode mn, AbstractInsnNode[] instructions,
String fieldName, int pos) {
private void replaceTestableUtilField(MethodNode mn, AbstractInsnNode[] instructions, String fieldName, int pos) {
InsnList il = new InsnList();
if (FIELD_SOURCE_METHOD.equals(fieldName)) {
il.add(new MethodInsnNode(INVOKESTATIC, CLASS_TESTABLE_UTIL, METHOD_CURRENT_SOURCE_METHOD_NAME,
SIGNATURE_CURRENT_SOURCE_METHOD_NAME, false));
} else if (FIELD_MOCK_CONTEXT.equals(fieldName)) {
il.add(new FieldInsnNode(GETSTATIC, CLASS_MOCK_CONTEXT_HOLDER, FIELD_PARAMETERS, SIGNATURE_PARAMETERS));
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));
il.add(new MethodInsnNode(INVOKESTATIC, CLASS_MOCK_CONTEXT_UTIL, METHOD_PARAMETERS,
SIGNATURE_PARAMETERS, false));
}
if (il.size() > 0) {
mn.instructions.insert(instructions[pos], il);
mn.instructions.remove(instructions[pos]);
}
return mn.instructions.toArray();
}
public static String getTestCaseMark() {
String testClass = MockContextUtil.context.get().testClassName;
String testCaseName = MockContextUtil.context.get().testCaseName;
return testClass + "::" + testCaseName;
}
}

View File

@ -35,18 +35,11 @@ public class MockClassHandler extends BaseClassWithContextHandler {
mn.access &= ~ACC_PROTECTED;
mn.access |= ACC_PUBLIC;
injectInvokeRecorder(mn);
handleInstruction(cn, mn);
handleTestableUtil(mn);
}
}
}
private void handleInstruction(ClassNode cn, MethodNode mn) {
AbstractInsnNode[] instructions = mn.instructions.toArray();
for (int i = 0; i < instructions.length; i++) {
instructions = handleTestableUtil(cn, mn, instructions, i);
}
}
private void addGetInstanceMethod(ClassNode cn) {
MethodNode getInstanceMethod = new MethodNode(ACC_PUBLIC | ACC_STATIC, GET_TESTABLE_REF,
VOID_ARGS + ClassUtil.toByteCodeClassName(mockClassName), null, null);

View File

@ -18,7 +18,9 @@ public class TestClassHandler extends BaseClassWithContextHandler {
// JUnit 4
"org.junit.Test",
// JUnit 5
"org.junit.jupiter.api.Test"
"org.junit.jupiter.api.Test",
// TestNG
"org.testng.annotations.Test"
);
public TestClassHandler(String mockClassName) {
@ -32,7 +34,7 @@ public class TestClassHandler extends BaseClassWithContextHandler {
@Override
protected void transform(ClassNode cn) {
for (MethodNode mn : cn.methods) {
handleInstruction(cn, mn);
handleTestableUtil(mn);
handleTestCaseMethod(cn, mn);
}
}
@ -56,12 +58,4 @@ public class TestClassHandler extends BaseClassWithContextHandler {
mn.instructions.insertBefore(mn.instructions.getFirst(), il);
}
private void handleInstruction(ClassNode cn, MethodNode mn) {
AbstractInsnNode[] instructions = mn.instructions.toArray();
// Note: instructions.length will change when instructions updated
for (int i = 0; i < instructions.length; i++) {
instructions = handleTestableUtil(cn, mn, instructions, i);
}
}
}

View File

@ -1,18 +0,0 @@
package com.alibaba.testable.agent.model;
import com.alibaba.testable.core.util.UnnullableMap;
import java.util.HashMap;
import java.util.Map;
/**
* @author flin
*/
public class MockContextHolder {
/**
* Mock method name ( key value )
*/
public static final Map<String, HashMap<String, Object>> parameters = UnnullableMap.of(new HashMap<String, Object>());
}