diff --git a/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java new file mode 100644 index 000000000..06ce1c628 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java @@ -0,0 +1,209 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections4.queue; + +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Queue; + +import org.apache.commons.collections4.collection.AbstractCollectionTest; + +/** + * Abstract test class for {@link java.util.Queue} methods and contracts. + *

+ * To use, simply extend this class, and implement + * the {@link #makeObject} method. + *

+ * If your {@link Queue} fails one of these tests by design, + * you may still use this base set of cases. Simply override the + * test case (method) your {@link Queue} fails or override one of the + * protected methods from AbstractCollectionTest. + * + * @since 4.0 + * @version $Id$ + */ +public abstract class AbstractQueueTest extends AbstractCollectionTest { + + /** + * JUnit constructor. + * + * @param testName the test class name + */ + public AbstractQueueTest(final String testName) { + super(testName); + } + + //----------------------------------------------------------------------- + /** + * Returns true if the collections produced by + * {@link #makeObject()} and {@link #makeFullCollection()} + * support the set operation.

+ * Default implementation returns true. Override if your collection + * class does not support set. + */ + public boolean isSetSupported() { + return true; + } + + //----------------------------------------------------------------------- + /** + * Verifies that the test queue implementation matches the confirmed queue + * implementation. + */ + @Override + public void verify() { + super.verify(); + final Iterator iterator1 = getCollection().iterator(); + final Iterator iterator2 = getConfirmed().iterator(); + while (iterator2.hasNext()) { + assertTrue(iterator1.hasNext()); + final Object o1 = iterator1.next(); + final Object o2 = iterator2.next(); + assertEquals(o1, o2); + } + } + + //----------------------------------------------------------------------- + /** + * Returns an empty {@link ArrayList}. + */ + @Override + public Collection makeConfirmedCollection() { + final ArrayList list = new ArrayList(); + return list; + } + + /** + * Returns a full {@link ArrayList}. + */ + @Override + public Collection makeConfirmedFullCollection() { + final ArrayList list = new ArrayList(); + list.addAll(Arrays.asList(getFullElements())); + return list; + } + + /** + * Returns {@link #makeObject()}. + * + * @return an empty queue to be used for testing + */ + @Override + public abstract Queue makeObject(); + + /** + * {@inheritDoc} + */ + @Override + public Queue makeFullCollection() { + // only works if queue supports optional "addAll(Collection)" + final Queue queue = makeObject(); + queue.addAll(Arrays.asList(getFullElements())); + return queue; + } + + //----------------------------------------------------------------------- + /** + * Returns the {@link #collection} field cast to a {@link Queue}. + * + * @return the collection field as a Queue + */ + @Override + public Queue getCollection() { + return (Queue) super.getCollection(); + } + + //----------------------------------------------------------------------- + @SuppressWarnings("unchecked") + public void testEmptyQueueSerialization() throws IOException, ClassNotFoundException { + final Queue queue = makeObject(); + if (!(queue instanceof Serializable && isTestSerialization())) { + return; + } + + final byte[] objekt = writeExternalFormToBytes((Serializable) queue); + final Queue queue2 = (Queue) readExternalFormFromBytes(objekt); + + assertEquals("Both queues are empty", 0, queue.size()); + assertEquals("Both queues are empty", 0, queue2.size()); + } + + @SuppressWarnings("unchecked") + public void testFullQueueSerialization() throws IOException, ClassNotFoundException { + final Queue queue = makeFullCollection(); + final int size = getFullElements().length; + if (!(queue instanceof Serializable && isTestSerialization())) { + return; + } + + final byte[] objekt = writeExternalFormToBytes((Serializable) queue); + final Queue queue2 = (Queue) readExternalFormFromBytes(objekt); + + assertEquals("Both queues are same size", size, queue.size()); + assertEquals("Both queues are same size", size, queue2.size()); + } + + /** + * Compare the current serialized form of the Queue + * against the canonical version in SVN. + */ + @SuppressWarnings("unchecked") + public void testEmptyQueueCompatibility() throws IOException, ClassNotFoundException { + /** + * Create canonical objects with this code + Queue queue = makeEmptyQueue(); + if (!(queue instanceof Serializable)) return; + + writeExternalFormToDisk((Serializable) queue, getCanonicalEmptyCollectionName(queue)); + */ + + // test to make sure the canonical form has been preserved + final Queue queue = makeObject(); + if (queue instanceof Serializable && !skipSerializedCanonicalTests() + && isTestSerialization()) { + final Queue queue2 = (Queue) readExternalFormFromDisk(getCanonicalEmptyCollectionName(queue)); + assertEquals("Queue is empty", 0, queue2.size()); + } + } + + /** + * Compare the current serialized form of the Queue + * against the canonical version in SVN. + */ + @SuppressWarnings("unchecked") + public void testFullQueueCompatibility() throws IOException, ClassNotFoundException { + /** + * Create canonical objects with this code + Queue queue = makeFullQueue(); + if (!(queue instanceof Serializable)) return; + + writeExternalFormToDisk((Serializable) queue, getCanonicalFullCollectionName(queue)); + */ + + // test to make sure the canonical form has been preserved + 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()); + } + } + +} diff --git a/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java index e729a9eb6..8baf05129 100644 --- a/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java @@ -27,25 +27,16 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.Queue; -import junit.framework.Test; - -import org.apache.commons.collections4.BulkTest; -import org.apache.commons.collections4.collection.AbstractCollectionTest; - /** * Test cases for CircularFifoQueue. * * @since 4.0 * @version $Id$ */ -public class CircularFifoQueueTest extends AbstractCollectionTest { +public class CircularFifoQueueTest extends AbstractQueueTest { - public CircularFifoQueueTest(final String n) { - super(n); - } - - public static Test suite() { - return BulkTest.makeSuite(CircularFifoQueueTest.class); + public CircularFifoQueueTest(final String testName) { + super(testName); } //----------------------------------------------------------------------- @@ -115,7 +106,7 @@ public class CircularFifoQueueTest extends AbstractCollectionTest { * @return an empty CircularFifoQueue */ @Override - public Collection makeObject() { + public Queue makeObject() { return new CircularFifoQueue(100); } @@ -156,7 +147,7 @@ public class CircularFifoQueueTest extends AbstractCollectionTest { final int size = getConfirmed().size(); for (int i = 0; i < size; i++) { final Object o1 = getCollection().remove(); - final Object o2 = getConfirmed().remove(0); + final Object o2 = ((List) getConfirmed()).remove(0); assertEquals("Removed objects should be equal", o1, o2); verify(); } @@ -419,7 +410,7 @@ public class CircularFifoQueueTest extends AbstractCollectionTest { resetFull(); final CircularFifoQueue queue = getCollection(); - final List confirmed = getConfirmed(); + final List confirmed = (List) getConfirmed(); for (int i = 0; i < confirmed.size(); i++) { assertEquals(confirmed.get(i), queue.get(i)); } @@ -453,11 +444,4 @@ public class CircularFifoQueueTest extends AbstractCollectionTest { return (CircularFifoQueue) super.getCollection(); } - /** - * {@inheritDoc} - */ - @Override - public List getConfirmed() { - return (List) super.getConfirmed(); - } } diff --git a/src/test/java/org/apache/commons/collections4/queue/PredicatedQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/PredicatedQueueTest.java index 6019a2cc4..8eda7908b 100644 --- a/src/test/java/org/apache/commons/collections4/queue/PredicatedQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/PredicatedQueueTest.java @@ -24,6 +24,7 @@ import java.util.Queue; import org.apache.commons.collections4.Predicate; import org.apache.commons.collections4.collection.PredicatedCollectionTest; +import org.apache.commons.collections4.functors.TruePredicate; /** * Extension of {@link PredicatedCollectionTest} for exercising the @@ -32,7 +33,7 @@ import org.apache.commons.collections4.collection.PredicatedCollectionTest; * @since 4.0 * @version $Id$ */ -public class PredicatedQueueTest extends PredicatedCollectionTest { +public class PredicatedQueueTest extends AbstractQueueTest { public PredicatedQueueTest(final String testName) { super(testName); @@ -40,6 +41,8 @@ public class PredicatedQueueTest extends PredicatedCollectionTest { //--------------------------------------------------------------- + protected Predicate truePredicate = TruePredicate.truePredicate(); + protected Queue decorateCollection(final Queue queue, final Predicate predicate) { return PredicatedQueue.predicatedQueue(queue, predicate); } @@ -50,7 +53,7 @@ public class PredicatedQueueTest extends PredicatedCollectionTest { } @Override - public Collection makeFullCollection() { + public Queue makeFullCollection() { final Queue queue = new LinkedList(); queue.addAll(Arrays.asList(getFullElements())); return decorateCollection(queue, truePredicate); @@ -70,6 +73,13 @@ public class PredicatedQueueTest extends PredicatedCollectionTest { //------------------------------------------------------------ + protected Predicate testPredicate = + new Predicate() { + public boolean evaluate(final E o) { + return o instanceof String; + } + }; + public Queue makeTestQueue() { return decorateCollection(new LinkedList(), testPredicate); } diff --git a/src/test/java/org/apache/commons/collections4/queue/TransformedQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/TransformedQueueTest.java index 00da6dc19..bb53b0ef8 100644 --- a/src/test/java/org/apache/commons/collections4/queue/TransformedQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/TransformedQueueTest.java @@ -31,7 +31,7 @@ import org.apache.commons.collections4.collection.TransformedCollectionTest; * @since 4.0 * @version $Id$ */ -public class TransformedQueueTest extends AbstractCollectionTest { +public class TransformedQueueTest extends AbstractQueueTest { public TransformedQueueTest(final String testName) { super(testName); diff --git a/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java index 795a3fc97..b700dc340 100644 --- a/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java @@ -30,7 +30,7 @@ import org.apache.commons.collections4.collection.AbstractCollectionTest; * @since 4.0 * @version $Id$ */ -public class UnmodifiableQueueTest extends AbstractCollectionTest { +public class UnmodifiableQueueTest extends AbstractQueueTest { public UnmodifiableQueueTest(final String testName) { super(testName); @@ -38,12 +38,12 @@ public class UnmodifiableQueueTest extends AbstractCollectionTest { //----------------------------------------------------------------------- @Override - public Collection makeObject() { + public Queue makeObject() { return UnmodifiableQueue.unmodifiableQueue(new LinkedList()); } @Override - public Collection makeFullCollection() { + public Queue makeFullCollection() { final Queue queue = new LinkedList(); queue.addAll(Arrays.asList(getFullElements())); return UnmodifiableQueue.unmodifiableQueue(queue); diff --git a/src/test/resources/data/test/PredicatedQueue.fullCollection.version4.obj b/src/test/resources/data/test/PredicatedQueue.fullCollection.version4.obj index 497f78452..46b2cd5e4 100644 Binary files a/src/test/resources/data/test/PredicatedQueue.fullCollection.version4.obj and b/src/test/resources/data/test/PredicatedQueue.fullCollection.version4.obj differ