Update serialization handling with tests

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131452 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-12-24 23:09:26 +00:00
parent ae60862fd6
commit ceb49988b8
2 changed files with 71 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/AbstractMapBag.java,v 1.5 2003/12/07 01:15:36 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/AbstractMapBag.java,v 1.6 2003/12/24 23:09:26 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -79,7 +79,7 @@ import org.apache.commons.collections.set.UnmodifiableSet;
* the number of occurrences of that element in the bag.
*
* @since Commons Collections 3.0
* @version $Revision: 1.5 $ $Date: 2003/12/07 01:15:36 $
* @version $Revision: 1.6 $ $Date: 2003/12/24 23:09:26 $
*
* @author Chuck Burdick
* @author Michael A. Smith
@ -518,9 +518,10 @@ public abstract class AbstractMapBag implements Bag {
this.map = map;
int entrySize = in.readInt();
for (int i = 0; i < entrySize; i++) {
Object key = in.readObject();
int value = in.readInt();
map.put(key, new MutableInteger(value));
Object obj = in.readObject();
int count = in.readInt();
map.put(obj, new MutableInteger(count));
size += count;
}
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bag/AbstractTestBag.java,v 1.3 2003/12/02 23:36:12 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bag/AbstractTestBag.java,v 1.4 2003/12/24 23:09:26 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -57,6 +57,8 @@
*/
package org.apache.commons.collections.bag;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
@ -76,7 +78,7 @@ import org.apache.commons.collections.Bag;
* you may still use this base set of cases. Simply override the
* test case (method) your {@link Bag} fails.
*
* @version $Revision: 1.3 $ $Date: 2003/12/02 23:36:12 $
* @version $Revision: 1.4 $ $Date: 2003/12/24 23:09:26 $
*
* @author Chuck Burdick
* @author Stephen Colebourne
@ -397,4 +399,65 @@ public abstract class AbstractTestBag extends AbstractTestObject {
assertEquals(1, c);
}
//-----------------------------------------------------------------------
public void testEmptyBagSerialization() throws IOException, ClassNotFoundException {
Bag bag = makeBag();
if (!(bag instanceof Serializable)) return;
byte[] objekt = writeExternalFormToBytes((Serializable) bag);
Bag bag2 = (Bag) readExternalFormFromBytes(objekt);
assertEquals("Bag should be empty",0, bag.size());
assertEquals("Bag should be empty",0, bag2.size());
}
public void testFullBagSerialization() throws IOException, ClassNotFoundException {
Bag bag = makeBag();
bag.add("A");
bag.add("A");
bag.add("B");
bag.add("B");
bag.add("C");
int size = bag.size();
if (!(bag instanceof Serializable)) return;
byte[] objekt = writeExternalFormToBytes((Serializable) bag);
Bag bag2 = (Bag) readExternalFormFromBytes(objekt);
assertEquals("Bag should be same size", size, bag.size());
assertEquals("Bag should be same size", size, bag2.size());
}
/**
* Compare the current serialized form of the Bag
* against the canonical version in CVS.
*/
public void testEmptyBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
Bag bag = makeBag();
if(bag instanceof Serializable && !skipSerializedCanonicalTests()) {
Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
assertTrue("Bag is empty",bag2.size() == 0);
assertEquals(bag, bag2);
}
}
/**
* Compare the current serialized form of the Bag
* against the canonical version in CVS.
*/
public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
Bag bag = makeBag();
bag.add("A");
bag.add("A");
bag.add("B");
bag.add("B");
bag.add("C");
if(bag instanceof Serializable && !skipSerializedCanonicalTests()) {
Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
assertEquals("Bag is the right size",bag.size(), bag2.size());
assertEquals(bag, bag2);
}
}
}