diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 521b33ba5c..9f87645c6f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -42,6 +42,8 @@ Fix generics in org.apache.commons.collections4.IteratorUtils.chainedIterator(Collection). Javadoc: Document interaction between peek and filter iterator #515. Javadoc: Update ClosureUtils Javadoc to match runtime. + Javadoc: Update ClosureUtils Javadoc to match runtime. + Migrate to JUnit 5. LayerManager.Builder implements Supplier. Add CollectionUtils.duplicateList(Collection). 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 40d2ef8892..93a3be1dec 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 70b8954aa9..8bfd27a1c7 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 void testFullIterator() { 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 b01f538a84..39b711e4ee 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 void testRemove() { // override 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 3f3c4a1640..02c5e25460 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.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 void testIndexedArray() { 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 void testIndexedArray() { () -> 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 void testIterator() { 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 071b4a71a1..8bfec1c95d 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 boolean supportsRemove() { @Test public void testListIterator() { final ListIterator 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 52f8f82387..56f72d26d6 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 @@ 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 2f3f515b9e..90e83f112c 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.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 void testNullComparator() { final List l2 = Arrays.asList(2, 4, 6); final CollatingIterator 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 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 1536b1084c..ef02977ecc 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 void testIterator() { 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 c520edce31..20df473c16 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 void testIterator() { final Iterator 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 5fd06163f0..e3c381255c 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 void testIterator() { final ListIterator 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 84842dd06e..783368c7a6 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 void testIterator() { final ListIterator 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 ac34971236..fb4137cb22 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 void testIterator() { final Iterator 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 b3f140675b..0efd6d18bf 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 ObjectArrayListIterator makeObject() { @Test public void testListIterator() { final ListIterator 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 3438538c31..e84ef9ad0e 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 @@ 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 boolean supportsRemove() { public void testIterator() { final Iterator 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 31ff4b9f32..e0f3f7b2f5 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 @@ 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 boolean supportsRemove() { public void testIterator() { final Iterator 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 b8d4a53883..c2716d8840 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 @@ 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 void testIterator() { 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 079fa961e0..a971592a3e 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 @@ 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 void testIterator() { final Iterator 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 491e8fa7e8..2615d9de0a 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 void testCursorRemove() { 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 void testListIteratorRemove() { list.add((E) "5"); final ListIterator 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 void testListIteratorRemove() { 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 2356612db7..dc2d10bbf1 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 void testAddNodeAfter() { resetEmpty(); final AbstractLinkedListForJava21 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 void testRemoveFirst() { resetEmpty(); final AbstractLinkedListForJava21 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 void testRemoveLast() { resetEmpty(); final AbstractLinkedListForJava21 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 6afea17e26..de335229f2 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 @@ 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 void testClone() { @Test public void testGetByIndex() { resetEmpty(); - LinkedMap 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 lm = getMap(); + assertThrows(IndexOutOfBoundsException.class, () -> lm.get(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> lm.get(lm.size())); int i = 0; for (final MapIterator it = lm.mapIterator(); it.hasNext(); i++) { assertSame(it.next(), lm.get(i)); @@ -164,27 +150,12 @@ public void testGetByIndex() { @Test public void testGetValueByIndex() { resetEmpty(); - LinkedMap 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 lm = getMap(); + assertThrows(IndexOutOfBoundsException.class, () -> lm.getValue(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> lm.getValue(lm.size())); int i = 0; for (final MapIterator it = lm.mapIterator(); it.hasNext(); i++) { it.next(); @@ -276,27 +247,12 @@ public void testInsertionOrder() { @Test public void testRemoveByIndex() { resetEmpty(); - LinkedMap 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 lm = getMap(); + assertThrows(IndexOutOfBoundsException.class, () -> lm.remove(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> lm.remove(lm.size())); final List list = new ArrayList<>(); for (final MapIterator it = lm.mapIterator(); it.hasNext();) { list.add(it.next());