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 final class ReflectionUtilsTest {
34 private final ReflectionUtilsTestClass testClass = new ReflectionUtilsTestClass();
35
36 @Test
37 void simpleVariableAccess() throws Exception {
38 assertEquals("woohoo", ReflectionUtils.getValueIncludingSuperclasses("myString", testClass));
39 }
40
41 @Test
42 void complexVariableAccess() throws Exception {
43 Map<String, Object> map = ReflectionUtils.getVariablesAndValuesIncludingSuperclasses(testClass);
44
45 Map myMap = (Map) map.get("myMap");
46
47 assertEquals("myValue", myMap.get("myKey"));
48 assertEquals("myOtherValue", myMap.get("myOtherKey"));
49 }
50
51 @Test
52 void superClassVariableAccess() throws Exception {
53 assertEquals("super-duper", ReflectionUtils.getValueIncludingSuperclasses("mySuperString", testClass));
54 }
55
56 @Test
57 void settingVariableValue() throws Exception {
58 ReflectionUtils.setVariableValueInObject(testClass, "mySettableString", "mySetString");
59
60 assertEquals("mySetString", ReflectionUtils.getValueIncludingSuperclasses("mySettableString", testClass));
61
62 ReflectionUtils.setVariableValueInObject(testClass, "myParentsSettableString", "myParentsSetString");
63
64 assertEquals(
65 "myParentsSetString",
66 ReflectionUtils.getValueIncludingSuperclasses("myParentsSettableString", testClass));
67 }
68
69 @SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
70 private static class ReflectionUtilsTestClass extends AbstractReflectionUtilsTestClass {
71
72 private String myString = "woohoo";
73
74 private String mySettableString;
75
76 @SuppressWarnings("CanBeFinal")
77 private Map<String, String> myMap = new HashMap<>();
78
79 public ReflectionUtilsTestClass() {
80 myMap.put("myKey", "myValue");
81 myMap.put("myOtherKey", "myOtherValue");
82 }
83 }
84
85 @SuppressWarnings("FieldMayBeFinal")
86 private static class AbstractReflectionUtilsTestClass {
87 private String mySuperString = "super-duper";
88
89 private String myParentsSettableString;
90 }
91 }