mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-09 20:00:21 +08:00
move CONSTRUCTOR to TestableConst
This commit is contained in:
parent
757becc08b
commit
5150868e8c
@ -1,10 +1,10 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
import com.alibaba.testable.core.annotation.TestableMock;
|
import com.alibaba.testable.core.annotation.TestableMock;
|
||||||
|
import com.alibaba.testable.core.tool.TestableConst;
|
||||||
import com.alibaba.testable.demo.model.BlackBox;
|
import com.alibaba.testable.demo.model.BlackBox;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
@ -19,7 +19,7 @@ class DemoMockTest {
|
|||||||
|
|
||||||
private DemoMock demoMock = new DemoMock();
|
private DemoMock demoMock = new DemoMock();
|
||||||
|
|
||||||
@TestableMock(targetMethod = CONSTRUCTOR)
|
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
|
||||||
private BlackBox createBlackBox(String text) {
|
private BlackBox createBlackBox(String text) {
|
||||||
return new BlackBox("mock_" + text);
|
return new BlackBox("mock_" + text);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
import com.alibaba.testable.core.annotation.TestableMock;
|
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 org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -29,7 +29,7 @@ class DemoTemplateTest {
|
|||||||
return new HashMap<K, V>() {{ put(key, (V)(value.toString() + "_mock_map")); }};
|
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() {
|
private <T> HashSet<T> newHashSet() {
|
||||||
HashSet<T> set = new HashSet<>();
|
HashSet<T> set = new HashSet<>();
|
||||||
set.add((T)"insert_mock");
|
set.add((T)"insert_mock");
|
||||||
@ -55,7 +55,7 @@ class DemoTemplateTest {
|
|||||||
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
|
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
|
||||||
//}
|
//}
|
||||||
//
|
//
|
||||||
//@TestableMock(targetMethod = TestableTool.CONSTRUCTOR)
|
//@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
|
||||||
//private HashSet newHashSet() {
|
//private HashSet newHashSet() {
|
||||||
// HashSet<Object> set = new HashSet<>();
|
// HashSet<Object> set = new HashSet<>();
|
||||||
// set.add("insert_mock");
|
// set.add("insert_mock");
|
||||||
|
@ -2,6 +2,7 @@ package com.alibaba.testable.demo
|
|||||||
|
|
||||||
import com.alibaba.testable.core.annotation.TestableMock
|
import com.alibaba.testable.core.annotation.TestableMock
|
||||||
import com.alibaba.testable.core.matcher.InvokeVerifier.verify
|
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.core.tool.TestableTool.*
|
||||||
import com.alibaba.testable.demo.model.BlackBox
|
import com.alibaba.testable.demo.model.BlackBox
|
||||||
import com.alibaba.testable.demo.model.ColorBox
|
import com.alibaba.testable.demo.model.ColorBox
|
||||||
@ -15,7 +16,7 @@ import java.util.concurrent.Executors
|
|||||||
*/
|
*/
|
||||||
internal class DemoMockTest {
|
internal class DemoMockTest {
|
||||||
|
|
||||||
@TestableMock(targetMethod = CONSTRUCTOR)
|
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
|
||||||
private fun createBlackBox(text: String) = BlackBox("mock_$text")
|
private fun createBlackBox(text: String) = BlackBox("mock_$text")
|
||||||
|
|
||||||
@TestableMock
|
@TestableMock
|
||||||
|
@ -3,7 +3,7 @@ package com.alibaba.testable.demo
|
|||||||
import com.alibaba.testable.core.annotation.MockWith
|
import com.alibaba.testable.core.annotation.MockWith
|
||||||
import com.alibaba.testable.core.annotation.TestableMock
|
import com.alibaba.testable.core.annotation.TestableMock
|
||||||
import com.alibaba.testable.core.model.MockDiagnose
|
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.Assertions
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -27,7 +27,7 @@ internal class DemoTemplateTest {
|
|||||||
return mutableMapOf(key to (value.toString() + "_mock_map") as V)
|
return mutableMapOf(key to (value.toString() + "_mock_map") as V)
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestableMock(targetMethod = TestableTool.CONSTRUCTOR)
|
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
|
||||||
private fun newHashSet(): HashSet<*> {
|
private fun newHashSet(): HashSet<*> {
|
||||||
val set = HashSet<Any>()
|
val set = HashSet<Any>()
|
||||||
set.add("insert_mock")
|
set.add("insert_mock")
|
||||||
|
@ -87,8 +87,8 @@ private BlackBox secretBox(BlackBox ignore) {
|
|||||||
```java
|
```java
|
||||||
// 要覆写的构造函数签名为`BlackBox(String)`
|
// 要覆写的构造函数签名为`BlackBox(String)`
|
||||||
// 无需在Mock方法参数列表增加额外参数,由于使用了`targetMethod`参数,Mock方法的名称随意起
|
// 无需在Mock方法参数列表增加额外参数,由于使用了`targetMethod`参数,Mock方法的名称随意起
|
||||||
// 此处的`CONSTRUCTOR`为`TestableTool`辅助类提供的常量,值为"<init>"
|
// 此处的`TestableConst.CONSTRUCTOR`为`TestableMock`提供的辅助常量,值为"<init>"
|
||||||
@TestableMock(targetMethod = CONSTRUCTOR)
|
@TestableMock(targetMethod = TestableConst.CONSTRUCTOR)
|
||||||
private BlackBox createBlackBox(String text) {
|
private BlackBox createBlackBox(String text) {
|
||||||
return new BlackBox("mock_" + text);
|
return new BlackBox("mock_" + text);
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ 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 CONSTRUCTOR = "<init>";
|
|
||||||
public static final String TEST_POSTFIX = "Test";
|
public static final String TEST_POSTFIX = "Test";
|
||||||
public static final String TESTABLE_INJECT_REF = "_testableInternalRef";
|
public static final String TESTABLE_INJECT_REF = "_testableInternalRef";
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool;
|
|||||||
import com.alibaba.testable.agent.model.MethodInfo;
|
import com.alibaba.testable.agent.model.MethodInfo;
|
||||||
import com.alibaba.testable.agent.util.BytecodeUtil;
|
import com.alibaba.testable.agent.util.BytecodeUtil;
|
||||||
import com.alibaba.testable.agent.util.ClassUtil;
|
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.util.LogUtil;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import org.objectweb.asm.tree.*;
|
import org.objectweb.asm.tree.*;
|
||||||
@ -39,7 +40,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
|||||||
Set<MethodInfo> memberInjectMethods = new HashSet<MethodInfo>();
|
Set<MethodInfo> memberInjectMethods = new HashSet<MethodInfo>();
|
||||||
Set<MethodInfo> newOperatorInjectMethods = new HashSet<MethodInfo>();
|
Set<MethodInfo> newOperatorInjectMethods = new HashSet<MethodInfo>();
|
||||||
for (MethodInfo mi : injectMethods) {
|
for (MethodInfo mi : injectMethods) {
|
||||||
if (mi.getName().equals(ConstPool.CONSTRUCTOR)) {
|
if (mi.getName().equals(TestableConst.CONSTRUCTOR)) {
|
||||||
newOperatorInjectMethods.add(mi);
|
newOperatorInjectMethods.add(mi);
|
||||||
} else {
|
} else {
|
||||||
memberInjectMethods.add(mi);
|
memberInjectMethods.add(mi);
|
||||||
@ -68,7 +69,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
|||||||
node.owner, node.getOpcode(), rangeStart, i);
|
node.owner, node.getOpcode(), rangeStart, i);
|
||||||
i = rangeStart;
|
i = rangeStart;
|
||||||
}
|
}
|
||||||
} else if (ConstPool.CONSTRUCTOR.equals(node.name)) {
|
} else if (TestableConst.CONSTRUCTOR.equals(node.name)) {
|
||||||
// it's a new operation
|
// it's a new operation
|
||||||
String newOperatorInjectMethodName = getNewOperatorInjectMethodName(newOperatorInjectMethods, node);
|
String newOperatorInjectMethodName = getNewOperatorInjectMethodName(newOperatorInjectMethods, node);
|
||||||
if (newOperatorInjectMethodName != null) {
|
if (newOperatorInjectMethodName != null) {
|
||||||
@ -126,7 +127,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
|||||||
case Opcodes.INVOKEVIRTUAL:
|
case Opcodes.INVOKEVIRTUAL:
|
||||||
case Opcodes.INVOKEINTERFACE:
|
case Opcodes.INVOKEINTERFACE:
|
||||||
stackLevel += stackEffectOfInvocation(instructions[i]) + 1;
|
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
|
// constructor must be INVOKESPECIAL and implicitly pop 1 more stack
|
||||||
stackLevel++;
|
stackLevel++;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool;
|
|||||||
import com.alibaba.testable.agent.tool.ImmutablePair;
|
import com.alibaba.testable.agent.tool.ImmutablePair;
|
||||||
import com.alibaba.testable.agent.util.AnnotationUtil;
|
import com.alibaba.testable.agent.util.AnnotationUtil;
|
||||||
import com.alibaba.testable.agent.util.ClassUtil;
|
import com.alibaba.testable.agent.util.ClassUtil;
|
||||||
|
import com.alibaba.testable.core.tool.TestableConst;
|
||||||
import org.objectweb.asm.tree.*;
|
import org.objectweb.asm.tree.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -133,7 +134,7 @@ public class TestClassHandler extends BaseClassHandler {
|
|||||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||||
String method = AnnotationUtil.getAnnotationParameter
|
String method = AnnotationUtil.getAnnotationParameter
|
||||||
(an, ConstPool.FIELD_TARGET_METHOD, null, String.class);
|
(an, ConstPool.FIELD_TARGET_METHOD, null, String.class);
|
||||||
if (ConstPool.CONSTRUCTOR.equals(method)) {
|
if (TestableConst.CONSTRUCTOR.equals(method)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import com.alibaba.testable.agent.model.MethodInfo;
|
|||||||
import com.alibaba.testable.agent.tool.ComparableWeakRef;
|
import com.alibaba.testable.agent.tool.ComparableWeakRef;
|
||||||
import com.alibaba.testable.agent.util.AnnotationUtil;
|
import com.alibaba.testable.agent.util.AnnotationUtil;
|
||||||
import com.alibaba.testable.agent.util.ClassUtil;
|
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.util.LogUtil;
|
||||||
import com.alibaba.testable.core.model.MockDiagnose;
|
import com.alibaba.testable.core.model.MockDiagnose;
|
||||||
import org.objectweb.asm.ClassReader;
|
import org.objectweb.asm.ClassReader;
|
||||||
@ -98,7 +99,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left);
|
String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left);
|
||||||
String targetMethod = AnnotationUtil.getAnnotationParameter(
|
String targetMethod = AnnotationUtil.getAnnotationParameter(
|
||||||
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);
|
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);
|
String sourceClassName = ClassUtil.getSourceClassName(cn.name);
|
||||||
methodInfos.add(new MethodInfo(sourceClassName, targetMethod, mn.name, mn.desc));
|
methodInfos.add(new MethodInfo(sourceClassName, targetMethod, mn.name, mn.desc));
|
||||||
} else {
|
} else {
|
||||||
|
@ -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>";
|
||||||
|
|
||||||
|
}
|
@ -5,11 +5,6 @@ package com.alibaba.testable.core.tool;
|
|||||||
*/
|
*/
|
||||||
public class TestableTool {
|
public class TestableTool {
|
||||||
|
|
||||||
/**
|
|
||||||
* Name of the constructor method
|
|
||||||
*/
|
|
||||||
public static final String CONSTRUCTOR = "<init>";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of current test case method
|
* Name of current test case method
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user