001package org.apache.commons.beanutils2;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020import static java.lang.reflect.Modifier.isPublic;
021import static org.junit.Assert.assertArrayEquals;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertFalse;
024import static org.junit.Assert.assertNotNull;
025import static org.junit.Assert.assertNull;
026import static org.junit.Assert.assertTrue;
027
028import java.lang.reflect.Method;
029
030import org.apache.commons.beanutils2.testbeans.TestBean;
031import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
032import org.junit.Test;
033
034public class MethodsRegistryTestCase
035{
036
037    private final AccessibleObjectsRegistry<Method> methodsRegistry = AccessibleObjectsRegistry.getMethodsRegistry();
038
039    @Test
040    public void getWithName()
041    {
042        Method[] methods = TestBean.class.getMethods();
043        for ( Method method : methods )
044        {
045            Method methodFromRegistry =
046                methodsRegistry.get( false, TestBean.class, method.getName(), method.getParameterTypes() );
047            assertNotNull( methodFromRegistry );
048            assertEquals( method, methodFromRegistry );
049        }
050    }
051
052    @Test
053    public void getWithNameDifferentParameterType()
054        throws Exception
055    {
056        Method methodFromBean = TestBean.class.getMethod( "setBooleanProperty", boolean.class );
057        Method methodFromRegistry =
058            methodsRegistry.get( false, TestBean.class, methodFromBean.getName(), Boolean.class );
059        assertEquals( methodFromBean, methodFromRegistry );
060    }
061
062    @Test
063    public void getExactWithName()
064    {
065        Method[] methods = TestBean.class.getMethods();
066        for ( Method method : methods )
067        {
068            Method methodFromRegistry =
069                methodsRegistry.get( true, TestBean.class, method.getName(), method.getParameterTypes() );
070            assertNotNull( methodFromRegistry );
071            assertEquals( method, methodFromRegistry );
072        }
073    }
074
075    @Test
076    public void getExactWithNameDifferentParameterType()
077        throws Exception
078    {
079        Method methodFromBean = TestBean.class.getMethod( "setBooleanProperty", boolean.class );
080        Method methodFromRegistry = methodsRegistry.get( true, TestBean.class, methodFromBean.getName(), Boolean.class );
081        assertNull( methodFromRegistry );
082    }
083
084    @Test
085    public void resolveDirectly()
086        throws Exception
087    {
088        Method[] methods = TestBean.class.getMethods();
089        for ( Method method : methods )
090        {
091            Method methodFromRegistry =
092                methodsRegistry.resolveDirectly( TestBean.class, method.getName(), method.getParameterTypes() );
093            assertEquals( method, methodFromRegistry );
094        }
095    }
096
097    @Test
098    public void getAccessibleObjectsArray()
099    {
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}