You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/07/23 12:59:51 UTC

svn commit: r1364578 - in /commons/sandbox/beanutils2/trunk/src: changes/changes.xml test/java/org/apache/commons/beanutils2/ConstructorRegistryTestCase.java

Author: simonetripodi
Date: Mon Jul 23 10:59:51 2012
New Revision: 1364578

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

Added:
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConstructorRegistryTestCase.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=1364578&r1=1364577&r2=1364578&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Mon Jul 23 10:59:51 2012
@@ -23,6 +23,9 @@
   </properties>
   <body>
   <release version="0.1" date="201?-??-??" description="First release.">
+    <action dev="simonetripodi" type="add" issue="SANDBOX-428" due-to="Benedikt Ritter">
+      Implement unit tests for ConstructorRegistry
+    </action>
     <action dev="simonetripodi" type="add" issue="SANDBOX-427" due-to="Benedikt Ritter">
       Suppress m2e plugin life cycle mapping errors
     </action>

Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConstructorRegistryTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConstructorRegistryTestCase.java?rev=1364578&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConstructorRegistryTestCase.java (added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConstructorRegistryTestCase.java Mon Jul 23 10:59:51 2012
@@ -0,0 +1,204 @@
+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.String.format;
+import static java.lang.reflect.Modifier.isPublic;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Constructor;
+
+import org.junit.Test;
+
+public class ConstructorRegistryTestCase
+{
+
+    private static final String WRONG_FOUND = "ConstructorRegistry resolved wrong constructor!";
+
+    private static final String NON_FOUND = "ConstructorRegistry could not resolve constructor %s!";
+
+    private final AccessibleObjectsRegistry<Constructor<?>> constructorRegistry =
+        AccessibleObjectsRegistry.getConstructorsRegistry();
+
+    @Test
+    public void get()
+    {
+        Constructor<?>[] constructors = TestBean.class.getConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            Constructor<?> resolved = constructorRegistry.get( false, TestBean.class, constructor.getParameterTypes() );
+            assertNotNull( format( NON_FOUND, constructor ), resolved );
+            assertEquals( WRONG_FOUND, constructor, resolved );
+        }
+    }
+
+    @Test
+    public void getDifferentParameter()
+        throws Exception
+    {
+        Constructor<TestBean> floatConstructor = TestBean.class.getConstructor( float.class );
+        Constructor<?> resolved = constructorRegistry.get( false, TestBean.class, Float.class );
+        assertNotNull( format( NON_FOUND, floatConstructor ), resolved );
+        assertEquals( WRONG_FOUND, floatConstructor, resolved );
+    }
+
+    @Test
+    public void getExact()
+    {
+        Constructor<?>[] constructors = TestBean.class.getConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            Constructor<?> resolved = constructorRegistry.get( true, TestBean.class, constructor.getParameterTypes() );
+            assertNotNull( format( NON_FOUND, constructor ), resolved );
+            assertEquals( WRONG_FOUND, constructor, resolved );
+        }
+    }
+
+    @Test
+    public void getExactDifferentParameter()
+        throws Exception
+    {
+        Constructor<?> resolved = constructorRegistry.get( true, TestBean.class, Float.class );
+        assertNull( format( "Constructor resolved constructor [%s] for parameter type Float on type TestBean although TestBean does not describe such a constructor.",
+                            resolved ), resolved );
+    }
+
+    @Test
+    public void getWithName()
+    {
+        // TODO does the constructorRegistry need this method? constructors don't have names.
+        Constructor<?>[] constructors = TestBean.class.getConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            Constructor<?> resolved = constructorRegistry.get( true, TestBean.class,
+                                                               "name is ignored by Constructors Registry",
+                                                               constructor.getParameterTypes() );
+            assertNotNull( format( NON_FOUND, constructor ), resolved );
+            assertEquals( WRONG_FOUND, constructor, resolved );
+        }
+    }
+
+    @Test
+    public void resolveDirectly()
+        throws Exception
+    {
+        Constructor<?>[] constructors = TestBean.class.getConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            Constructor<?> resolved =
+                constructorRegistry.resolveDirectly( TestBean.class, null, constructor.getParameterTypes() );
+            assertNotNull( format( NON_FOUND, constructor ), resolved );
+            assertEquals( WRONG_FOUND, constructor, resolved );
+        }
+    }
+
+    @Test
+    public void getAccessibleObjectsArrayForTestBean()
+    {
+        Constructor<?>[] constructors = constructorRegistry.getAccessibleObjectsArray( TestBean.class );
+        assertNotNull( constructors );
+        assertArrayEquals( TestBean.class.getConstructors(), constructors );
+    }
+
+    @Test
+    public void matchesNull()
+    {
+        // this method always returns true
+        boolean matches = constructorRegistry.matches( null, null );
+        assertTrue( matches );
+    }
+
+    @Test
+    public void matches()
+    {
+        // this method always returns true
+        Constructor<?>[] constructors = TestBean.class.getConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            boolean matches = constructorRegistry.matches( constructor, "this returns true" );
+            assertTrue( matches );
+        }
+    }
+
+    @Test
+    public void getParameterTypes()
+    {
+        Constructor<?>[] constructors = TestBean.class.getDeclaredConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            Class<?>[] typesFromRegistry = constructorRegistry.getParameterTypes( constructor );
+            assertArrayEquals( constructor.getParameterTypes(), typesFromRegistry );
+        }
+    }
+
+    @Test
+    public void resolveAccessibleNull()
+    {
+        assertNull( constructorRegistry.resolveAccessible( Object.class, null ) );
+    }
+
+    @Test
+    public void resolveAccessible()
+    {
+        Constructor<?>[] constructors = TestBean.class.getDeclaredConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            Constructor<?> resolved = constructorRegistry.resolveAccessible( TestBean.class, constructor );
+            if ( isPublic( constructor.getModifiers() ) )
+            {
+                assertEquals( constructor, resolved );
+            }
+            else
+            {
+                assertNull( resolved );
+            }
+        }
+    }
+
+    @Test
+    public void resolveAccessiblePrivateClass()
+    {
+        Constructor<?>[] constructors = PrivateTestBean.class.getDeclaredConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            Constructor<?> resolved = constructorRegistry.resolveAccessible( TestBean.class, constructor );
+            // PrivateTestBean is a private class, so no constructor is accessible.
+            assertNull( resolved );
+        }
+    }
+
+    @SuppressWarnings( "unused" ) // class is used for tests only
+    private static class PrivateTestBean
+    {
+
+        public PrivateTestBean()
+        {
+            // empty default constructor
+        }
+
+        private PrivateTestBean( int i )
+        {
+            // int constructor
+        }
+    }
+
+}

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

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

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