commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r894507 - in /commons/proper/collections/trunk: ./ src/java/org/apache/commons/collections/functors/ src/test/org/apache/commons/collections/
Date Tue, 29 Dec 2009 23:12:21 GMT
Author: mbenson
Date: Tue Dec 29 23:12:18 2009
New Revision: 894507

URL: http://svn.apache.org/viewvc?rev=894507&view=rev
Log:
[COLLECTIONS-343] applied patch to preserve singleton integrity with readResolve

Added:
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestUtils.java
  (with props)
Modified:
    commons/proper/collections/trunk/pom.xml
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/CloneTransformer.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionClosure.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionFactory.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FalsePredicate.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPClosure.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPTransformer.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NotNullPredicate.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NullPredicate.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/StringValueTransformer.java
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TruePredicate.java
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTransformerUtils.java

Modified: commons/proper/collections/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/pom.xml?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/pom.xml (original)
+++ commons/proper/collections/trunk/pom.xml Tue Dec 29 23:12:18 2009
@@ -157,6 +157,9 @@
       <name>Gerhard Froehlich</name>
     </contributor>
     <contributor>
+      <name>Goran Hacek</name>
+    </contributor>
+    <contributor>
       <name>Paul Jack</name>
     </contributor>
     <contributor>

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/CloneTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/CloneTransformer.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/CloneTransformer.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/CloneTransformer.java
Tue Dec 29 23:12:18 2009
@@ -69,4 +69,8 @@
         return PrototypeFactory.getInstance(input).create();
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionClosure.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionClosure.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionClosure.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionClosure.java
Tue Dec 29 23:12:18 2009
@@ -65,4 +65,8 @@
         throw new FunctorException("ExceptionClosure invoked");
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionFactory.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionFactory.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionFactory.java
Tue Dec 29 23:12:18 2009
@@ -65,4 +65,8 @@
         throw new FunctorException("ExceptionFactory invoked");
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java
Tue Dec 29 23:12:18 2009
@@ -66,4 +66,8 @@
         throw new FunctorException("ExceptionPredicate invoked");
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java
Tue Dec 29 23:12:18 2009
@@ -66,4 +66,8 @@
         throw new FunctorException("ExceptionTransformer invoked");
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FalsePredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FalsePredicate.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FalsePredicate.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FalsePredicate.java
Tue Dec 29 23:12:18 2009
@@ -76,4 +76,8 @@
         return false;
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPClosure.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPClosure.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPClosure.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPClosure.java
Tue Dec 29 23:12:18 2009
@@ -63,19 +63,8 @@
         // do nothing
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public boolean equals(Object arg0) {
-        return arg0 == this || arg0 instanceof NOPClosure<?>;
+    private Object readResolve() {
+        return INSTANCE;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public int hashCode() {
-        return System.identityHashCode(INSTANCE);
-    }
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPTransformer.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPTransformer.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NOPTransformer.java
Tue Dec 29 23:12:18 2009
@@ -38,7 +38,7 @@
 
     /**
      * Factory returning the singleton instance.
-     * 
+     *
      * @return the singleton instance
      * @since Commons Collections 3.1
      */
@@ -56,7 +56,7 @@
 
     /**
      * Transforms the input to result by doing nothing.
-     * 
+     *
      * @param input  the input object to transform
      * @return the transformed result which is the input
      */
@@ -64,4 +64,8 @@
         return input;
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NotNullPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NotNullPredicate.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NotNullPredicate.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NotNullPredicate.java
Tue Dec 29 23:12:18 2009
@@ -32,13 +32,13 @@
 
     /** Serial version UID */
     private static final long serialVersionUID = 7533784454832764388L;
-    
+
     /** Singleton predicate instance */
     public static final Predicate<Object> INSTANCE = new NotNullPredicate<Object>();
 
     /**
      * Factory returning the singleton instance.
-     * 
+     *
      * @return the singleton instance
      * @since Commons Collections 3.1
      */
@@ -56,7 +56,7 @@
 
     /**
      * Evaluates the predicate returning true if the object does not equal null.
-     * 
+     *
      * @param object  the object to evaluate
      * @return true if not null
      */
@@ -64,4 +64,8 @@
         return (object != null);
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NullPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NullPredicate.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NullPredicate.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NullPredicate.java
Tue Dec 29 23:12:18 2009
@@ -32,13 +32,13 @@
 
     /** Serial version UID */
     private static final long serialVersionUID = 7533784454832764388L;
-    
+
     /** Singleton predicate instance */
     public static final Predicate<?> INSTANCE = new NullPredicate<Object>();
 
     /**
      * Factory returning the singleton instance.
-     * 
+     *
      * @return the singleton instance
      * @since Commons Collections 3.1
      * @deprecated use {@link #nullPredicate()} instead.
@@ -50,7 +50,7 @@
 
     /**
      * Factory returning the singleton instance.
-     * 
+     *
      * @return the singleton instance
      * @since Commons Collections 3.1
      */
@@ -68,7 +68,7 @@
 
     /**
      * Evaluates the predicate returning true if the input is null.
-     * 
+     *
      * @param object  the input object
      * @return true if input is null
      */
@@ -76,4 +76,8 @@
         return (object == null);
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/StringValueTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/StringValueTransformer.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/StringValueTransformer.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/StringValueTransformer.java
Tue Dec 29 23:12:18 2009
@@ -39,7 +39,7 @@
 
     /**
      * Factory returning the singleton instance.
-     * 
+     *
      * @return the singleton instance
      * @since Commons Collections 3.1
      */
@@ -57,7 +57,7 @@
 
     /**
      * Transforms the input to result by calling <code>String.valueOf</code>.
-     * 
+     *
      * @param input  the input object to transform
      * @return the transformed result
      */
@@ -65,4 +65,8 @@
         return String.valueOf(input);
     }
 
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TruePredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TruePredicate.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TruePredicate.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TruePredicate.java
Tue Dec 29 23:12:18 2009
@@ -33,25 +33,25 @@
 
     /** Serial version UID */
     private static final long serialVersionUID = 3374767158756189740L;
-    
+
     /** Singleton predicate instance */
     public static final Predicate<?> INSTANCE = new TruePredicate<Object>();
 
     /**
      * Factory returning the singleton instance.
-     * 
+     *
      * @return the singleton instance
      * @since Commons Collections 3.1
      * @deprecated
      */
-    @Deprecated 
+    @Deprecated
     public static <T> Predicate<T> getInstance() {
         return truePredicate();
     }
 
     /**
      * Factory returning the singleton instance.
-     * 
+     *
      * @return the singleton instance
      * @since Commons Collections 3.1
      */
@@ -69,11 +69,16 @@
 
     /**
      * Evaluates the predicate returning true always.
-     * 
+     *
      * @param object  the input object
      * @return true always
      */
     public boolean evaluate(T object) {
         return true;
     }
+
+    private Object readResolve() {
+        return INSTANCE;
+    }
+
 }

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
(original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
Tue Dec 29 23:12:18 2009
@@ -27,6 +27,7 @@
 import junit.textui.TestRunner;
 
 import org.apache.commons.collections.functors.EqualPredicate;
+import org.apache.commons.collections.functors.ExceptionClosure;
 import org.apache.commons.collections.functors.FalsePredicate;
 import org.apache.commons.collections.functors.NOPClosure;
 import org.apache.commons.collections.functors.TruePredicate;
@@ -425,4 +426,24 @@
         assertEquals(ClosureUtils.nopClosure(), ClosureUtils.asClosure(null));
     }
 
