You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2005/10/07 04:02:45 UTC

svn commit: r306976 - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/ test/org/apache/commons/beanutils/

Author: niallp
Date: Thu Oct  6 19:02:35 2005
New Revision: 306976

URL: http://svn.apache.org/viewcvs?rev=306976&view=rev
Log:
Make WrapDynaBean Serializable

Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/LazyDynaMapTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java?rev=306976&r1=306975&r2=306976&view=diff
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/WrapDynaBean.java Thu Oct  6 19:02:35 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
 
 package org.apache.commons.beanutils;
 
-
+import java.io.Serializable;
 
 
 
@@ -44,7 +44,7 @@
  * @version $Revision$ $Date$
  */
 
-public class WrapDynaBean implements DynaBean {
+public class WrapDynaBean implements DynaBean, Serializable {
 
 
     // ---------------------------------------------------------- Constructors
@@ -60,7 +60,7 @@
 
         super();
         this.instance = instance;
-        this.dynaClass = WrapDynaClass.createDynaClass(instance.getClass());
+        this.dynaClass = (WrapDynaClass)getDynaClass();
 
     }
 
@@ -72,7 +72,7 @@
      * The <code>DynaClass</code> "base class" that this DynaBean
      * is associated with.
      */
-    protected WrapDynaClass dynaClass = null;
+    protected transient WrapDynaClass dynaClass = null;
 
 
     /**
@@ -186,6 +186,10 @@
      * properties available for this DynaBean.
      */
     public DynaClass getDynaClass() {
+
+        if (dynaClass == null) {
+            dynaClass = WrapDynaClass.createDynaClass(instance.getClass());
+        }
 
         return (this.dynaClass);
 

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/LazyDynaMapTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/LazyDynaMapTestCase.java?rev=306976&r1=306975&r2=306976&view=diff
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/LazyDynaMapTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/LazyDynaMapTestCase.java Thu Oct  6 19:02:35 2005
@@ -494,8 +494,8 @@
         // containing some properties
         LazyDynaMap orig = new LazyDynaMap(new TreeMap());
         orig.set("indexProp", 0, "indexVal0");
-        orig.set("indexProp", 2, "indexVal1");
-        assertEquals("Index prop size", 1, ((List)orig.get("indexProp")).size());
+        orig.set("indexProp", 1, "indexVal1");
+        assertEquals("Index prop size", 2, ((List)orig.get("indexProp")).size());
         
         LazyDynaMap newOne = (LazyDynaMap)orig.newInstance();
         Map newMap = newOne.getMap();

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java?rev=306976&r1=306975&r2=306976&view=diff
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java Thu Oct  6 19:02:35 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.io.Serializable;
 
 
 /**
@@ -32,7 +33,7 @@
  * @version $Revision$ $Date$
  */
 
-public class TestBean {
+public class TestBean implements Serializable {
 
     // ----------------------------------------------------------- Constructors
 

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java?rev=306976&r1=306975&r2=306976&view=diff
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/WrapDynaBeanTestCase.java Thu Oct  6 19:02:35 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,7 +17,10 @@
 
 package org.apache.commons.beanutils;
 
-
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
@@ -153,13 +156,6 @@
 
     }
 
-
-    /**
-     * Suppress serialization and deserialization tests.  WrapDynaClass
-     * is not serializable.
-     */
-    public void testSerialization() { }
-    
     /** Tests getInstance method */
     public void testGetInstance() {
         AlphaBean alphaBean = new AlphaBean("Now On Air... John Peel");
@@ -177,6 +173,57 @@
         assertTrue("Object type is WrapDynaBean", createdInstance instanceof WrapDynaBean);
         WrapDynaBean dynaBean = (WrapDynaBean) createdInstance;
         assertTrue("Object type is AlphaBean", dynaBean.getInstance() instanceof AlphaBean);
+    }
+
+
+    /**
+     * Serialization and deserialization tests.
+     * (WrapDynaBean is now serializable, although WrapDynaClass still is not)
+     */
+    public void testSerialization() {
+
+        // Create a bean and set a value
+        WrapDynaBean origBean = new WrapDynaBean(new TestBean());
+        Integer newValue = new Integer(789);
+        assertEquals("origBean default", new Integer(123), (Integer)origBean.get("intProperty"));
+        origBean.set("intProperty", newValue); 
+        assertEquals("origBean new value", newValue, (Integer)origBean.get("intProperty"));
+        
+        // Serialize/Deserialize & test value
+        WrapDynaBean bean = (WrapDynaBean)serializeDeserialize(origBean, "First Test");
+        assertEquals("bean value", newValue, (Integer)bean.get("intProperty"));
+        
+    }
+
+    /**
+     * Do serialization and deserialization.
+     */
+    private Object serializeDeserialize(Object target, String text) {
+
+        // Serialize the test object
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            ObjectOutputStream oos = new ObjectOutputStream(baos);
+            oos.writeObject(target);
+            oos.flush();
+            oos.close();
+        } catch (Exception e) {
+            fail(text + ": Exception during serialization: " + e);
+        }
+
+        // Deserialize the test object
+        Object result = null;
+        try {
+            ByteArrayInputStream bais =
+                new ByteArrayInputStream(baos.toByteArray());
+            ObjectInputStream ois = new ObjectInputStream(bais);
+            result = ois.readObject();
+            bais.close();
+        } catch (Exception e) {
+            fail(text + ": Exception during deserialization: " + e);
+        }
+        return result;
+
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org