mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
refactor and remove unit test framework dependence
This commit is contained in:
parent
78d3b2ea90
commit
efccedc996
@ -8,6 +8,7 @@ public class ConstPool {
|
||||
public static final String DOT = ".";
|
||||
public static final String SLASH = "/";
|
||||
|
||||
public static final String CONSTRUCTOR = "<init>";
|
||||
public static final String TEST_POSTFIX = "Test";
|
||||
public static final String TESTABLE_INJECT_REF = "_testableInternalRef";
|
||||
|
||||
|
@ -16,7 +16,6 @@ import java.util.Set;
|
||||
*/
|
||||
public class SourceClassHandler extends BaseClassHandler {
|
||||
|
||||
private static final String CONSTRUCTOR = "<init>";
|
||||
private final List<MethodInfo> injectMethods;
|
||||
|
||||
public SourceClassHandler(List<MethodInfo> injectMethods) {
|
||||
@ -27,7 +26,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
||||
protected void transform(ClassNode cn) {
|
||||
List<MethodInfo> methods = new ArrayList<MethodInfo>();
|
||||
for (MethodNode m : cn.methods) {
|
||||
if (!CONSTRUCTOR.equals(m.name)) {
|
||||
if (!ConstPool.CONSTRUCTOR.equals(m.name)) {
|
||||
methods.add(new MethodInfo(m.name, m.desc));
|
||||
}
|
||||
}
|
||||
@ -51,7 +50,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
||||
instructions = replaceMemberCallOps(cn, mn, instructions, rangeStart, i);
|
||||
i = rangeStart;
|
||||
}
|
||||
} else if (CONSTRUCTOR.equals(node.name)) {
|
||||
} else if (ConstPool.CONSTRUCTOR.equals(node.name)) {
|
||||
String newOperatorInjectMethodName = getNewOperatorInjectMethodName(newOperatorInjectMethods, node);
|
||||
if (newOperatorInjectMethodName.length() > 0) {
|
||||
int rangeStart = getConstructorStart(instructions, node.owner, i);
|
||||
|
@ -2,7 +2,6 @@ package com.alibaba.testable.agent.handler;
|
||||
|
||||
import com.alibaba.testable.agent.constant.ConstPool;
|
||||
import com.alibaba.testable.agent.util.ClassUtil;
|
||||
import com.alibaba.testable.agent.util.CollectionUtil;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -13,17 +12,6 @@ import java.util.List;
|
||||
*/
|
||||
public class TestClassHandler extends BaseClassHandler {
|
||||
|
||||
private static final List<String> TEST_ANNOTATIONS = new ArrayList<String>();
|
||||
|
||||
static {
|
||||
// JUnit4
|
||||
TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.junit.Test"));
|
||||
// JUnit5
|
||||
TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.junit.jupiter.api.Test"));
|
||||
// TestNG
|
||||
TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.testng.annotations.Test"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void transform(ClassNode cn) {
|
||||
for (MethodNode m : cn.methods) {
|
||||
@ -39,7 +27,7 @@ public class TestClassHandler extends BaseClassHandler {
|
||||
for (AnnotationNode n : mn.visibleAnnotations) {
|
||||
visibleAnnotationNames.add(n.desc);
|
||||
}
|
||||
if (CollectionUtil.containsAny(visibleAnnotationNames, TEST_ANNOTATIONS)) {
|
||||
if (!visibleAnnotationNames.contains(ConstPool.TESTABLE_INJECT) && couldBeTestMethod(mn)) {
|
||||
InsnList il = new InsnList();
|
||||
il.add(new VarInsnNode(ALOAD, 0));
|
||||
il.add(new FieldInsnNode(PUTSTATIC, cn.name, ConstPool.TESTABLE_INJECT_REF, ClassUtil.toByteCodeClassName(cn.name)));
|
||||
@ -47,4 +35,8 @@ public class TestClassHandler extends BaseClassHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean couldBeTestMethod(MethodNode mn) {
|
||||
return (mn.access & (ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC)) == 0 ;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -82,8 +82,7 @@ public class ClassUtil {
|
||||
travelingClass = false;
|
||||
}
|
||||
} else {
|
||||
if (b == TYPE_BYTE || b == TYPE_CHAR || b == TYPE_DOUBLE || b == TYPE_FLOAT
|
||||
|| b == TYPE_INT || b == TYPE_LONG || b == TYPE_SHORT || b == TYPE_BOOL) {
|
||||
if (isPrimaryType(b)) {
|
||||
parameterTypes.add(b);
|
||||
} else if (b == TYPE_CLASS) {
|
||||
travelingClass = true;
|
||||
@ -133,4 +132,9 @@ public class ClassUtil {
|
||||
return className.replace(ConstPool.SLASH, ConstPool.DOT).substring(1, className.length() - 1);
|
||||
}
|
||||
|
||||
private static boolean isPrimaryType(byte b) {
|
||||
return b == TYPE_BYTE || b == TYPE_CHAR || b == TYPE_DOUBLE || b == TYPE_FLOAT
|
||||
|| b == TYPE_INT || b == TYPE_LONG || b == TYPE_SHORT || b == TYPE_BOOL;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -69,10 +69,10 @@ public class EnableTestableProcessor extends BaseProcessor {
|
||||
private void writeBinaryFile(String path, String fileName, byte[] content) {
|
||||
try {
|
||||
FileObject resource = cx.filter.createResource(StandardLocation.SOURCE_OUTPUT, path, fileName);
|
||||
OutputStream out = resource.openOutputStream();
|
||||
out.write(content);
|
||||
out.flush();
|
||||
out.close();
|
||||
try (OutputStream out = resource.openOutputStream()) {
|
||||
out.write(content);
|
||||
out.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
cx.logger.error("Failed to write " + fileName);
|
||||
|
@ -7,11 +7,11 @@ import com.alibaba.testable.core.constant.ConstPool;
|
||||
*/
|
||||
public class TestableUtil {
|
||||
|
||||
public static String currentMemberMethodName(Object testClassRef) {
|
||||
return currentMemberMethodName(testClassRef.getClass());
|
||||
public static String sourceMemberMethodName(Object testClassRef) {
|
||||
return sourceMemberMethodName(testClassRef.getClass());
|
||||
}
|
||||
|
||||
public static String currentMemberMethodName(Class<?> testClass) {
|
||||
public static String sourceMemberMethodName(Class<?> testClass) {
|
||||
StackTraceElement[] stack = getMainThread().getStackTrace();
|
||||
String testClassName = getRealClassName(testClass);
|
||||
String sourceClassName = testClassName.substring(0, testClassName.length() - ConstPool.TEST_POSTFIX.length());
|
||||
|
Loading…
Reference in New Issue
Block a user