You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2005/04/16 21:55:38 UTC

svn commit: r161600 - in jakarta/commons/proper/chain/trunk/src: java/org/apache/commons/chain/impl/ContextBase.java test/org/apache/commons/chain/impl/ContextBaseTestCase.java

Author: craigmcc
Date: Sat Apr 16 12:55:37 2005
New Revision: 161600

URL: http://svn.apache.org/viewcvs?view=rev&rev=161600
Log:
Make ContextBase live up to the Serializable contract that it inherits
by virtue of extending HashMap, and add a unit test to prove it works.
Fixed in nightly build 20050417.

However, this unit test pointed out a problem with the design of the
concrete WebContext subclasses (ServletWebContext, PortletWebContext,
FacesWebContext).  It is not going to be possible to make these classes
Serializable because the underlying container objects they wrap are not
Serializable -- and there is no way to un-inherit the "implements Serializable"
characteristic of the ContextBase base class.

PR:  Bugzilla #34409
Submitted By:  Jeff Ramsdale <jramsdale AT solutionsiq.com>

Modified:
    jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/impl/ContextBase.java
    jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/impl/ContextBaseTestCase.java

Modified: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/impl/ContextBase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/impl/ContextBase.java?view=diff&r1=161599&r2=161600
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/impl/ContextBase.java (original)
+++ jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/impl/ContextBase.java Sat Apr 16 12:55:37 2005
@@ -90,19 +90,25 @@
     // ------------------------------------------------------ Instance Variables
 
 
+    // NOTE - PropertyDescriptor instances are not Serializable, so the
+    // following variables must be declared as transient.  When a ContextBase
+    // instance is deserialized, the no-arguments constructor is called,
+    // and the initialize() method called there will repoopulate them.
+    // Therefore, no special restoration activity is required.
+
     /**
      * <p>The <code>PropertyDescriptor</code>s for all JavaBeans properties
      * of this {@link Context} implementation class, keyed by property name.
      * This collection is allocated only if there are any JavaBeans
      * properties.</p>
      */
-    private Map descriptors = null;
+    private transient Map descriptors = null;
 
 
     /**
      * <p>The same <code>PropertyDescriptor</code>s as an array.</p>
      */
-    private PropertyDescriptor[] pd = null;
+    private transient PropertyDescriptor[] pd = null;
 
 
     /**

Modified: jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/impl/ContextBaseTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/impl/ContextBaseTestCase.java?view=diff&r1=161599&r2=161600
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/impl/ContextBaseTestCase.java (original)
+++ jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/impl/ContextBaseTestCase.java Sat Apr 16 12:55:37 2005
@@ -16,6 +16,10 @@
 package org.apache.commons.chain.impl;
 
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -27,6 +31,7 @@
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 import org.apache.commons.chain.Context;
+import org.apache.commons.chain.web.WebContext;
 
 
 
@@ -336,6 +341,47 @@
         assertTrue(context.containsValue("baz value"));
 
     }
+
+
+    // Test serialization
+    public void testSeriaization() throws Exception {
+
+        // ContextBase is implicitly declared Serializable because it
+        // extends HashMap.  However, it is not possible to make
+        // the concrete subclasses of WebContext Serializable, because
+        // the underlying container objects that they wrap will not be.
+        // Therefore, skip testing serializability of these implementations
+        if (context instanceof WebContext) {
+            return;
+        }
+
+        // Set up the context with some parameters
+        context.put("foo", "foo value");
+        context.put("bar", "bar value");
+        context.put("baz", "baz value");
+        checkAttributeCount(3);
+
+        // Serialize to a byte array
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(context);
+        oos.close();
+
+        // Deserialize back to a new object
+        ByteArrayInputStream bais =
+          new ByteArrayInputStream(baos.toByteArray());
+        ObjectInputStream ois = new ObjectInputStream(bais);
+        context = (Context) ois.readObject();
+        ois.close();
+
+        // Do some rudimentary checks to make sure we have the same contents
+        assertTrue(context.containsKey("foo"));
+        assertTrue(context.containsKey("bar"));
+        assertTrue(context.containsKey("baz"));
+        checkAttributeCount(3);
+
+    }
+
 
 
     // -------------------------------------------------------- Support Methods



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