View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * This is used to test ReflectionUtils for correctness.
28   *
29   * @author Jesse McConnell
30   * @see org.codehaus.plexus.util.ReflectionUtils
31   * @since 3.4.0
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  }