mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-24 03:10:14 +08:00
move private accessor to tool package
This commit is contained in:
parent
a623bd0a33
commit
b062dbc592
@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
* 演示快速创建任意对象和使用路径访问成员
|
||||
* Demonstrate quick object construction and access members by path
|
||||
*/
|
||||
class DemoOmniMethodsTest {
|
||||
class OmniConstructorTest {
|
||||
|
||||
@Test
|
||||
void should_construct_any_class() {
|
@ -1,8 +1,6 @@
|
||||
package com.alibaba.demo.basic;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.demo.basic.DemoPrivateAccess;
|
||||
import com.alibaba.testable.processor.annotation.EnablePrivateAccess;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -12,64 +10,46 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* 演示私有成员访问功能
|
||||
* Demonstrate private member access functionality
|
||||
* 演示使用`PrivateAccessor`工具类访问私有成员
|
||||
* Demonstrate access private member via `PrivateAccessor` class
|
||||
*/
|
||||
@EnablePrivateAccess
|
||||
class DemoPrivateAccessTest {
|
||||
class PrivateAccessorTest {
|
||||
|
||||
private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess();
|
||||
|
||||
@Test
|
||||
void should_access_private_method() {
|
||||
List<String> list = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
|
||||
assertEquals("member", demoPrivateAccess.privateFunc());
|
||||
assertEquals("member", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc"));
|
||||
assertEquals("abc + hello + 1", demoPrivateAccess.privateFuncWithArgs(list, "hello", 1));
|
||||
assertEquals("abc + hello + 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFuncWithArgs", list, "hello", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_access_private_field() {
|
||||
demoPrivateAccess.count = 2;
|
||||
assertEquals(Integer.valueOf(2), demoPrivateAccess.count);
|
||||
|
||||
PrivateAccessor.set(demoPrivateAccess, "count", 3);
|
||||
assertEquals(Integer.valueOf(3), PrivateAccessor.get(demoPrivateAccess, "count"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_access_private_static_method() {
|
||||
assertEquals("static", DemoPrivateAccess.privateStaticFunc());
|
||||
assertEquals("static", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFunc"));
|
||||
assertEquals("hello + 1", DemoPrivateAccess.privateStaticFuncWithArgs("hello", 1));
|
||||
assertEquals("hello + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", "hello", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_access_private_static_field() {
|
||||
DemoPrivateAccess.staticCount = 2;
|
||||
assertEquals(Integer.valueOf(2), DemoPrivateAccess.staticCount);
|
||||
|
||||
PrivateAccessor.setStatic(DemoPrivateAccess.class, "staticCount", 3);
|
||||
assertEquals(Integer.valueOf(3), PrivateAccessor.getStatic(DemoPrivateAccess.class, "staticCount"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_update_final_field() {
|
||||
demoPrivateAccess.pi = 4.13;
|
||||
assertEquals(Double.valueOf(4.13), demoPrivateAccess.pi);
|
||||
|
||||
PrivateAccessor.set(demoPrivateAccess, "pi", 3.14);
|
||||
assertEquals(Double.valueOf(3.14), PrivateAccessor.get(demoPrivateAccess, "pi"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_use_null_parameter() {
|
||||
demoPrivateAccess.pi = null;
|
||||
assertNull(demoPrivateAccess.pi);
|
||||
assertEquals("null + 1", DemoPrivateAccess.privateStaticFuncWithArgs(null, 1));
|
||||
|
||||
PrivateAccessor.set(demoPrivateAccess, "pi", null);
|
||||
assertNull(PrivateAccessor.get(demoPrivateAccess, "pi"));
|
||||
assertEquals("null + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", null, 1));
|
@ -0,0 +1,59 @@
|
||||
package com.alibaba.demo.basic;
|
||||
|
||||
import com.alibaba.testable.processor.annotation.EnablePrivateAccess;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* 演示使用`@EnablePrivateAccess`注解访问私有成员
|
||||
* Demonstrate access private member via `@EnablePrivateAccess` annotation
|
||||
*/
|
||||
@EnablePrivateAccess(srcClass = DemoPrivateAccess.class)
|
||||
class PrivateProcessorTest {
|
||||
|
||||
private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess();
|
||||
|
||||
@Test
|
||||
void should_access_private_method() {
|
||||
List<String> list = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
|
||||
assertEquals("member", demoPrivateAccess.privateFunc());
|
||||
assertEquals("abc + hello + 1", demoPrivateAccess.privateFuncWithArgs(list, "hello", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_access_private_field() {
|
||||
demoPrivateAccess.count = 2;
|
||||
assertEquals(Integer.valueOf(2), demoPrivateAccess.count);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_access_private_static_method() {
|
||||
assertEquals("static", DemoPrivateAccess.privateStaticFunc());
|
||||
assertEquals("hello + 1", DemoPrivateAccess.privateStaticFuncWithArgs("hello", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_access_private_static_field() {
|
||||
DemoPrivateAccess.staticCount = 2;
|
||||
assertEquals(Integer.valueOf(2), DemoPrivateAccess.staticCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_update_final_field() {
|
||||
demoPrivateAccess.pi = 4.13;
|
||||
assertEquals(Double.valueOf(4.13), demoPrivateAccess.pi);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_use_null_parameter() {
|
||||
demoPrivateAccess.pi = null;
|
||||
assertNull(demoPrivateAccess.pi);
|
||||
assertEquals("null + 1", DemoPrivateAccess.privateStaticFuncWithArgs(null, 1));
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.demo.basic.model.BlackBox
|
||||
import com.alibaba.demo.basic.model.Box
|
||||
import com.alibaba.demo.basic.model.Color
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||
import com.alibaba.demo.basic.model.mock.Box
|
||||
import com.alibaba.demo.basic.model.mock.Color
|
||||
|
||||
/**
|
||||
* 演示父类变量引用子类对象时的Mock场景
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.demo.basic.model.BlackBox
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.demo.basic.model.BlackBox
|
||||
import com.alibaba.demo.basic.model.ColorBox
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||
import com.alibaba.demo.basic.model.mock.ColorBox
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
|
||||
|
@ -9,7 +9,7 @@ class DemoPrivateAccess {
|
||||
/**
|
||||
* a private member field
|
||||
*/
|
||||
private var count = 0
|
||||
var count = 0
|
||||
|
||||
/**
|
||||
* a constant field
|
||||
@ -19,7 +19,7 @@ class DemoPrivateAccess {
|
||||
/**
|
||||
* private member method
|
||||
*/
|
||||
private fun privateFunc(list: List<String>, str: String, i: Int): String {
|
||||
fun privateFunc(list: List<String>, str: String, i: Int): String {
|
||||
return list.reduce { a: String, s: String -> a + s } + " + " + "$str + $i"
|
||||
}
|
||||
|
||||
@ -28,12 +28,12 @@ class DemoPrivateAccess {
|
||||
/**
|
||||
* a private static field
|
||||
*/
|
||||
private var staticCount = 0
|
||||
var staticCount = 0
|
||||
|
||||
/**
|
||||
* private static method
|
||||
*/
|
||||
private fun privateStaticFunc(str: String, i: Int): String {
|
||||
fun privateStaticFunc(str: String, i: Int): String {
|
||||
return "$str + $i"
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.alibaba.demo.basic.model
|
||||
package com.alibaba.demo.basic.model.mock
|
||||
|
||||
|
||||
class BlackBox(var input: String) : Box(), Color {
|
@ -1,4 +1,4 @@
|
||||
package com.alibaba.demo.basic.model
|
||||
package com.alibaba.demo.basic.model.mock
|
||||
|
||||
abstract class Box {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.alibaba.demo.basic.model
|
||||
package com.alibaba.demo.basic.model.mock
|
||||
|
||||
interface Color {
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.HashMap
|
||||
|
||||
class Container {
|
||||
var args: List<String?> = ArrayList()
|
||||
var command: String? = null
|
||||
var env: Map<String, String> = HashMap()
|
||||
var image: String? = null
|
||||
var imagePullPolicy: String? = null
|
||||
var livenessProbe: Probe? = null
|
||||
var name: String? = null
|
||||
var ports: List<ContainerPort?> = ArrayList()
|
||||
var readinessProbe: Probe? = null
|
||||
var startupProbe: Probe? = null
|
||||
var stdin: Boolean? = null
|
||||
var stdinOnce: Boolean? = null
|
||||
var terminationMessagePath: String? = null
|
||||
var terminationMessagePolicy: String? = null
|
||||
var tty: Boolean? = null
|
||||
var workingDir: String? = null
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
class ContainerPort {
|
||||
var containerPort: Int? = null
|
||||
var hostIP: String? = null
|
||||
var hostPort: Int? = null
|
||||
var name: String? = null
|
||||
var protocol: String? = null
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
class ExecAction {
|
||||
var command: String? = null
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
class ObjectMeta {
|
||||
var annotations: Map<String, String>? = null
|
||||
var clusterName: String? = null
|
||||
var creationTimestamp: String? = null
|
||||
var deletionGracePeriodSeconds: Long? = null
|
||||
var deletionTimestamp: String? = null
|
||||
var finalizers: List<String?> = ArrayList<String?>()
|
||||
var generateName: String? = null
|
||||
var generation: Long? = null
|
||||
var labels: Map<String, String>? = null
|
||||
var name: String? = null
|
||||
var namespace: String? = null
|
||||
var resourceVersion: String? = null
|
||||
var selfLink: String? = null
|
||||
var uid: String? = null
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
/**
|
||||
* 这是一个简化了的Kubernetes Pod模型
|
||||
* This is a simplified kubernetes pod model
|
||||
*/
|
||||
class Pod {
|
||||
var apiVersion = "v1"
|
||||
var kind = "Pod"
|
||||
var metadata: ObjectMeta? = null
|
||||
var spec: PodSpec? = null
|
||||
var status: PodStatus? = null
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
class PodSpec {
|
||||
var activeDeadlineSeconds: Long? = null
|
||||
var automountServiceAccountToken: Boolean? = null
|
||||
var containers: Array<Container> = arrayOf()
|
||||
var dnsPolicy: String? = null
|
||||
var enableServiceLinks: Boolean? = null
|
||||
var hostIPC: Boolean? = null
|
||||
var hostNetwork: Boolean? = null
|
||||
var hostPID: Boolean? = null
|
||||
var hostname: String? = null
|
||||
var initContainers: Array<Container> = arrayOf()
|
||||
var nodeName: String? = null
|
||||
var nodeSelector: Map<String, String>? = null
|
||||
var preemptionPolicy: String? = null
|
||||
var priority: Int? = null
|
||||
var priorityClassName: String? = null
|
||||
var restartPolicy: String? = null
|
||||
var runtimeClassName: String? = null
|
||||
var schedulerName: String? = null
|
||||
var serviceAccount: String? = null
|
||||
var serviceAccountName: String? = null
|
||||
var setHostnameAsFQDN: Boolean? = null
|
||||
var shareProcessNamespace: Boolean? = null
|
||||
var subdomain: String? = null
|
||||
var terminationGracePeriodSeconds: Long? = null
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
enum class PodStatus(status: String) {
|
||||
/**
|
||||
* waiting
|
||||
*/
|
||||
WAITING("waiting"),
|
||||
|
||||
/**
|
||||
* running
|
||||
*/
|
||||
RUNNING("running"),
|
||||
|
||||
/**
|
||||
* terminated
|
||||
*/
|
||||
TERMINATED("terminated")
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.alibaba.demo.basic.model.omni
|
||||
|
||||
class Probe {
|
||||
var exec: ExecAction? = null
|
||||
var failureThreshold: Int? = null
|
||||
var initialDelaySeconds: Int? = null
|
||||
var periodSeconds: Int? = null
|
||||
var successThreshold: Int? = null
|
||||
var timeoutSeconds: Int? = null
|
||||
}
|
@ -2,9 +2,9 @@ package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.matcher.InvokeVerifier
|
||||
import com.alibaba.demo.basic.model.BlackBox
|
||||
import com.alibaba.demo.basic.model.Box
|
||||
import com.alibaba.demo.basic.model.Color
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||
import com.alibaba.demo.basic.model.mock.Box
|
||||
import com.alibaba.demo.basic.model.mock.Color
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
|
@ -4,7 +4,7 @@ import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.error.VerifyFailedError
|
||||
import com.alibaba.testable.core.matcher.InvokeMatcher
|
||||
import com.alibaba.testable.core.matcher.InvokeVerifier
|
||||
import com.alibaba.demo.basic.model.BlackBox
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
|
@ -5,8 +5,8 @@ import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.matcher.InvokeVerifier.verify
|
||||
import com.alibaba.testable.core.tool.TestableTool.SOURCE_METHOD
|
||||
import com.alibaba.testable.core.tool.TestableTool.MOCK_CONTEXT
|
||||
import com.alibaba.demo.basic.model.BlackBox
|
||||
import com.alibaba.demo.basic.model.ColorBox
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||
import com.alibaba.demo.basic.model.mock.ColorBox
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.Executors
|
||||
|
@ -0,0 +1,97 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.demo.basic.model.omni.Container
|
||||
import com.alibaba.demo.basic.model.omni.Pod
|
||||
import com.alibaba.demo.basic.model.omni.PodStatus
|
||||
import com.alibaba.demo.basic.model.omni.Probe
|
||||
import com.alibaba.testable.core.tool.OmniAccessor
|
||||
import com.alibaba.testable.core.tool.OmniConstructor
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
/**
|
||||
* 演示快速创建任意对象和使用路径访问成员
|
||||
* Demonstrate quick object construction and access members by path
|
||||
*/
|
||||
internal class OmniConstructorTest {
|
||||
@Test
|
||||
fun should_construct_any_class() {
|
||||
val pod = OmniConstructor.newInstance(Pod::class.java)
|
||||
val container = OmniConstructor.newInstance(Container::class.java)
|
||||
|
||||
// 所有基础类型初始化为默认数值
|
||||
Assertions.assertEquals(0L, pod.spec!!.activeDeadlineSeconds)
|
||||
|
||||
// 所有枚举类型初始化为第一个可选值
|
||||
Assertions.assertEquals(PodStatus.WAITING, pod.status)
|
||||
|
||||
// 所有数组类型初始化为空数组
|
||||
Assertions.assertEquals(0, pod.spec!!.containers.size)
|
||||
|
||||
// 任意深度的子孙成员对象都会被初始化
|
||||
Assertions.assertEquals("", container.readinessProbe!!.exec!!.command)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_get_any_member() {
|
||||
val pod = OmniConstructor.newInstance(Pod::class.java)
|
||||
pod.spec!!.containers = OmniConstructor.newArray(Container::class.java, 3)
|
||||
pod.spec!!.containers[0].command = "container-cmd-1st"
|
||||
pod.spec!!.containers[0].livenessProbe!!.exec!!.command = "liveness-cmd-1st"
|
||||
pod.spec!!.containers[0].readinessProbe!!.exec!!.command = "readness-cmd-1st"
|
||||
pod.spec!!.containers[0].startupProbe!!.exec!!.command = "startup-cmd-1st"
|
||||
pod.spec!!.containers[1].command = "container-cmd-2nd"
|
||||
pod.spec!!.containers[1].livenessProbe!!.exec!!.command = "liveness-cmd-2nd"
|
||||
pod.spec!!.containers[1].readinessProbe!!.exec!!.command = "readness-cmd-2nd"
|
||||
pod.spec!!.containers[1].startupProbe!!.exec!!.command = "startup-cmd-2nd"
|
||||
pod.spec!!.containers[2].command = "container-cmd-3rd"
|
||||
pod.spec!!.containers[2].livenessProbe!!.exec!!.command = "liveness-cmd-3rd"
|
||||
pod.spec!!.containers[2].readinessProbe!!.exec!!.command = "readness-cmd-3rd"
|
||||
pod.spec!!.containers[2].startupProbe!!.exec!!.command = "startup-cmd-3rd"
|
||||
|
||||
// 使用成员名快速读取成员变量
|
||||
val commands = OmniAccessor.get<String>(pod, "command")
|
||||
Assertions.assertEquals(12, commands.size)
|
||||
Assertions.assertEquals("container-cmd-1st", commands[0])
|
||||
Assertions.assertEquals("liveness-cmd-1st", commands[3])
|
||||
Assertions.assertEquals("readness-cmd-1st", commands[6])
|
||||
Assertions.assertEquals("startup-cmd-1st", commands[9])
|
||||
|
||||
// 使用成员类型快速读取成员变量
|
||||
val probes: List<Probe> = OmniAccessor.get(pod, "{Probe}")
|
||||
Assertions.assertEquals(9, probes.size)
|
||||
Assertions.assertEquals("liveness-cmd-1st", probes[0].exec!!.command)
|
||||
Assertions.assertEquals("readness-cmd-1st", probes[3].exec!!.command)
|
||||
Assertions.assertEquals("startup-cmd-1st", probes[6].exec!!.command)
|
||||
|
||||
// 使用模糊路径快速读取成员变量
|
||||
val startupCommands = OmniAccessor.get<String>(pod, "startupProbe/*/command")
|
||||
Assertions.assertEquals(3, startupCommands.size)
|
||||
Assertions.assertEquals("startup-cmd-1st", startupCommands[0])
|
||||
Assertions.assertEquals("startup-cmd-2nd", startupCommands[1])
|
||||
Assertions.assertEquals("startup-cmd-3rd", startupCommands[2])
|
||||
|
||||
// 使用带下标的路径读取成员变量
|
||||
val firstStartupCommands: List<Probe> = OmniAccessor.get(pod, "containers[0]/livenessProbe")
|
||||
Assertions.assertEquals(1, firstStartupCommands.size)
|
||||
Assertions.assertEquals("liveness-cmd-1st", firstStartupCommands[0].exec!!.command)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_set_any_member() {
|
||||
val pod = OmniConstructor.newInstance(Pod::class.java)
|
||||
pod.spec!!.containers = OmniConstructor.newArray(Container::class.java, 3)
|
||||
|
||||
// 使用模糊路径批量给成员变量赋值
|
||||
OmniAccessor.set(pod, "containers/command", "container-cmd")
|
||||
OmniAccessor.set(pod, "{Probe}/*/command", "probe-cmd")
|
||||
Assertions.assertEquals("container-cmd", pod.spec!!.containers[0].command)
|
||||
Assertions.assertEquals("probe-cmd", pod.spec!!.containers[1].readinessProbe!!.exec!!.command)
|
||||
Assertions.assertEquals("probe-cmd", pod.spec!!.containers[2].livenessProbe!!.exec!!.command)
|
||||
|
||||
// 使用带下标的路径给成员变量赋值
|
||||
OmniAccessor.set(pod, "containers[1]/*/*/command", "probe-cmd-2nd")
|
||||
Assertions.assertEquals("probe-cmd", pod.spec!!.containers[0].livenessProbe!!.exec!!.command)
|
||||
Assertions.assertEquals("probe-cmd-2nd", pod.spec!!.containers[1].livenessProbe!!.exec!!.command)
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test
|
||||
* 演示私有成员访问功能
|
||||
* Demonstrate private member access functionality
|
||||
*/
|
||||
internal class DemoPrivateAccessTest {
|
||||
internal class PrivateAccessorTest {
|
||||
|
||||
private val demoPrivateAccess = DemoPrivateAccess()
|
||||
|
@ -59,7 +59,7 @@ Executing the unit test again will print out the signatures of all mock methods,
|
||||
[VERBOSE] Test case "should_set_mock_context"
|
||||
[DIAGNOSE] Found 6 test cases
|
||||
[DIAGNOSE] Handling mock class com/alibaba/testable/demo/basic/DemoMockTest$Mock
|
||||
[VERBOSE] Mock constructor "createBlackBox" as "com.alibaba.demo.basic.model.BlackBox(java.lang.String)"
|
||||
[VERBOSE] Mock constructor "createBlackBox" as "com.alibaba.demo.basic.model.mock.BlackBox(java.lang.String)"
|
||||
[VERBOSE] Mock method "innerFunc" as "com.alibaba.demo.basic.DemoMock::innerFunc(java.lang.String) : java.lang.String"
|
||||
... ...
|
||||
[VERBOSE] Mock method "callFromDifferentMethod" as "()Ljava/lang/String;"
|
||||
@ -68,10 +68,10 @@ Executing the unit test again will print out the signatures of all mock methods,
|
||||
[DIAGNOSE] Handling method <init>
|
||||
[VERBOSE] Line 13, constructing "java.lang.Object()"
|
||||
[DIAGNOSE] Handling method newFunc
|
||||
[VERBOSE] Line 19, constructing "com.alibaba.demo.basic.model.BlackBox(java.lang.String)"
|
||||
[VERBOSE] Line 19, constructing "com.alibaba.demo.basic.model.mock.BlackBox(java.lang.String)"
|
||||
[DIAGNOSE] Line 19, mock method "createBlackBox" used
|
||||
[VERBOSE] Line 19, invoking "com.alibaba.demo.basic.DemoMockTest$Mock::createBlackBox(java.lang.String) : com.alibaba.demo.basic.model.BlackBox"
|
||||
[VERBOSE] Line 20, invoking "com.alibaba.demo.basic.model.BlackBox::get() : java.lang.String"
|
||||
[VERBOSE] Line 19, invoking "com.alibaba.demo.basic.DemoMockTest$Mock::createBlackBox(java.lang.String) : com.alibaba.demo.basic.model.mock.BlackBox"
|
||||
[VERBOSE] Line 20, invoking "com.alibaba.demo.basic.model.mock.BlackBox::get() : java.lang.String"
|
||||
[DIAGNOSE] Handling method outerFunc
|
||||
[VERBOSE] Line 27, constructing "java.lang.StringBuilder()"
|
||||
[VERBOSE] Line 27, invoking "java.lang.StringBuilder::append(java.lang.String) : java.lang.StringBuilder"
|
||||
|
@ -64,7 +64,7 @@ class DemoTest {
|
||||
[VERBOSE] Test case "should_set_mock_context"
|
||||
[DIAGNOSE] Found 6 test cases
|
||||
[DIAGNOSE] Handling mock class com/alibaba/testable/demo/basic/DemoMockTest$Mock
|
||||
[VERBOSE] Mock constructor "createBlackBox" as "com.alibaba.demo.basic.model.BlackBox(java.lang.String)"
|
||||
[VERBOSE] Mock constructor "createBlackBox" as "com.alibaba.demo.basic.model.mock.BlackBox(java.lang.String)"
|
||||
[VERBOSE] Mock method "innerFunc" as "com.alibaba.demo.basic.DemoMock::innerFunc(java.lang.String) : java.lang.String"
|
||||
... ...
|
||||
[VERBOSE] Mock method "callFromDifferentMethod" as "()Ljava/lang/String;"
|
||||
@ -73,10 +73,10 @@ class DemoTest {
|
||||
[DIAGNOSE] Handling method <init>
|
||||
[VERBOSE] Line 13, constructing "java.lang.Object()"
|
||||
[DIAGNOSE] Handling method newFunc
|
||||
[VERBOSE] Line 19, constructing "com.alibaba.demo.basic.model.BlackBox(java.lang.String)"
|
||||
[VERBOSE] Line 19, constructing "com.alibaba.demo.basic.model.mock.BlackBox(java.lang.String)"
|
||||
[DIAGNOSE] Line 19, mock method "createBlackBox" used
|
||||
[VERBOSE] Line 19, invoking "com.alibaba.demo.basic.DemoMockTest$Mock::createBlackBox(java.lang.String) : com.alibaba.demo.basic.model.BlackBox"
|
||||
[VERBOSE] Line 20, invoking "com.alibaba.demo.basic.model.BlackBox::get() : java.lang.String"
|
||||
[VERBOSE] Line 19, invoking "com.alibaba.demo.basic.DemoMockTest$Mock::createBlackBox(java.lang.String) : com.alibaba.demo.basic.model.mock.BlackBox"
|
||||
[VERBOSE] Line 20, invoking "com.alibaba.demo.basic.model.mock.BlackBox::get() : java.lang.String"
|
||||
[DIAGNOSE] Handling method outerFunc
|
||||
[VERBOSE] Line 27, constructing "java.lang.StringBuilder()"
|
||||
[VERBOSE] Line 27, invoking "java.lang.StringBuilder::append(java.lang.String) : java.lang.StringBuilder"
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.alibaba.testable.agent.handler;
|
||||
|
||||
import com.alibaba.testable.agent.model.MethodInfo;
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.InsnNode;
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.alibaba.testable.agent.transformer;
|
||||
|
||||
import com.alibaba.testable.agent.tool.ImmutablePair;
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.testable.agent.util;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -16,8 +16,8 @@ public class OmniConstructor {
|
||||
/**
|
||||
* 快速创建任意指定类型的测试对象
|
||||
*
|
||||
* @param clazz 目标类型
|
||||
* @return 返回新建的对象
|
||||
* @param clazz 期望的对象类型
|
||||
* @return 返回新创建的对象
|
||||
*/
|
||||
public static <T> T newInstance(Class<T> clazz) {
|
||||
try {
|
||||
@ -49,9 +49,9 @@ public class OmniConstructor {
|
||||
/**
|
||||
* 快速创建任意指定类型的对象数组
|
||||
*
|
||||
* @param clazz 目标类型
|
||||
* @param clazz 期望的对象类型
|
||||
* @param size 数组大小
|
||||
* @return 返回新建的数组
|
||||
* @return 返回新创建的对象数组
|
||||
*/
|
||||
public static <T> T[] newArray(Class<T> clazz, int size) {
|
||||
T[] array = (T[])Array.newInstance(clazz, size);
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.alibaba.testable.core.accessor;
|
||||
package com.alibaba.testable.core.tool;
|
||||
|
||||
import com.alibaba.testable.core.exception.MemberAccessException;
|
||||
import com.alibaba.testable.core.util.TypeUtil;
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
import com.alibaba.testable.core.model.MockContext;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.alibaba.testable.core.tool;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.tool.PrivateAccessor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
@ -6,7 +6,7 @@ package com.alibaba.testable.processor.constant;
|
||||
public final class ConstPool {
|
||||
|
||||
public static final String ENABLE_PRIVATE_ACCESS = "com.alibaba.testable.processor.annotation.EnablePrivateAccess";
|
||||
public static final String TESTABLE_PRIVATE_ACCESSOR = "com.alibaba.testable.core.accessor.PrivateAccessor";
|
||||
public static final String TESTABLE_PRIVATE_ACCESSOR = "com.alibaba.testable.core.tool.PrivateAccessor";
|
||||
public static final String TEST_POSTFIX = "Test";
|
||||
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.alibaba.testable.processor.constant.ConstPool.TESTABLE_PRIVATE_ACCESSOR;
|
||||
|
||||
/**
|
||||
* Validate parameter of PrivateAccessor methods to prevent broken by refactor
|
||||
*
|
||||
@ -17,7 +19,6 @@ import java.util.Map;
|
||||
public class PrivateAccessChecker {
|
||||
|
||||
private static final String CLASS_NAME_PRIVATE_ACCESSOR = "PrivateAccessor";
|
||||
private static final String CLASS_NAME_PRIVATE_ACCESSOR_FULL = "com.alibaba.testable.core.accessor.PrivateAccessor";
|
||||
private static final List<String> FIELD_ACCESS_METHOD = Arrays.asList(new String[]
|
||||
{ "get", "set", "getStatic", "setStatic" }.clone());
|
||||
private static final List<String> FIELD_INVOKE_METHOD = Arrays.asList(new String[]
|
||||
@ -77,7 +78,7 @@ public class PrivateAccessChecker {
|
||||
|
||||
private boolean isPrivateAccessorWithFullRef(JCTree.JCFieldAccess fieldAccess) {
|
||||
return fieldAccess.selected instanceof JCTree.JCFieldAccess &&
|
||||
fieldAccess.selected.toString().equals(CLASS_NAME_PRIVATE_ACCESSOR_FULL);
|
||||
fieldAccess.selected.toString().equals(TESTABLE_PRIVATE_ACCESSOR);
|
||||
}
|
||||
|
||||
private boolean checkParameterCount(Map<String, List<Integer>> methods, String target, int parameterCount) {
|
||||
|
Loading…
Reference in New Issue
Block a user