Author: manik.surtani(a)jboss.com
Date: 2008-09-23 10:52:05 -0400 (Tue, 23 Sep 2008)
New Revision: 6780
Modified:
core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
core/trunk/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/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java 2008-09-23
11:39:22 UTC (rev 6779)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java 2008-09-23
14:52:05 UTC (rev 6780)
@@ -45,6 +45,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;
@@ -170,7 +171,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/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java 2008-09-23
11:39:22 UTC (rev 6779)
+++
core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java 2008-09-23
14:52:05 UTC (rev 6780)
@@ -18,6 +18,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;
@@ -34,6 +36,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
{
CacheMarshaller200 cm200 = new CacheMarshaller200();
@@ -208,3 +242,20 @@
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;
+ }
+}
Show replies by date