From efccedc99611690e557baf069d3f0295e3e06689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Mon, 12 Oct 2020 19:01:01 +0800 Subject: [PATCH] refactor and remove unit test framework dependence --- .../testable/agent/constant/ConstPool.java | 1 + .../agent/handler/SourceClassHandler.java | 5 ++--- .../agent/handler/TestClassHandler.java | 18 +++++------------- .../alibaba/testable/agent/util/ClassUtil.java | 8 ++++++-- .../processor/EnableTestableProcessor.java | 8 ++++---- .../testable/core/util/TestableUtil.java | 6 +++--- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java b/agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java index e7146e8..4f3a7ad 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java +++ b/agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java @@ -8,6 +8,7 @@ public class ConstPool { public static final String DOT = "."; public static final String SLASH = "/"; + public static final String CONSTRUCTOR = ""; public static final String TEST_POSTFIX = "Test"; public static final String TESTABLE_INJECT_REF = "_testableInternalRef"; diff --git a/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index 29fe953..bdf9ac2 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -16,7 +16,6 @@ import java.util.Set; */ public class SourceClassHandler extends BaseClassHandler { - private static final String CONSTRUCTOR = ""; private final List injectMethods; public SourceClassHandler(List injectMethods) { @@ -27,7 +26,7 @@ public class SourceClassHandler extends BaseClassHandler { protected void transform(ClassNode cn) { List methods = new ArrayList(); 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); diff --git a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java index 211894f..67a49d1 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java @@ -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 TEST_ANNOTATIONS = new ArrayList(); - - 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 ; + } + } diff --git a/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java b/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java index 54d1df0..dca36a6 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java +++ b/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java @@ -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; + } + } diff --git a/core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java b/core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java index f927f42..61ebad9 100644 --- a/core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java +++ b/core/src/main/java/com/alibaba/testable/core/processor/EnableTestableProcessor.java @@ -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); diff --git a/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java b/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java index ece39d9..45f3d6b 100644 --- a/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java +++ b/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java @@ -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());