+    // misc tests
+    //------------------------------------------------------------------
+
+    /**
+     * Test that all Closure singletones hold singleton pattern in
+     * serialization/deserialization process.
+     */
+    public void testSingletonPatternInSerialization() {
+        final Object[] singletones = new Object[] {
+                ExceptionClosure.INSTANCE,
+                NOPClosure.INSTANCE,
+        };
+
+        for (final Object original : singletones) {
+            TestUtils.assertSameAfterSerialization(
+                    "Singletone patern broken for " + original.getClass(),
+                    original
+            );
+        }
+    }
 }

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java
(original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java
Tue Dec 29 23:12:18 2009
@@ -27,6 +27,7 @@
 import java.util.TimeZone;
 
 import org.apache.commons.collections.functors.ConstantFactory;
+import org.apache.commons.collections.functors.ExceptionFactory;
 import org.junit.Test;
 
 /**
@@ -75,10 +76,10 @@
         }
         fail();
     }
-    
+
     // nullFactory
     //------------------------------------------------------------------
-    
+
     public void testNullFactory() {
         Factory<Object> factory = FactoryUtils.nullFactory();
         assertNotNull(factory);
@@ -88,7 +89,7 @@
 
     // constantFactory
     //------------------------------------------------------------------
-    
+
     public void testConstantFactoryNull() {
         Factory<Object> factory = FactoryUtils.constantFactory(null);
         assertNotNull(factory);
@@ -106,7 +107,7 @@
 
     // prototypeFactory
     //------------------------------------------------------------------
-    
+
     public void testPrototypeFactoryNull() {
         assertSame(ConstantFactory.NULL_INSTANCE, FactoryUtils.prototypeFactory(null));
     }
@@ -118,7 +119,7 @@
         Date created = factory.create();
         assertTrue(proto != created);
         assertEquals(proto, created);
-        
+
         // check serialisation works
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream(buffer);
@@ -136,7 +137,7 @@
         Object created = factory.create();
         assertTrue(proto != created);
         assertEquals(proto, created);
-        
+
         // check serialisation works
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream(buffer);
@@ -162,7 +163,7 @@
         Integer created = factory.create();
         assertTrue(proto != created);
         assertEquals(proto, created);
-        
+
         // check serialisation works
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream(buffer);
@@ -213,7 +214,7 @@
             return false;
         }
     }
-    
+
     @SuppressWarnings("serial")
     public static class Mock2 implements Serializable {
         private final Object iVal;
@@ -229,7 +230,7 @@
             return false;
         }
     }
-    
+
     public static class Mock3 {
         private static int cCounter = 0;
         private final int iVal;
@@ -240,10 +241,10 @@
             return iVal;
         }
     }
-    
+
     // instantiateFactory
     //------------------------------------------------------------------
-    
+
     @Test(expected=IllegalArgumentException.class)
     public void instantiateFactoryNull() {
         FactoryUtils.instantiateFactory(null);
@@ -282,4 +283,24 @@
         assertEquals(new Date(1000 * 60 * 60 * 24), created);
     }
 
+    // misc tests
+    //------------------------------------------------------------------
+
+    /**
+     * Test that all Factory singletones hold singleton pattern in
+     * serialization/deserialization process.
+     */
+    public void testSingletonPatternInSerialization() {
+        final Object[] singletones = new Object[] {
+                ExceptionFactory.INSTANCE,
+        };
+
+        for (final Object original : singletones) {
+            TestUtils.assertSameAfterSerialization(
+                    "Singletone patern broken for " + original.getClass(),
+                    original
+            );
+        }
+    }
+
 }

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java
(original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java
Tue Dec 29 23:12:18 2009
@@ -33,7 +33,10 @@
 import org.apache.commons.collections.functors.AllPredicate;
 import org.apache.commons.collections.functors.BasicPredicateTestBase;
 import org.apache.commons.collections.functors.EqualPredicate;
+import org.apache.commons.collections.functors.ExceptionPredicate;
 import org.apache.commons.collections.functors.FalsePredicate;
+import org.apache.commons.collections.functors.NotNullPredicate;
+import org.apache.commons.collections.functors.NullPredicate;
 import org.apache.commons.collections.functors.TruePredicate;
 import org.junit.Test;
 
@@ -830,6 +833,30 @@
         } catch (IllegalArgumentException ex) {}
     }
 
