View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import static java.lang.reflect.Modifier.isPublic;
21  import static org.junit.Assert.assertArrayEquals;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.lang.reflect.Method;
29  
30  import org.apache.commons.beanutils2.testbeans.TestBean;
31  import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
32  import org.junit.Test;
33  
34  public class MethodsRegistryTestCase
35  {
36  
37      private final AccessibleObjectsRegistry<Method> methodsRegistry = AccessibleObjectsRegistry.getMethodsRegistry();
38  
39      @Test
40      public void getWithName()
41      {
42          Method[] methods = TestBean.class.getMethods();
43          for ( Method method : methods )
44          {
45              Method methodFromRegistry =
46                  methodsRegistry.get( false, TestBean.class, method.getName(), method.getParameterTypes() );
47              assertNotNull( methodFromRegistry );
48              assertEquals( method, methodFromRegistry );
49          }
50      }
51  
52      @Test
53      public void getWithNameDifferentParameterType()
54          throws Exception
55      {
56          Method methodFromBean = TestBean.class.getMethod( "setBooleanProperty", boolean.class );
57          Method methodFromRegistry =
58              methodsRegistry.get( false, TestBean.class, methodFromBean.getName(), Boolean.class );
59          assertEquals( methodFromBean, methodFromRegistry );
60      }
61  
62      @Test
63      public void getExactWithName()
64      {
65          Method[] methods = TestBean.class.getMethods();
66          for ( Method method : methods )
67          {
68              Method methodFromRegistry =
69                  methodsRegistry.get( true, TestBean.class, method.getName(), method.getParameterTypes() );
70              assertNotNull( methodFromRegistry );
71              assertEquals( method, methodFromRegistry );
72          }
73      }
74  
75      @Test
76      public void getExactWithNameDifferentParameterType()
77          throws Exception
78      {
79          Method methodFromBean = TestBean.class.getMethod( "setBooleanProperty", boolean.class );
80          Method methodFromRegistry = methodsRegistry.get( true, TestBean.class, methodFromBean.getName(), Boolean.class );
81          assertNull( methodFromRegistry );
82      }
83  
84      @Test
85      public void resolveDirectly()
86          throws Exception
87      {
88          Method[] methods = TestBean.class.getMethods();
89          for ( Method method : methods )
90          {
91              Method methodFromRegistry =
92                  methodsRegistry.resolveDirectly( TestBean.class, method.getName(), method.getParameterTypes() );
93              assertEquals( method, methodFromRegistry );
94          }
95      }
96  
97      @Test
98      public void getAccessibleObjectsArray()
99      {
100         Method[] methods = methodsRegistry.getAccessibleObjectsArray( TestBean.class );
101         assertNotNull( methods );
102         assertArrayEquals( TestBean.class.getMethods(), methods );
103     }
104 
105     @Test
106     public void matches()
107     {
108         Method[] methods = TestBean.class.getMethods();
109         for ( Method method : methods )
110         {
111             assertTrue( methodsRegistry.matches( method, method.getName() ) );
112             assertFalse( methodsRegistry.matches( method, "This should not match..." ) );
113         }
114     }
115 
116     @Test
117     public void getParameterTypes()
118     {
119         Method[] methods = TestBean.class.getMethods();
120         for ( Method method : methods )
121         {
122             Class<?>[] parameterTypes = methodsRegistry.getParameterTypes( method );
123             assertArrayEquals( method.getParameterTypes(), parameterTypes );
124         }
125     }
126 
127     @Test
128     public void resolveAccessibleNull()
129     {
130         Method method = methodsRegistry.resolveAccessible( TestBean.class, null );
131         assertNull( method );
132     }
133 
134     @Test
135     public void resolveAccessible()
136     {
137         Method[] methods = ThrowingExceptionBean.class.getDeclaredMethods();
138         for ( Method method : methods )
139         {
140             Method methodFromRegistry = methodsRegistry.resolveAccessible( ThrowingExceptionBean.class, method );
141             if ( isPublic( method.getModifiers() ) )
142             {
143                 assertEquals( method, methodFromRegistry );
144             } else {
145                 assertNull( methodFromRegistry );
146             }
147         }
148         // TODO this only test part of resolveAccessible() how can we test the rest?
149     }
150 
151 }