commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1243720 - in /commons/sandbox/beanutils2/trunk: ./ src/main/java/org/apache/commons/beanutils2/ src/test/java/org/apache/commons/beanutils2/
Date Mon, 13 Feb 2012 22:30:54 GMT
Author: simonetripodi
Date: Mon Feb 13 22:30:54 2012
New Revision: 1243720

URL: http://svn.apache.org/viewvc?rev=1243720&view=rev
Log:
[SANDBOX-389] implement populate() on DefaultBeanAccessor - patch provided by Benedikt Ritter

Added:
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/PopulateTestCase.java
  (with props)
Modified:
    commons/sandbox/beanutils2/trunk/pom.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/DefaultBeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TestBean.java

Modified: commons/sandbox/beanutils2/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/pom.xml?rev=1243720&r1=1243719&r2=1243720&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/pom.xml (original)
+++ commons/sandbox/beanutils2/trunk/pom.xml Mon Feb 13 22:30:54 2012
@@ -99,6 +99,12 @@
       <version>11.0.1</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>

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=1243720&r1=1243719&r2=1243720&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 Feb 13 22:30:54 2012
@@ -125,7 +125,9 @@ public interface BeanAccessor<B>
     Map<String, Object> describe()
         throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
IntrospectionException;
 
-    void populate( Map<String, Object> properties );
+    void populate( Map<String, Object> properties )
+        throws IllegalAccessException, IllegalArgumentException, IntrospectionException,
InvocationTargetException,
+        NoSuchMethodException;
 
     // methods invocation
 

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=1243720&r1=1243719&r2=1243720&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 Feb 13 22:30:54 2012
@@ -250,8 +250,20 @@ final class DefaultBeanAccessor<B>
      * {@inheritDoc}
      */
     public void populate( Map<String, Object> properties )
