Skip to content

Commit 3a0728f

Browse files
Add tests for ReflectionUtil.kt
1 parent 3c0f43a commit 3a0728f

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

utbot-core/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ apply from: "${parent.projectDir}/gradle/include/jvm-project.gradle"
77
dependencies {
88
implementation group: 'io.github.microutils', name: 'kotlin-logging', version: kotlin_logging_version
99
implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '5.5.0'
10+
11+
testImplementation project(':utbot-sample')
12+
testImplementation group: 'junit', name: 'junit', version: junit4_version
1013
}
1114

1215
shadowJar {

utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ object Reflection {
4040

4141
inline fun <R> AccessibleObject.withAccessibility(block: () -> R): R {
4242
val prevAccessibility = isAccessible
43+
isAccessible = true
44+
4345
try {
4446
return block()
4547
} finally {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.utbot.common
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Assertions.assertNotNull
5+
import org.junit.jupiter.api.Test
6+
import org.utbot.examples.reflection.ClassWithDifferentModifiers
7+
import org.utbot.examples.reflection.ClassWithDifferentModifiers.Wrapper
8+
9+
class ReflectionUtilTest {
10+
private val testedClass = ClassWithDifferentModifiers::class.java
11+
12+
@Test
13+
fun testPackagePrivateInvoke() {
14+
val method = testedClass.declaredMethods.first { it.name == "packagePrivateMethod"}
15+
val instance = ClassWithDifferentModifiers()
16+
17+
method.apply {
18+
withAccessibility {
19+
assertEquals(1, invoke(instance))
20+
}
21+
}
22+
}
23+
24+
@Test
25+
fun testPrivateInvoke() {
26+
val method = testedClass.declaredMethods.first { it.name == "privateMethod"}
27+
val instance = ClassWithDifferentModifiers()
28+
29+
method.apply {
30+
withAccessibility {
31+
assertEquals(1, invoke(instance))
32+
}
33+
}
34+
35+
}
36+
37+
@Test
38+
fun testPrivateFieldSetting() {
39+
val field = testedClass.declaredFields.first { it.name == "privateField" }
40+
val instance = ClassWithDifferentModifiers()
41+
42+
field.apply {
43+
withAccessibility {
44+
set(instance, 0)
45+
}
46+
}
47+
}
48+
49+
@Test
50+
fun testPrivateFieldGetting() {
51+
val field = testedClass.declaredFields.first { it.name == "privateField" }
52+
val instance = ClassWithDifferentModifiers()
53+
54+
field.apply {
55+
withAccessibility {
56+
assertEquals(0, get(instance))
57+
}
58+
}
59+
60+
}
61+
62+
@Test
63+
fun testPrivateFieldGettingAfterSetting() {
64+
val field = testedClass.declaredFields.first { it.name == "privateField" }
65+
val instance = ClassWithDifferentModifiers()
66+
67+
68+
field.apply {
69+
withAccessibility {
70+
set(instance, 1)
71+
}
72+
73+
withAccessibility {
74+
assertEquals(1, get(instance))
75+
}
76+
}
77+
}
78+
79+
@Test
80+
fun testPrivateStaticFinalFieldSetting() {
81+
val field = testedClass.declaredFields.first { it.name == "privateStaticFinalField" }
82+
83+
field.apply {
84+
withAccessibility {
85+
set(null, Wrapper(2))
86+
}
87+
}
88+
}
89+
90+
@Test
91+
fun testPrivateStaticFinalFieldGetting() {
92+
val field = testedClass.declaredFields.first { it.name == "privateStaticFinalField" }
93+
94+
field.apply {
95+
withAccessibility {
96+
val value = get(null) as? Wrapper
97+
assertNotNull(value)
98+
}
99+
}
100+
}
101+
102+
@Test
103+
fun testPrivateStaticFinalFieldGettingAfterSetting() {
104+
val field = testedClass.declaredFields.first { it.name == "privateStaticFinalField" }
105+
106+
field.apply {
107+
withAccessibility {
108+
set(null, Wrapper(3))
109+
}
110+
111+
withAccessibility {
112+
val value = (get(null) as? Wrapper)?.x
113+
assertEquals(3, value)
114+
}
115+
}
116+
}
117+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.utbot.examples.reflection;
2+
3+
public class ClassWithDifferentModifiers {
4+
@SuppressWarnings({"All"})
5+
private int privateField;
6+
7+
private static final Wrapper privateStaticFinalField = new Wrapper(1);
8+
9+
public ClassWithDifferentModifiers() {
10+
privateField = 0;
11+
}
12+
13+
int packagePrivateMethod() {
14+
return 1;
15+
}
16+
17+
private int privateMethod() {
18+
return 1;
19+
}
20+
21+
public static class Wrapper {
22+
public int x;
23+
public Wrapper(int x) {
24+
this.x = x;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)