Applying a unit test for COLLECTIONS-220. AbstractTestObject is refactored to provide a utility method that serializes and then deserializes. Dave Meikle's fix for said unit test is also applied.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@637495 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2008-03-16 00:26:44 +00:00
parent b147d16e64
commit 7cf943172b
3 changed files with 28 additions and 9 deletions

View File

@ -115,6 +115,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S
private void writeObject(ObjectOutputStream out) throws IOException { private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject(); out.defaultWriteObject();
out.writeInt(size()); out.writeInt(size());
out.writeInt(buffer.length);
for (Iterator it = iterator(); it.hasNext();) { for (Iterator it = iterator(); it.hasNext();) {
out.writeObject(it.next()); out.writeObject(it.next());
} }
@ -130,7 +131,8 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject(); in.defaultReadObject();
int size = in.readInt(); int size = in.readInt();
buffer = new Object[size + 1]; int length = in.readInt();
buffer = new Object[length];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
buffer[i] = in.readObject(); buffer[i] = in.readObject();
} }

View File

@ -138,9 +138,7 @@ public abstract class AbstractTestObject extends BulkTest {
} }
} }
public void testSerializeDeserializeThenCompare() throws Exception { protected Object serializeDeserialize(Object obj) throws Exception {
Object obj = makeObject();
if (obj instanceof Serializable && isTestSerialization()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer); ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(obj); out.writeObject(obj);
@ -149,6 +147,14 @@ public abstract class AbstractTestObject extends BulkTest {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
Object dest = in.readObject(); Object dest = in.readObject();
in.close(); in.close();
return dest;
}
public void testSerializeDeserializeThenCompare() throws Exception {
Object obj = makeObject();
if (obj instanceof Serializable && isTestSerialization()) {
Object dest = serializeDeserialize(obj);
if (isEqualsCheckable()) { if (isEqualsCheckable()) {
assertEquals("obj != deserialize(serialize(obj))", obj, dest); assertEquals("obj != deserialize(serialize(obj))", obj, dest);
} }

View File

@ -412,6 +412,17 @@ public class TestUnboundedFifoBuffer extends AbstractTestCollection {
assertEquals(0, test.tail); assertEquals(0, test.tail);
} }
//-----------------------------------------------------------------------
public void testCollections220() throws Exception {
UnboundedFifoBuffer buffer = new UnboundedFifoBuffer();
buffer = (UnboundedFifoBuffer) serializeDeserialize(buffer);
// test size() gets incremented
buffer.add("Foo");
assertEquals(1, buffer.size());
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public String getCompatibilityVersion() { public String getCompatibilityVersion() {
return "3.1"; return "3.1";