From 15ee56bd0f91ef807a3e729ab3410a834ef3fcef Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 11 Nov 2013 13:52:13 +0000 Subject: [PATCH] Add more tests for queue interface. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1540719 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections4/queue/AbstractQueueTest.java | 180 +++++++++++++++++- 1 file changed, 177 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java index 57428c899..7a7c48ad8 100644 --- a/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.Queue; import org.apache.commons.collections4.collection.AbstractCollectionTest; @@ -37,8 +38,6 @@ import org.apache.commons.collections4.collection.AbstractCollectionTest; * test case (method) your {@link Queue} fails or override one of the * protected methods from AbstractCollectionTest. * - * TODO: add more tests for Queue interface - * * @since 4.0 * @version $Id$ */ @@ -133,6 +132,181 @@ public abstract class AbstractQueueTest extends AbstractCollectionTest { return (Queue) super.getCollection(); } + //----------------------------------------------------------------------- + /** + * Tests {@link Queue#offer(Object)}. + */ + public void testQueueOffer() { + if (!isAddSupported()) { + return; + } + + final E[] elements = getFullElements(); + for (final E element : elements) { + resetEmpty(); + final boolean r = getCollection().offer(element); + getConfirmed().add(element); + verify(); + assertTrue("Empty queue changed after add", r); + assertEquals("Queue size is 1 after first add", 1, getCollection().size()); + } + + resetEmpty(); + int size = 0; + for (final E element : elements) { + final boolean r = getCollection().offer(element); + getConfirmed().add(element); + verify(); + if (r) { + size++; + } + assertEquals("Queue size should grow after add", size, getCollection().size()); + assertTrue("Queue should contain added element", getCollection().contains(element)); + } + } + + /** + * Tests {@link Queue#element()}. + */ + public void testQueueElement() { + resetEmpty(); + + try { + getCollection().element(); + fail("Queue.element should throw NoSuchElementException"); + } catch (final NoSuchElementException e) { + // expected + } + + resetFull(); + + assertTrue(getConfirmed().contains(getCollection().element())); + + if (!isRemoveSupported()) { + return; + } + + final int max = getFullElements().length; + for (int i = 0; i < max; i++) { + final E element = getCollection().element(); + + if (!isNullSupported()) { + assertNotNull(element); + } + + assertTrue(getConfirmed().contains(element)); + + getCollection().remove(element); + getConfirmed().remove(element); + + verify(); + } + + try { + getCollection().element(); + fail("Queue.element should throw NoSuchElementException"); + } catch (final NoSuchElementException e) { + // expected + } + } + + /** + * Tests {@link Queue#peek()}. + */ + public void testQueuePeek() { + if (!isRemoveSupported()) { + return; + } + + resetEmpty(); + + E element = getCollection().peek(); + assertNull(element); + + resetFull(); + + final int max = getFullElements().length; + for (int i = 0; i < max; i++) { + element = getCollection().peek(); + + if (!isNullSupported()) { + assertNotNull(element); + } + + assertTrue(getConfirmed().contains(element)); + + getCollection().remove(element); + getConfirmed().remove(element); + + verify(); + } + + element = getCollection().peek(); + assertNull(element); + } + + /** + * Tests {@link Queue#remove()}. + */ + public void testQueueRemove() { + if (!isRemoveSupported()) { + return; + } + + resetEmpty(); + + try { + getCollection().remove(); + fail("Queue.remove should throw NoSuchElementException"); + } catch (final NoSuchElementException e) { + // expected + } + + resetFull(); + + final int max = getFullElements().length; + for (int i = 0; i < max; i++) { + final E element = getCollection().remove(); + final boolean success = getConfirmed().remove(element); + assertTrue("remove should return correct element", success); + verify(); + } + + try { + getCollection().element(); + fail("Queue.remove should throw NoSuchElementException"); + } catch (final NoSuchElementException e) { + // expected + } + } + + /** + * Tests {@link Queue#poll()}. + */ + public void testQueuePoll() { + if (!isRemoveSupported()) { + return; + } + + resetEmpty(); + + E element = getCollection().poll(); + assertNull(element); + + resetFull(); + + final int max = getFullElements().length; + for (int i = 0; i < max; i++) { + element = getCollection().poll(); + final boolean success = getConfirmed().remove(element); + assertTrue("poll should return correct element", success); + verify(); + } + + element = getCollection().poll(); + assertNull(element); + } + //----------------------------------------------------------------------- @SuppressWarnings("unchecked") public void testEmptyQueueSerialization() throws IOException, ClassNotFoundException { @@ -204,7 +378,7 @@ public abstract class AbstractQueueTest extends AbstractCollectionTest { final Queue queue = makeFullCollection(); if(queue instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { final Queue queue2 = (Queue) readExternalFormFromDisk(getCanonicalFullCollectionName(queue)); - assertEquals("Queues is the right size",queue.size(), queue2.size()); + assertEquals("Queues are not the right size", queue.size(), queue2.size()); } }