From 7cf943172b154019d7c6a66f4290272b2144e29a Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sun, 16 Mar 2008 00:26:44 +0000 Subject: [PATCH] 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 --- .../buffer/UnboundedFifoBuffer.java | 4 +++- .../collections/AbstractTestObject.java | 22 ++++++++++++------- .../buffer/TestUnboundedFifoBuffer.java | 11 ++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java b/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java index 6efc14204..964683e0e 100644 --- a/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java @@ -115,6 +115,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); + out.writeInt(buffer.length); for (Iterator it = iterator(); it.hasNext();) { out.writeObject(it.next()); } @@ -130,7 +131,8 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); int size = in.readInt(); - buffer = new Object[size + 1]; + int length = in.readInt(); + buffer = new Object[length]; for (int i = 0; i < size; i++) { buffer[i] = in.readObject(); } diff --git a/src/test/org/apache/commons/collections/AbstractTestObject.java b/src/test/org/apache/commons/collections/AbstractTestObject.java index 41647af42..256705669 100644 --- a/src/test/org/apache/commons/collections/AbstractTestObject.java +++ b/src/test/org/apache/commons/collections/AbstractTestObject.java @@ -138,17 +138,23 @@ public abstract class AbstractTestObject extends BulkTest { } } + protected Object serializeDeserialize(Object obj) throws Exception { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(buffer); + out.writeObject(obj); + out.close(); + + ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); + Object dest = in.readObject(); + in.close(); + + return dest; + } + public void testSerializeDeserializeThenCompare() throws Exception { Object obj = makeObject(); if (obj instanceof Serializable && isTestSerialization()) { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(buffer); - out.writeObject(obj); - out.close(); - - ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); - Object dest = in.readObject(); - in.close(); + Object dest = serializeDeserialize(obj); if (isEqualsCheckable()) { assertEquals("obj != deserialize(serialize(obj))", obj, dest); } diff --git a/src/test/org/apache/commons/collections/buffer/TestUnboundedFifoBuffer.java b/src/test/org/apache/commons/collections/buffer/TestUnboundedFifoBuffer.java index 2c175bfd2..aa15ff504 100644 --- a/src/test/org/apache/commons/collections/buffer/TestUnboundedFifoBuffer.java +++ b/src/test/org/apache/commons/collections/buffer/TestUnboundedFifoBuffer.java @@ -412,6 +412,17 @@ public class TestUnboundedFifoBuffer extends AbstractTestCollection { 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() { return "3.1";