add kotlin test case for omni

This commit is contained in:
金戟 2021-03-24 13:23:02 +08:00
parent 52878364ee
commit 3acf341d1f
4 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.alibaba.demo.basic.model.omni
/**
* 我是一个只有私有构造方法的类
* This class have only private constructor
*/
class Child private constructor() {
// ---------- 内部成员字段 ----------
var grandChild: GrandChild? = null
var subChild: InnerChild? = null
/**
* 这是一个私有内部类
* An private inner class
*/
inner class InnerChild {
private val secret: String? = null
}
}

View File

@ -0,0 +1,9 @@
package com.alibaba.demo.basic.model.omni
class GrandChild {
// ---------- 内部成员字段 ----------
var value = 0
var content: String? = null
}

View File

@ -0,0 +1,19 @@
package com.alibaba.demo.basic.model.omni
import java.lang.IllegalArgumentException
/**
* 我是一个虽有构造方法但无法正常构造的类
* This class have constructor with exception throw
*/
class Parent {
init {
throw IllegalArgumentException()
}
// ---------- 内部成员字段 ----------
var child: Child? = null
var children: Array<Child>? = null
}

View File

@ -0,0 +1,86 @@
package com.alibaba.demo.basic
import com.alibaba.demo.basic.model.omni.Child
import com.alibaba.demo.basic.model.omni.Parent
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 DemoOmniMethodsTest {
@Test
fun should_construct_any_class() {
val parent = OmniConstructor.newInstance(Parent::class.java)
// 任意深度的子孙成员对象都不为空
Assertions.assertNotNull(parent.child?.grandChild?.content)
// 所有基础类型初始化为默认数值
Assertions.assertEquals(0, parent.child?.grandChild?.value)
Assertions.assertEquals("", parent.child?.grandChild?.content)
// 所有数组类型初始化为空数组
Assertions.assertEquals(0, parent.children?.size)
}
@Test
fun should_get_any_member() {
val parent = OmniConstructor.newInstance(Parent::class.java)
parent.children = OmniConstructor.newArray(Child::class.java, 3)
parent.child?.grandChild?.content = "from child"
parent.children?.get(0)?.grandChild?.content = "from 1st children"
parent.children?.get(1)?.grandChild?.content = "from 2nd children"
parent.children?.get(2)?.grandChild?.content = "from 3rd children"
// 使用成员名称快速读取成员对象
var contents = OmniAccessor.get<String?>(parent, "content")
Assertions.assertEquals(4, contents.size)
Assertions.assertEquals("from child", contents[0])
Assertions.assertEquals("from 1st children", contents[1])
Assertions.assertEquals("from 2nd children", contents[2])
Assertions.assertEquals("from 3rd children", contents[3])
// 使用成员类型快速读取成员对象
contents = OmniAccessor.get(parent, "{Child}/{GrandChild}/content")
Assertions.assertEquals(1, contents.size)
Assertions.assertEquals("from child", contents[0])
// 使用带下标的路径读取成员对象
Assertions.assertEquals("from 2nd children", OmniAccessor.getFirst(parent, "children[1]/{GrandChild}/content"))
Assertions.assertEquals("from 3rd children", OmniAccessor.getFirst(parent, "{Child[]}[2]/{GrandChild}/content"))
// 使用模糊路径快速读取成员对象
Assertions.assertEquals("from 1st children", OmniAccessor.getFirst(parent, "{C*[]}[0]/*/con*t"))
}
@Test
fun should_set_any_member() {
val parent = OmniConstructor.newInstance(Parent::class.java)
parent.children = OmniConstructor.newArray(Child::class.java, 3)
// 使用指定路径快速给成员对象赋值
OmniAccessor.set(parent, "child/grandChild/content", "demo child")
Assertions.assertEquals("demo child", parent.child?.grandChild?.content)
// 使用带下标的路径给成员对象赋值
OmniAccessor.set(parent, "children[1]/grandChild/content", "demo children[1]")
Assertions.assertEquals("demo children[1]", parent.children?.get(1)?.grandChild?.content)
// 使用模糊路径批量给成员对象赋值
OmniAccessor.set(parent, "child*/*/content", "demo in batch")
Assertions.assertEquals("demo in batch", parent.child?.grandChild?.content)
Assertions.assertEquals("demo in batch", parent.children?.get(0)?.grandChild?.content)
Assertions.assertEquals("demo in batch", parent.children?.get(1)?.grandChild?.content)
Assertions.assertEquals("demo in batch", parent.children?.get(2)?.grandChild?.content)
// 读写私有内部类类型的成员(使用类型名引用内部类时,无需带外部类名)
Assertions.assertEquals("", OmniAccessor.getFirst(parent, "subChild/secret"))
OmniAccessor.set(parent, "{InnerChild}/secret", "inner-class secret")
Assertions.assertEquals("inner-class secret", OmniAccessor.getFirst(parent, "subChild/secret"))
}
}