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 9fa8063c76..8dc9f4403e 100644 --- a/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java @@ -16,6 +16,10 @@ */ package org.apache.commons.collections4.queue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNull; + import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; @@ -169,11 +173,14 @@ public void testQueueOffer() { public void testQueueElement() { resetEmpty(); - try { + Exception noSuchElementException = assertThrows(NoSuchElementException.class, () -> { getCollection().element(); - fail("Queue.element should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected + }); + String message = noSuchElementException.getMessage(); + if (null == message) { + assertNull(message); + } else { + assertTrue(message.contains("queue is empty")); } resetFull(); @@ -200,11 +207,14 @@ public void testQueueElement() { verify(); } - try { + noSuchElementException = assertThrows(NoSuchElementException.class, () -> { getCollection().element(); - fail("Queue.element should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected + }); + message = noSuchElementException.getMessage(); + if (null == message) { + assertNull(message); + } else { + assertTrue(message.contains("queue is empty")); } } @@ -253,11 +263,14 @@ public void testQueueRemove() { resetEmpty(); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { getCollection().remove(); - fail("Queue.remove should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected + }); + String message = exception.getMessage(); + if (null == message) { + assertNull(message); + } else { + assertTrue(message.contains("queue is empty")); } resetFull(); @@ -270,11 +283,14 @@ public void testQueueRemove() { verify(); } - try { + Exception noSuchElementException = assertThrows(NoSuchElementException.class, () -> { getCollection().element(); - fail("Queue.remove should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected + }); + message = exception.getMessage(); + if (null == message) { + assertNull(message); + } else { + assertTrue(message.contains("queue is empty")); } } @@ -313,8 +329,8 @@ public void testEmptyQueueSerialization() throws IOException, ClassNotFoundExcep return; } - final byte[] objekt = writeExternalFormToBytes((Serializable) queue); - final Queue queue2 = (Queue) readExternalFormFromBytes(objekt); + final byte[] object = writeExternalFormToBytes((Serializable) queue); + final Queue queue2 = (Queue) readExternalFormFromBytes(object); assertEquals("Both queues are empty", 0, queue.size()); assertEquals("Both queues are empty", 0, queue2.size()); @@ -328,8 +344,8 @@ public void testFullQueueSerialization() throws IOException, ClassNotFoundExcept return; } - final byte[] objekt = writeExternalFormToBytes((Serializable) queue); - final Queue queue2 = (Queue) readExternalFormFromBytes(objekt); + final byte[] object = writeExternalFormToBytes((Serializable) queue); + final Queue queue2 = (Queue) readExternalFormFromBytes(object); assertEquals("Both queues are same size", size, queue.size()); assertEquals("Both queues are same size", 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 ac9796e006..66805e6130 100644 --- a/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java @@ -16,6 +16,10 @@ */ package org.apache.commons.collections4.queue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNull; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; @@ -120,16 +124,16 @@ public void testCircularFifoQueueCircular() { list.add((E) "C"); final Queue queue = new CircularFifoQueue<>(list); - assertEquals(true, queue.contains("A")); - assertEquals(true, queue.contains("B")); - assertEquals(true, queue.contains("C")); + assertTrue(queue.contains("A")); + assertTrue(queue.contains("B")); + assertTrue(queue.contains("C")); queue.add((E) "D"); assertEquals(false, queue.contains("A")); - assertEquals(true, queue.contains("B")); - assertEquals(true, queue.contains("C")); - assertEquals(true, queue.contains("D")); + assertTrue(queue.contains("B")); + assertTrue(queue.contains("C")); + assertTrue(queue.contains("D")); assertEquals("B", queue.peek()); assertEquals("B", queue.remove()); @@ -150,48 +154,40 @@ public void testCircularFifoQueueRemove() { verify(); } - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { getCollection().remove(); - fail("Empty queue should raise Underflow."); - } catch (final NoSuchElementException e) { - // expected - } + }); + assertTrue(exception.getMessage().contains("queue is empty")); } /** * Tests that the constructor correctly throws an exception. */ public void testConstructorException1() { - try { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { new CircularFifoQueue(0); - } catch (final IllegalArgumentException ex) { - return; - } - fail(); + }); + assertTrue(exception.getMessage().contains("The size must be greater than 0")); } /** * Tests that the constructor correctly throws an exception. */ public void testConstructorException2() { - try { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { new CircularFifoQueue(-20); - } catch (final IllegalArgumentException ex) { - return; - } - fail(); + }); + assertTrue(exception.getMessage().contains("The size must be greater than 0")); } /** * Tests that the constructor correctly throws an exception. */ public void testConstructorException3() { - try { + Exception exception = assertThrows(NullPointerException.class, () -> { new CircularFifoQueue(null); - } catch (final NullPointerException ex) { - return; - } - fail(); + }); + assertNull(exception.getMessage()); } @SuppressWarnings("unchecked") @@ -374,7 +370,7 @@ public void testRepeatedSerialization() throws Exception { final CircularFifoQueue b = new CircularFifoQueue<>(2); b.add((E) "a"); assertEquals(1, b.size()); - assertEquals(true, b.contains("a")); + assertTrue(b.contains("a")); ByteArrayOutputStream bos = new ByteArrayOutputStream(); new ObjectOutputStream(bos).writeObject(b); @@ -383,11 +379,11 @@ public void testRepeatedSerialization() throws Exception { new ByteArrayInputStream(bos.toByteArray())).readObject(); assertEquals(1, b2.size()); - assertEquals(true, b2.contains("a")); + assertTrue(b2.contains("a")); b2.add((E) "b"); assertEquals(2, b2.size()); - assertEquals(true, b2.contains("a")); - assertEquals(true, b2.contains("b")); + assertTrue(b2.contains("a")); + assertTrue(b2.contains("b")); bos = new ByteArrayOutputStream(); new ObjectOutputStream(bos).writeObject(b2); @@ -396,12 +392,12 @@ public void testRepeatedSerialization() throws Exception { new ByteArrayInputStream(bos.toByteArray())).readObject(); assertEquals(2, b3.size()); - assertEquals(true, b3.contains("a")); - assertEquals(true, b3.contains("b")); + assertTrue(b3.contains("a")); + assertTrue(b3.contains("b")); b3.add((E) "c"); assertEquals(2, b3.size()); - assertEquals(true, b3.contains("b")); - assertEquals(true, b3.contains("c")); + assertTrue(b3.contains("b")); + assertTrue(b3.contains("c")); } public void testGetIndex() { @@ -424,13 +420,10 @@ public void testGetIndex() { public void testAddNull() { final CircularFifoQueue b = new CircularFifoQueue<>(2); - try { + Exception exception = assertThrows(NullPointerException.class, () -> { b.add(null); - fail(); - } catch (final NullPointerException ex) { - return; - } - fail(); + }); + assertTrue(exception.getMessage().contains("element")); } public void testDefaultSizeAndGetError1() { @@ -442,12 +435,10 @@ public void testDefaultSizeAndGetError1() { fifo.add((E) "4"); fifo.add((E) "5"); assertEquals(5, fifo.size()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { fifo.get(5); - } catch (final NoSuchElementException ex) { - return; - } - fail(); + }); + assertTrue(exception.getMessage().contains("The specified index 5 is outside the available range [0, 5)")); } public void testDefaultSizeAndGetError2() { @@ -459,12 +450,15 @@ public void testDefaultSizeAndGetError2() { fifo.add((E) "4"); fifo.add((E) "5"); assertEquals(5, fifo.size()); - try { + Exception exception2 = assertThrows(NoSuchElementException.class, () -> { fifo.get(-2); - } catch (final NoSuchElementException ex) { - return; + }); + String message = exception2.getMessage(); + if (null == message) { + assertNull(message); + } else { + assertTrue(message.contains("The specified index -2 is outside the available range")); } - fail(); } @Override 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); + } +} 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 5c6ba658ce..ba29267c54 100644 --- a/src/test/java/org/apache/commons/collections4/queue/TransformedQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/TransformedQueueTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.queue; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; @@ -74,12 +76,12 @@ public void testTransformedQueue() { for (int i = 0; i < elements.length; i++) { queue.add(elements[i]); assertEquals(i + 1, queue.size()); - assertEquals(true, queue.contains(Integer.valueOf((String) elements[i]))); + assertTrue(queue.contains(Integer.valueOf((String) elements[i]))); assertEquals(false, queue.contains(elements[i])); } assertEquals(false, queue.remove(elements[0])); - assertEquals(true, queue.remove(Integer.valueOf((String) elements[0]))); + assertTrue(queue.remove(Integer.valueOf((String) elements[0]))); } @@ -92,12 +94,12 @@ public void testTransformedQueue_decorateTransform() { TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER); assertEquals(elements.length, queue.size()); for (final Object el : elements) { - assertEquals(true, queue.contains(Integer.valueOf((String) el))); + assertTrue(queue.contains(Integer.valueOf((String) el))); assertEquals(false, queue.contains(el)); } assertEquals(false, queue.remove(elements[0])); - assertEquals(true, queue.remove(Integer.valueOf((String) elements[0]))); + assertTrue(queue.remove(Integer.valueOf((String) elements[0]))); } @Override 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 8bfe133ee4..973b17f216 100644 --- a/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java @@ -16,6 +16,10 @@ */ package org.apache.commons.collections4.queue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNull; + import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; @@ -85,10 +89,10 @@ public boolean isNullSupported() { @Override public void testQueueRemove() { resetEmpty(); - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { getCollection().remove(); - fail(); - } catch (final UnsupportedOperationException ex) {} + }); + assertNull(exception.getMessage()); } public void testUnmodifiable() { @@ -100,27 +104,27 @@ public void testDecorateFactory() { final Queue queue = makeFullCollection(); assertSame(queue, UnmodifiableQueue.unmodifiableQueue(queue)); - try { + Exception exception = assertThrows(NullPointerException.class, () -> { UnmodifiableQueue.unmodifiableQueue(null); - fail(); - } catch (final NullPointerException ex) {} + }); + assertTrue(exception.getMessage().contains("collection")); } public void testOffer() { final Queue queue = makeFullCollection(); final E e = null; - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { queue.offer(e); - fail(); - } catch (final UnsupportedOperationException ex) {} + }); + assertNull(exception.getMessage()); } public void testPoll() { final Queue queue = makeFullCollection(); - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { queue.poll(); - fail(); - } catch (final UnsupportedOperationException ex) {} + }); + assertNull(exception.getMessage()); } //-----------------------------------------------------------------------