You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2013/02/05 19:54:04 UTC

svn commit: r1442693 - in /commons/sandbox/beanutils2/trunk/src: main/java/org/apache/commons/beanutils2/ test/java/org/apache/commons/beanutils2/ test/java/org/apache/commons/beanutils2/testbeans/

Author: britter
Date: Tue Feb  5 18:54:04 2013
New Revision: 1442693

URL: http://svn.apache.org/viewvc?rev=1442693&view=rev
Log:
Implement setting of mapped properties

Added:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java   (with props)
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetterAccessor.java   (with props)
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetMappedPropertyTestCase.java   (with props)
Modified:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanProperties.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanProperties.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanProperties.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanProperties.java?rev=1442693&r1=1442692&r2=1442693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanProperties.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanProperties.java Tue Feb  5 18:54:04 2013
@@ -134,4 +134,7 @@ public interface BeanProperties<B>
      */
     Map<String, PropertyDescriptor> getPropertiesIndex()
         throws IntrospectionException;
+
+    Method getMappedWriteMethod( String propertyName )
+        throws IntrospectionException;
 }

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java?rev=1442693&r1=1442692&r2=1442693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java Tue Feb  5 18:54:04 2013
@@ -177,8 +177,17 @@ final class DefaultBeanAccessor<B>
      */
     public MappedPropertySetterAccessor<B> setMapped( String propertyName )
     {
-        // TODO
-        return null;
+        checkNotNull( propertyName, "Parameter 'propertyName' must not be null!" );
+        Method mappedWriteMethod;
+        try
+        {
+            mappedWriteMethod = properties.getMappedWriteMethod( propertyName );
+        }
+        catch ( IntrospectionException e )
+        {
+            throw new NoSuchPropertyException( propertyName, getBeanClass(), e );
+        }
+        return new DefaultMappedPropertySetterAccessor<B>( bean, propertyName, mappedWriteMethod);
     }
 
     /**

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanProperties.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanProperties.java?rev=1442693&r1=1442692&r2=1442693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanProperties.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanProperties.java Tue Feb  5 18:54:04 2013
@@ -218,4 +218,12 @@ final class DefaultBeanProperties<B>
         return registry.getPropertiesIndex( beanClass );
     }
 
+    public Method getMappedWriteMethod( String propertyName )
+        throws IntrospectionException
+    {
+        MappedPropertyDescriptor mappedPropertyDescriptor = getMappedPropertyDescriptor( propertyName );
+
+        return checkSetterMethod( mappedPropertyDescriptor.getMappedWriteMethod(), propertyName );
+    }
+
 }

Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java?rev=1442693&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java (added)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java Tue Feb  5 18:54:04 2013
@@ -0,0 +1,83 @@
+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 org.apache.commons.beanutils2.Assertions.checkArgument;
+import static org.apache.commons.beanutils2.TypeUtils.isAssignmentCompatible;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+class DefaultMappedPropertySetter<B>
+    implements BeanPropertySetter<B>
+{
+    // FIXME Code in this class is almost identical with DefaultIndexedPropertySetter
+    private final B bean;
+
+    private final String propertyName;
+
+    private final Method mappedSetterMethod;
+
+    private final String key;
+
+    public DefaultMappedPropertySetter( B bean, String propertyName, Method mappedSetterMethod, String key )
+    {
+        this.bean = bean;
+        this.propertyName = propertyName;
+        this.mappedSetterMethod = mappedSetterMethod;
+        this.key = key;
+    }
+
+    public <V> BeanAccessor<B> with( V value )
+    {
+        Class<?> paramType = mappedSetterMethod.getParameterTypes()[1];
+
+        if ( value == null )
+        {
+            checkArgument( !paramType.isPrimitive(), "Null can not be assigned to a primitive property!" );
+        }
+        else
+        {
+            checkArgument( isAssignmentCompatible( paramType, value.getClass() ),
+                           "Value of type %s is not compatible to parameter type %s!", value.getClass().getName(),
+                           paramType.getName() );
+        }
+
+        invokeSetter( value );
+
+        return new DefaultBeanAccessor<B>( bean );
+    }
+
+    private <V> void invokeSetter( V value )
+    {
+        try
+        {
+            mappedSetterMethod.invoke( bean, key, value );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new PropertySetterNotAccessibleException( propertyName, mappedSetterMethod.getName(),
+                                                            bean.getClass(), e );
+        }
+        catch ( InvocationTargetException e )
+        {
+            throw new PropertySetterInvocationException( propertyName, mappedSetterMethod.getName(), bean.getClass(), e );
+        }
+    }
+
+}

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

Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetterAccessor.java?rev=1442693&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetterAccessor.java (added)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetterAccessor.java Tue Feb  5 18:54:04 2013
@@ -0,0 +1,46 @@
+package org.apache.commons.beanutils2;
+
+import static org.apache.commons.beanutils2.Assertions.checkNotNull;
+
+import java.lang.reflect.Method;
+
+/*
+ * 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.
+ */
+
+class DefaultMappedPropertySetterAccessor<B>
+    implements MappedPropertySetterAccessor<B>
+{
+
+    private final B bean;
+    private final String propertyName;
+    private final Method mappedSetterMethod;
+
+    public DefaultMappedPropertySetterAccessor( B bean, String propertyName, Method mappedSetterMethod )
+    {
+        this.bean = bean;
+        this.propertyName = propertyName;
+        this.mappedSetterMethod = mappedSetterMethod;
+    }
+
+    public BeanPropertySetter<B> of( String key )
+    {
+        checkNotNull( key, "Can not get mapped property '%s' in bean of type '%s' for null!", propertyName,
+                      bean.getClass().getName() );
+        return new DefaultMappedPropertySetter<B>( bean, propertyName, mappedSetterMethod, key );
+    }
+
+}

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

Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetMappedPropertyTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetMappedPropertyTestCase.java?rev=1442693&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetMappedPropertyTestCase.java (added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetMappedPropertyTestCase.java Tue Feb  5 18:54:04 2013
@@ -0,0 +1,115 @@
+package org.apache.commons.beanutils2;
+
+import static org.apache.commons.beanutils2.BeanUtils.on;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.apache.commons.beanutils2.testbeans.TestBean;
+import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SetMappedPropertyTestCase
+{
+
+    private TestBean testBean;
+
+    private ThrowingExceptionBean exceptionBean;
+
+    @Before
+    public void setUp()
+    {
+        testBean = new TestBean();
+        exceptionBean = new ThrowingExceptionBean();
+    }
+
+    @Test( expected = NullPointerException.class )
+    public void setMappedNull()
+    {
+        on( testBean ).setMapped( null );
+    }
+
+    @Test( expected = NoSuchPropertyException.class )
+    public void setMappedUnknown()
+    {
+        on( testBean ).setMapped( "unknown" );
+    }
+
+    @Test( expected = IllegalArgumentException.class )
+    public void getMappedNotMappedProperty()
+        throws Exception
+    {
+        on( testBean ).getMapped( "intProperty" );
+    }
+
+    @Test( expected = NullPointerException.class )
+    public void getMappedIntNullKey()
+        throws Exception
+    {
+        on( testBean ).getMapped( "mappedIntProperty" ).of( null );
+    }
+
+    @Test
+    public void setMappedUnknownKey()
+        throws Exception
+    {
+        assertNull( testBean.getMappedProperty( "unknownKey" ) );
+
+        String value = "value";
+        on( testBean ).setMapped( "mappedProperty" ).of( "unknownKey" ).with( value );
+        assertEquals( value, testBean.getMappedProperty( "unknownKey" ) );
+    }
+
+    @Test
+    public void setMappedMappedProperty()
+        throws Exception
+    {
+        String value = "value";
+        on( testBean ).setMapped( "mappedProperty" ).of( "First Key" ).with( value );
+        assertEquals( value, testBean.getMappedProperty( "First Key" ) );
+    }
+
+    @Test
+    public void setMappedMappedIntProperty()
+        throws Exception
+    {
+        // FIXME we have to call the getter to initialize the internal map. Change TestBean instead?
+        testBean.getMappedIntProperty( "One" );
+        on( testBean ).setMapped( "mappedIntProperty" ).of( "One" ).with( Integer.valueOf( 5 ) );
+        assertEquals( 5, testBean.getMappedIntProperty( "One" ) );
+    }
+
+    @Test
+    public void setMappedMappedObjects()
+        throws Exception
+    {
+        String value = "Value";
+        on( testBean ).setMapped( "mappedObjects" ).of( "First Key" ).with( value );
+        assertEquals( value, testBean.getMappedObjects( "First Key" ) );
+    }
+
+    @Test( expected = PropertySetterInvocationException.class )
+    public void setExceptionMapped()
+    {
+        on( exceptionBean ).setMapped( "exceptionMapped" ).of( "A Key" ).with( new RuntimeException() );
+    }
+
+    @Test( expected = NoSuchPropertyException.class )
+    public void getPrivateMapped()
+    {
+        on( exceptionBean ).setMapped( "privateMapped" );
+    }
+
+    @Test( expected = NoSuchPropertyException.class )
+    public void getProtectedMapped()
+    {
+        on( exceptionBean ).setMapped( "protecedMapped" );
+    }
+
+    @Test( expected = NoSuchPropertyException.class )
+    public void getDefaultMapped()
+    {
+        on( exceptionBean ).setMapped( "defaultMapped" );
+    }
+
+}

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

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java?rev=1442693&r1=1442692&r2=1442693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java (original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java Tue Feb  5 18:54:04 2013
@@ -1,22 +1,20 @@
 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
+ * 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
+ *      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.
+ * 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 org.apache.commons.beanutils2.BeanUtils.on;

Modified: 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=1442693&r1=1442692&r2=1442693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java (original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testbeans/ThrowingExceptionBean.java Tue Feb  5 18:54:04 2013
@@ -140,7 +140,7 @@ public class ThrowingExceptionBean
         // do nothing
     }
 
-    public int getExceptionMapped( String key)
+    public RuntimeException getExceptionMapped( String key)
     {
         throw new RuntimeException( "Get indexed always throws an exception!" );
     }