From 41449e299874b3bd748dac7a15bf1f3af8fed2be Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 6 May 2020 10:04:17 +0800 Subject: [PATCH] [COLLECTIONS-758] Add test cases for AbstractQueueDecorator. --- .../queue/QueueDecoratorTest.java | 223 ++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 src/test/java/org/apache/commons/collections4/queue/QueueDecoratorTest.java diff --git a/src/test/java/org/apache/commons/collections4/queue/QueueDecoratorTest.java b/src/test/java/org/apache/commons/collections4/queue/QueueDecoratorTest.java new file mode 100644 index 0000000000..ce9337fdf5 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/queue/QueueDecoratorTest.java @@ -0,0 +1,223 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.commons.collections4.collection.AbstractCollectionTest; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Collection; +import java.util.Queue; +import java.util.NoSuchElementException; + +/** + * Test cases for AbstractQueueDecorator. + * + * @since 4.5 + */ +public class QueueDecoratorTest extends AbstractCollectionTest { + /** + * JUnit constructor. + * + * @param testName the test class name + */ + public QueueDecoratorTest(String testName) { + super(testName); + } + + @Override + public Collection makeConfirmedCollection() { + return new ArrayList<>(); + } + + @Override + public Collection makeConfirmedFullCollection() { + final ArrayList list = new ArrayList<>(); + list.addAll(Arrays.asList(getFullElements())); + return list; + } + + @Override + public Queue makeObject() { + return new QueueDecorator( new LinkedList()); + } + + /** + * {@inheritDoc} + */ + @Override + public Queue makeFullCollection() { + final Queue queue = makeObject(); + queue.addAll(Arrays.asList(getFullElements())); + return queue; + } + + /** + * @return the collection field as a Queue + */ + @Override + public Queue getCollection() { + return (Queue) super.getCollection(); + } + + /** + * Tests {@link AbstractQueueDecorator#offer(Object)}. + */ + public void testAbstractQueueDecoratorOffer() { + 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 AbstractQueueDecorator#poll()}. + */ + public void testAbstractQueueDecoratorPoll() { + 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); + } + + /** + * Tests {@link AbstractQueueDecorator#peek()}. + */ + public void testAbstractQueueDecoratorPeek() { + 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 AbstractQueueDecorator#remove()}. + */ + public void testAbstractQueueDecoratorRemove() { + if (!isRemoveSupported()) { + return; + } + + resetEmpty(); + + Exception exception = assertThrows(NoSuchElementException.class, () -> { + getCollection().remove(); + }); + assertNull(exception.getMessage()); + + 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(); + } + + Exception noSuchElementException = assertThrows(NoSuchElementException.class, () -> { + getCollection().element(); + }); + assertNull(noSuchElementException.getMessage()); + } + + public void testCreate() throws Exception { + resetEmpty(); + writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/QueueDecorator.emptyCollection.version4.obj"); + resetFull(); + writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/QueueDecorator.fullCollection.version4.obj"); + } + +} + +class QueueDecorator extends AbstractQueueDecorator { + /** + * Constructor that wraps (not copies). + * + * @param queue the queue to decorate, must not be null + * @throws NullPointerException if queue is null + */ + protected QueueDecorator(final Queue queue) { + super(queue); + } +}