diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html index e31655835..c7a0c43f5 100644 --- a/RELEASE-NOTES.html +++ b/RELEASE-NOTES.html @@ -71,6 +71,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
  • BlockingBuffer - now includes stack trace if InterupttedException occurs [33700]
  • BlockingBuffer - new methods that allow get and remove with a timeout [27691]
  • Transformed*Map - new factory decorateTransform() that transforms any existing entries in the map [30959]
  • +
  • PriorityBuffer - now Serializable [36163]
  • BUG FIXES

    diff --git a/data/test/PriorityBuffer.emptyCollection.version3.2.obj b/data/test/PriorityBuffer.emptyCollection.version3.2.obj new file mode 100644 index 000000000..74bce63b6 Binary files /dev/null and b/data/test/PriorityBuffer.emptyCollection.version3.2.obj differ diff --git a/data/test/PriorityBuffer.fullCollection.version3.2.obj b/data/test/PriorityBuffer.fullCollection.version3.2.obj new file mode 100644 index 000000000..159afd369 Binary files /dev/null and b/data/test/PriorityBuffer.fullCollection.version3.2.obj differ diff --git a/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java b/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java index f0b735bbb..3eb7f23bc 100644 --- a/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java @@ -15,6 +15,7 @@ */ package org.apache.commons.collections.buffer; +import java.io.Serializable; import java.util.AbstractCollection; import java.util.Comparator; import java.util.Iterator; @@ -57,8 +58,13 @@ import org.apache.commons.collections.BufferUnderflowException; * @author Michael A. Smith * @author Paul Jack * @author Stephen Colebourne + * @author Steve Phelps */ -public class PriorityBuffer extends AbstractCollection implements Buffer { +public class PriorityBuffer extends AbstractCollection + implements Buffer, Serializable { + + /** Serialization lock. */ + private static final long serialVersionUID = 6891186490470027896L; /** * The default capacity for the buffer. diff --git a/src/test/org/apache/commons/collections/buffer/TestPriorityBuffer.java b/src/test/org/apache/commons/collections/buffer/TestPriorityBuffer.java index 95ac1e42b..cf3a653c7 100644 --- a/src/test/org/apache/commons/collections/buffer/TestPriorityBuffer.java +++ b/src/test/org/apache/commons/collections/buffer/TestPriorityBuffer.java @@ -15,10 +15,12 @@ */ package org.apache.commons.collections.buffer; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; +import java.util.Iterator; import java.util.Random; import junit.framework.Test; @@ -37,6 +39,7 @@ import org.apache.commons.collections.comparators.ReverseComparator; * @version $Revision$ $Date$ * * @author Michael A. Smith + * @author Steve Phelps */ public class TestPriorityBuffer extends AbstractTestCollection { @@ -306,7 +309,6 @@ public class TestPriorityBuffer extends AbstractTestCollection { protected void checkOrder(PriorityBuffer h) { Integer lastNum = null; Integer num = null; - boolean fail = false; while (!h.isEmpty()) { num = (Integer) h.remove(); if (h.ascendingOrder) { @@ -336,5 +338,70 @@ public class TestPriorityBuffer extends AbstractTestCollection { } return buffer.toString(); } - + + /** + * Generates 500 randomly initialized heaps of size 100 + * and tests that after serializing and restoring them to a byte array + * that the following conditions hold: + * + * - the size of the restored heap is the same + * as the size of the orignal heap + * + * - all elements in the original heap are present in the restored heap + * + * - the heap order of the restored heap is intact as + * verified by checkOrder() + */ + public void testSerialization() { + int iterations = 500; + int heapSize = 100; + PriorityBuffer h; + Random randGenerator = new Random(); + for (int i = 0; i < iterations; i++) { + if (i < iterations / 2) { + h = new PriorityBuffer(true); + } else { + h = new PriorityBuffer(false); + } + for (int r = 0; r < heapSize; r++) { + h.add(new Integer(randGenerator.nextInt(heapSize))); + } + assertTrue(h.size() == heapSize); + PriorityBuffer h1 = serializeAndRestore(h); + assertTrue(h1.size() == heapSize); + Iterator hit = h.iterator(); + while (hit.hasNext()) { + Integer n = (Integer) hit.next(); + assertTrue(h1.contains(n)); + } + checkOrder(h1); + } + } + + public PriorityBuffer serializeAndRestore(PriorityBuffer h) { + PriorityBuffer h1 = null; + try { + byte[] objekt = writeExternalFormToBytes(h); + h1 = (PriorityBuffer) readExternalFormFromBytes(objekt); + } catch (IOException e) { + e.printStackTrace(); + fail(e.toString()); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + fail(e.toString()); + } + return h1; + } + + public String getCompatibilityVersion() { + return "3.2"; + } + +// public void testCreate() throws Exception { +// resetEmpty(); +// writeExternalFormToDisk((java.io.Serializable) collection, "C:/commons/collections/data/test/PriorityBuffer.emptyCollection.version3.2.obj"); +// resetFull(); +// writeExternalFormToDisk((java.io.Serializable) collection, "C:/commons/collections/data/test/PriorityBuffer.fullCollection.version3.2.obj"); +// } + }