in favor of static import

This commit is contained in:
金戟 2021-06-06 23:04:42 +08:00
parent 0551240c7d
commit b949f57d54
8 changed files with 88 additions and 85 deletions

View File

@ -1,11 +1,11 @@
package com.alibaba.demo.basic;
import com.alibaba.testable.core.tool.PrivateAccessor;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static com.alibaba.testable.core.tool.PrivateAccessor.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@ -20,39 +20,39 @@ class DemoPrivateAccessorTest {
@Test
void should_access_private_method() {
List<String> list = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
assertEquals("member", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc"));
assertEquals("abc + hello + 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFuncWithArgs", list, "hello", 1));
assertEquals("member", invoke(demoPrivateAccess, "privateFunc"));
assertEquals("abc + hello + 1", invoke(demoPrivateAccess, "privateFuncWithArgs", list, "hello", 1));
}
@Test
void should_access_private_field() {
PrivateAccessor.set(demoPrivateAccess, "count", 3);
assertEquals(Integer.valueOf(3), PrivateAccessor.get(demoPrivateAccess, "count"));
set(demoPrivateAccess, "count", 3);
assertEquals(Integer.valueOf(3), get(demoPrivateAccess, "count"));
}
@Test
void should_access_private_static_method() {
assertEquals("static", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFunc"));
assertEquals("hello + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", "hello", 1));
assertEquals("static", invokeStatic(DemoPrivateAccess.class, "privateStaticFunc"));
assertEquals("hello + 1", invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", "hello", 1));
}
@Test
void should_access_private_static_field() {
PrivateAccessor.setStatic(DemoPrivateAccess.class, "staticCount", 3);
assertEquals(Integer.valueOf(3), PrivateAccessor.getStatic(DemoPrivateAccess.class, "staticCount"));
setStatic(DemoPrivateAccess.class, "staticCount", 3);
assertEquals(Integer.valueOf(3), getStatic(DemoPrivateAccess.class, "staticCount"));
}
@Test
void should_update_final_field() {
PrivateAccessor.set(demoPrivateAccess, "pi", 3.14);
assertEquals(Double.valueOf(3.14), PrivateAccessor.get(demoPrivateAccess, "pi"));
set(demoPrivateAccess, "pi", 3.14);
assertEquals(Double.valueOf(3.14), get(demoPrivateAccess, "pi"));
}
@Test
void should_use_null_parameter() {
PrivateAccessor.set(demoPrivateAccess, "pi", null);
assertNull(PrivateAccessor.get(demoPrivateAccess, "pi"));
assertEquals("null + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", null, 1));
set(demoPrivateAccess, "pi", null);
assertNull(get(demoPrivateAccess, "pi"));
assertEquals("null + 1", invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", null, 1));
}
}

View File

@ -1,6 +1,6 @@
package com.alibaba.demo.basic
import com.alibaba.testable.core.tool.PrivateAccessor
import com.alibaba.testable.core.tool.PrivateAccessor.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
@ -15,31 +15,31 @@ internal class DemoPrivateAccessorTest {
@Test
fun should_access_private_method() {
val list = listOf("a", "b", "c");
assertEquals("abc + hello + 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc", list, "hello", 1))
assertEquals("abc + hello + 1", invoke(demoPrivateAccess, "privateFunc", list, "hello", 1))
}
@Test
fun should_access_private_field() {
PrivateAccessor.set(demoPrivateAccess, "count", 3)
assertEquals(3, PrivateAccessor.get(demoPrivateAccess, "count"))
set(demoPrivateAccess, "count", 3)
assertEquals(3, get(demoPrivateAccess, "count"))
}
@Test
fun should_access_private_static_method() {
val list = listOf("a", "b", "c");
assertEquals("hello + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess::class.java, "privateStaticFunc", "hello", 1))
assertEquals("abc * hello * 1", PrivateAccessor.invokeStatic(DemoPrivateAccess::class.java, "privateJvmStaticFunc", list, "hello", 1))
assertEquals("hello + 1", invokeStatic(DemoPrivateAccess::class.java, "privateStaticFunc", "hello", 1))
assertEquals("abc * hello * 1", invokeStatic(DemoPrivateAccess::class.java, "privateJvmStaticFunc", list, "hello", 1))
}
@Test
fun should_access_private_static_field() {
PrivateAccessor.setStatic(DemoPrivateAccess::class.java, "staticCount", 3)
assertEquals(3, PrivateAccessor.getStatic(DemoPrivateAccess::class.java, "staticCount"))
setStatic(DemoPrivateAccess::class.java, "staticCount", 3)
assertEquals(3, getStatic(DemoPrivateAccess::class.java, "staticCount"))
}
@Test
fun should_update_final_field() {
PrivateAccessor.set(demoPrivateAccess, "pi", 4.13)
set(demoPrivateAccess, "pi", 4.13)
assertEquals(4.13, demoPrivateAccess.pi)
}

View File

@ -1,7 +1,6 @@
package com.alibaba.testable.agent.handler;
import com.alibaba.testable.agent.model.MethodInfo;
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;
@ -10,6 +9,7 @@ import org.objectweb.asm.tree.VarInsnNode;
import java.util.ArrayList;
import static com.alibaba.testable.core.tool.PrivateAccessor.invoke;
import static org.junit.jupiter.api.Assertions.*;
import static org.objectweb.asm.Opcodes.*;
@ -31,8 +31,8 @@ class SourceClassHandlerTest {
new MethodInsnNode(INVOKEVIRTUAL, "com/alibaba/testable/demo/BlackBox", "callMe", "()Ljava/lang/String;", false),
new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "startsWith", "(Ljava/lang/String;)Z", false)
};
assertEquals(2, PrivateAccessor.invoke(handler, "getMemberMethodStart", instructions, 3));
assertEquals(2, PrivateAccessor.invoke(handler, "getMemberMethodStart", instructions, 6));
assertEquals(0, PrivateAccessor.invoke(handler, "getMemberMethodStart", instructions, 9));
assertEquals(2, invoke(handler, "getMemberMethodStart", instructions, 3));
assertEquals(2, invoke(handler, "getMemberMethodStart", instructions, 6));
assertEquals(0, invoke(handler, "getMemberMethodStart", instructions, 9));
}
}

