Author: simonetripodi
Date: Mon Jul 23 11:06:22 2012
New Revision: 1364579

URL: http://svn.apache.org/viewvc?rev=1364579&view=rev
Log:
[SANDBOX-429] Implement unit tests for MethodsRegistry - patch provided by 
Benedikt Ritter

Added:
    
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java
   (with props)
    
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/
    
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java
   (with props)
Modified:
    commons/sandbox/beanutils2/trunk/src/changes/changes.xml

Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/changes/changes.xml?rev=1364579&r1=1364578&r2=1364579&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Mon Jul 23 
11:06:22 2012
@@ -23,6 +23,9 @@
   </properties>
   <body>
   <release version="0.1" date="201?-??-??" description="First release.">
+    <action dev="simonetripodi" type="add" issue="SANDBOX-429" 
due-to="Benedikt Ritter">
+      Implement unit tests for MethodsRegistry
+    </action>
     <action dev="simonetripodi" type="add" issue="SANDBOX-428" 
due-to="Benedikt Ritter">
       Implement unit tests for ConstructorRegistry
     </action>

Added: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java?rev=1364579&view=auto
==============================================================================
--- 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java
 (added)
+++ 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java
 Mon Jul 23 11:06:22 2012
@@ -0,0 +1,150 @@
+package org.apache.commons.beanutils2;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import static java.lang.reflect.Modifier.isPublic;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Method;
+
+import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
+import org.junit.Test;
+
+public class MethodsRegistryTestCase
+{
+
+    private final AccessibleObjectsRegistry<Method> methodsRegistry = 
AccessibleObjectsRegistry.getMethodsRegistry();
+
+    @Test
+    public void getWithName()
+    {
+        Method[] methods = TestBean.class.getMethods();
+        for ( Method method : methods )
+        {
+            Method methodFromRegistry =
+                methodsRegistry.get( false, TestBean.class, method.getName(), 
method.getParameterTypes() );
+            assertNotNull( methodFromRegistry );
+            assertEquals( method, methodFromRegistry );
+        }
+    }
+
+    @Test
+    public void getWithNameDifferentParameterType()
+        throws Exception
+    {
+        Method methodFromBean = TestBean.class.getMethod( 
"setBooleanProperty", boolean.class );
+        Method methodFromRegistry =
+            methodsRegistry.get( false, TestBean.class, 
methodFromBean.getName(), Boolean.class );
+        assertEquals( methodFromBean, methodFromRegistry );
+    }
+
+    @Test
+    public void getExactWithName()
+    {
+        Method[] methods = TestBean.class.getMethods();
+        for ( Method method : methods )
+        {
+            Method methodFromRegistry =
+                methodsRegistry.get( true, TestBean.class, method.getName(), 
method.getParameterTypes() );
+            assertNotNull( methodFromRegistry );
+            assertEquals( method, methodFromRegistry );
+        }
+    }
+
+    @Test
+    public void getExactWithNameDifferentParameterType()
+        throws Exception
+    {
+        Method methodFromBean = TestBean.class.getMethod( 
"setBooleanProperty", boolean.class );
+        Method methodFromRegistry = methodsRegistry.get( true, TestBean.class, 
methodFromBean.getName(), Boolean.class );
+        assertNull( methodFromRegistry );
+    }
+
+    @Test
+    public void resolveDirectly()
+        throws Exception
+    {
+        Method[] methods = TestBean.class.getMethods();
+        for ( Method method : methods )
+        {
+            Method methodFromRegistry =
+                methodsRegistry.resolveDirectly( TestBean.class, 
method.getName(), method.getParameterTypes() );
+            assertEquals( method, methodFromRegistry );
+        }
+    }
+
+    @Test
+    public void getAccessibleObjectsArray()
+    {
+        Method[] methods = methodsRegistry.getAccessibleObjectsArray( 
TestBean.class );
+        assertNotNull( methods );
+        assertArrayEquals( TestBean.class.getMethods(), methods );
+    }
+
+    @Test
+    public void matches()
+    {
+        Method[] methods = TestBean.class.getMethods();
+        for ( Method method : methods )
+        {
+            assertTrue( methodsRegistry.matches( method, method.getName() ) );
+            assertFalse( methodsRegistry.matches( method, "This should not 
match..." ) );
+        }
+    }
+
+    @Test
+    public void getParameterTypes()
+    {
+        Method[] methods = TestBean.class.getMethods();
+        for ( Method method : methods )
+        {
+            Class<?>[] parameterTypes = methodsRegistry.getParameterTypes( 
method );
+            assertArrayEquals( method.getParameterTypes(), parameterTypes );
+        }
+    }
+
+    @Test
+    public void resolveAccessibleNull()
+    {
+        Method method = methodsRegistry.resolveAccessible( TestBean.class, 
null );
+        assertNull( method );
+    }
+
+    @Test
+    public void resolveAccessible()
+    {
+        Method[] methods = ThrowingExceptionBean.class.getDeclaredMethods();
+        for ( Method method : methods )
+        {
+            Method methodFromRegistry = methodsRegistry.resolveAccessible( 
ThrowingExceptionBean.class, method );
+            if ( isPublic( method.getModifiers() ) )
+            {
+                assertEquals( method, methodFromRegistry );
+            } else {
+                assertNull( methodFromRegistry );
+            }
+        }
+        // TODO this only test part of resolveAccessible() how can we test the 
rest?
+    }
+
+}

Propchange: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MethodsRegistryTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java?rev=1364579&view=auto
==============================================================================
--- 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java
 (added)
+++ 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java
 Mon Jul 23 11:06:22 2012
@@ -0,0 +1,148 @@
+package org.apache.commons.beanutils2.testbeans;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Utility test class that is used to evaluate if exceptions are thrown 
correctly.
+ */
+public class ThrowingExceptionBean
+{
+
+    public ThrowingExceptionBean()
+    {
+        // default constructor
+    }
+
+    /**
+     * Rethrows the exception that is passed in.
+     *
+     * @param e The exception to throw
+     */
+    public ThrowingExceptionBean( RuntimeException e )
+    {
+        throw e;
+    }
+
+    @SuppressWarnings( "unused" )
+    private ThrowingExceptionBean( Integer i )
+    {
+        // used in illegal access test cases
+    }
+
+    protected ThrowingExceptionBean( Short s )
+    {
+        // used in illegal access test cases
+    }
+
+    ThrowingExceptionBean( Byte b )
+    {
+        // used in illegal access test cases
+    }
+
+    public String getExceptionProperty()
+    {
+        throw new RuntimeException();
+    }
+
+    public void setExceptionProperty( String string )
+    {
+        throw new RuntimeException();
+    }
+
+    @SuppressWarnings( "unused" )
+    // used in IllegalAccessException test cases via reflection
+    private String getPrivateProperty()
+    {
+        return "";
+    }
+
+    @SuppressWarnings( "unused" )
+    // used in IllegalAccessException test cases via reflection
+    private void setPrivateProperty()
+    {
+        // do nothing
+    }
+
+    protected String getProtectedProperty()
+    {
+        return "";
+    }
+
+    protected void setProtectedProperty()
+    {
+        // do nothing
+    }
+
+    String getDefaultProperty()
+    {
+        return "";
+    }
+
+    void setDefaultProperty()
+    {
+        // do nothing
+    }
+
+    public int getExceptionIndexed( int index )
+    {
+        throw new RuntimeException( "Get indexed always throws an exception!" 
);
+    }
+
+    public void setExceptionIndexed( int index, RuntimeException e )
+    {
+        throw e;
+    }
+
+    @SuppressWarnings( "unused" ) // used in IllegalAccessException test cases
+    private int getPrivateIndexed( int index )
+    {
+        return 1;
+    }
+
+    @SuppressWarnings( "unused" ) // used in IllegalAccessException test cases
+    private void setPrivateIndexed( int index, int value )
+    {
+        // do nothing
+    }
+
+    protected int getProtectedIndexed( int index )
+    {
+        return 1;
+    }
+
+    protected void setProtectedIndexed( int index, int value )
+    {
+        // do nothing
+    }
+
+    int getDefaultIndexed( int index )
+    {
+        return 1;
+    }
+
+    void setDefaultIndexed( int index, int value )
+    {
+        // do nothing
+    }
+
+    public static void staticException( String message )
+    {
+        throw new RuntimeException( message );
+    }
+
+}

Propchange: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: 
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to