This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
commit 0ea368660286c856bc21d8891c435d0443393f23 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Oct 20 09:35:26 2024 -0400 [COLLECTIONS-777] Migrate to JUnit 5 [COLLECTIONS-809] Update try/catch/fail logic to assertThrows --- src/changes/changes.xml | 2 + .../collections4/bag/CollectionBagTest.java | 2 +- .../iterators/AbstractIteratorTest.java | 8 +-- .../iterators/AbstractMapIteratorTest.java | 8 +-- .../collections4/iterators/ArrayIterator2Test.java | 31 ++------- .../iterators/ArrayListIteratorTest.java | 13 +--- .../iterators/BoundedIteratorTest.java | 2 +- .../iterators/CollatingIteratorTest.java | 7 +- .../collections4/iterators/IteratorChainTest.java | 6 +- .../iterators/LazyIteratorChainTest.java | 9 +-- .../iterators/ListIteratorWrapper2Test.java | 21 +----- .../iterators/ListIteratorWrapperTest.java | 21 +----- .../iterators/ObjectArrayIteratorTest.java | 9 +-- .../iterators/ObjectArrayListIteratorTest.java | 13 +--- .../iterators/SingletonIterator2Test.java | 10 +-- .../iterators/SingletonIteratorTest.java | 10 +-- .../iterators/SingletonListIteratorTest.java | 13 +--- .../iterators/UniqueFilterIteratorTest.java | 10 +-- .../list/CursorableLinkedListTest.java | 18 +---- .../DefaultAbstractLinkedListForJava21Test.java | 17 +---- .../commons/collections4/map/LinkedMapTest.java | 76 +++++----------------- 21 files changed, 57 insertions(+), 249 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 521b33ba5..9f87645c6 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -42,6 +42,8 @@ <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix generics in org.apache.commons.collections4.IteratorUtils.chainedIterator(Collection).</action> <action type="fix" dev="ggregory" due-to="Benjamin Confino, Gary Gregory" issue="COLLECTIONS-856">Javadoc: Document interaction between peek and filter iterator #515.</action> <action type="fix" dev="ggregory" due-to="Elia Bertolina, Gary Gregory" issue="COLLECTIONS-815">Javadoc: Update ClosureUtils Javadoc to match runtime.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory" issue="COLLECTIONS-815">Javadoc: Update ClosureUtils Javadoc to match runtime.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory" issue="COLLECTIONS-777">Migrate to JUnit 5.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">LayerManager.Builder implements Supplier.</action> <action type="add" dev="ggregory" due-to="Gary Gregory, hemanth0525">Add CollectionUtils.duplicateList(Collection).</action> diff --git a/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java b/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java index 40d2ef889..93a3be1de 100644 --- a/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java +++ b/src/test/java/org/apache/commons/collections4/bag/CollectionBagTest.java @@ -17,8 +17,8 @@ package org.apache.commons.collections4.bag; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; import java.io.Serializable; 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 70b8954aa..8bfd27a1c 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java @@ -16,11 +16,11 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 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 static org.junit.jupiter.api.Assertions.fail; import java.util.Iterator; import java.util.NoSuchElementException; @@ -132,11 +132,7 @@ public abstract class AbstractIteratorTest<E> extends AbstractObjectTest { assertTrue(it.hasNext(), "hasNext() should return true for at least one element"); // 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"); - } + assertDoesNotThrow(it::next, "Full iterators must have at least one element"); // iterate through while (it.hasNext()) { 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 b01f538a8..39b711e4e 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java @@ -299,16 +299,12 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest assertThrows(UnsupportedOperationException.class, () -> it.remove()); return; } - it.remove(); confirmed.remove(key); assertFalse(map.containsKey(key)); verify(); - - try { - it.remove(); // second remove fails - } catch (final IllegalStateException ex) { - } + // second remove fails + assertThrows(NoSuchElementException.class, it::remove, "Full iterators must have at least one element"); 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 3f3c4a164..02c5e2546 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java @@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertAll; 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.fail; import java.util.Iterator; import java.util.NoSuchElementException; @@ -85,10 +84,7 @@ public class ArrayIterator2Test<E> extends AbstractIteratorTest<E> { iter.next(); } - assertEquals( - count, - testArray.length - 2, - "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") "); + assertEquals(count, testArray.length - 2, "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") "); assertAll( () -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, -1), "new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException"), @@ -99,17 +95,11 @@ public class ArrayIterator2Test<E> extends AbstractIteratorTest<E> { () -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, 0, testArray.length + 1), "new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException"), () -> assertThrows(IllegalArgumentException.class, () -> makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2), - "new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException") - ); - - 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"); - } + "new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException")); + + iter = makeArrayIterator(testArray, 1, 1); + // MODIFIED: an iterator over a zero-length section of array + // should be perfectly legal behavior } @Test @@ -118,17 +108,10 @@ public class ArrayIterator2Test<E> extends AbstractIteratorTest<E> { for (final int element : testArray) { final Integer testValue = Integer.valueOf(element); final Number iterValue = (Number) iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); } } 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 071b4a71a..8bfec1c95 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ArrayListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ArrayListIteratorTest.java @@ -62,30 +62,19 @@ public class ArrayListIteratorTest<E> extends ArrayIteratorTest<E> { @Test public void testListIterator() { final ListIterator<E> iter = makeObject(); - // TestArrayIterator#testIterator() has already tested the iterator forward, // now we need to test it in reverse - // fast-forward the iterator to the end... while (iter.hasNext()) { iter.next(); } - for (int x = testArray.length - 1; x >= 0; x--) { final Object testValue = testArray[x]; final Object iterValue = iter.previous(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasPrevious(), "Iterator should now be empty"); - - try { - iter.previous(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } - + assertThrows(NoSuchElementException.class, iter::previous); } /** diff --git a/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.java index 52f8f8238..56f72d26d 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.java @@ -18,9 +18,9 @@ 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 static org.junit.jupiter.api.Assertions.assertNull; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/test/java/org/apache/commons/collections4/iterators/CollatingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/CollatingIteratorTest.java index 2f3f515b9..90e83f112 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/CollatingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/CollatingIteratorTest.java @@ -20,6 +20,7 @@ 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.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; @@ -224,11 +225,7 @@ public class CollatingIteratorTest extends AbstractIteratorTest<Integer> { final List<Integer> l2 = Arrays.asList(2, 4, 6); final CollatingIterator<Integer> collatingIterator1 = new CollatingIterator<>(null, l1.iterator(), l2.iterator()); - try { - collatingIterator1.next(); - } catch (final NullPointerException e) { - assertTrue(e.getMessage().startsWith("You must invoke setComparator")); - } + assertThrows(NullPointerException.class, collatingIterator1::next, "You must invoke setComparator"); int i = 0; final CollatingIterator<Integer> collatingIterator2 = new CollatingIterator<>(null, l1.iterator(), l2.iterator()); diff --git a/src/test/java/org/apache/commons/collections4/iterators/IteratorChainTest.java b/src/test/java/org/apache/commons/collections4/iterators/IteratorChainTest.java index 1536b1084..ef02977ec 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/IteratorChainTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/IteratorChainTest.java @@ -145,11 +145,7 @@ public class IteratorChainTest extends AbstractIteratorTest<String> { assertEquals(testValue, iterValue, "Iteration value is correct"); } assertFalse(iter.hasNext(), "Iterator should now be empty"); - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); } @Test 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 c520edce3..20df473c1 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/LazyIteratorChainTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/LazyIteratorChainTest.java @@ -133,17 +133,10 @@ public class LazyIteratorChainTest extends AbstractIteratorTest<String> { final Iterator<String> iter = makeObject(); for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); } @Test 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 5fd06163f..e3c381255 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapper2Test.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapper2Test.java @@ -74,39 +74,22 @@ public class ListIteratorWrapper2Test<E> extends AbstractIteratorTest<E> { final ListIterator<E> iter = makeObject(); for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } - + assertThrows(NoSuchElementException.class, iter::next); // now, read it backwards for (int i = testArray.length - 1; i > -1; --i) { final Object testValue = testArray[i]; final E iterValue = iter.previous(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - - try { - iter.previous(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } - + assertThrows(NoSuchElementException.class, iter::previous); // now, read it forwards again for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - } @Test 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 84842dd06..783368c7a 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ListIteratorWrapperTest.java @@ -75,39 +75,22 @@ public class ListIteratorWrapperTest<E> extends AbstractIteratorTest<E> { final ListIterator<E> iter = makeObject(); for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } - + assertThrows(NoSuchElementException.class, iter::next); // now, read it backwards for (int i = testArray.length - 1; i > -1; --i) { final Object testValue = testArray[i]; final E iterValue = iter.previous(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - - try { - iter.previous(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } - + assertThrows(NoSuchElementException.class, iter::previous); // now, read it forwards again for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - } @Test diff --git a/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java index ac3497123..fb4137cb2 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java @@ -77,17 +77,10 @@ public class ObjectArrayIteratorTest<E> extends AbstractIteratorTest<E> { final Iterator<E> iter = makeObject(); for (final String testValue : testArray) { final E iterValue = iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); } @Test diff --git a/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayListIteratorTest.java index b3f140675..0efd6d18b 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayListIteratorTest.java @@ -59,30 +59,19 @@ public class ObjectArrayListIteratorTest<E> extends ObjectArrayIteratorTest<E> { @Test public void testListIterator() { final ListIterator<E> iter = makeObject(); - // TestArrayIterator#testIterator() has already tested the iterator forward, // now we need to test it in reverse - // fast-forward the iterator to the end... while (iter.hasNext()) { iter.next(); } - for (int x = testArray.length - 1; x >= 0; x--) { final Object testValue = testArray[x]; final Object iterValue = iter.previous(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasPrevious(), "Iterator should now be empty"); - - try { - iter.previous(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } - + assertThrows(NoSuchElementException.class, iter::previous); } /** diff --git a/src/test/java/org/apache/commons/collections4/iterators/SingletonIterator2Test.java b/src/test/java/org/apache/commons/collections4/iterators/SingletonIterator2Test.java index 3438538c3..e84ef9ad0 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/SingletonIterator2Test.java +++ b/src/test/java/org/apache/commons/collections4/iterators/SingletonIterator2Test.java @@ -18,6 +18,7 @@ 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.Iterator; @@ -70,17 +71,10 @@ public class SingletonIterator2Test<E> extends AbstractIteratorTest<E> { public void testIterator() { final Iterator<E> iter = makeObject(); assertTrue(iter.hasNext(), "Iterator has a first item"); - final E iterValue = iter.next(); assertEquals(testValue, iterValue, "Iteration value is correct"); - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); } @Test diff --git a/src/test/java/org/apache/commons/collections4/iterators/SingletonIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/SingletonIteratorTest.java index 31ff4b9f3..e0f3f7b2f 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/SingletonIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/SingletonIteratorTest.java @@ -18,6 +18,7 @@ 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.Iterator; @@ -73,17 +74,10 @@ public class SingletonIteratorTest<E> extends AbstractIteratorTest<E> { public void testIterator() { final Iterator<E> iter = makeObject(); assertTrue(iter.hasNext(), "Iterator has a first item"); - final E iterValue = iter.next(); assertEquals(testValue, iterValue, "Iteration value is correct"); - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); } @Test diff --git a/src/test/java/org/apache/commons/collections4/iterators/SingletonListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/SingletonListIteratorTest.java index b8d4a5388..c2716d884 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/SingletonListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/SingletonListIteratorTest.java @@ -18,6 +18,7 @@ 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.ListIterator; @@ -105,17 +106,9 @@ public class SingletonListIteratorTest<E> extends AbstractListIteratorTest<E> { assertEquals(1, iter.nextIndex(), "Iteration next index"); assertEquals(0, iter.previousIndex(), "Iteration previous index"); - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); iter.previous(); - try { - iter.previous(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::previous); } @Test diff --git a/src/test/java/org/apache/commons/collections4/iterators/UniqueFilterIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/UniqueFilterIteratorTest.java index 079fa961e..a971592a3 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/UniqueFilterIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/UniqueFilterIteratorTest.java @@ -18,6 +18,7 @@ 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 java.util.ArrayList; import java.util.Iterator; @@ -77,17 +78,10 @@ public class UniqueFilterIteratorTest<E> extends AbstractIteratorTest<E> { final Iterator<E> iter = makeObject(); for (final String testValue : testArray) { final E iterValue = iter.next(); - assertEquals(testValue, iterValue, "Iteration value is correct"); } - assertFalse(iter.hasNext(), "Iterator should now be empty"); - - try { - iter.next(); - } catch (final Exception e) { - assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown"); - } + assertThrows(NoSuchElementException.class, iter::next); } } diff --git a/src/test/java/org/apache/commons/collections4/list/CursorableLinkedListTest.java b/src/test/java/org/apache/commons/collections4/list/CursorableLinkedListTest.java index 491e8fa7e..2615d9de0 100644 --- a/src/test/java/org/apache/commons/collections4/list/CursorableLinkedListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/CursorableLinkedListTest.java @@ -454,11 +454,7 @@ public class CursorableLinkedListTest<E> extends AbstractLinkedListTest<E> { assertEquals("3", it.next()); it.remove(); assertEquals("[4, 5]", list.toString()); - try { - it.remove(); - } catch (final IllegalStateException e) { - // expected - } + assertThrows(IllegalStateException.class, it::remove); assertEquals("4", it.next()); assertEquals("5", it.next()); it.remove(); @@ -1124,11 +1120,7 @@ public class CursorableLinkedListTest<E> extends AbstractLinkedListTest<E> { list.add((E) "5"); final ListIterator<E> it = list.listIterator(); - try { - it.remove(); - } catch (final IllegalStateException e) { - // expected - } + assertThrows(IllegalStateException.class, it::remove); assertEquals("1", it.next()); assertEquals("2", it.next()); assertEquals("[1, 2, 3, 4, 5]", list.toString()); @@ -1143,11 +1135,7 @@ public class CursorableLinkedListTest<E> extends AbstractLinkedListTest<E> { assertEquals("3", it.next()); it.remove(); assertEquals("[4, 5]", list.toString()); - try { - it.remove(); - } catch (final IllegalStateException e) { - // expected - } + assertThrows(IllegalStateException.class, it::remove); assertEquals("4", it.next()); assertEquals("5", it.next()); it.remove(); diff --git a/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java b/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java index 2356612db..dc2d10bbf 100644 --- a/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java +++ b/src/test/java/org/apache/commons/collections4/list/DefaultAbstractLinkedListForJava21Test.java @@ -101,12 +101,8 @@ public class DefaultAbstractLinkedListForJava21Test<E> extends AbstractListTest< resetEmpty(); final AbstractLinkedListForJava21<E> list = getCollection(); if (!isAddSupported()) { - try { - list.addFirst(null); - } catch (final UnsupportedOperationException ex) { - } + assertThrows(UnsupportedOperationException.class, () -> list.addFirst(null)); } - list.addFirst((E) "value1"); list.addNodeAfter(list.getNode(0, false), (E) "value2"); assertEquals("value1", list.getFirst()); @@ -155,12 +151,8 @@ public class DefaultAbstractLinkedListForJava21Test<E> extends AbstractListTest< resetEmpty(); final AbstractLinkedListForJava21<E> list = getCollection(); if (!isRemoveSupported()) { - try { - list.removeFirst(); - } catch (final UnsupportedOperationException ex) { - } + assertThrows(UnsupportedOperationException.class, list::removeFirst); } - list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" })); assertEquals("value1", list.removeFirst()); checkNodes(); @@ -181,10 +173,7 @@ public class DefaultAbstractLinkedListForJava21Test<E> extends AbstractListTest< resetEmpty(); final AbstractLinkedListForJava21<E> list = getCollection(); if (!isRemoveSupported()) { - try { - list.removeLast(); - } catch (final UnsupportedOperationException ex) { - } + assertThrows(UnsupportedOperationException.class, list::removeLast); } list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" })); diff --git a/src/test/java/org/apache/commons/collections4/map/LinkedMapTest.java b/src/test/java/org/apache/commons/collections4/map/LinkedMapTest.java index 6afea17e2..de335229f 100644 --- a/src/test/java/org/apache/commons/collections4/map/LinkedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/LinkedMapTest.java @@ -19,6 +19,7 @@ package org.apache.commons.collections4.map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.Iterator; @@ -134,27 +135,12 @@ public class LinkedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { @Test public void testGetByIndex() { resetEmpty(); - LinkedMap<K, V> lm = getMap(); - try { - lm.get(0); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lm.get(-1); - } catch (final IndexOutOfBoundsException ex) { - } - + assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(0)); + assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(-1)); resetFull(); - lm = getMap(); - try { - lm.get(-1); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lm.get(lm.size()); - } catch (final IndexOutOfBoundsException ex) { - } - + final LinkedMap<K, V> lm = getMap(); + assertThrows(IndexOutOfBoundsException.class, () -> lm.get(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> lm.get(lm.size())); int i = 0; for (final MapIterator<K, V> it = lm.mapIterator(); it.hasNext(); i++) { assertSame(it.next(), lm.get(i)); @@ -164,27 +150,12 @@ public class LinkedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { @Test public void testGetValueByIndex() { resetEmpty(); - LinkedMap<K, V> lm = getMap(); - try { - lm.getValue(0); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lm.getValue(-1); - } catch (final IndexOutOfBoundsException ex) { - } - + assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(0)); + assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(-1)); resetFull(); - lm = getMap(); - try { - lm.getValue(-1); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lm.getValue(lm.size()); - } catch (final IndexOutOfBoundsException ex) { - } - + LinkedMap<K, V> lm = getMap(); + assertThrows(IndexOutOfBoundsException.class, () -> lm.getValue(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> lm.getValue(lm.size())); int i = 0; for (final MapIterator<K, V> it = lm.mapIterator(); it.hasNext(); i++) { it.next(); @@ -276,27 +247,12 @@ public class LinkedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { @Test public void testRemoveByIndex() { resetEmpty(); - LinkedMap<K, V> lm = getMap(); - try { - lm.remove(0); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lm.remove(-1); - } catch (final IndexOutOfBoundsException ex) { - } - + assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(0)); + assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(-1)); resetFull(); - lm = getMap(); - try { - lm.remove(-1); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lm.remove(lm.size()); - } catch (final IndexOutOfBoundsException ex) { - } - + LinkedMap<K, V> lm = getMap(); + assertThrows(IndexOutOfBoundsException.class, () -> lm.remove(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> lm.remove(lm.size())); final List<K> list = new ArrayList<>(); for (final MapIterator<K, V> it = lm.mapIterator(); it.hasNext();) { list.add(it.next());