Making MultiValueMap serializable as per COLLECTIONS-240
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@656960 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c1351569a7
commit
e6d4f46544
Binary file not shown.
Binary file not shown.
|
@ -16,6 +16,11 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.collections.map;
|
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.AbstractCollection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -61,7 +66,10 @@ import org.apache.commons.collections.iterators.IteratorChain;
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
* @since Commons Collections 3.2
|
* @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. */
|
/** The factory for creating value collections. */
|
||||||
private final Factory collectionFactory;
|
private final Factory collectionFactory;
|
||||||
|
@ -124,6 +132,32 @@ public class MultiValueMap extends AbstractMapDecorator implements MultiMap {
|
||||||
this.collectionFactory = collectionFactory;
|
this.collectionFactory = collectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* 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.
|
* Clear the map.
|
||||||
|
@ -424,7 +458,7 @@ public class MultiValueMap extends AbstractMapDecorator implements MultiMap {
|
||||||
/**
|
/**
|
||||||
* Inner class that provides a simple reflection factory.
|
* 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;
|
private final Class clazz;
|
||||||
|
|
||||||
public ReflectionFactory(Class clazz) {
|
public ReflectionFactory(Class clazz) {
|
||||||
|
|
|
@ -32,6 +32,8 @@ import junit.framework.TestSuite;
|
||||||
import org.apache.commons.collections.IteratorUtils;
|
import org.apache.commons.collections.IteratorUtils;
|
||||||
import org.apache.commons.collections.MultiMap;
|
import org.apache.commons.collections.MultiMap;
|
||||||
|
|
||||||
|
import org.apache.commons.collections.AbstractTestObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TestMultiValueMap.
|
* TestMultiValueMap.
|
||||||
*
|
*
|
||||||
|
@ -39,7 +41,7 @@ import org.apache.commons.collections.MultiMap;
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @since Commons Collections 3.2
|
* @since Commons Collections 3.2
|
||||||
*/
|
*/
|
||||||
public class TestMultiValueMap extends TestCase {
|
public class TestMultiValueMap extends AbstractTestObject {
|
||||||
|
|
||||||
public TestMultiValueMap(String testName) {
|
public TestMultiValueMap(String testName) {
|
||||||
super(testName);
|
super(testName);
|
||||||
|
@ -353,4 +355,55 @@ public class TestMultiValueMap extends TestCase {
|
||||||
assertEquals(new MultiValueMap(), map);
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue