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/01/30 16:37:21 UTC
svn commit: r1237721 - in /commons/sandbox/beanutils2/trunk/src: changes/
main/java/org/apache/commons/beanutils2/
test/java/org/apache/commons/beanutils2/
Author: simonetripodi
Date: Mon Jan 30 15:37:20 2012
New Revision: 1237721
URL: http://svn.apache.org/viewvc?rev=1237721&view=rev
Log:
[SANDBOX-373] Implement setProperty( String name ) on DefaultBeanAccessor - patch provided by Benedikt Ritter
Added:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java (with props)
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java (with props)
Modified:
commons/sandbox/beanutils2/trunk/src/changes/changes.xml
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanPropertySetter.java
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/changes/changes.xml?rev=1237721&r1=1237720&r2=1237721&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Mon Jan 30 15:37:20 2012
@@ -23,6 +23,9 @@
</properties>
<body>
<release version="0.1" date="201?-??-??" description="First release.">
+ <action dev="simonetripodi" type="update" issue="SANDBOX-373" due-to="Benedikt Ritter">
+ Implement setProperty( String name ) on DefaultBeanAccessor
+ </action>
<action dev="simonetripodi" type="update" issue="SANDBOX-371" due-to="Benedikt Ritter">
Make sure that a property is readable in DefaultBeanAccessor.getProperty( String name )
</action>
Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java?rev=1237721&r1=1237720&r2=1237721&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java Mon Jan 30 15:37:20 2012
@@ -42,7 +42,7 @@ public interface BeanAccessor<B>
// set
BeanPropertySetter<B> setProperty( String name )
- throws IllegalAccessException, InvocationTargetException, NoSuchMethodException;
+ throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException;
// clone
Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanPropertySetter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanPropertySetter.java?rev=1237721&r1=1237720&r2=1237721&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanPropertySetter.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanPropertySetter.java Mon Jan 30 15:37:20 2012
@@ -1,5 +1,7 @@
package org.apache.commons.beanutils2;
+import java.lang.reflect.InvocationTargetException;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -20,6 +22,7 @@ package org.apache.commons.beanutils2;
public interface BeanPropertySetter<B>
{
- <V> BeanAccessor<B> withValue( V value );
+ <V> BeanAccessor<B> withValue( V value )
+ throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
}
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=1237721&r1=1237720&r2=1237721&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 Mon Jan 30 15:37:20 2012
@@ -85,10 +85,18 @@ final class DefaultBeanAccessor<B>
}
public BeanPropertySetter<B> setProperty( String name )
- throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
+ throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException
{
- // TODO Auto-generated method stub
- return null;
+ PropertyDescriptor propertyDescriptor = registry.getPropertyDescriptor( bean.getClass(), name );
+ propertyDescriptor = checkNotNull( propertyDescriptor, "Property '%s' does not exist in bean of type %s",
+ name, bean.getClass().getName() );
+
+ if ( propertyDescriptor.getWriteMethod() == null )
+ {
+ throw new NoSuchMethodException( String.format( "Bean of type %s does not provide a setter for property '%s'!",
+ bean.getClass().getName(), name ) );
+ }
+ return new DefaultBeanPropertySetter<B>( bean, propertyDescriptor.getWriteMethod() );
}
public B cloneBean()
Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java?rev=1237721&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java (added)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java Mon Jan 30 15:37:20 2012
@@ -0,0 +1,64 @@
+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;
+
+final class DefaultBeanPropertySetter<B>
+ implements BeanPropertySetter<B>
+{
+
+ private final B bean;
+
+ private final Method setterMethod;
+
+ public DefaultBeanPropertySetter( B bean, Method setterMethod )
+ {
+ this.bean = bean;
+ this.setterMethod = setterMethod;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public <V> BeanAccessor<B> withValue( V value )
+ throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
+ {
+ Class<?> paramType = setterMethod.getParameterTypes()[0];
+
+ 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() );
+ }
+
+ setterMethod.invoke( bean, value );
+
+ return new DefaultBeanAccessor<B>( bean );
+ }
+
+}
Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: 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=1237721&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java (added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java Mon Jan 30 15:37:20 2012
@@ -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.assertFalse;
+import static org.junit.Assert.assertNull;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/*
+ * 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.
+ */
+
+public class SetPropertyTestCase
+{
+
+ private TestBean testBean;
+
+ @Before
+ public void setUp()
+ {
+ testBean = new TestBean();
+ }
+
+ @After
+ public void tearDown()
+ {
+ testBean = null;
+ }
+
+ @Test
+ public void setSimpleBoolean()
+ throws Exception
+ {
+ on( testBean ).setProperty( "booleanProperty" ).withValue( false );
+ assertFalse( testBean.getBooleanProperty() );
+ }
+
+ @Test
+ public void setSimpleByte()
+ throws Exception
+ {
+ byte byteValue = (byte) 36;
+ on( testBean ).setProperty( "byteProperty" ).withValue( byteValue );
+ assertEquals( byteValue, testBean.getByteProperty() );
+ }
+
+ // TODO test methods for all setters
+
+ /**
+ * Test if setting null to an object property works.
+ */
+ @Test
+ public void setPropertyToNull() throws Exception
+ {
+ on(testBean).setProperty( "stringProperty" ).withValue( null );
+ assertNull( testBean.getStringProperty() );
+ }
+
+ /**
+ * Tests if trying to set a nonexistent property causes an NPE.
+ */
+ @Test( expected = NullPointerException.class )
+ public void setNonExistentProperty()
+ throws Exception
+ {
+ on( testBean ).setProperty( "nonExistent" ).withValue( null );
+ }
+
+ /**
+ * Test if trying to set a property with an incompatible value type causes an IllegalArgumentException.
+ */
+ @Test( expected = IllegalArgumentException.class )
+ public void setPropertyWithInCompatibleValue()
+ throws Exception
+ {
+ on( testBean ).setProperty( "booleanProperty" ).withValue( 'x' );
+ }
+
+ /**
+ * Tests if setting null to a primitive property causes an IllegalArgumentException.
+ */
+ @Test( expected = IllegalArgumentException.class )
+ public void setNullToPrimitiveProperty()
+ throws Exception
+ {
+ on( testBean ).setProperty( "booleanProperty" ).withValue( null );
+ }
+
+ /**
+ * Test if trying to set a read only property causes a NoSuchMethodException.
+ */
+ @Test( expected = NoSuchMethodException.class )
+ public void setReadOnlyProperty()
+ throws Exception
+ {
+ on( testBean ).setProperty( "readOnlyProperty" ).withValue( 15 );
+ }
+
+}
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetPropertyTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain