Author: bayard
Date: Fri May 16 01:09:42 2008
New Revision: 656960
URL:
http://svn.apache.org/viewvc?rev=656960&view=revLog:
Making MultiValueMap serializable as per COLLECTIONS-240
Added:
commons/proper/collections/trunk/data/test/MultiValueMap.emptyCollection.version3.3.obj (with props)
commons/proper/collections/trunk/data/test/MultiValueMap.fullCollection.version3.3.obj (with props)
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/MultiValueMap.java
commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestMultiValueMap.java
Added: commons/proper/collections/trunk/data/test/MultiValueMap.emptyCollection.version3.3.obj
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/data/test/MultiValueMap.emptyCollection.version3.3.obj?rev=656960&view=auto==============================================================================
Binary file - no diff available.
Propchange: commons/proper/collections/trunk/data/test/MultiValueMap.emptyCollection.version3.3.obj
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: commons/proper/collections/trunk/data/test/MultiValueMap.fullCollection.version3.3.obj
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/data/test/MultiValueMap.fullCollection.version3.3.obj?rev=656960&view=auto==============================================================================
Binary file - no diff available.
Propchange: commons/proper/collections/trunk/data/test/MultiValueMap.fullCollection.version3.3.obj
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/MultiValueMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/MultiValueMap.java?rev=656960&r1=656959&r2=656960&view=diff==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/MultiValueMap.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/MultiValueMap.java Fri May 16 01:09:42 2008
@@ -16,6 +16,11 @@
*/
package org.apache.commons.collections.map;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
@@ -61,7 +66,10 @@
* @version $Revision$ $Date$
* @since Commons Collections 3.2
*/
-public class MultiValueMap extends AbstractMapDecorator implements MultiMap {
+public class MultiValueMap extends AbstractMapDecorator implements MultiMap, Serializable {
+
+ /** Serialization version */
+ private static final long serialVersionUID = -2214159910087182007L;
/** The factory for creating value collections. */
private final Factory collectionFactory;
@@ -126,6 +134,32 @@
//-----------------------------------------------------------------------
/**
+ * Write the map out using a custom routine.
+ *
+ * @param out the output stream
+ * @throws IOException
+ * @since Commons Collections 3.3
+ */
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ out.defaultWriteObject();
+ out.writeObject(map);
+ }
+
+ /**
+ * Read the map in using a custom routine.
+ *
+ * @param in the input stream
+ * @throws IOException
+ * @throws ClassNotFoundException
+ * @since Commons Collections 3.3
+ */
+ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+ in.defaultReadObject();
+ map = (Map) in.readObject();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
* Clear the map.
*/
public void clear() {
@@ -424,7 +458,7 @@
/**
* Inner class that provides a simple reflection factory.
*/
- private static class ReflectionFactory implements Factory {
+ private static class ReflectionFactory implements Factory, Serializable {
private final Class clazz;
public ReflectionFactory(Class clazz) {
Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestMultiValueMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestMultiValueMap.java?rev=656960&r1=656959&r2=656960&view=diff==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestMultiValueMap.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestMultiValueMap.java Fri May 16 01:09:42 2008
@@ -32,6 +32,8 @@
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.MultiMap;
+import org.apache.commons.collections.AbstractTestObject;
+
/**
* TestMultiValueMap.
*
@@ -39,7 +41,7 @@
* @author Stephen Colebourne
* @since Commons Collections 3.2
*/
-public class TestMultiValueMap extends TestCase {
+public class TestMultiValueMap extends AbstractTestObject {
public TestMultiValueMap(String testName) {
super(testName);
@@ -353,4 +355,55 @@
assertEquals(new MultiValueMap(), map);
}
+ //-----------------------------------------------------------------------
+ // Manual serialization testing as this class cannot easily
+ // extend the AbstractTestMap
+ //-----------------------------------------------------------------------
+
+ public String getCompatibilityVersion() {
+ return "3.3";
+ }
+
+ public Object makeObject() {
+ Map m = makeEmptyMap();
+ m.put("a", "1");
+ m.put("a", "1b");
+ m.put("b", "2");
+ m.put("c", "3");
+ m.put("c", "3b");
+ m.put("d", "4");
+ return m;
+ }
+
+ private Map makeEmptyMap() {
+ return new MultiValueMap();
+ }
+
+// public void testCreate() throws Exception {
+// writeExternalFormToDisk(
+// (java.io.Serializable) makeEmptyMap(),
+// "/tmp/MultiValueMap.emptyCollection.version3.3.obj");
+//
+// writeExternalFormToDisk(
+// (java.io.Serializable) makeObject(),
+// "/tmp/MultiValueMap.fullCollection.version3.3.obj");
+// }
+
+ public void testEmptyMapCompatibility() throws Exception {
+ Map map = makeEmptyMap();
+ Map map2 = (Map) readExternalFormFromDisk(getCanonicalEmptyCollectionName(map));
+ assertEquals("Map is empty", 0, map2.size());
+ }
+ public void testFullMapCompatibility() throws Exception {
+ Map map = (Map) makeObject();
+ Map map2 = (Map) readExternalFormFromDisk(getCanonicalFullCollectionName(map));
+ assertEquals("Map is the right size", map.size(), map2.size());
+ for (Iterator it = map.keySet().iterator(); it.hasNext();) {
+ Object key = it.next();
+ assertEquals( "Map had inequal elements", map.get(key), map2.get(key) );
+ map2.remove(key);
+ }
+ assertEquals("Map had extra values", 0, map2.size());
+ }
+
}