From a814e51f66845a552a4f6d19ab0c650a4fcebc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sun, 26 Jul 2020 20:20:32 +0800 Subject: [PATCH] implement util function to get current member method and test case --- .../testable/agent/constant/ConstPool.java | 2 + .../transformer/TestableClassTransformer.java | 4 +- .../processor/EnableTestableProcessor.java | 5 ++- .../alibaba/testable/core/util/ConstPool.java | 1 + .../testable/core/util/TestableUtil.java | 40 +++++++++++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java 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 ba9dcc6..b1d2755 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 @@ -11,6 +11,8 @@ public class ConstPool { public static final String DOT = "."; public static final String SLASH = "/"; + public static final String TEST_POSTFIX = "Test"; + public static final List SYS_CLASSES = new ArrayList(); static { SYS_CLASSES.add("java/lang/StringBuilder"); diff --git a/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index dfbc678..131ae05 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -1,5 +1,6 @@ package com.alibaba.testable.agent.transformer; +import com.alibaba.testable.agent.constant.ConstPool; import com.alibaba.testable.agent.handler.SourceClassHandler; import com.alibaba.testable.agent.handler.TestClassHandler; import com.alibaba.testable.agent.util.ClassUtil; @@ -19,7 +20,6 @@ public class TestableClassTransformer implements ClassFileTransformer { private static final String ENABLE_TESTABLE = "com.alibaba.testable.core.annotation.EnableTestable"; private static final String ENABLE_TESTABLE_INJECT = "com.alibaba.testable.core.annotation.EnableTestableInject"; - private static final String TEST_POSTFIX = "Test"; private static final Set loadedClassNames = new HashSet(); @@ -31,7 +31,7 @@ public class TestableClassTransformer implements ClassFileTransformer { } List annotations = ClassUtil.getAnnotations(className); - List testAnnotations = ClassUtil.getAnnotations(className + TEST_POSTFIX); + List testAnnotations = ClassUtil.getAnnotations(className + ConstPool.TEST_POSTFIX); try { if (annotations.contains(ENABLE_TESTABLE_INJECT) || testAnnotations.contains(ENABLE_TESTABLE)) { loadedClassNames.add(className); 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 9b12587..62e074e 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 @@ -2,6 +2,7 @@ package com.alibaba.testable.core.processor; import com.alibaba.testable.core.annotation.EnableTestable; import com.alibaba.testable.core.translator.EnableTestableTranslator; +import com.alibaba.testable.core.util.ConstPool; import com.alibaba.testable.core.util.ResourceUtil; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.tree.JCTree; @@ -42,7 +43,7 @@ public class EnableTestableProcessor extends BaseProcessor { } private boolean isTestClass(Name name) { - return name.toString().endsWith("Test"); + return name.toString().endsWith(ConstPool.TEST_POSTFIX); } private void processClassElement(Symbol.ClassSymbol clazz) { @@ -56,7 +57,7 @@ public class EnableTestableProcessor extends BaseProcessor { private String getOriginClassName(Symbol.ClassSymbol clazz) { String testClassName = clazz.getSimpleName().toString(); - return testClassName.substring(0, testClassName.length() - "Test".length()); + return testClassName.substring(0, testClassName.length() - ConstPool.TEST_POSTFIX.length()); } private void createTestableAgentJar() { diff --git a/core/src/main/java/com/alibaba/testable/core/util/ConstPool.java b/core/src/main/java/com/alibaba/testable/core/util/ConstPool.java index 283b13a..3c3c5ba 100644 --- a/core/src/main/java/com/alibaba/testable/core/util/ConstPool.java +++ b/core/src/main/java/com/alibaba/testable/core/util/ConstPool.java @@ -15,5 +15,6 @@ public final class ConstPool { public static final String TESTABLE_PRIVATE_ACCESSOR = "com.alibaba.testable.core.accessor.PrivateAccessor"; public static final String ANNOTATION_TESTABLE_INJECT = "com.alibaba.testable.core.annotation.TestableInject"; public static final String TESTABLE_SETUP_METHOD_NAME = "testableSetup"; + public static final String TEST_POSTFIX = "Test"; } 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 new file mode 100644 index 0000000..43c4b3a --- /dev/null +++ b/core/src/main/java/com/alibaba/testable/core/util/TestableUtil.java @@ -0,0 +1,40 @@ +package com.alibaba.testable.core.util; + +/** + * @author flin + */ +public class TestableUtil { + + private static final String TESTABLE_NE = "n.e"; + + public static String currentMemberMethodName() { + StackTraceElement[] stack = Thread.currentThread().getStackTrace(); + for (int i = 0; i < stack.length; i++) { + if (stack[i].getClassName().equals(TESTABLE_NE)) { + return stack[i + 1].getMethodName(); + } + } + return ""; + } + + public static String currentTestCaseName(Object testClassRef) { + StackTraceElement[] stack = getMainThread().getStackTrace(); + String testClassName = testClassRef.getClass().getName(); + for (int i = stack.length - 1; i >= 0; i--) { + if (stack[i].getClassName().equals(testClassName)) { + return stack[i].getMethodName(); + } + } + return ""; + } + + private static Thread getMainThread() { + for (Thread t : Thread.getAllStackTraces().keySet()) { + if (t.getId() == 1L) { + return t; + } + } + return Thread.currentThread(); + } + +}