move CONSTRUCTOR to TestableConst

This commit is contained in:
金戟 2020-12-10 08:45:01 +08:00
parent 757becc08b
commit 5150868e8c
11 changed files with 32 additions and 21 deletions

View File

@ -1,10 +1,10 @@
package com.alibaba.testable.demo;
import com.alibaba.testable.core.annotation.TestableMock;
import com.alibaba.testable.core.tool.TestableConst;
import com.alibaba.testable.demo.model.BlackBox;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.concurrent.Executors;
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
@ -19,7 +19,7 @@ class DemoMockTest {
private DemoMock demoMock = new DemoMock();
@TestableMock(targetMethod = CONSTRUCTOR)
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
private BlackBox createBlackBox(String text) {
return new BlackBox("mock_" + text);
}

View File

@ -1,7 +1,7 @@
package com.alibaba.testable.demo;
import com.alibaba.testable.core.annotation.TestableMock;
import com.alibaba.testable.core.tool.TestableTool;
import com.alibaba.testable.core.tool.TestableConst;
import org.junit.jupiter.api.Test;
import java.util.*;
@ -29,7 +29,7 @@ class DemoTemplateTest {
return new HashMap<K, V>() {{ put(key, (V)(value.toString() + "_mock_map")); }};
}
@TestableMock(targetMethod = TestableTool.CONSTRUCTOR)
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
private <T> HashSet<T> newHashSet() {
HashSet<T> set = new HashSet<>();
set.add((T)"insert_mock");
@ -55,7 +55,7 @@ class DemoTemplateTest {
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
//}
//
//@TestableMock(targetMethod = TestableTool.CONSTRUCTOR)
//@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
//private HashSet newHashSet() {
// HashSet<Object> set = new HashSet<>();
// set.add("insert_mock");

View File

@ -2,6 +2,7 @@ package com.alibaba.testable.demo
import com.alibaba.testable.core.annotation.TestableMock
import com.alibaba.testable.core.matcher.InvokeVerifier.verify
import com.alibaba.testable.core.tool.TestableConst
import com.alibaba.testable.core.tool.TestableTool.*
import com.alibaba.testable.demo.model.BlackBox
import com.alibaba.testable.demo.model.ColorBox
@ -15,7 +16,7 @@ import java.util.concurrent.Executors
*/
internal class DemoMockTest {
@TestableMock(targetMethod = CONSTRUCTOR)
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
private fun createBlackBox(text: String) = BlackBox("mock_$text")
@TestableMock

View File

@ -3,7 +3,7 @@ package com.alibaba.testable.demo
import com.alibaba.testable.core.annotation.MockWith
import com.alibaba.testable.core.annotation.TestableMock
import com.alibaba.testable.core.model.MockDiagnose
import com.alibaba.testable.core.tool.TestableTool
import com.alibaba.testable.core.tool.TestableConst
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.util.*
@ -27,7 +27,7 @@ internal class DemoTemplateTest {
return mutableMapOf(key to (value.toString() + "_mock_map") as V)
}
@TestableMock(targetMethod = TestableTool.CONSTRUCTOR)
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
private fun newHashSet(): HashSet<*> {
val set = HashSet<Any>()
set.add("insert_mock")

View File

@ -87,8 +87,8 @@ private BlackBox secretBox(BlackBox ignore) {
```java
// 要覆写的构造函数签名为`BlackBox(String)`
// 无需在Mock方法参数列表增加额外参数由于使用了`targetMethod`参数Mock方法的名称随意起
// 此处的`CONSTRUCTOR`为`TestableTool`辅助类提供的常量,值为"<init>"
@TestableMock(targetMethod = CONSTRUCTOR)
// 此处的`TestableConst.CONSTRUCTOR`为`TestableMock`提供的辅助常量,值为"<init>"
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
private BlackBox createBlackBox(String text) {
return new BlackBox("mock_" + text);
}

View File

@ -8,7 +8,6 @@ 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";

View File

@ -4,6 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool;
import com.alibaba.testable.agent.model.MethodInfo;
import com.alibaba.testable.agent.util.BytecodeUtil;
import com.alibaba.testable.agent.util.ClassUtil;
import com.alibaba.testable.core.tool.TestableConst;
import com.alibaba.testable.core.util.LogUtil;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
@ -39,7 +40,7 @@ public class SourceClassHandler extends BaseClassHandler {
Set<MethodInfo> memberInjectMethods = new HashSet<MethodInfo>();
Set<MethodInfo> newOperatorInjectMethods = new HashSet<MethodInfo>();
for (MethodInfo mi : injectMethods) {
if (mi.getName().equals(ConstPool.CONSTRUCTOR)) {
if (mi.getName().equals(TestableConst.CONSTRUCTOR)) {
newOperatorInjectMethods.add(mi);
} else {
memberInjectMethods.add(mi);
@ -68,7 +69,7 @@ public class SourceClassHandler extends BaseClassHandler {
node.owner, node.getOpcode(), rangeStart, i);
i = rangeStart;
}
} else if (ConstPool.CONSTRUCTOR.equals(node.name)) {
} else if (TestableConst.CONSTRUCTOR.equals(node.name)) {
// it's a new operation
String newOperatorInjectMethodName = getNewOperatorInjectMethodName(newOperatorInjectMethods, node);
if (newOperatorInjectMethodName != null) {
@ -126,7 +127,7 @@ public class SourceClassHandler extends BaseClassHandler {
case Opcodes.INVOKEVIRTUAL:
case Opcodes.INVOKEINTERFACE:
stackLevel += stackEffectOfInvocation(instructions[i]) + 1;
if (((MethodInsnNode)instructions[i]).name.equals(ConstPool.CONSTRUCTOR)) {
if (((MethodInsnNode)instructions[i]).name.equals(TestableConst.CONSTRUCTOR)) {
// constructor must be INVOKESPECIAL and implicitly pop 1 more stack
stackLevel++;
}

View File

@ -4,6 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool;
import com.alibaba.testable.agent.tool.ImmutablePair;
import com.alibaba.testable.agent.util.AnnotationUtil;
import com.alibaba.testable.agent.util.ClassUtil;
import com.alibaba.testable.core.tool.TestableConst;
import org.objectweb.asm.tree.*;
import java.util.ArrayList;
@ -133,7 +134,7 @@ public class TestClassHandler extends BaseClassHandler {
for (AnnotationNode an : mn.visibleAnnotations) {
String method = AnnotationUtil.getAnnotationParameter
(an, ConstPool.FIELD_TARGET_METHOD, null, String.class);
if (ConstPool.CONSTRUCTOR.equals(method)) {
if (TestableConst.CONSTRUCTOR.equals(method)) {
return true;
}
}

View File

@ -9,6 +9,7 @@ import com.alibaba.testable.agent.model.MethodInfo;
import com.alibaba.testable.agent.tool.ComparableWeakRef;
import com.alibaba.testable.agent.util.AnnotationUtil;
import com.alibaba.testable.agent.util.ClassUtil;
import com.alibaba.testable.core.tool.TestableConst;
import com.alibaba.testable.core.util.LogUtil;
import com.alibaba.testable.core.model.MockDiagnose;
import org.objectweb.asm.ClassReader;
@ -98,7 +99,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left);
String targetMethod = AnnotationUtil.getAnnotationParameter(
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);
if (targetMethod.equals(ConstPool.CONSTRUCTOR)) {
if (targetMethod.equals(TestableConst.CONSTRUCTOR)) {
String sourceClassName = ClassUtil.getSourceClassName(cn.name);
methodInfos.add(new MethodInfo(sourceClassName, targetMethod, mn.name, mn.desc));
} else {

View File

@ -0,0 +1,13 @@
package com.alibaba.testable.core.tool;
/**
* @author flin
*/
public class TestableConst {
/**
* Name of the constructor method
*/
public static final String CONSTRUCTOR = "<init>";
}

View File

@ -5,11 +5,6 @@ package com.alibaba.testable.core.tool;
*/
public class TestableTool {
/**
* Name of the constructor method
*/
public static final String CONSTRUCTOR = "<init>";
/**
* Name of current test case method
*/