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 junit.framework.TestCase;
20  
21  import java.util.Map;
22  import java.util.HashMap;
23  
24  /**
25   * This is used to test ReflectionUtils for correctness.
26   *
27   * @author Jesse McConnell
28   * @version $Id:$
29   * @see org.codehaus.plexus.util.ReflectionUtils
30   */
31  public final class ReflectionUtilsTest
32      extends TestCase
33  {
34      public ReflectionUtilsTestClass testClass = new ReflectionUtilsTestClass();
35  
36      protected void setUp()
37          throws Exception
38      {
39  
40      }
41  
42      public void testSimpleVariableAccess()
43          throws IllegalAccessException
44      {
45          assertEquals( "woohoo", (String) ReflectionUtils.getValueIncludingSuperclasses( "myString", testClass ) );
46      }
47  
48      public void testComplexVariableAccess()
49          throws IllegalAccessException
50      {
51          Map<String, Object> map = ReflectionUtils.getVariablesAndValuesIncludingSuperclasses( testClass );
52  
53          Map myMap = (Map) map.get( "myMap" );
54  
55          assertEquals( "myValue", (String) myMap.get( "myKey" ) );
56          assertEquals( "myOtherValue", (String) myMap.get( "myOtherKey" ) );
57  
58      }
59  
60      public void testSuperClassVariableAccess()
61          throws IllegalAccessException
62      {
63          assertEquals( "super-duper",
64                        (String) ReflectionUtils.getValueIncludingSuperclasses( "mySuperString", testClass ) );
65      }
66  
67      public void testSettingVariableValue()
68          throws IllegalAccessException
69      {
70          ReflectionUtils.setVariableValueInObject( testClass, "mySettableString", "mySetString" );
71  
72          assertEquals( "mySetString",
73                        (String) ReflectionUtils.getValueIncludingSuperclasses( "mySettableString", testClass ) );
74  
75          ReflectionUtils.setVariableValueInObject( testClass, "myParentsSettableString", "myParentsSetString" );
76  
77          assertEquals( "myParentsSetString",
78                        (String) ReflectionUtils.getValueIncludingSuperclasses( "myParentsSettableString", testClass ) );
79      }
80  
81      private class ReflectionUtilsTestClass
82          extends AbstractReflectionUtilsTestClass
83  
84      {
85          private String myString = "woohoo";
86  
87          private String mySettableString;
88  
89          private Map<String, String> myMap = new HashMap<String, String>();
90  
91          public ReflectionUtilsTestClass()
92          {
93              myMap.put( "myKey", "myValue" );
94              myMap.put( "myOtherKey", "myOtherValue" );
95          }
96      }
97  
98      private class AbstractReflectionUtilsTestClass
99      {
100         private String mySuperString = "super-duper";
101 
102         private String myParentsSettableString;
103     }
104 }