1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
149 }
150
151 }