implement util function to get current member method and test case

This commit is contained in:
金戟 2020-07-26 20:20:32 +08:00
parent 5ac7f354f3
commit a814e51f66
5 changed files with 48 additions and 4 deletions

View File

@ -11,6 +11,8 @@ public class ConstPool {
public static final String DOT = "."; public static final String DOT = ".";
public static final String SLASH = "/"; public static final String SLASH = "/";
public static final String TEST_POSTFIX = "Test";
public static final List<String> SYS_CLASSES = new ArrayList<String>(); public static final List<String> SYS_CLASSES = new ArrayList<String>();
static { static {
SYS_CLASSES.add("java/lang/StringBuilder"); SYS_CLASSES.add("java/lang/StringBuilder");

View File

@ -1,5 +1,6 @@
package com.alibaba.testable.agent.transformer; 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.SourceClassHandler;
import com.alibaba.testable.agent.handler.TestClassHandler; import com.alibaba.testable.agent.handler.TestClassHandler;
import com.alibaba.testable.agent.util.ClassUtil; 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 = "com.alibaba.testable.core.annotation.EnableTestable";
private static final String ENABLE_TESTABLE_INJECT = "com.alibaba.testable.core.annotation.EnableTestableInject"; private static final String ENABLE_TESTABLE_INJECT = "com.alibaba.testable.core.annotation.EnableTestableInject";
private static final String TEST_POSTFIX = "Test";
private static final Set<String> loadedClassNames = new HashSet<String>(); private static final Set<String> loadedClassNames = new HashSet<String>();
@ -31,7 +31,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
} }
List<String> annotations = ClassUtil.getAnnotations(className); List<String> annotations = ClassUtil.getAnnotations(className);
List<String> testAnnotations = ClassUtil.getAnnotations(className + TEST_POSTFIX); List<String> testAnnotations = ClassUtil.getAnnotations(className + ConstPool.TEST_POSTFIX);
try { try {
if (annotations.contains(ENABLE_TESTABLE_INJECT) || testAnnotations.contains(ENABLE_TESTABLE)) { if (annotations.contains(ENABLE_TESTABLE_INJECT) || testAnnotations.contains(ENABLE_TESTABLE)) {
loadedClassNames.add(className); loadedClassNames.add(className);

View File

@ -2,6 +2,7 @@ package com.alibaba.testable.core.processor;
import com.alibaba.testable.core.annotation.EnableTestable; import com.alibaba.testable.core.annotation.EnableTestable;
import com.alibaba.testable.core.translator.EnableTestableTranslator; import com.alibaba.testable.core.translator.EnableTestableTranslator;
import com.alibaba.testable.core.util.ConstPool;
import com.alibaba.testable.core.util.ResourceUtil; import com.alibaba.testable.core.util.ResourceUtil;
import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
@ -42,7 +43,7 @@ public class EnableTestableProcessor extends BaseProcessor {
} }
private boolean isTestClass(Name name) { private boolean isTestClass(Name name) {
return name.toString().endsWith("Test"); return name.toString().endsWith(ConstPool.TEST_POSTFIX);
} }
private void processClassElement(Symbol.ClassSymbol clazz) { private void processClassElement(Symbol.ClassSymbol clazz) {
@ -56,7 +57,7 @@ public class EnableTestableProcessor extends BaseProcessor {
private String getOriginClassName(Symbol.ClassSymbol clazz) { private String getOriginClassName(Symbol.ClassSymbol clazz) {
String testClassName = clazz.getSimpleName().toString(); 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() { private void createTestableAgentJar() {

View File

@ -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 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 ANNOTATION_TESTABLE_INJECT = "com.alibaba.testable.core.annotation.TestableInject";
public static final String TESTABLE_SETUP_METHOD_NAME = "testableSetup"; public static final String TESTABLE_SETUP_METHOD_NAME = "testableSetup";
public static final String TEST_POSTFIX = "Test";
} }

View File

@ -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();
}
}