1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.junit.jupiter.api.Test;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25
26
27
28
29
30
31
32
33
34 public final class ReflectionUtilsTest {
35 public ReflectionUtilsTestClass testClass = new ReflectionUtilsTestClass();
36
37
38
39
40
41
42 @Test
43 public void testSimpleVariableAccess() throws IllegalAccessException {
44 assertEquals("woohoo", (String) ReflectionUtils.getValueIncludingSuperclasses("myString", testClass));
45 }
46
47
48
49
50
51
52 @org.junit.jupiter.api.Test
53 public void testComplexVariableAccess() throws IllegalAccessException {
54 Map<String, Object> map = ReflectionUtils.getVariablesAndValuesIncludingSuperclasses(testClass);
55
56 Map myMap = (Map) map.get("myMap");
57
58 assertEquals("myValue", (String) myMap.get("myKey"));
59 assertEquals("myOtherValue", (String) myMap.get("myOtherKey"));
60 }
61
62
63
64
65
66
67 @Test
68 public void testSuperClassVariableAccess() throws IllegalAccessException {
69 assertEquals("super-duper", (String) ReflectionUtils.getValueIncludingSuperclasses("mySuperString", testClass));
70 }
71
72
73
74
75
76
77 @Test
78 public void testSettingVariableValue() throws IllegalAccessException {
79 ReflectionUtils.setVariableValueInObject(testClass, "mySettableString", "mySetString");
80
81 assertEquals(
82 "mySetString", (String) ReflectionUtils.getValueIncludingSuperclasses("mySettableString", testClass));
83
84 ReflectionUtils.setVariableValueInObject(testClass, "myParentsSettableString", "myParentsSetString");
85
86 assertEquals("myParentsSetString", (String)
87 ReflectionUtils.getValueIncludingSuperclasses("myParentsSettableString", testClass));
88 }
89
90 private class ReflectionUtilsTestClass extends AbstractReflectionUtilsTestClass {
91
92 private String myString = "woohoo";
93
94 private String mySettableString;
95
96 private Map<String, String> myMap = new HashMap<String, String>();
97
98 public ReflectionUtilsTestClass() {
99 myMap.put("myKey", "myValue");
100 myMap.put("myOtherKey", "myOtherValue");
101 }
102 }
103
104 private class AbstractReflectionUtilsTestClass {
105 private String mySuperString = "super-duper";
106
107 private String myParentsSettableString;
108 }
109 }