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   * @version $Id:$
31   * @see org.codehaus.plexus.util.ReflectionUtils
32   * @since 3.4.0
33   */
34  public final class ReflectionUtilsTest {
35      public ReflectionUtilsTestClass testClass = new ReflectionUtilsTestClass();
36  
37      /**
38       * <p>testSimpleVariableAccess.</p>
39       *
40       * @throws java.lang.IllegalAccessException if any.
41       */
42      @Test
43      public void testSimpleVariableAccess() throws IllegalAccessException {
44          assertEquals("woohoo", (String) ReflectionUtils.getValueIncludingSuperclasses("myString", testClass));
45      }
46  
47      /**
48       * <p>testComplexVariableAccess.</p>
49       *
50       * @throws java.lang.IllegalAccessException if any.
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       * <p>testSuperClassVariableAccess.</p>
64       *
65       * @throws java.lang.IllegalAccessException if any.
66       */
67      @Test
68      public void testSuperClassVariableAccess() throws IllegalAccessException {
69          assertEquals("super-duper", (String) ReflectionUtils.getValueIncludingSuperclasses("mySuperString", testClass));
70      }
71  
72      /**
73       * <p>testSettingVariableValue.</p>
74       *
75       * @throws java.lang.IllegalAccessException if any.
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 }