commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils BasicDynaBeanTestCase.java
Date Wed, 18 Dec 2002 06:20:41 GMT
craigmcc    2002/12/17 22:20:41

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        BasicDynaBean.java BasicDynaClass.java
               beanutils/src/test/org/apache/commons/beanutils
                        BasicDynaBeanTestCase.java
  Log:
  Make BasicDynaBean and BasicDynaClass implement Serializable, including a
  JUnit test case method that ensures successful operation.  Based on a
  suggested patch included in the bug report (but with a slightly altered
  technique).
  
  PR: Bugzilla #15067
  Submitted by:	Peter Rossbach <pr at webapp.de> and Roman Stumm
  
  Revision  Changes    Path
  1.7       +10 -5     jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BasicDynaBean.java
  
  Index: BasicDynaBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BasicDynaBean.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BasicDynaBean.java	25 Oct 2002 00:15:50 -0000	1.6
  +++ BasicDynaBean.java	18 Dec 2002 06:20:40 -0000	1.7
  @@ -63,6 +63,7 @@
   package org.apache.commons.beanutils;
   
   
  +import java.io.Serializable;
   import java.lang.reflect.Array;
   import java.util.HashMap;
   import java.util.List;
  @@ -76,11 +77,15 @@
    * <p><strong>IMPLEMENTATION NOTE</strong> - Instances of this class
that are
    * accessed from multiple threads simultaneously need to be synchronized.</p>
    *
  + * <p><strong>IMPLEMENTATION NOTE</strong> - Instances of this class
can be
  + * successfully serialized and deserialized <strong>ONLY</strong> if all
  + * property values are <code>Serializable</code>.</p>
  + *
    * @author Craig McClanahan
    * @version $Revision$ $Date$
    */
   
  -public class BasicDynaBean implements DynaBean {
  +public class BasicDynaBean implements DynaBean, Serializable {
   
   
       // ---------------------------------------------------------- Constructors
  
  
  
  1.7       +12 -6     jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BasicDynaClass.java
  
  Index: BasicDynaClass.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BasicDynaClass.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BasicDynaClass.java	23 Jan 2002 22:35:58 -0000	1.6
  +++ BasicDynaClass.java	18 Dec 2002 06:20:40 -0000	1.7
  @@ -63,6 +63,7 @@
   package org.apache.commons.beanutils;
   
   
  +import java.io.Serializable;
   import java.lang.reflect.Constructor;
   import java.lang.reflect.InvocationTargetException;
   import java.util.HashMap;
  @@ -80,7 +81,7 @@
    * @version $Revision$ $Date$
    */
   
  -public class BasicDynaClass implements DynaClass {
  +public class BasicDynaClass implements DynaClass, Serializable {
   
   
       // ----------------------------------------------------------- Constructors
  @@ -138,7 +139,7 @@
        * The constructor of the <code>dynaBeanClass</code> that we will use
        * for creating new instances.
        */
  -    protected Constructor constructor = null;
  +    protected transient Constructor constructor = null;
   
   
       /**
  @@ -248,6 +249,11 @@
               throws IllegalAccessException, InstantiationException {
   
           try {
  +            // Refind the constructor after a deserialization (if needed)
  +            if (constructor == null) {
  +                setDynaBeanClass(this.dynaBeanClass);
  +            }
  +            // Invoke the constructor to create a new bean instance
               return ((DynaBean) constructor.newInstance(constructorValues));
           } catch (InvocationTargetException e) {
               throw new InstantiationException
  
  
  
  1.7       +83 -4     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BasicDynaBeanTestCase.java
  
  Index: BasicDynaBeanTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BasicDynaBeanTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BasicDynaBeanTestCase.java	25 Oct 2002 00:27:42 -0000	1.6
  +++ BasicDynaBeanTestCase.java	18 Dec 2002 06:20:41 -0000	1.7
  @@ -63,6 +63,11 @@
   package org.apache.commons.beanutils;
   
   
  +import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
  +import java.io.ObjectInputStream;
  +import java.io.ObjectOutputStream;
  +
   import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.List;
  @@ -676,6 +681,80 @@
           } catch (Throwable t) {
               fail("Exception: " + t);
           }
  +
  +    }
  +
  +
  +    /**
  +     * Test serialization and deserialization.
  +     */
  +    public void testSerialization() {
  +
  +        // Serialize the test bean
  +        ByteArrayOutputStream baos = new ByteArrayOutputStream();
  +        try {
  +            ObjectOutputStream oos = new ObjectOutputStream(baos);
  +            oos.writeObject(bean);
  +            oos.flush();
  +            oos.close();
  +        } catch (Exception e) {
  +            fail("Exception during serialization: " + e);
  +        }
  +
  +        // Deserialize the test bean
  +        try {
  +            bean = null;
  +            ByteArrayInputStream bais =
  +                new ByteArrayInputStream(baos.toByteArray());
  +            ObjectInputStream ois = new ObjectInputStream(bais);
  +            bean = (DynaBean) ois.readObject();
  +            bais.close();
  +        } catch (Exception e) {
  +            fail("Exception during deserialization: " + e);
  +        }
  +
  +        // Confirm property values
  +        testGetDescriptorArguments();
  +        testGetDescriptorBoolean();
  +        testGetDescriptorDouble();
  +        testGetDescriptorFloat();
  +        testGetDescriptorInt();
  +        testGetDescriptorLong();
  +        testGetDescriptorSecond();
  +        testGetDescriptorShort();
  +        testGetDescriptorString();
  +        testGetDescriptors();
  +        testGetIndexedArguments();
  +        testGetIndexedValues();
  +        testGetMappedArguments();
  +        testGetMappedValues();
  +        testGetSimpleArguments();
  +        testGetSimpleBoolean();
  +        testGetSimpleDouble();
  +        testGetSimpleFloat();
  +        testGetSimpleInt();
  +        testGetSimpleLong();
  +        testGetSimpleShort();
  +        testGetSimpleString();
  +        testMappedContains();
  +        testMappedRemove();
  +
  +        // Ensure that we can create a new instance of the same DynaClass
  +        try {
  +            bean = bean.getDynaClass().newInstance();
  +        } catch (Exception e) {
  +            fail("Exception creating new instance: " + e);
  +        }
  +        testGetDescriptorArguments();
  +        testGetDescriptorBoolean();
  +        testGetDescriptorDouble();
  +        testGetDescriptorFloat();
  +        testGetDescriptorInt();
  +        testGetDescriptorLong();
  +        testGetDescriptorSecond();
  +        testGetDescriptorShort();
  +        testGetDescriptorString();
  +        testGetDescriptors();
   
       }
   
  
  
  

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


Mime
View raw message