support junit 5 parameterized test

This commit is contained in:
金戟 2021-03-01 23:47:50 +08:00
parent e50633b884
commit 1ff6ec5961
8 changed files with 43 additions and 22 deletions

View File

@ -68,7 +68,7 @@ public class SourceClassHandler extends BaseClassHandler {
MethodInsnNode node = (MethodInsnNode)instructions[i];
if (CONSTRUCTOR.equals(node.name)) {
LogUtil.verbose(" Line %d, constructing \"%s\"", getLineNum(instructions, i),
MethodUtil.toJavaDesc(node.owner, node.desc));
MethodUtil.toJavaMethodDesc(node.owner, node.desc));
MethodInfo newOperatorInjectMethod = getNewOperatorInjectMethod(newOperatorInjectMethods, node);
if (newOperatorInjectMethod != null) {
// it's a new operation and an inject method for it exist
@ -83,7 +83,7 @@ public class SourceClassHandler extends BaseClassHandler {
}
} else {
LogUtil.verbose(" Line %d, invoking \"%s\"", getLineNum(instructions, i),
MethodUtil.toJavaDesc(node.owner, node.name, node.desc));
MethodUtil.toJavaMethodDesc(node.owner, node.name, node.desc));
MethodInfo mockMethod = getMemberInjectMethodName(memberInjectMethods, node);
if (mockMethod != null) {
// it's a member or static method and an inject method for it exist

View File

@ -1,10 +1,11 @@
package com.alibaba.testable.agent.handler.test;
import com.alibaba.testable.agent.model.TestCaseMethodType;
import com.alibaba.testable.agent.util.CollectionUtil;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;
import java.util.List;
import java.util.Set;
abstract public class Framework {
@ -22,7 +23,7 @@ abstract public class Framework {
hasTestAfterMethod = true;
return true;
} else {
return methodAnnotations.contains(getTestAnnotation());
return CollectionUtil.containsAny(methodAnnotations, getTestAnnotations());
}
}
@ -31,7 +32,7 @@ abstract public class Framework {
return TestCaseMethodType.OTHERS;
}
for (AnnotationNode an : mn.visibleAnnotations) {
if (an.desc.equals(getTestAnnotation())) {
if (getTestAnnotations().contains(an.desc)) {
return TestCaseMethodType.TEST;
} else if (an.desc.equals(getTestAfterAnnotation())) {
return TestCaseMethodType.AFTER_TEST;
@ -40,7 +41,7 @@ abstract public class Framework {
return TestCaseMethodType.OTHERS;
}
public abstract String getTestAnnotation();
public abstract List<String> getTestAnnotations();
public abstract String getTestAfterAnnotation();

View File

@ -1,13 +1,16 @@
package com.alibaba.testable.agent.handler.test;
import java.util.Collections;
import java.util.List;
public class JUnit4Framework extends Framework {
private static final String ANNOTATION_TEST = "Lorg/junit/Test;";
private static final String ANNOTATION_AFTER_TEST = "Lorg/junit/After;";
@Override
public String getTestAnnotation() {
return ANNOTATION_TEST;
public List<String> getTestAnnotations() {
return Collections.singletonList(ANNOTATION_TEST);
}
@Override

View File

@ -1,13 +1,17 @@
package com.alibaba.testable.agent.handler.test;
import java.util.Arrays;
import java.util.List;
public class JUnit5Framework extends Framework {
private static final String ANNOTATION_TEST = "Lorg/junit/jupiter/api/Test;";
private static final String ANNOTATION_PARAMETERIZED_TEST = "Lorg/junit/jupiter/params/ParameterizedTest;";
private static final String ANNOTATION_AFTER_TEST = "Lorg/junit/jupiter/api/AfterEach;";
@Override
public String getTestAnnotation() {
return ANNOTATION_TEST;
public List<String> getTestAnnotations() {
return Arrays.asList(ANNOTATION_TEST, ANNOTATION_PARAMETERIZED_TEST);
}
@Override

View File

@ -1,13 +1,16 @@
package com.alibaba.testable.agent.handler.test;
import java.util.Collections;
import java.util.List;
public class TestNgFramework extends Framework {
private static final String ANNOTATION_TEST = "Lorg/testng/annotations/Test;";
private static final String ANNOTATION_AFTER_TEST = "Lorg/testng/annotations/AfterMethod;";
@Override
public String getTestAnnotation() {
return ANNOTATION_TEST;
public List<String> getTestAnnotations() {
return Collections.singletonList(ANNOTATION_TEST);
}
@Override

View File

@ -1,6 +1,7 @@
package com.alibaba.testable.agent.handler.test;
import com.alibaba.testable.agent.model.TestCaseMethodType;
import com.alibaba.testable.agent.util.CollectionUtil;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;
@ -12,7 +13,7 @@ public class TestNgOnClassFramework extends TestNgFramework {
@Override
public boolean fit(Set<String> classAnnotations, Set<String> methodAnnotations) {
if (classAnnotations.contains(getTestAnnotation())) {
if (CollectionUtil.containsAny(classAnnotations, getTestAnnotations())) {
if (methodAnnotations.contains(getTestAfterAnnotation())) {
hasTestAfterMethod = true;
}

View File

@ -16,7 +16,6 @@ import org.objectweb.asm.tree.MethodNode;
import java.util.ArrayList;
import java.util.List;
import static com.alibaba.testable.agent.constant.ByteCodeConst.TYPE_CLASS;
import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassName;
import static com.alibaba.testable.agent.util.MethodUtil.isStatic;
import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR;
@ -88,10 +87,10 @@ public class MockClassParser {
String fullClassName = toDotSeparateFullClassName(an.desc);
if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name,
MethodUtil.toJavaDesc(MethodUtil.getReturnType(mn.desc), mn.desc));
MethodUtil.toJavaMethodDesc(MethodUtil.getReturnType(mn.desc), mn.desc));
addMockConstructor(methodInfos, cn, mn);
} else if (fullClassName.equals(ConstPool.MOCK_METHOD) && AnnotationUtil.isValidMockMethod(mn, an)) {
LogUtil.verbose(" Mock method \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaDesc(
LogUtil.verbose(" Mock method \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc(
getTargetMethodOwner(mn, an), getTargetMethodName(mn, an), getTargetMethodDesc(mn, an)));
String targetMethod = AnnotationUtil.getAnnotationParameter(
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);

View File

@ -112,8 +112,8 @@ public class MethodUtil {
* @param desc method constructor in bytecode format
* @return java style constructor descriptor
*/
public static String toJavaDesc(String owner, String desc) {
String parameters = MethodUtil.extractParameters(desc);
public static String toJavaMethodDesc(String owner, String desc) {
String parameters = toJavaParameterDesc(extractParameters(desc));
return String.format("%s(%s)", owner, parameters);
}
@ -124,9 +124,19 @@ public class MethodUtil {
* @param desc method descriptor in bytecode format
* @return java style method descriptor
*/
public static String toJavaDesc(String owner, String name, String desc) {
String returnType = MethodUtil.getReturnType(desc);
String parameters = MethodUtil.extractParameters(desc);
return String.format("%s %s::%s(%s)", returnType, owner, name, parameters);
public static String toJavaMethodDesc(String owner, String name, String desc) {
String ownerInDotFormat = ClassUtil.toDotSeparatedName(owner);
String returnType = toJavaParameterDesc(getReturnType(desc));
String parameters = toJavaParameterDesc(extractParameters(desc));
return String.format("%s %s::%s(%s)", returnType, ownerInDotFormat, name, parameters);
}
/**
* convert bytecode style parameter descriptor to java style descriptor
* @param desc bytecode style descriptor
* @return java style descriptor
*/
private static String toJavaParameterDesc(String desc) {
return desc;
}
}