commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1547901 - in /commons/proper/beanutils/trunk/src: main/java/org/apache/commons/beanutils/WrapDynaBean.java test/java/org/apache/commons/beanutils/WrapDynaBeanTestCase.java
Date Wed, 04 Dec 2013 20:34:22 GMT
Author: oheger
Date: Wed Dec  4 20:34:21 2013
New Revision: 1547901

URL: http://svn.apache.org/r1547901
Log:
[BEANUTILS-455] WrapDynaBean now allows passing its class to the constructor.

This makes it possible to construct the class externally (using custom
configuration) and then pass the fully initialized class object to the bean
instance.

Modified:
    commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/WrapDynaBean.java
    commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/WrapDynaBeanTestCase.java

Modified: commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/WrapDynaBean.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/WrapDynaBean.java?rev=1547901&r1=1547900&r2=1547901&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/WrapDynaBean.java
(original)
+++ commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/WrapDynaBean.java
Wed Dec  4 20:34:21 2013
@@ -58,12 +58,27 @@ public class WrapDynaBean implements Dyn
      */
     public WrapDynaBean(Object instance) {
 
-        super();
-        this.instance = instance;
-        this.dynaClass = (WrapDynaClass)getDynaClass();
+        this(instance, null);
 
     }
 
+    /**
+     * Creates a new instance of {@code WrapDynaBean}, associates it with the specified
+     * JavaBean instance, and initializes the bean's {@code DynaClass}. Using this
+     * constructor this {@code WrapDynaBean} instance can be assigned a class which has
+     * been configured externally. If no {@code WrapDynaClass} is provided, a new one is
+     * created using a standard mechanism.
+     *
+     * @param instance JavaBean instance to be wrapped
+     * @param cls the optional {@code WrapDynaClass} to be used for this bean
+     * @since 1.9
+     */
+    public WrapDynaBean(Object instance, WrapDynaClass cls) {
+
+        this.instance = instance;
+        this.dynaClass = (cls != null) ? cls : (WrapDynaClass) getDynaClass();
+
+    }
 
     // ---------------------------------------------------- Instance Variables
 

Modified: commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/WrapDynaBeanTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/WrapDynaBeanTestCase.java?rev=1547901&r1=1547900&r2=1547901&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/WrapDynaBeanTestCase.java
(original)
+++ commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/WrapDynaBeanTestCase.java
Wed Dec  4 20:34:21 2013
@@ -100,6 +100,16 @@ public class WrapDynaBeanTestCase extend
      */
     public void testSimpleProperties() {
 
+        checkSimplePropertyAccess();
+
+    }
+
+
+    /**
+     * Helper method for testing whether basic access to properties works as
+     * expected.
+     */
+    private void checkSimplePropertyAccess() {
         // Invalid getter
         try {
             bean.get("invalidProperty");
@@ -132,7 +142,6 @@ public class WrapDynaBeanTestCase extend
         } catch (IllegalArgumentException t) {
             fail("Get threw exception: " + t);
         }
-
     }
 
     /**
@@ -312,4 +321,13 @@ public class WrapDynaBeanTestCase extend
 
     }
 
+    /**
+     * Tests whether a WrapDynaClass can be provided when constructing a bean.
+     */
+    public void testInitWithDynaClass() {
+        WrapDynaClass clazz = WrapDynaClass.createDynaClass(TestBean.class);
+        bean = new WrapDynaBean(new TestBean(), clazz);
+        assertSame("Wrong DynaClass", clazz, bean.getDynaClass());
+        checkSimplePropertyAccess();
+    }
 }



Mime
View raw message