+        throws IllegalAccessException, IllegalArgumentException, IntrospectionException,
InvocationTargetException,
+        NoSuchMethodException
     {
-        // TODO
+        for ( Entry<String, Object> entry : properties.entrySet() )
+        {
+            if ( entry.getKey() != null )
+            {
+                PropertyDescriptor propertyDescriptor = registry.getPropertyDescriptor( bean.getClass(),
entry.getKey() );
+                if ( propertyDescriptor != null && propertyDescriptor.getWriteMethod()
!= null )
+                {
+                    set( entry.getKey() ).with( entry.getValue() );
+                }
+            }
+        }
     }
 
     /**

Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/PopulateTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/PopulateTestCase.java?rev=1243720&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/PopulateTestCase.java
(added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/PopulateTestCase.java
Mon Feb 13 22:30:54 2012
@@ -0,0 +1,180 @@
+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.BeanUtils.on;
+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.util.HashMap;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PopulateTestCase
+{
+
+    private TestBean target;
+
+    private Map<String, Object> properties;
+
+    @Before
+    public void setUp()
+    {
+        target = new TestBean();
+        properties = new HashMap<String, Object>();
+    }
+
+    @After
+    public void tearDown()
+    {
+        target = null;
+        properties = null;
+    }
+
+    @Test
+    public void populateEmptry()
+        throws Exception
+    {
+        on( target ).populate( properties );
+        assertTrue( target.equals( new TestBean() ) );
+    }
+
+    @Test( expected = NullPointerException.class )
+    public void populateNull()
+        throws Exception
+    {
+        on( target ).populate( null );
+    }
+
+    @Test
+    public void populateWithNullKey()
+        throws Exception
+    {
+        properties.put( null, new Object() );
+        on( target ).populate( properties );
+        assertTrue( target.equals( new TestBean() ) );
+    }
+
+    @Test
+    public void populateNullValue()
+        throws Exception
+    {
+        target.setStringProperty( "Hello World!" );
+        properties.put( "stringProperty", null );
+        on( target ).populate( properties );
+        assertNull( target.getStringProperty() );
+    }
+
+    @Test
+    public void populateUnknownKey()
+        throws Exception
+    {
+        properties.put( "unkown", "String value" );
+        on( target ).populate( properties );
+        assertTrue( target.equals( new TestBean() ) );
+    }
+
+    @Test( expected = IllegalArgumentException.class )
+    public void populateNullToPrimitive()
+        throws Exception
+    {
+        properties.put( "intProperty", null );
+        on( target ).populate( properties );
+    }
+
+    @Test
+    public void populatePropertyArrays()
+        throws Exception
+    {
+        setUpArrayProperties();
+        on( target ).populate( properties );
+        validateArrayPropertiesOnTarget();
+    }
+
+    private void setUpArrayProperties()
+    {
+        int intArray[] = new int[] { 123, 456, 789 };
+        String stringArray[] = new String[] { "New String 0", "New String 1" };
+        properties.put( "intArray", intArray );
+        properties.put( "stringArray", stringArray );
+    }
+
+    private void validateArrayPropertiesOnTarget()
+    {
+        int[] intArray = target.getIntArray();
+        assertNotNull( "intArray is not present", intArray );
+        assertEquals( "intArray has the worng length", 3, intArray.length );
+        assertEquals( "intArray[0] has the wrong value", 123, intArray[0] );
+        assertEquals( "intArray[1] has the wrong value", 456, intArray[1] );
+        assertEquals( "intArray[2] has the wrong value", 789, intArray[2] );
+        String[] stringArray = target.getStringArray();
+        assertNotNull( "stringArray is not present", stringArray );
+        assertEquals( "stringArray has the wrong length", 2, stringArray.length );
+        assertEquals( "stringArray[0] has the wrong value", "New String 0", stringArray[0]
);
+        assertEquals( "stringArray[1] has the wrong value", "New String 1", stringArray[1]
);
+    }
+
+    @Test
+    public void populateScalar()
+        throws Exception
+    {
+        setUpScalarProperties();
+        on( target ).populate( properties );
+        validateScalarPropertiesOnTarget();
+    }
+
+    private void setUpScalarProperties()
+    {
+        properties.put( "booleanProperty", false );
+        // booleanSecond is left at true
+        properties.put( "byteProperty", (byte) 111 );
+        properties.put( "doubleProperty", 432.0 );
+        // floatProperty is left at 123.0
+        properties.put( "intProperty", 543 );
+        properties.put( "longProperty", 123456789l );
+        properties.put( "nullProperty", "Non-null value" );
+        properties.put( "shortProperty", (short) 654 );
+        // stringProperty is left at "This is a string"
+        properties.put( "writeOnlyProperty", "New writeOnlyProperty value" );
+        properties.put( "readOnlyProperty", "New readOnlyProperty value" );
+    }
+
+    private void validateScalarPropertiesOnTarget()
+    {
+        assertEquals( "booleanProperty has the wrong value!", false, target.getBooleanProperty()
);
+        assertEquals( "booleanSecond has the wrong value!", true, target.isBooleanSecond()
);
+        assertEquals( "byteProperty has the wrong value!", (byte) 111, target.getByteProperty()
);
+        assertEquals( "doubleProperty has the wrong value!", 432.0, target.getDoubleProperty(),
0.005 );
+        assertEquals( "floatProperty has the wrong value!", (float) 123.0, target.getFloatProperty(),
(float) 0.005 );
+        assertEquals( "intProperty has the wrong value!", 543, target.getIntProperty() );
+        assertEquals( "longProperty has the wrong value!", 123456789l, target.getLongProperty()
);
+        assertEquals( "nullProperty has the wrong value!", "Non-null value", target.getNullProperty()
);
+        assertEquals( "shortProperty has the wrong value!", (short) 654, target.getShortProperty()
);
+        assertEquals( "stringProperty has the wrong value!", "This is a string", target.getStringProperty()
);
+        assertEquals( "writeOnlyProperty has the wrong value!", "New writeOnlyProperty value",
+                      target.getWriteOnlyPropertyValue() );
+        assertEquals( "readOnlyProperty has the wrong value!", new TestBean().getReadOnlyProperty(),
+                      target.getReadOnlyProperty() );
+    }
+
+}

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

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

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

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TestBean.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TestBean.java?rev=1243720&r1=1243719&r2=1243720&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TestBean.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TestBean.java
Mon Feb 13 22:30:54 2012
@@ -19,11 +19,13 @@ package org.apache.commons.beanutils2;
  * under the License.
  */
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.io.Serializable;
+
+import org.apache.commons.lang3.builder.EqualsBuilder;
 
 /**
  * General purpose test bean for JUnit tests for the "beanutils" component.
@@ -272,7 +274,7 @@ public class TestBean
     /**
      * An integer array property accessed as an indexed property.
      */
