[jbosscache-commits] JBoss Cache SVN: r6781 - in core/branches/2.2.X/src: test/java/org/jboss/cache/marshall and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Sep 23 10:52:26 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-09-23 10:52:26 -0400 (Tue, 23 Sep 2008)
New Revision: 6781

Modified:
   core/branches/2.2.X/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
   core/branches/2.2.X/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java
Log:
JBCACHE-1412:  Marshaller uses equals() to test object equality instead of identity when performing reference counting

Modified: core/branches/2.2.X/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
===================================================================
--- core/branches/2.2.X/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java	2008-09-23 14:52:05 UTC (rev 6780)
+++ core/branches/2.2.X/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java	2008-09-23 14:52:26 UTC (rev 6781)
@@ -28,6 +28,7 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.IdentityHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -146,7 +147,7 @@
    public void objectToObjectStream(Object o, ObjectOutputStream out, Fqn region) throws Exception
    {
       if (trace) log.trace("Marshalling object " + o);
-      Map<Object, Integer> refMap = useRefs ? new HashMap<Object, Integer>() : null;
+      Map<Object, Integer> refMap = useRefs ? new IdentityHashMap<Object, Integer>() : null;
       ClassLoader toUse = defaultClassLoader;
       Thread current = Thread.currentThread();
       ClassLoader old = current.getContextClassLoader();

Modified: core/branches/2.2.X/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java
===================================================================
--- core/branches/2.2.X/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java	2008-09-23 14:52:05 UTC (rev 6780)
+++ core/branches/2.2.X/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java	2008-09-23 14:52:26 UTC (rev 6781)
@@ -16,6 +16,8 @@
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ExecutorService;
@@ -32,6 +34,38 @@
       expectedMarshallerClass = CacheMarshaller200.class;
    }
 
+   public void testBadEquals() throws Exception
+   {
+      // object1 and Object2 should NOT be equal, even though their equals() methods are broken.
+      Broken o1 = new Broken();
+      Broken o2 = new Broken();
+
+      o1.name = "o1";
+      o2.name = "o2";
+
+      assert o1 != o2;
+      assert o1.equals(o2);
+
+      List<Broken> l = new ArrayList<Broken>(2); // lists will allow "duplicate" entries.
+      l.add(o1);
+      l.add(o2);
+
+      CacheMarshaller200 cm200 = new CacheMarshaller200();
+      ByteArrayOutputStream bout = new ByteArrayOutputStream();
+      ObjectOutputStream out = new ObjectOutputStream(bout);
+      cm200.objectToObjectStream(l, out);
+      out.close();
+      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
+      List<Broken> l2 = (List<Broken>) cm200.objectFromObjectStream(in);
+
+      assert l2.size() == 2;
+      assert l2.get(0).name.equals("o1");
+      assert l2.get(1).name.equals("o2");
+
+      assert l2.get(0) != l2.get(1);
+      assert l2.get(0).equals(l2.get(1));
+   }
+
    public void testRegionalisedStream() throws Exception
    {
       // need to test what's going on with 
@@ -205,3 +239,21 @@
       assert throwables.size() == 0 : "Should not have caught any exceptions!";
    }
 }
+
+class Broken implements Serializable
+{
+   String name;
+
+   @Override
+   public boolean equals(Object o)
+   {
+      return true;
+   }
+
+   @Override
+   public int hashCode()
+   {
+      return 10;
+   }
+}
+




More information about the jbosscache-commits mailing list