diff --git a/src/test/java/org/apache/commons/collections4/iterators/AbstractOrderedMapIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/AbstractOrderedMapIteratorTest.java index b6c967737f..18cec702ac 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractOrderedMapIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractOrderedMapIteratorTest.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.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -67,11 +73,13 @@ public void testEmptyMapIterator() { super.testEmptyMapIterator(); final OrderedMapIterator it = makeEmptyIterator(); - assertEquals(false, it.hasPrevious()); - try { + assertFalse(it.hasPrevious()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.previous(); - fail(); - } catch (final NoSuchElementException ex) {} + }); + if (null != exception.getMessage()) { + assertTrue(exception.getMessage().contains("Iterator contains no elements")); + } } //----------------------------------------------------------------------- @@ -89,24 +97,24 @@ public void testFullMapIterator() { final OrderedMapIterator it = makeObject(); final Map map = getMap(); - assertEquals(true, it.hasNext()); - assertEquals(false, it.hasPrevious()); + assertTrue(it.hasNext()); + assertFalse(it.hasPrevious()); 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)); - assertEquals(true, it.hasPrevious()); + assertTrue(it.hasPrevious()); verify(); } @@ -114,17 +122,17 @@ public void testFullMapIterator() { // getKey final Object key = it.previous(); assertSame("it.previous() should equals getKey()", key, it.getKey()); - assertTrue("Key must be in map", map.containsKey(key)); - assertTrue("Key must be unique", set.remove(key)); + assertTrue(map.containsKey(key)); + assertTrue(set.remove(key)); // getValue final Object 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)); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); verify(); } @@ -145,8 +153,8 @@ public void testMapIteratorOrder() { assertEquals("keySet() not consistent", new ArrayList<>(map.keySet()), new ArrayList<>(map.keySet())); final Iterator it2 = map.keySet().iterator(); - assertEquals(true, it.hasNext()); - assertEquals(true, it2.hasNext()); + assertTrue(it.hasNext()); + assertTrue(it2.hasNext()); final List list = new ArrayList<>(); while (it.hasNext()) { final K key = it.next(); diff --git a/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java index 74d51f18df..2d0829b19a 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.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; @@ -56,24 +61,19 @@ 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 testNullArray() { - try { + Exception exception = assertThrows(NullPointerException.class, () -> { new ArrayIterator<>(null); - fail("Constructor should throw a NullPointerException when constructed with a null array"); - } catch (final NullPointerException e) { - // expected - } + }); + assertNull(exception.getMessage()); } public void testReset() { 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 eeb548580c..cd05c735bb 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.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.Arrays; import java.util.Collections; @@ -81,11 +87,10 @@ public void testBounded() { assertEquals("f", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -114,11 +119,10 @@ public void testSameAsDecorated() { assertEquals("g", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -130,11 +134,10 @@ public void testSameAsDecorated() { public void testEmptyBounded() { final Iterator iter = new BoundedIterator<>(testList.iterator(), 3, 0); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -143,11 +146,10 @@ public void testEmptyBounded() { */ @Test public void testNegativeOffset() { - try { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { new BoundedIterator<>(testList.iterator(), -1, 4); - fail("Expected IllegalArgumentException."); - } catch (final IllegalArgumentException iae) { /* Success case */ - } + }); + assertTrue(exception.getMessage().contains("Offset parameter must not be negative")); } /** @@ -156,11 +158,10 @@ public void testNegativeOffset() { */ @Test public void testNegativeMax() { - try { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { new BoundedIterator<>(testList.iterator(), 3, -1); - fail("Expected IllegalArgumentException."); - } catch (final IllegalArgumentException iae) { /* Success case */ - } + }); + assertTrue(exception.getMessage().contains("Max parameter must not be negative.")); } /** @@ -172,11 +173,10 @@ public void testNegativeMax() { public void testOffsetGreaterThanSize() { final Iterator iter = new BoundedIterator<>(testList.iterator(), 10, 4); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -203,11 +203,10 @@ public void testMaxGreaterThanSize() { assertEquals("g", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -219,11 +218,10 @@ public void testRemoveWithoutCallingNext() { final List testListCopy = new ArrayList<>(testList); final Iterator iter = new BoundedIterator<>(testListCopy.iterator(), 1, 5); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("Expected IllegalStateException."); - } catch (final IllegalStateException ise) { /* Success case */ - } + }); + assertTrue(exception.getMessage().contains("remove() can not be called before calling next()")); } /** @@ -239,11 +237,10 @@ public void testRemoveCalledTwice() { assertEquals("b", iter.next()); iter.remove(); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("Expected IllegalStateException."); - } catch (final IllegalStateException ise) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -271,11 +268,10 @@ public void testRemoveFirst() { assertEquals("f", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -303,11 +299,10 @@ public void testRemoveMiddle() { assertEquals("f", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -331,21 +326,19 @@ public void testRemoveLast() { assertEquals("f", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); iter.remove(); assertFalse(testListCopy.contains("f")); assertFalse(iter.hasNext()); - try { + exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -364,10 +357,9 @@ public void remove() { final Iterator iter = new BoundedIterator<>(mockIterator, 1, 5); assertTrue(iter.hasNext()); assertEquals("b", iter.next()); - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { iter.remove(); - fail("Expected UnsupportedOperationException."); - } catch (final UnsupportedOperationException usoe) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java index 461f9f2453..02ad1d1b7b 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/FilterIteratorTest.java @@ -17,6 +17,11 @@ package org.apache.commons.collections4.iterators; import static org.apache.commons.collections4.functors.TruePredicate.truePredicate; +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.Arrays; @@ -121,11 +126,11 @@ public void testSetIterator() { final FilterIterator filterIterator = new FilterIterator<>(iter1); filterIterator.setPredicate(truePredicate()); // this iterator has elements - assertEquals(true, filterIterator.hasNext()); + assertTrue(filterIterator.hasNext()); // this iterator has no elements filterIterator.setIterator(iter2); - assertEquals(false, filterIterator.hasNext()); + assertFalse(filterIterator.hasNext()); } /** @@ -147,12 +152,10 @@ public void testSetPredicate() { private void verifyNoMoreElements() { assertTrue(!iterator.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iterator.next(); - fail("NoSuchElementException expected"); - } catch (final NoSuchElementException e) { - // success - } + }); + assertNull(exception.getMessage()); } private void verifyElementsInPredicate(final String[] elements) { @@ -179,7 +182,7 @@ private void verifyElementsInPredicate(final String[] elements) { if (iterator.hasNext()) { final Object last = iterator.next(); iterator.remove(); - assertTrue("Base of FilterIterator still contains removed element.", !list.contains(last)); + assertTrue(!list.contains(last)); } } 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 e3d8e325d5..fc4e315eb5 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/IteratorChainTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/IteratorChainTest.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; @@ -76,17 +81,15 @@ public void testIterator() { for (final String testValue : testArray) { final Object iterValue = iter.next(); - assertEquals( "Iteration value is correct", testValue, iterValue ); + 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() { @@ -117,12 +120,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(); @@ -134,9 +135,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() { @@ -148,26 +149,26 @@ public void testFirstIteratorIsEmptyBug() { final IteratorChain chain = new IteratorChain<>(); chain.addIterator(empty.iterator()); chain.addIterator(notEmpty.iterator()); - 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 IteratorChain chain = new IteratorChain<>(); 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/IteratorEnumerationTest.java b/src/test/java/org/apache/commons/collections4/iterators/IteratorEnumerationTest.java index 2c6c4f0f96..8b45d9abe2 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/IteratorEnumerationTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/IteratorEnumerationTest.java @@ -16,12 +16,17 @@ */ 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.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; import org.junit.Test; -import static org.junit.Assert.*; /** * Tests the IteratorEnumeration. @@ -42,11 +47,9 @@ public void testEnumeration() { assertEquals("c", enumeration.nextElement()); assertFalse(enumeration.hasMoreElements()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { enumeration.nextElement(); - fail("NoSuchElementException expected"); - } catch (final NoSuchElementException e) { - // expected - } + }); + assertNull(exception.getMessage()); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/NodeListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/NodeListIteratorTest.java index bde677637e..b8cf60387a 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/NodeListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/NodeListIteratorTest.java @@ -17,6 +17,9 @@ package org.apache.commons.collections4.iterators; import static org.easymock.EasyMock.*; +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; @@ -112,12 +115,10 @@ public boolean supportsRemove() { //----------------------------------------------------------------------- public void testNullConstructor(){ - try{ + Exception exception = assertThrows(NullPointerException.class, () -> { new NodeListIterator((Node) null); - fail("NullPointerException expected!"); - }catch(final NullPointerException e){ - // expected. - } + }); + assertTrue(exception.getMessage().contains("node")); } /** 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 f17d3d1ca1..8c9d95443b 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.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; @@ -73,25 +78,19 @@ 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 testNullArray() { - try { + Exception exception = assertThrows(NullPointerException.class, () -> { makeArrayIterator(null); - - fail("Constructor should throw a NullPointerException when constructed with a null array"); - } catch (final NullPointerException e) { - // expected - } + }); + assertNull(exception.getMessage()); } @SuppressWarnings("unchecked") 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 7d9975d5f5..b8a87ab6cc 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayListIteratorTest.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,16 +73,12 @@ public void testListIterator() { assertEquals("Iteration value is correct", 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()); } /** @@ -98,20 +99,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((E[]) 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((E[]) 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/ObjectGraphIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/ObjectGraphIteratorTest.java index caff4ed312..85a7d5f606 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ObjectGraphIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ObjectGraphIteratorTest.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.assertSame; +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; @@ -74,35 +80,31 @@ public ObjectGraphIterator makeObject() { public void testIteratorConstructor_null1() { final Iterator it = new ObjectGraphIterator<>(null); - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } - try { + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); + exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail(); - } catch (final IllegalStateException ex) { - } + }); + assertTrue(exception.getMessage().contains("Iterator remove() cannot be called at this time")); } public void testIteratorConstructor_null_next() { final Iterator it = new ObjectGraphIterator<>(null); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } public void testIteratorConstructor_null_remove() { final Iterator it = new ObjectGraphIterator<>(null); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail(); - } catch (final IllegalStateException ex) { - } + }); + assertTrue(exception.getMessage().contains("Iterator remove() cannot be called at this time")); } //----------------------------------------------------------------------- @@ -110,17 +112,15 @@ public void testIteratorConstructorIteration_Empty() { final List> iteratorList = new ArrayList<>(); final Iterator it = new ObjectGraphIterator<>(iteratorList.iterator()); - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } - try { + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); + exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail(); - } catch (final IllegalStateException ex) { - } + }); + assertTrue(exception.getMessage().contains("Iterator remove() cannot be called at this time")); } public void testIteratorConstructorIteration_Simple() { @@ -131,15 +131,14 @@ public void testIteratorConstructorIteration_Simple() { final Iterator it = new ObjectGraphIterator<>(iteratorList.iterator()); for (int i = 0; i < 6; i++) { - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(testArray[i], it.next()); } - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } public void testIteratorConstructorIteration_SimpleNoHasNext() { @@ -152,11 +151,10 @@ public void testIteratorConstructorIteration_SimpleNoHasNext() { for (int i = 0; i < 6; i++) { assertEquals(testArray[i], it.next()); } - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } public void testIteratorConstructorIteration_WithEmptyIterators() { @@ -171,15 +169,14 @@ public void testIteratorConstructorIteration_WithEmptyIterators() { final Iterator it = new ObjectGraphIterator<>(iteratorList.iterator()); for (int i = 0; i < 6; i++) { - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(testArray[i], it.next()); } - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } public void testIteratorConstructorRemove() { @@ -193,7 +190,7 @@ public void testIteratorConstructorRemove() { assertEquals(testArray[i], it.next()); it.remove(); } - assertEquals(false, it.hasNext()); + assertFalse(it.hasNext()); assertEquals(0, list1.size()); assertEquals(0, list2.size()); assertEquals(0, list3.size()); @@ -208,10 +205,10 @@ public void testIteration_IteratorOfIterators() { final Iterator it = new ObjectGraphIterator<>(iteratorList.iterator(), null); for (int i = 0; i < 6; i++) { - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(testArray[i], it.next()); } - assertEquals(false, it.hasNext()); + assertFalse(it.hasNext()); } public void testIteration_IteratorOfIteratorsWithEmptyIterators() { @@ -226,41 +223,38 @@ public void testIteration_IteratorOfIteratorsWithEmptyIterators() { final Iterator it = new ObjectGraphIterator<>(iteratorList.iterator(), null); for (int i = 0; i < 6; i++) { - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(testArray[i], it.next()); } - assertEquals(false, it.hasNext()); + assertFalse(it.hasNext()); } //----------------------------------------------------------------------- public void testIteration_RootNull() { final Iterator it = new ObjectGraphIterator<>(null, null); - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } - try { + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); + exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail(); - } catch (final IllegalStateException ex) { - } + }); + assertTrue(exception.getMessage().contains("Iterator remove() cannot be called at this time")); } public void testIteration_RootNoTransformer() { final Forest forest = new Forest(); final Iterator it = new ObjectGraphIterator<>(forest, null); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(forest, it.next()); - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } public void testIteration_Transformed1() { @@ -268,14 +262,13 @@ public void testIteration_Transformed1() { final Leaf l1 = forest.addTree().addBranch().addLeaf(); final Iterator it = new ObjectGraphIterator<>(forest, new LeafFinder()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l1, it.next()); - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } public void testIteration_Transformed2() { @@ -296,22 +289,21 @@ public void testIteration_Transformed2() { final Iterator it = new ObjectGraphIterator<>(forest, new LeafFinder()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l1, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l2, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l3, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l4, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l5, it.next()); - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } public void testIteration_Transformed3() { @@ -332,22 +324,21 @@ public void testIteration_Transformed3() { final Iterator it = new ObjectGraphIterator<>(forest, new LeafFinder()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l1, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l2, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l3, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l4, it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertSame(l5, it.next()); - assertEquals(false, it.hasNext()); - try { + assertFalse(it.hasNext()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException ex) { - } + }); + assertTrue(exception.getMessage().contains("No more elements in the iteration")); } //----------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/collections4/iterators/PeekingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/PeekingIteratorTest.java index cdf10f5aa3..3bba363a59 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/PeekingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/PeekingIteratorTest.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.Arrays; import java.util.Collections; @@ -106,12 +112,10 @@ public void testIteratorExhausted() { assertFalse(it.hasNext()); assertNull(it.peek()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.element(); - fail(); - } catch (final NoSuchElementException e) { - // expected - } + }); + assertNull(exception.getMessage()); } @Test @@ -123,12 +127,10 @@ public void testIllegalRemove() { assertTrue(it.hasNext()); assertEquals("b", it.peek()); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { it.remove(); - fail(); - } catch (final IllegalStateException e) { - // expected - } + }); + assertTrue(exception.getMessage().contains("peek() or element() called before remove()")); } private void validate(final Iterator iter, final E... items) { diff --git a/src/test/java/org/apache/commons/collections4/iterators/PermutationIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/PermutationIteratorTest.java index 2f38a095e6..953033fedd 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/PermutationIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/PermutationIteratorTest.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.Arrays; import java.util.HashSet; @@ -72,7 +78,7 @@ public PermutationIterator makeObject() { @SuppressWarnings("boxing") // OK in test code public void testPermutationResultSize() { int factorial = 1; - for (int i = 0; i < 8; i++, factorial*=i) { + for (int i = 0; i < 8; i++, factorial *= i) { final List list = new ArrayList<>(); for (int j = 0; j < i; j++) { list.add(j); @@ -164,12 +170,10 @@ public void testPermutationException() { resultsList.add(permutation); } //asking for another permutation should throw an exception - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.next(); - fail(); - } catch (final NoSuchElementException e) { - // expected - } + }); + assertNull(exception.getMessage()); } public void testPermutatorHasMore() { diff --git a/src/test/java/org/apache/commons/collections4/iterators/ReverseListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/ReverseListIteratorTest.java index ba6516f2e1..0be9e704f4 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/ReverseListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/ReverseListIteratorTest.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.Arrays; import java.util.List; @@ -54,24 +60,22 @@ public ReverseListIterator makeObject() { public void testEmptyListIteratorIsIndeedEmpty() { final ListIterator it = makeEmptyIterator(); - assertEquals(false, it.hasNext()); + assertFalse(it.hasNext()); assertEquals(-1, it.nextIndex()); // reversed index - assertEquals(false, it.hasPrevious()); + assertFalse(it.hasPrevious()); assertEquals(0, it.previousIndex()); // reversed index // 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) { - } + }); + assertNull(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) { - } + }); + assertNull(exception.getMessage()); } @Override @@ -83,8 +87,8 @@ public void testWalkForwardAndBack() { } // check state at end - assertEquals(false, it.hasNext()); - assertEquals(true, it.hasPrevious()); + assertFalse(it.hasNext()); + assertTrue(it.hasPrevious()); // this had to be commented out, as there is a bug in the JDK before JDK1.5 // where calling previous at the start of an iterator would push the cursor @@ -105,41 +109,40 @@ public void testWalkForwardAndBack() { } // check state at start - assertEquals(true, it.hasNext()); - assertEquals(false, it.hasPrevious()); - try { + assertTrue(it.hasNext()); + assertFalse(it.hasPrevious()); + Exception exception = assertThrows(NoSuchElementException.class, () -> { it.previous(); - fail("NoSuchElementException must be thrown from previous at start of ListIterator"); - } catch (final NoSuchElementException e) { - } + }); + assertNull(exception.getMessage()); } //----------------------------------------------------------------------- public void testReverse() { final ListIterator it = makeObject(); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(3, it.nextIndex()); - assertEquals(false, it.hasPrevious()); + assertFalse(it.hasPrevious()); assertEquals(4, it.previousIndex()); assertEquals("Four", it.next()); assertEquals(2, it.nextIndex()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(3, it.previousIndex()); - assertEquals(true, it.hasPrevious()); + assertTrue(it.hasPrevious()); assertEquals("Three", it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(1, it.nextIndex()); - assertEquals(true, it.hasPrevious()); + assertTrue(it.hasPrevious()); assertEquals(2, it.previousIndex()); assertEquals("Two", it.next()); - assertEquals(true, it.hasNext()); + assertTrue(it.hasNext()); assertEquals(0, it.nextIndex()); - assertEquals(true, it.hasPrevious()); + assertTrue(it.hasPrevious()); assertEquals(1, it.previousIndex()); assertEquals("One", it.next()); - assertEquals(false, it.hasNext()); + assertFalse(it.hasNext()); assertEquals(-1, it.nextIndex()); - assertEquals(true, it.hasPrevious()); + assertTrue(it.hasPrevious()); assertEquals(0, it.previousIndex()); assertEquals("One", it.previous()); assertEquals("Two", it.previous()); diff --git a/src/test/java/org/apache/commons/collections4/iterators/SkippingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/SkippingIteratorTest.java index af96438986..f9b1d63b14 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/SkippingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/SkippingIteratorTest.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.Arrays; import java.util.Collections; @@ -83,11 +89,10 @@ public void testSkipping() { assertEquals("g", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -115,11 +120,10 @@ public void testSameAsDecorated() { assertEquals("g", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -131,11 +135,10 @@ public void testSameAsDecorated() { public void testOffsetGreaterThanSize() { final Iterator iter = new SkippingIterator<>(testList.iterator(), 10); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -144,11 +147,10 @@ public void testOffsetGreaterThanSize() { */ @Test public void testNegativeOffset() { - try { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { new SkippingIterator<>(testList.iterator(), -1); - fail("Expected IllegalArgumentException."); - } catch (final IllegalArgumentException iae) { /* Success case */ - } + }); + assertTrue(exception.getMessage().contains("Offset parameter must not be negative")); } /** @@ -160,11 +162,10 @@ public void testRemoveWithoutCallingNext() { final List testListCopy = new ArrayList<>(testList); final Iterator iter = new SkippingIterator<>(testListCopy.iterator(), 1); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("Expected IllegalStateException."); - } catch (final IllegalStateException ise) { /* Success case */ - } + }); + assertTrue(exception.getMessage().contains("remove() can not be called before calling next()")); } /** @@ -180,11 +181,10 @@ public void testRemoveCalledTwice() { assertEquals("b", iter.next()); iter.remove(); - try { + Exception exception = assertThrows(IllegalStateException.class, () -> { iter.remove(); - fail("Expected IllegalStateException."); - } catch (final IllegalStateException ise) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -208,11 +208,10 @@ public void testRemoveFirst() { assertEquals("g", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -238,11 +237,10 @@ public void testRemoveMiddle() { assertEquals("g", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -260,21 +258,19 @@ public void testRemoveLast() { assertEquals("g", iter.next()); assertFalse(iter.hasNext()); - try { + Exception exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); iter.remove(); assertFalse(testListCopy.contains("g")); assertFalse(iter.hasNext()); - try { + exception = assertThrows(NoSuchElementException.class, () -> { iter.next(); - fail("Expected NoSuchElementException."); - } catch (final NoSuchElementException nsee) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } /** @@ -293,10 +289,9 @@ public void remove() { final Iterator iter = new SkippingIterator<>(mockIterator, 1); assertTrue(iter.hasNext()); assertEquals("b", iter.next()); - try { + Exception exception = assertThrows(UnsupportedOperationException.class, () -> { iter.remove(); - fail("Expected UnsupportedOperationException."); - } catch (final UnsupportedOperationException usoe) { /* Success case */ - } + }); + assertNull(exception.getMessage()); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableIteratorTest.java index 6ba8fd9f5f..936bd72345 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableIteratorTest.java @@ -16,6 +16,10 @@ */ package org.apache.commons.collections4.iterators; +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; import java.util.Arrays; import java.util.Collections; @@ -74,10 +78,10 @@ public void testDecorateFactory() { it = testList.iterator(); assertTrue(it != UnmodifiableIterator.unmodifiableIterator(it)); - try { + Exception exception = assertThrows(NullPointerException.class, () -> { UnmodifiableIterator.unmodifiableIterator(null); - fail(); - } catch (final NullPointerException ex) {} + }); + assertTrue(exception.getMessage().contains("iterator")); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableListIteratorTest.java index 7f6ca26156..1573b876fb 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableListIteratorTest.java @@ -16,6 +16,10 @@ */ package org.apache.commons.collections4.iterators; +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; import java.util.Arrays; import java.util.Collections; @@ -84,10 +88,10 @@ public void testDecorateFactory() { it = testList.listIterator(); assertTrue(it != UnmodifiableListIterator.umodifiableListIterator(it)); - try { + Exception exception = assertThrows(NullPointerException.class, () -> { UnmodifiableListIterator.umodifiableListIterator(null); - fail(); - } catch (final NullPointerException ex) {} + }); + assertTrue(exception.getMessage().contains("iterator")); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableMapIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableMapIteratorTest.java index 03da805140..96ac0a4a46 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableMapIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableMapIteratorTest.java @@ -16,6 +16,10 @@ */ package org.apache.commons.collections4.iterators; +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.HashMap; import java.util.Map; @@ -86,10 +90,10 @@ public void testDecorateFactory() { it = getMap().mapIterator(); assertTrue(it != UnmodifiableMapIterator.unmodifiableMapIterator(it)); - try { + Exception exception = assertThrows(NullPointerException.class, () -> { UnmodifiableMapIterator.unmodifiableMapIterator(null); - fail(); - } catch (final NullPointerException ex) {} + }); + assertTrue(exception.getMessage().contains("iterator")); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableOrderedMapIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableOrderedMapIteratorTest.java index c35e9c43d9..ccefdaf79a 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableOrderedMapIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/UnmodifiableOrderedMapIteratorTest.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.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.HashMap; import java.util.Map; import java.util.TreeMap; @@ -88,10 +93,10 @@ public void testDecorateFactory() { it = getMap().mapIterator(); assertTrue(it != UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it)); - try { + Exception exception = assertThrows(NullPointerException.class, () -> { UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(null); - fail(); - } catch (final NullPointerException ex) {} + }); + assertTrue(exception.getMessage().contains("iterator")); } } diff --git a/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java b/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java index c36c1d5c27..6ee46a5170 100644 --- a/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/FixedSizeListTest.java @@ -16,10 +16,11 @@ */ package org.apache.commons.collections4.list; +import static org.junit.jupiter.api.Assertions.assertEquals; +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 org.junit.Assert; import java.util.ArrayList; import java.util.Arrays; @@ -81,9 +82,9 @@ public void testListAllowsMutationOfUnderlyingCollection() { final int sizeBefore = fixedSizeList.size(); // final boolean changed = decoratedList.add("New Value"); - Assert.assertTrue(changed); + assertTrue(changed); // - Assert.assertEquals("Modifying an the underlying list is allowed", + assertEquals("Modifying an the underlying list is allowed", sizeBefore + 1, fixedSizeList.size()); } @@ -131,19 +132,19 @@ public void testSubList() { final FixedSizeList fixedSizeList = initFixedSizeList(); final List subFixedSizeList = fixedSizeList.subList(1, 1); - Assert.assertNotNull(subFixedSizeList); - Assert.assertEquals(0, subFixedSizeList.size()); + assertNotNull(subFixedSizeList); + assertEquals(0, subFixedSizeList.size()); } public void testIsFull() { final FixedSizeList fixedSizeList = initFixedSizeList(); - Assert.assertTrue(fixedSizeList.isFull()); + assertTrue(fixedSizeList.isFull()); } public void testMaxSize() { final FixedSizeList fixedSizeList = initFixedSizeList(); - Assert.assertEquals(2, fixedSizeList.maxSize()); + assertEquals(2, fixedSizeList.maxSize()); } }