From da7b8bad6f02ff313bd8905f79e648c39c97b226 Mon Sep 17 00:00:00 2001 From: dota17 Date: Tue, 19 May 2020 11:16:27 +0800 Subject: [PATCH] Updated the fail() exceptions with Junit5. --- .../iterators/AbstractIteratorTest.java | 52 +++++--- .../iterators/AbstractListIteratorTest.java | 97 ++++++++------ .../iterators/AbstractMapIteratorTest.java | 126 +++++++++++------- .../iterators/ArrayIterator2Test.java | 85 +++++------- .../iterators/ArrayListIteratorTest.java | 35 +++-- .../iterators/LazyIteratorChainTest.java | 49 +++---- .../iterators/ListIteratorWrapper2Test.java | 38 +++--- .../iterators/ListIteratorWrapperTest.java | 57 ++++---- .../iterators/LoopingIteratorTest.java | 66 ++++----- .../iterators/LoopingListIteratorTest.java | 41 +++--- 10 files changed, 341 insertions(+), 305 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java index 9389de040c..b7d85ef2ff 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java @@ -16,6 +16,11 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Iterator; import java.util.NoSuchElementException; @@ -107,13 +112,14 @@ public void testEmptyIterator() { final Iterator it = makeEmptyIterator(); // hasNext() should return false - assertEquals("hasNext() should return false for empty iterators", false, it.hasNext()); + assertFalse(it.hasNext()); // next() should throw a NoSuchElementException - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail("NoSuchElementException must be thrown when Iterator is exhausted"); - } catch (final NoSuchElementException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } verify(); @@ -131,14 +137,10 @@ public void testFullIterator() { final Iterator it = makeObject(); // hasNext() must be true (ensure makeFullIterator is correct!) - assertEquals("hasNext() should return true for at least one element", true, it.hasNext()); + assertTrue(it.hasNext()); // next() must not throw exception (ensure makeFullIterator is correct!) - try { - it.next(); - } catch (final NoSuchElementException e) { - fail("Full iterators must have at least one element"); - } + it.next(); // iterate through while (it.hasNext()) { @@ -147,10 +149,11 @@ public void testFullIterator() { } // next() must throw NoSuchElementException now - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail("NoSuchElementException must be thrown when Iterator is exhausted"); - } catch (final NoSuchElementException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } assertNotNull(it.toString()); @@ -164,17 +167,22 @@ public void testRemove() { if (!supportsRemove()) { // check for UnsupportedOperationException if not supported - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { it.remove(); - } catch (final UnsupportedOperationException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } return; } // should throw IllegalStateException before next() called - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } verify(); // remove after next should be fine @@ -182,10 +190,12 @@ public void testRemove() { it.remove(); // should throw IllegalStateException for second remove() - try { + exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java index 04a5596cf6..3bbae29f23 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java @@ -16,6 +16,12 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.ArrayList; import java.util.ListIterator; import java.util.NoSuchElementException; @@ -97,23 +103,25 @@ public void testEmptyListIteratorIsIndeedEmpty() { final ListIterator it = makeEmptyIterator(); - assertEquals(false, it.hasNext()); + assertFalse(it.hasNext()); assertEquals(0, it.nextIndex()); - assertEquals(false, it.hasPrevious()); + assertFalse(it.hasPrevious()); assertEquals(-1, it.previousIndex()); // next() should throw a NoSuchElementException - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail("NoSuchElementException must be thrown from empty ListIterator"); - } catch (final NoSuchElementException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } // previous() should throw a NoSuchElementException - try { + exception = assertThrows(NoSuchElementException.class, () -> { it.previous(); - fail("NoSuchElementException must be thrown from empty ListIterator"); - } catch (final NoSuchElementException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } } @@ -128,12 +136,13 @@ public void testWalkForwardAndBack() { } // check state at end - assertEquals(false, it.hasNext()); - assertEquals(true, it.hasPrevious()); - try { + assertFalse(it.hasNext()); + assertTrue(it.hasPrevious()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail("NoSuchElementException must be thrown from next at end of ListIterator"); - } catch (final NoSuchElementException e) { + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("element")); } // loop back through comparing @@ -146,12 +155,13 @@ public void testWalkForwardAndBack() { } // check state at start - assertEquals(true, it.hasNext()); - assertEquals(false, it.hasPrevious()); - try { + assertTrue(it.hasNext()); + assertFalse(it.hasPrevious()); + exception = assertThrows(NoSuchElementException.class, () -> { it.previous(); - fail("NoSuchElementException must be thrown from previous at start of ListIterator"); - } catch (final NoSuchElementException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } } @@ -159,20 +169,22 @@ public void testWalkForwardAndBack() { * Test add behavior. */ public void testAdd() { - ListIterator it = makeObject(); + final ListIterator it1 = makeObject(); final E addValue = addSetValue(); if (!supportsAdd()) { // check for UnsupportedOperationException if not supported - try { - it.add(addValue); - fail("UnsupportedOperationException must be thrown from add of " + it.getClass().getSimpleName()); - } catch (final UnsupportedOperationException ex) {} + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { + it1.add(addValue); + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("not support")); + } return; } // add at start should be OK, added should be previous - it = makeObject(); + ListIterator it = makeObject(); it.add(addValue); assertEquals(addValue, it.previous()); @@ -200,18 +212,22 @@ public void testSet() { if (!supportsSet()) { // check for UnsupportedOperationException if not supported - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { it.set(addSetValue()); - fail("UnsupportedOperationException must be thrown from set in " + it.getClass().getSimpleName()); - } catch (final UnsupportedOperationException ex) {} + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("not support")); + } return; } // should throw IllegalStateException before next() called - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.set(addSetValue()); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } // set after next should be fine it.next(); @@ -227,11 +243,10 @@ public void testRemoveThenSet() { if (supportsRemove() && supportsSet()) { it.next(); it.remove(); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.set(addSetValue()); - fail("IllegalStateException must be thrown from set after remove"); - } catch (final IllegalStateException e) { - } + }); + assertNull(exception.getMessage()); } } @@ -241,10 +256,11 @@ public void testAddThenSet() { if (supportsAdd() && supportsSet()) { it.next(); it.add(addSetValue()); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.set(addSetValue()); - fail("IllegalStateException must be thrown from set after add"); - } catch (final IllegalStateException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } } } @@ -259,10 +275,11 @@ public void testAddThenRemove() { if (supportsAdd() && supportsRemove()) { it.next(); it.add(addSetValue()); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail("IllegalStateException must be thrown from remove after add"); - } catch (final IllegalStateException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java index 775801c289..6d31f82fce 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java @@ -16,6 +16,12 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashSet; import java.util.Map; import java.util.NoSuchElementException; @@ -116,39 +122,47 @@ public void testEmptyMapIterator() { } final MapIterator it = makeEmptyIterator(); - assertEquals(false, it.hasNext()); + assertFalse(it.hasNext()); // next() should throw a NoSuchElementException - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) {} - + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("Iterator contains no elements")); + } // getKey() should throw an IllegalStateException - try { + exception = assertThrows(IllegalStateException.class, () -> { it.getKey(); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } // getValue() should throw an IllegalStateException - try { + exception = assertThrows(IllegalStateException.class, () -> { it.getValue(); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } if (!supportsSetValue()) { // setValue() should throw an UnsupportedOperationException/IllegalStateException - try { + exception = assertThrows(Exception.class, () -> { it.setValue(addSetValues()[0]); - fail(); - } catch (final UnsupportedOperationException ex) { - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } } else { // setValue() should throw an IllegalStateException - try { + exception = assertThrows(IllegalStateException.class, () -> { it.setValue(addSetValues()[0]); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); + } } } @@ -163,23 +177,23 @@ public void testFullMapIterator() { final MapIterator it = makeObject(); final Map map = getMap(); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); final Set set = new HashSet<>(); while (it.hasNext()) { // getKey final K key = it.next(); assertSame("it.next() should equals getKey()", key, it.getKey()); - assertTrue("Key must be in map", map.containsKey(key)); - assertTrue("Key must be unique", set.add(key)); + assertTrue(map.containsKey(key)); + assertTrue(set.add(key)); // getValue final V value = it.getValue(); if (!isGetStructuralModify()) { assertSame("Value must be mapped to key", map.get(key), value); } - assertTrue("Value must be in map", map.containsValue(value)); + assertTrue(map.containsValue(value)); verify(); } @@ -196,15 +210,17 @@ public void testMapIteratorSet() { final MapIterator it = makeObject(); final Map map = getMap(); final Map confirmed = getConfirmedMap(); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); final K key = it.next(); final V value = it.getValue(); if (!supportsSetValue()) { - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { it.setValue(newValue); - fail(); - } catch (final UnsupportedOperationException ex) {} + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("not support")); + } return; } final V old = it.setValue(newValue); @@ -212,11 +228,10 @@ public void testMapIteratorSet() { assertSame("Key must not change after setValue", key, it.getKey()); assertSame("Value must be changed after setValue", newValue, it.getValue()); assertSame("setValue must return old value", value, old); - assertEquals("Map must contain key", true, map.containsKey(key)); + assertTrue(map.containsKey(key)); // test against confirmed, as map may contain value twice - assertEquals("Map must not contain old value", - confirmed.containsValue(old), map.containsValue(old)); - assertEquals("Map must contain new value", true, map.containsValue(newValue)); + assertEquals(confirmed.containsValue(old), map.containsValue(old)); + assertTrue(map.containsValue(newValue)); verify(); it.setValue(newValue); // same value - should be OK @@ -238,26 +253,29 @@ public void testRemove() { // override final MapIterator it = makeObject(); final Map map = getMap(); final Map confirmed = getConfirmedMap(); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); final K key = it.next(); if (!supportsRemove()) { - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { it.remove(); - fail(); - } catch (final UnsupportedOperationException ex) { + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("not support")); } return; } it.remove(); confirmed.remove(key); - assertEquals(false, map.containsKey(key)); + assertFalse(map.containsKey(key)); verify(); - try { - it.remove(); // second remove fails - } catch (final IllegalStateException ex) { + Exception exception = assertThrows(IllegalStateException.class, () -> { + it.remove(); + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("can only be called")); } verify(); } @@ -271,7 +289,7 @@ public void testMapIteratorSetRemoveSet() { final MapIterator it = makeObject(); final Map confirmed = getConfirmedMap(); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); final K key = it.next(); it.setValue(newValue); @@ -279,10 +297,12 @@ public void testMapIteratorSetRemoveSet() { confirmed.remove(key); verify(); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.setValue(newValue); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("can only be called")); + } verify(); } @@ -294,17 +314,19 @@ public void testMapIteratorRemoveGetKey() { final MapIterator it = makeObject(); final Map confirmed = getConfirmedMap(); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); final K key = it.next(); it.remove(); confirmed.remove(key); verify(); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.getKey(); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("can only be called")); + } verify(); } @@ -316,17 +338,19 @@ public void testMapIteratorRemoveGetValue() { final MapIterator it = makeObject(); final Map confirmed = getConfirmedMap(); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); final K key = it.next(); it.remove(); confirmed.remove(key); verify(); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.getValue(); - fail(); - } catch (final IllegalStateException ex) {} + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("can only be called")); + } verify(); } diff --git a/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java b/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java index e7acf4947c..cc80c1c248 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java @@ -16,6 +16,11 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Iterator; import java.util.NoSuchElementException; @@ -64,18 +69,15 @@ public void testIterator() { final Integer testValue = Integer.valueOf(element); final Number iterValue = (Number) iter.next(); - assertEquals("Iteration value is correct", testValue, iterValue); + assertEquals(testValue, iterValue); } - assertTrue("Iterator should now be empty", !iter.hasNext()); + assertTrue(!iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - } catch (final Exception e) { - assertTrue( - "NoSuchElementException must be thrown", - e.getClass().equals(new NoSuchElementException().getClass())); - } + }); + assertNull(exception.getMessage()); } public void testIndexedArray() { @@ -86,7 +88,7 @@ public void testIndexedArray() { iter.next(); } - assertEquals("the count should be right using ArrayIterator(Object,2) ", count, testArray.length - 2); + assertEquals(count, testArray.length - 2); iter = makeArrayIterator(testArray, 1, testArray.length - 1); count = 0; @@ -95,53 +97,34 @@ public void testIndexedArray() { iter.next(); } - assertEquals( - "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ", - count, - testArray.length - 2); + assertEquals(count, testArray.length - 2); - try { - iter = makeArrayIterator(testArray, -1); - fail("new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException aioobe) { - // expected - } + Exception exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> { + makeArrayIterator(testArray, -1); + }); + assertTrue(exception.getMessage().contains("Attempt to make an ArrayIterator that starts before the start of the array.")); - try { - iter = makeArrayIterator(testArray, testArray.length + 1); - fail("new ArrayIterator(Object,length+1) should throw an ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException aioobe) { - // expected - } + exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> { + makeArrayIterator(testArray, testArray.length + 1); + }); + assertTrue(exception.getMessage().contains("Attempt to make an ArrayIterator that starts beyond the end of the array")); - try { - iter = makeArrayIterator(testArray, 0, -1); - fail("new ArrayIterator(Object,0,-1) should throw an ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException aioobe) { - // expected - } - try { - iter = makeArrayIterator(testArray, 0, testArray.length + 1); - fail("new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException aioobe) { - // expected - } + exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> { + makeArrayIterator(testArray, 0, -1); + }); + assertTrue(exception.getMessage().contains("Attempt to make an ArrayIterator that ends before the start of the array.")); - try { - iter = makeArrayIterator(testArray, 1, 1); - // expected not to fail - } catch (final IllegalArgumentException iae) { - // MODIFIED: an iterator over a zero-length section of array - // should be perfectly legal behavior - fail("new ArrayIterator(Object,1,1) should NOT throw an IllegalArgumentException"); - } + exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> { + makeArrayIterator(testArray, 0, testArray.length + 1); + }); + assertTrue(exception.getMessage().contains("Attempt to make an ArrayIterator that")); - try { - iter = makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2); - fail("new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException"); - } catch (final IllegalArgumentException iae) { - // expected - } + makeArrayIterator(testArray, 1, 1); + + exception = assertThrows(IllegalArgumentException.class, () -> { + makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2); + }); + assertTrue(exception.getMessage().contains("End index must not be less than start index.")); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/ArrayListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/ArrayListIteratorTest.java index 0cf8bf32cf..788bf32df4 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ArrayListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ArrayListIteratorTest.java @@ -16,6 +16,11 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Arrays; import java.util.ListIterator; import java.util.NoSuchElementException; @@ -68,18 +73,15 @@ public void testListIterator() { final Object testValue = testArray[x]; final Object iterValue = iter.previous(); - assertEquals("Iteration value is correct", testValue, iterValue); + assertEquals(testValue, iterValue); } - assertTrue("Iterator should now be empty", !iter.hasPrevious()); + assertTrue(!iter.hasPrevious()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.previous(); - } catch (final Exception e) { - assertTrue( - "NoSuchElementException must be thrown", - e.getClass().equals(new NoSuchElementException().getClass())); - } + }); + assertNull(exception.getMessage()); } @@ -101,20 +103,15 @@ public void testListIteratorSet() { x++; } - assertTrue("The two arrays should have the same value, i.e. {0,1,2}", Arrays.equals(testData, result)); + assertTrue(Arrays.equals(testData, result)); // a call to set() before a call to next() or previous() should throw an IllegalStateException - iter = makeArrayListIterator(testArray); - - try { - iter.set((E) "should fail"); - fail("ListIterator#set should fail if next() or previous() have not yet been called."); - } catch (final IllegalStateException e) { - // expected - } catch (final Throwable t) { // should never happen - fail(t.toString()); - } + final ListIterator iter1 = makeArrayListIterator(testArray); + Exception exception = assertThrows(IllegalStateException.class, () -> { + iter1.set((E) "should fail"); + }); + assertTrue(exception.getMessage().contains("must call next() or previous() before a call to set()")); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/LazyIteratorChainTest.java b/src/test/java/org/apache/commons/collections4/iterators/LazyIteratorChainTest.java index 3827c41692..856c25f583 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/LazyIteratorChainTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/LazyIteratorChainTest.java @@ -16,6 +16,11 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -93,14 +98,12 @@ public void testIterator() { assertEquals( "Iteration value is correct", testValue, iterValue ); } - assertTrue("Iterator should now be empty", !iter.hasNext()); + assertTrue(!iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - } catch (final Exception e) { - assertTrue("NoSuchElementException must be thrown", - e.getClass().equals(new NoSuchElementException().getClass())); - } + }); + assertNull(exception.getMessage()); } public void testRemoveFromFilteredIterator() { @@ -131,12 +134,10 @@ public void testRemoveFromFilteredIterator() { public void testRemove() { final Iterator iter = makeObject(); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("Calling remove before the first call to next() should throw an exception"); - } catch (final IllegalStateException e) { - - } + }); + assertNull(exception.getMessage()); for (final String testValue : testArray) { final String iterValue = iter.next(); @@ -148,9 +149,9 @@ public void testRemove() { } } - assertTrue("List is empty", list1.size() == 0); - assertTrue("List is empty", list2.size() == 1); - assertTrue("List is empty", list3.size() == 0); + assertTrue(list1.size() == 0); + assertTrue(list2.size() == 1); + assertTrue(list3.size() == 0); } public void testFirstIteratorIsEmptyBug() { @@ -171,25 +172,25 @@ protected Iterator nextIterator(final int count) { return null; } }; - assertTrue("should have next", chain.hasNext()); + assertTrue(chain.hasNext()); assertEquals("A", chain.next()); - assertTrue("should have next", chain.hasNext()); + assertTrue(chain.hasNext()); assertEquals("B", chain.next()); - assertTrue("should have next", chain.hasNext()); + assertTrue(chain.hasNext()); assertEquals("C", chain.next()); - assertTrue("should not have next", !chain.hasNext()); + assertTrue(!chain.hasNext()); } public void testEmptyChain() { final LazyIteratorChain chain = makeEmptyIterator(); assertEquals(false, chain.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { chain.next(); - fail(); - } catch (final NoSuchElementException ex) {} - try { + }); + assertTrue(exception.getMessage().contains("Iterator contains no elements")); + exception = assertThrows(IllegalStateException.class, () -> { chain.remove(); - fail(); - } catch (final IllegalStateException ex) {} + }); + assertTrue(exception.getMessage().contains("Iterator contains no elements")); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapper2Test.java b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapper2Test.java index b5cfbd2789..5a771e750b 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapper2Test.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapper2Test.java @@ -16,6 +16,12 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.ArrayList; import java.util.List; import java.util.ListIterator; @@ -70,14 +76,12 @@ public void testIterator() { assertEquals("Iteration value is correct", testValue, iterValue); } - assertTrue("Iterator should now be empty", !iter.hasNext()); + assertTrue(!iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - } catch (final Exception e) { - assertTrue("NoSuchElementException must be thrown", - e.getClass().equals(new NoSuchElementException().getClass())); - } + }); + assertNull(exception.getMessage()); // now, read it backwards for (int i = testArray.length - 1; i > -1; --i) { @@ -87,12 +91,10 @@ public void testIterator() { assertEquals( "Iteration value is correct", testValue, iterValue ); } - try { + exception = assertThrows(NoSuchElementException.class, () -> { iter.previous(); - } catch (final Exception e) { - assertTrue("NoSuchElementException must be thrown", - e.getClass().equals(new NoSuchElementException().getClass())); - } + }); + assertNull(exception.getMessage()); // now, read it forwards again for (final String testValue : testArray) { @@ -111,11 +113,10 @@ public void testRemove() { assertEquals(-1, iter.previousIndex()); assertEquals(0, iter.nextIndex()); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("ListIteratorWrapper#remove() should fail; must be initially positioned first"); - } catch (final IllegalStateException e) { - } + }); + assertNull(exception.getMessage()); //no change from invalid op: assertEquals(-1, iter.previousIndex()); @@ -136,11 +137,10 @@ public void testRemove() { assertEquals(-1, iter.previousIndex()); assertEquals(0, iter.nextIndex()); - try { + exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("ListIteratorWrapper#remove() should fail; must be repositioned first"); - } catch (final IllegalStateException e) { - } + }); + assertNull(exception.getMessage()); //no change from invalid op: assertEquals(-1, iter.previousIndex()); diff --git a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java index a1a19603a4..807ab08e1f 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java @@ -16,6 +16,12 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.ArrayList; import java.util.List; import java.util.ListIterator; @@ -68,16 +74,16 @@ public void testIterator() { for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals("Iteration value is correct", testValue, iterValue); + assertEquals(testValue, iterValue); } - assertTrue("Iterator should now be empty", !iter.hasNext()); + assertTrue(!iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - } catch (final Exception e) { - assertTrue("NoSuchElementException must be thrown", - e.getClass().equals(new NoSuchElementException().getClass())); + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } // now, read it backwards @@ -85,21 +91,21 @@ public void testIterator() { final Object testValue = testArray[i]; final E iterValue = iter.previous(); - assertEquals( "Iteration value is correct", testValue, iterValue ); + assertEquals(testValue, iterValue ); } - try { + exception = assertThrows(NoSuchElementException.class, () -> { iter.previous(); - } catch (final Exception e) { - assertTrue("NoSuchElementException must be thrown", - e.getClass().equals(new NoSuchElementException().getClass())); + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } // now, read it forwards again for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals("Iteration value is correct", testValue, iterValue); + assertEquals(testValue, iterValue); } } @@ -112,11 +118,10 @@ public void testRemove() { assertEquals(-1, iter.previousIndex()); assertEquals(0, iter.nextIndex()); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("ListIteratorWrapper#remove() should fail; must be initially positioned first"); - } catch (final IllegalStateException e) { - } + }); + assertNotNull(exception.getMessage()); //no change from invalid op: assertEquals(-1, iter.previousIndex()); @@ -137,11 +142,10 @@ public void testRemove() { assertEquals(-1, iter.previousIndex()); assertEquals(0, iter.nextIndex()); - try { + exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("ListIteratorWrapper#remove() should fail; must be repositioned first"); - } catch (final IllegalStateException e) { - } + }); + assertNotNull(exception.getMessage()); //no change from invalid op: assertEquals(-1, iter.previousIndex()); @@ -172,10 +176,11 @@ public void testRemove() { assertEquals(-1, iter.previousIndex()); assertEquals(0, iter.nextIndex()); - try { + exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("ListIteratorWrapper does not support the remove() method while dug into the cache via previous()"); - } catch (final IllegalStateException e) { + }); + if (null != exception.getMessage()) { + assertNotNull(exception.getMessage()); } //no change from invalid op: @@ -214,15 +219,15 @@ public void testReset() { assertFalse("No previous elements after reset()", iter.hasPrevious()); // after reset, the results should be the same as before - assertEquals("First element should be the same", first, iter.next()); - assertEquals("Second elment should be the same", second, iter.next()); + assertEquals(first, iter.next()); + assertEquals(second, iter.next()); // after passing the point, where we resetted, continuation should work as expected for (int i = 2; i < testArray.length; i++) { final Object testValue = testArray[i]; final E iterValue = iter.next(); - assertEquals("Iteration value is correct", testValue, iterValue); + assertEquals(testValue, iterValue); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/LoopingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/LoopingIteratorTest.java index 7d9b7600f9..facfec8d3c 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/LoopingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/LoopingIteratorTest.java @@ -16,6 +16,11 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -23,8 +28,6 @@ import org.junit.Test; -import static org.junit.Assert.*; - /** * Tests the LoopingIterator class. * @@ -36,11 +39,10 @@ public class LoopingIteratorTest { */ @Test public void testConstructorEx() throws Exception { - try { + Exception exception = assertThrows(NullPointerException.class, () -> { new LoopingIterator<>(null); - fail(); - } catch (final NullPointerException ex) { - } + }); + assertTrue(exception.getMessage().contains("collection")); } /** @@ -51,13 +53,12 @@ public void testConstructorEx() throws Exception { public void testLooping0() throws Exception { final List list = new ArrayList<>(); final LoopingIterator loop = new LoopingIterator<>(list); - assertTrue("hasNext should return false", !loop.hasNext()); + assertTrue(!loop.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { loop.next(); - fail("NoSuchElementException was not thrown during next() call."); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("There are no elements for this iterator to loop on")); } /** @@ -69,13 +70,13 @@ public void testLooping1() throws Exception { final List list = Arrays.asList("a"); final LoopingIterator loop = new LoopingIterator<>(list); - assertTrue("1st hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); - assertTrue("2nd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); - assertTrue("3rd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); } @@ -89,13 +90,13 @@ public void testLooping2() throws Exception { final List list = Arrays.asList("a", "b"); final LoopingIterator loop = new LoopingIterator<>(list); - assertTrue("1st hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); - assertTrue("2nd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("b", loop.next()); - assertTrue("3rd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); } @@ -109,16 +110,16 @@ public void testLooping3() throws Exception { final List list = Arrays.asList("a", "b", "c"); final LoopingIterator loop = new LoopingIterator<>(list); - assertTrue("1st hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); - assertTrue("2nd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("b", loop.next()); - assertTrue("3rd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("c", loop.next()); - assertTrue("4th hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); } @@ -131,29 +132,28 @@ public void testLooping3() throws Exception { public void testRemoving1() throws Exception { final List list = new ArrayList<>(Arrays.asList("a", "b", "c")); final LoopingIterator loop = new LoopingIterator<>(list); - assertEquals("list should have 3 elements.", 3, list.size()); + assertEquals(3, list.size()); - assertTrue("1st hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("a", loop.next()); loop.remove(); // removes a - assertEquals("list should have 2 elements.", 2, list.size()); + assertEquals(2, list.size()); - assertTrue("2nd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("b", loop.next()); loop.remove(); // removes b - assertEquals("list should have 1 elements.", 1, list.size()); + assertEquals(1, list.size()); - assertTrue("3rd hasNext should return true", loop.hasNext()); + assertTrue(loop.hasNext()); assertEquals("c", loop.next()); loop.remove(); // removes c - assertEquals("list should have 0 elements.", 0, list.size()); + assertEquals(0, list.size()); - assertFalse("4th hasNext should return false", loop.hasNext()); - try { + assertFalse(loop.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { loop.next(); - fail("Expected NoSuchElementException to be thrown."); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("There are no elements for this iterator to loop on")); } /** diff --git a/src/test/java/org/apache/commons/collections4/iterators/LoopingListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/LoopingListIteratorTest.java index a3bf48bd6a..af4c03c993 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/LoopingListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/LoopingListIteratorTest.java @@ -16,13 +16,17 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; import org.junit.Test; -import static org.junit.Assert.*; /** * Tests the LoopingListIterator class. @@ -35,11 +39,10 @@ public class LoopingListIteratorTest { */ @Test public void testConstructorEx() throws Exception { - try { + Exception exception = assertThrows(NullPointerException.class, () -> { new LoopingListIterator<>(null); - fail(); - } catch (final NullPointerException ex) { - } + }); + assertTrue(exception.getMessage().contains("collection")); } /** @@ -52,17 +55,15 @@ public void testLooping0() throws Exception { assertFalse(loop.hasNext()); assertFalse(loop.hasPrevious()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { loop.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("There are no elements for this iterator to loop on")); - try { + exception = assertThrows(NoSuchElementException.class, () -> { loop.previous(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("There are no elements for this iterator to loop on")); } /** @@ -189,11 +190,10 @@ public void testRemovingElementsAndIteratingForward() { assertEquals(0, list.size()); assertFalse(loop.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { loop.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("There are no elements for this iterator to loop on")); } /** @@ -220,11 +220,10 @@ public void testRemovingElementsAndIteratingBackwards() { assertEquals(0, list.size()); assertFalse(loop.hasPrevious()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { loop.previous(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("There are no elements for this iterator to loop on")); } /**