-    private int intIndexed[] = { 0, 10, 20, 30, 40 };
+    private final int intIndexed[] = { 0, 10, 20, 30, 40 };
 
     public int getIntIndexed( int index )
     {
@@ -505,7 +507,7 @@ public class TestBean
     /**
      * A read-only String property.
      */
-    private String readOnlyProperty = "Read Only String Property";
+    private final String readOnlyProperty = "Read Only String Property";
 
     public String getReadOnlyProperty()
     {
@@ -545,7 +547,7 @@ public class TestBean
     /**
      * A String array property accessed as an indexed property.
      */
-    private String[] stringIndexed = { "String 0", "String 1", "String 2", "String 3", "String
4" };
+    private final String[] stringIndexed = { "String 0", "String 1", "String 2", "String
3", "String 4" };
 
     public String getStringIndexed( int index )
     {
@@ -629,6 +631,59 @@ public class TestBean
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals( Object obj )
+    {
+        if ( obj == this )
+        {
+            return true;
+        }
+        if ( obj == null )
+        {
+            return false;
+        }
+        if ( !( obj instanceof TestBean ) )
+        {
+            return false;
+        }
+
+        TestBean rhs = (TestBean) obj;
+        return new EqualsBuilder()
+                        .append( this.booleanProperty, rhs.booleanProperty )
+                        .append( this.booleanSecond,rhs.booleanSecond )
+                        .append( this.invalidBoolean,rhs.invalidBoolean )
+                        .append( this.byteProperty,rhs.byteProperty )
+                        .append( this.dateProperty,rhs.dateProperty )
+                        .append( this.doubleProperty,rhs.doubleProperty )
+                        .append( this.floatProperty,rhs.floatProperty )
+                        .append( this.intProperty,rhs.intProperty )
+                        .append( this.longProperty,rhs.longProperty )
+                        .append( this.shortProperty,rhs.shortProperty )
+                        .append( this.nullProperty,rhs.nullProperty )
+                        .append( this.anotherNested, rhs.anotherNested )
+                        .append( this.dateArrayProperty, rhs.dateArrayProperty )
+                        .append( this.dupProperty, rhs.dupProperty )
+                        .append( this.intArray, rhs.intArray )
+                        .append( this.intIndexed, rhs.intIndexed )
+                        .append( this.listIndexed, rhs.listIndexed )
+                        .append( this.mappedIntProperty, rhs.mappedIntProperty )
+                        .append( this.mappedNested, rhs.mappedNested )
+                        .append( this.mappedObjects, rhs.mappedObjects )
+                        .append( this.mappedProperty, rhs.mappedProperty )
+                        .append( this.mapProperty, rhs.mapProperty )
+                        .append( this.nested, rhs.nested )
+                        .append( this.readOnlyProperty, rhs.readOnlyProperty )
+                        .append( this.string2dArray, rhs.string2dArray )
+                        .append( this.stringArray, rhs.stringArray)
+                        .append( this.stringIndexed, rhs.stringIndexed )
+                        .append( this.stringProperty, rhs.stringProperty )
+                        .append( this.writeOnlyProperty, rhs.writeOnlyProperty )
+                        .build();
+    }
+
     // ------------------------------------------------------- Static Variables
 
     /**



Mime
View raw message