View File

@ -1,11 +1,12 @@
package com.alibaba.testable.agent.util;
import com.alibaba.testable.agent.tool.ImmutablePair;
import com.alibaba.testable.core.tool.PrivateAccessor;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.*;
import static com.alibaba.testable.core.tool.PrivateAccessor.invokeStatic;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class MethodUtilTest {
@ -67,12 +68,12 @@ class MethodUtilTest {
@Test
void should_convert_bytecode_parameters() {
assertEquals("", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", ""));
assertEquals("void", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "V"));
assertEquals("int, long", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "IJ"));
assertEquals("int[], long[]", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "[I[J"));
assertEquals("int, java.lang.String", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "ILjava/lang/String;"));
assertEquals("java.lang.String, int, long[]", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "Ljava/lang/String;I[J"));
assertEquals("", invokeStatic(MethodUtil.class, "toJavaParameterDesc", ""));
assertEquals("void", invokeStatic(MethodUtil.class, "toJavaParameterDesc", "V"));
assertEquals("int, long", invokeStatic(MethodUtil.class, "toJavaParameterDesc", "IJ"));
assertEquals("int[], long[]", invokeStatic(MethodUtil.class, "toJavaParameterDesc", "[I[J"));
assertEquals("int, java.lang.String", invokeStatic(MethodUtil.class, "toJavaParameterDesc", "ILjava/lang/String;"));
assertEquals("java.lang.String, int, long[]", invokeStatic(MethodUtil.class, "toJavaParameterDesc", "Ljava/lang/String;I[J"));
}
}

View File

@ -1,6 +1,5 @@
package com.alibaba.testable.core.util;
import com.alibaba.testable.core.tool.PrivateAccessor;
import com.alibaba.testable.core.model.MockContext;
import java.util.HashSet;
@ -8,6 +7,7 @@ import java.util.Map;
import java.util.Set;
import static com.alibaba.testable.core.constant.ConstPool.*;
import static com.alibaba.testable.core.tool.PrivateAccessor.*;
public class MockAssociationUtil {
@ -67,11 +67,11 @@ public class MockAssociationUtil {
*/
public static Object invokeOrigin(Class<?> originClass, String originMethod, Object... args) {
if (originMethod.equals(CONSTRUCTOR)) {
return PrivateAccessor.construct(originClass, args);
return construct(originClass, args);
} else if (args[0] == null) {
return PrivateAccessor.invokeStatic(originClass, originMethod, CollectionUtil.slice(args, 1));
return invokeStatic(originClass, originMethod, CollectionUtil.slice(args, 1));
} else {
return PrivateAccessor.invoke(args[0], originMethod, CollectionUtil.slice(args, 1));
return invoke(args[0], originMethod, CollectionUtil.slice(args, 1));
}
}

View File

@ -4,13 +4,14 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static com.alibaba.testable.core.tool.PrivateAccessor.*;
import static org.junit.jupiter.api.Assertions.*;
class OmniAccessorTest {
@Test
void should_generate_member_index() {
List<String> index = PrivateAccessor.invokeStatic(OmniAccessor.class, "generateMemberIndex", DemoParent.class);
List<String> index = invokeStatic(OmniAccessor.class, "generateMemberIndex", DemoParent.class);
assertEquals(34, index.size());
assertEquals("/c{DemoChild}", index.get(0));
assertEquals("/c{DemoChild}/gc{DemoGrandChild}", index.get(1));
@ -50,48 +51,48 @@ class OmniAccessorTest {
@Test
void should_to_pattern() {
assertEquals(".*/", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", ""));
assertEquals(".*/abc\\{[^}]+\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc"));
assertEquals(".*/[^{]+\\{Abc\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "{Abc}"));
assertEquals(".*/abc\\{[^}]+\\}/xyz\\{[^}]+\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/xyz"));
assertEquals(".*/[^{]+\\{Abc\\}/xyz\\{[^}]+\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "{Abc}/xyz"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/{Xyz}"));
assertEquals(".*/[^{]+\\{Abc\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "{Abc}/{Xyz}"));
assertEquals(".*/[^{]+\\{Abc\\[\\]\\}/[^{]+\\{Xyz\\[\\]\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "{Abc[]}/{Xyz[]}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{Xyz\\[\\]\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc[1]/{Xyz[]}[2]"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{Xyz\\}/demo\\{[^}]+\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/{Xyz}/demo"));
assertEquals(".*/abc\\{[^}]+\\}/de[^{]*\\{[^}]+\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/de*/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]*mo\\{[^}]+\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/*mo/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/d[^{]*o\\{[^}]+\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/d*o/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{De[^}]*\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/{De*}/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{[^}]*mo\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/{*mo}/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{D[^}]*o\\}/[^{]+\\{Xyz\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/{D*o}/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^/]+/[^{]+\\{Xyz\\}/[^/]+/demo\\{[^}]+\\}", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toPattern", "abc/*/{Xyz}/*/demo"));
assertEquals(".*/", invokeStatic(OmniAccessor.class, "toPattern", ""));
assertEquals(".*/abc\\{[^}]+\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc"));
assertEquals(".*/[^{]+\\{Abc\\}", invokeStatic(OmniAccessor.class, "toPattern", "{Abc}"));
assertEquals(".*/abc\\{[^}]+\\}/xyz\\{[^}]+\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/xyz"));
assertEquals(".*/[^{]+\\{Abc\\}/xyz\\{[^}]+\\}", invokeStatic(OmniAccessor.class, "toPattern", "{Abc}/xyz"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/{Xyz}"));
assertEquals(".*/[^{]+\\{Abc\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "{Abc}/{Xyz}"));
assertEquals(".*/[^{]+\\{Abc\\[\\]\\}/[^{]+\\{Xyz\\[\\]\\}", invokeStatic(OmniAccessor.class, "toPattern", "{Abc[]}/{Xyz[]}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{Xyz\\[\\]\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc[1]/{Xyz[]}[2]"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{Xyz\\}/demo\\{[^}]+\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/{Xyz}/demo"));
assertEquals(".*/abc\\{[^}]+\\}/de[^{]*\\{[^}]+\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/de*/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]*mo\\{[^}]+\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/*mo/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/d[^{]*o\\{[^}]+\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/d*o/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{De[^}]*\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/{De*}/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{[^}]*mo\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/{*mo}/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^{]+\\{D[^}]*o\\}/[^{]+\\{Xyz\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/{D*o}/{Xyz}"));
assertEquals(".*/abc\\{[^}]+\\}/[^/]+/[^{]+\\{Xyz\\}/[^/]+/demo\\{[^}]+\\}", invokeStatic(OmniAccessor.class, "toPattern", "abc/*/{Xyz}/*/demo"));
}
@Test
void should_to_parent() {
assertEquals("", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toParent", ""));
assertEquals("", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toParent", "abc"));
assertEquals("abc", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toParent", "abc/xyz"));
assertEquals("abc/def", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toParent", "abc/def/xyz"));
assertEquals("/abc/def", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toParent", "/abc/def/xyz"));
assertEquals("", invokeStatic(OmniAccessor.class, "toParent", ""));
assertEquals("", invokeStatic(OmniAccessor.class, "toParent", "abc"));
assertEquals("abc", invokeStatic(OmniAccessor.class, "toParent", "abc/xyz"));
assertEquals("abc/def", invokeStatic(OmniAccessor.class, "toParent", "abc/def/xyz"));
assertEquals("/abc/def", invokeStatic(OmniAccessor.class, "toParent", "/abc/def/xyz"));
}
@Test
void should_to_child() {
assertEquals("", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toChild", ""));
assertEquals("abc", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toChild", "abc"));
assertEquals("xyz", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toChild", "abc/xyz"));
assertEquals("xyz", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toChild", "abc/def/xyz"));
assertEquals("xyz", PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "toChild", "/abc/def/xyz"));
assertEquals("", invokeStatic(OmniAccessor.class, "toChild", ""));
assertEquals("abc", invokeStatic(OmniAccessor.class, "toChild", "abc"));
assertEquals("xyz", invokeStatic(OmniAccessor.class, "toChild", "abc/xyz"));
assertEquals("xyz", invokeStatic(OmniAccessor.class, "toChild", "abc/def/xyz"));
assertEquals("xyz", invokeStatic(OmniAccessor.class, "toChild", "/abc/def/xyz"));
}
@Test
void should_get_full_query_segments() {
String[] querySegments = new String[] { "c", "d" };
String[] memberSegments = new String[] { "a{A}", "b{B}", "c{C}", "d{D}" };
String[] fullQuerySegments = PrivateAccessor.invokeStatic(OmniAccessor.class, "calculateFullQueryPath", querySegments, memberSegments);
String[] fullQuerySegments = invokeStatic(OmniAccessor.class, "calculateFullQueryPath", querySegments, memberSegments);
assertEquals(5, fullQuerySegments.length);
assertEquals("", fullQuerySegments[0]);
assertEquals("", fullQuerySegments[1]);
@ -103,18 +104,18 @@ class OmniAccessorTest {
@Test
void should_get_by_path() {
DemoParent parent = prepareParentObject();
List<Object> obj = PrivateAccessor.invokeStatic(OmniAccessor.class, "getByPath", parent, "/c{DemoChild}/gc{DemoGrandChild}", "c/gc");
List<Object> obj = invokeStatic(OmniAccessor.class, "getByPath", parent, "/c{DemoChild}/gc{DemoGrandChild}", "c/gc");
assertTrue(obj.get(0) instanceof DemoGrandChild);
assertEquals(1, ((DemoGrandChild)obj.get(0)).get());
PrivateAccessor.set(parent.c, "gcs", new DemoGrandChild[] { new DemoGrandChild(), new DemoGrandChild() });
obj = PrivateAccessor.invokeStatic(OmniAccessor.class, "getByPath", parent, "/c{DemoChild}/gcs{DemoGrandChild[]}", "c/gcs");
set(parent.c, "gcs", new DemoGrandChild[] { new DemoGrandChild(), new DemoGrandChild() });
obj = invokeStatic(OmniAccessor.class, "getByPath", parent, "/c{DemoChild}/gcs{DemoGrandChild[]}", "c/gcs");
assertTrue(obj.get(0) instanceof DemoGrandChild[]);
assertEquals(2, ((DemoGrandChild[])obj.get(0)).length);
obj = PrivateAccessor.invokeStatic(OmniAccessor.class, "getByPath", parent, "/c{DemoChild}/gcs{DemoGrandChild[]}", "c/gcs[1]");
obj = invokeStatic(OmniAccessor.class, "getByPath", parent, "/c{DemoChild}/gcs{DemoGrandChild[]}", "c/gcs[1]");
assertTrue(obj.get(0) instanceof DemoGrandChild);
assertEquals(1, ((DemoGrandChild)obj.get(0)).get());
parent.cs = new DemoChild[] { null, prepareChildObject() };
obj = PrivateAccessor.invokeStatic(OmniAccessor.class, "getByPath", parent, "/cs{DemoChild[]}/gcs{DemoGrandChild[]}/i{int}", "c[1]/gcs[1]/i");
obj = invokeStatic(OmniAccessor.class, "getByPath", parent, "/cs{DemoChild[]}/gcs{DemoGrandChild[]}/i{int}", "c[1]/gcs[1]/i");
assertEquals(1, obj.get(0));
}
@ -122,13 +123,13 @@ class OmniAccessorTest {
void should_set_by_path_segment() {
DemoParent parent = prepareParentObject();
DemoChild child = prepareChildObject();
PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "setByPathSegment", parent.c, "gc{DemoGrandChild}", "gc", new DemoGrandChild());
invokeStatic(OmniAccessor.class, "setByPathSegment", parent.c, "gc{DemoGrandChild}", "gc", new DemoGrandChild());
assertEquals(1, parent.c.gc.get());
PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "setByPathSegment", parent, "cs{DemoChild[]}", "cs[2]", child);
invokeStatic(OmniAccessor.class, "setByPathSegment", parent, "cs{DemoChild[]}", "cs[2]", child);
assertNull(parent.cs[0]);
assertNull(parent.cs[1]);
assertEquals(5, parent.cs[2].gc.get());
PrivateAccessor.<String>invokeStatic(OmniAccessor.class, "setByPathSegment", parent, "cs{DemoChild[]}", "cs", child);
invokeStatic(OmniAccessor.class, "setByPathSegment", parent, "cs{DemoChild[]}", "cs", child);
assertEquals(5, parent.cs[0].gc.get());
assertEquals(5, parent.cs[1].gc.get());
assertEquals(5, parent.cs[2].gc.get());
@ -142,7 +143,7 @@ class OmniAccessorTest {
private DemoChild prepareChildObject() {
DemoChild child = OmniConstructor.newInstance(DemoChild.class);
PrivateAccessor.set(child, "gcs", new DemoGrandChild[] { null, new DemoGrandChild() });
set(child, "gcs", new DemoGrandChild[] { null, new DemoGrandChild() });
child.gc.set(5);
return child;
}

View File

@ -1,8 +1,8 @@
package com.alibaba.testable.core.util;
import com.alibaba.testable.core.tool.PrivateAccessor;
import org.junit.jupiter.api.Test;
import static com.alibaba.testable.core.tool.PrivateAccessor.invokeStatic;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CollectionUtilTest {
@ -10,7 +10,7 @@ class CollectionUtilTest {
@Test
void should_slice_array() {
Object[] args = new Object[]{"1", "2", "3"};
Object[] slicedArgs = PrivateAccessor.invokeStatic(CollectionUtil.class, "slice", args, 1);
Object[] slicedArgs = invokeStatic(CollectionUtil.class, "slice", args, 1);
assertEquals(2, slicedArgs.length);
assertEquals("2", slicedArgs[0]);
assertEquals("3", slicedArgs[1]);

View File

@ -1,25 +1,26 @@
package com.alibaba.testable.core.util;
import com.alibaba.testable.core.tool.PrivateAccessor;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static com.alibaba.testable.core.tool.PrivateAccessor.invokeStatic;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class MockAssociationUtilTest {
@Test
void should_associate_by_inner_mock_class() {
assertTrue((Boolean)PrivateAccessor.invokeStatic(MockAssociationUtil.class, "isAssociatedByInnerMockClass",
assertTrue((Boolean)invokeStatic(MockAssociationUtil.class, "isAssociatedByInnerMockClass",
"com.alibaba.testable.DemoTest", "com.alibaba.testable.DemoTest$Mock"));
assertFalse((Boolean)PrivateAccessor.invokeStatic(MockAssociationUtil.class, "isAssociatedByInnerMockClass",
assertFalse((Boolean)invokeStatic(MockAssociationUtil.class, "isAssociatedByInnerMockClass",
"com.alibaba.testable.DemoTest", "com.alibaba.testable.DemoTestMock"));
}
@Test
void should_associate_by_outer_mock_class() {
assertTrue((Boolean)PrivateAccessor.invokeStatic(MockAssociationUtil.class, "isAssociatedByOuterMockClass",
assertTrue((Boolean)invokeStatic(MockAssociationUtil.class, "isAssociatedByOuterMockClass",
"com.alibaba.testable.DemoTest", "com.alibaba.testable.DemoMock"));
assertFalse((Boolean)PrivateAccessor.invokeStatic(MockAssociationUtil.class, "isAssociatedByOuterMockClass",
assertFalse((Boolean)invokeStatic(MockAssociationUtil.class, "isAssociatedByOuterMockClass",
"com.alibaba.testable.DemoTester", "com.alibaba.testable.DemoMock"));
}