+    // misc tests
+    //------------------------------------------------------------------
+
+    /**
+     * Test that all Predicate singletones hold singleton pattern in
+     * serialization/deserialization process.
+     */
+    @Test public void testSingletonPatternInSerialization() {
+        final Object[] singletones = new Object[] {
+                ExceptionPredicate.INSTANCE,
+                FalsePredicate.INSTANCE,
+                NotNullPredicate.INSTANCE,
+                NullPredicate.INSTANCE,
+                TruePredicate.INSTANCE
+        };
+
+        for (final Object original : singletones) {
+            TestUtils.assertSameAfterSerialization(
+                    "Singletone patern broken for " + original.getClass(),
+                    original
+            );
+        }
+    }
+
     @Override
     protected Predicate<?> generatePredicate() {
         return truePredicate();  //Just return something to satisfy super class.

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTransformerUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTransformerUtils.java?rev=894507&r1=894506&r2=894507&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTransformerUtils.java
(original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTransformerUtils.java
Tue Dec 29 23:12:18 2009
@@ -28,10 +28,13 @@
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
 
+import org.apache.commons.collections.functors.CloneTransformer;
 import org.apache.commons.collections.functors.ConstantTransformer;
 import org.apache.commons.collections.functors.EqualPredicate;
+import org.apache.commons.collections.functors.ExceptionTransformer;
 import org.apache.commons.collections.functors.FalsePredicate;
 import org.apache.commons.collections.functors.NOPTransformer;
+import org.apache.commons.collections.functors.StringValueTransformer;
 import org.apache.commons.collections.functors.TruePredicate;
 
 /**
@@ -445,4 +448,27 @@
         assertEquals(new Date(1000L), trans.transform(Date.class));
     }
 
+    // misc tests
+    //------------------------------------------------------------------
+
+    /**
+     * Test that all Transformer singletones hold singleton pattern in
+     * serialization/deserialization process.
+     */
+    public void testSingletonPatternInSerialization() {
+        final Object[] singletones = new Object[] {
+                CloneTransformer.INSTANCE,
+                ExceptionTransformer.INSTANCE,
+                NOPTransformer.INSTANCE,
+                StringValueTransformer.INSTANCE,
+        };
+
+        for (final Object original : singletones) {
+            TestUtils.assertSameAfterSerialization(
+                    "Singletone patern broken for " + original.getClass(),
+                    original
+            );
+        }
+    }
+
 }

Added: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestUtils.java?rev=894507&view=auto
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestUtils.java
(added)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestUtils.java
Tue Dec 29 23:12:18 2009
@@ -0,0 +1,68 @@
+package org.apache.commons.collections;
+
+import static org.junit.Assert.assertSame;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+public final class TestUtils {
+
+    private TestUtils() {
+    }
+
+    /**
+     * Asserts that deserialization of the object returns the same object as the
+     * one that was serialized. Object is first serialized, then deserialized
+     * and finally check is preformed to see if original and deserialized
+     * object references are the same.
+     * <p>
+     * This method is especially good for testing singletone pattern on classes
+     * that support serialization.
+     *
+     * @param msg the identifying message for the <code>AssertionError</code>.
+     * @param o object that will be tested.
+     * @see #assertSameAfterSerialization(Object)
+     */
+    public static void assertSameAfterSerialization(String msg, Object o) {
+        try {
+            // write object to byte buffer
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final ObjectOutputStream oos = new ObjectOutputStream(baos);
+            oos.writeObject(o);
+            oos.close();
+
+            // read same object from byte buffer
+            final InputStream is = new ByteArrayInputStream(baos.toByteArray());
+            final ObjectInputStream ois = new ObjectInputStream(is);
+            final Object object = ois.readObject();
+            ois.close();
+
+            // assert that original object and deserialized objects are the same
+            assertSame(msg, o, object);
+        } catch (IOException e) {
+            // should never happen
+            throw new RuntimeException(e);
+        } catch (ClassNotFoundException e) {
+            // should never happen
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Asserts that deserialization of the object returns the same object as the
+     * one that was serialized.
+     * <p>
+     * Effect of method call is the same as:
+     * <code>assertSameAfterSerialization(null, o)</code>.
+     *
+     * @param o object that will be tested.
+     * @see #assertSameAfterSerialization(String, Object)
+     */
+    public static void assertSameAfterSerialization(Object o) {
+        assertSameAfterSerialization(null, o);
+    }
+}

Propchange: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message