diff --git a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java index e2523d817c..b544a4cc6d 100644 --- a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java @@ -1280,7 +1280,7 @@ public void testContainsAnyNullColl2() { final Collection list = new ArrayList<>(1); list.add("1"); final Collection list2 = null; - assertThrows(NullPointerException.class, () -> CollectionUtils.containsAny(list, list2)); + assertThrows(NullPointerException.class, () -> CollectionUtils.containsAny(list, list2)); } @Test @@ -1288,7 +1288,7 @@ public void testContainsAnyNullColl3() { final Collection list = new ArrayList<>(1); list.add("1"); final String[] array = null; - assertThrows(NullPointerException.class, () -> CollectionUtils.containsAny(list, array)); + assertThrows(NullPointerException.class, () -> CollectionUtils.containsAny(list, array)); } @Test @@ -1311,19 +1311,19 @@ public void testDisjunctionAsUnionMinusIntersection() { public void testDisjunctionNullColl1() { final Collection list = new ArrayList<>(1); list.add("1"); - assertThrows(NullPointerException.class, () -> CollectionUtils.disjunction(null, list)); + assertThrows(NullPointerException.class, () -> CollectionUtils.disjunction(null, list)); } @Test public void testDisjunctionNullColl2() { final Collection list = new ArrayList<>(1); list.add("1"); - assertThrows(NullPointerException.class, () -> CollectionUtils.disjunction(list, null)); + assertThrows(NullPointerException.class, () -> CollectionUtils.disjunction(list, null)); } @Test public void testGetCardinalityMapNull() { - assertThrows(NullPointerException.class, () -> CollectionUtils.getCardinalityMap(null)); + assertThrows(NullPointerException.class, () -> CollectionUtils.getCardinalityMap(null)); } @Test @@ -1369,21 +1369,21 @@ public int hash(final Integer o) { @Test public void testHashCodeNullEquator() { - assertThrows(NullPointerException.class, () -> CollectionUtils.hashCode(collectionB, null)); + assertThrows(NullPointerException.class, () -> CollectionUtils.hashCode(collectionB, null)); } @Test public void testIntersectionNullColl1() { final Collection list = new ArrayList<>(1); list.add("1"); - assertThrows(NullPointerException.class, () -> CollectionUtils.intersection(null, list)); + assertThrows(NullPointerException.class, () -> CollectionUtils.intersection(null, list)); } @Test public void testIntersectionNullColl2() { final Collection list = new ArrayList<>(1); list.add("1"); - assertThrows(NullPointerException.class, () -> CollectionUtils.intersection(list, null)); + assertThrows(NullPointerException.class, () -> CollectionUtils.intersection(list, null)); } // ----------------------------------------------------------------------- @@ -1482,7 +1482,7 @@ public int hash(final Integer o) { return o.intValue() % 2 == 0 ? Integer.valueOf(0).hashCode() : Integer.valueOf(1).hashCode(); } }; - assertThrows(NullPointerException.class, () -> CollectionUtils.isEqualCollection(null, list, e)); + assertThrows(NullPointerException.class, () -> CollectionUtils.isEqualCollection(null, list, e)); } @Test @@ -1504,12 +1504,12 @@ public int hash(final Integer o) { return o.intValue() % 2 == 0 ? Integer.valueOf(0).hashCode() : Integer.valueOf(1).hashCode(); } }; - assertThrows(NullPointerException.class, () -> CollectionUtils.isEqualCollection(list, null, e)); + assertThrows(NullPointerException.class, () -> CollectionUtils.isEqualCollection(list, null, e)); } @Test public void testIsEqualCollectionNullEquator() { - assertThrows(NullPointerException.class, () -> CollectionUtils.isEqualCollection(collectionA, collectionA, null)); + assertThrows(NullPointerException.class, () -> CollectionUtils.isEqualCollection(collectionA, collectionA, null)); } @Test @@ -1530,7 +1530,7 @@ public void testIsEqualCollectionToSelf() { @Test public void testIsFullNullColl() { - assertThrows(NullPointerException.class, () -> CollectionUtils.isFull(null)); + assertThrows(NullPointerException.class, () -> CollectionUtils.isFull(null)); } @Test diff --git a/src/test/java/org/apache/commons/collections4/bidimap/AbstractBidiMapTest.java b/src/test/java/org/apache/commons/collections4/bidimap/AbstractBidiMapTest.java index 6ec599b894..ce803b070b 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/AbstractBidiMapTest.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/AbstractBidiMapTest.java @@ -556,27 +556,23 @@ public void testBidiMapIteratorSet() { // at this point // key1=newValue1, key2=newValue2 - try { - it.setValue(newValue1); // should remove key1 - fail(); - } catch (final IllegalArgumentException ex) { - return; // simplest way of dealing with tricky situation - } - confirmed.put(key2, newValue1); - AbstractBidiMapTest.this.getConfirmed().remove(key1); - assertEquals(newValue1, it.getValue()); - assertTrue(bidi.containsKey(it.getKey())); - assertTrue(bidi.containsValue(newValue1)); - assertEquals(newValue1, bidi.get(it.getKey())); - assertFalse(bidi.containsKey(key1)); - assertFalse(bidi.containsValue(newValue2)); - verify(); - - // check for ConcurrentModification - it.next(); // if you fail here, maybe you should be throwing an IAE, see above - if (isRemoveSupported()) { - it.remove(); - } + assertThrows(IllegalArgumentException.class, () -> it.setValue(newValue1)); // should remove key1 + // below code was previously never executed +// confirmed.put(key2, newValue1); +// AbstractBidiMapTest.this.getConfirmed().remove(key1); +// assertEquals(newValue1, it.getValue()); +// assertTrue(bidi.containsKey(it.getKey())); +// assertTrue(bidi.containsValue(newValue1)); +// assertEquals(newValue1, bidi.get(it.getKey())); +// assertFalse(bidi.containsKey(key1)); +// assertFalse(bidi.containsValue(newValue2)); +// verify(); +// +// // check for ConcurrentModification +// it.next(); // if you fail here, maybe you should be throwing an IAE, see above +// if (isRemoveSupported()) { +// it.remove(); +// } } } diff --git a/src/test/java/org/apache/commons/collections4/bidimap/DualHashBidiMapTest.java b/src/test/java/org/apache/commons/collections4/bidimap/DualHashBidiMapTest.java index ed13fe91bc..3a5c16d496 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/DualHashBidiMapTest.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/DualHashBidiMapTest.java @@ -22,7 +22,6 @@ /** * JUnit tests. - * */ public class DualHashBidiMapTest extends AbstractBidiMapTest { @@ -56,4 +55,5 @@ public String[] ignoredTests() { // resetFull(); // writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/DualHashBidiMap.fullCollection.version4.obj"); // } + } diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java index 2a1faa18ea..8ba620c8c2 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java @@ -16,10 +16,11 @@ */ package org.apache.commons.collections4.bloomfilter; +import static org.junit.jupiter.api.Assertions.assertThrows; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.util.List; import java.util.PrimitiveIterator.OfInt; @@ -261,12 +262,8 @@ public final void constructorTest_WrongShape() { final List lst = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); final Hasher hasher = new StaticHasher(lst.iterator(), anotherShape); - try { - createFilter(hasher, shape); - fail("Should throw IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // do nothing. - } + assertThrows(IllegalArgumentException.class, () -> createFilter(hasher, shape), + "Should throw IllegalArgumentException"); } /** @@ -297,12 +294,8 @@ public final void containsTest_BloomFilter_WrongShape() { final Shape anotherShape = new Shape(testFunctionX, 3, 72, 17); final Hasher hasher2 = new StaticHasher(lst.iterator(), anotherShape); final BloomFilter bf2 = createFilter(hasher2, anotherShape); - try { - bf.contains(bf2); - fail("Should throw IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // do nothing. - } + assertThrows(IllegalArgumentException.class, () -> bf.contains(bf2), + "Should throw IllegalArgumentException"); } /** @@ -340,12 +333,8 @@ public final void containsTest_Hasher_WrongShape() { final List lst2 = Arrays.asList(4, 5, 6, 7, 8, 9, 10); final Hasher hasher2 = new StaticHasher(lst2.iterator(), anotherShape); - try { - bf.contains(hasher2); - fail("Should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // do nothing - } + assertThrows(IllegalArgumentException.class, () -> bf.contains(hasher2), + "Should have thrown IllegalArgumentException"); } /** @@ -485,12 +474,8 @@ public final void mergeTest_BloomFilter_WrongShape() { final Hasher hasher2 = new StaticHasher(lst2.iterator(), anotherShape); final BloomFilter bf2 = createFilter(hasher2, anotherShape); - try { - bf.merge(bf2); - fail("Should throw IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // do nothing. - } + assertThrows(IllegalArgumentException.class, () -> bf.merge(bf2), + "Should throw IllegalArgumentException"); } /** @@ -524,12 +509,8 @@ public final void mergeTest_Hasher_WrongShape() { final List lst2 = Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27); final Hasher hasher2 = new StaticHasher(lst2.iterator(), anotherShape); - try { - bf.merge(hasher2); - fail("Should throw IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // do nothing. - } + assertThrows(IllegalArgumentException.class, () -> bf.merge(hasher2), + "Should throw IllegalArgumentException"); } /** @@ -638,4 +619,5 @@ public final void xorCardinalityTest_ExtraLongs() { assertEquals(20, bf.xorCardinality(bf2)); assertEquals(20, bf2.xorCardinality(bf)); } + } diff --git a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java index 62c036e0f6..fa969bb992 100644 --- a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java +++ b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.collection; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; @@ -118,7 +120,6 @@ * If your {@link Collection} fails one of these tests by design, * you may still use this base set of cases. Simply override the * test case (method) your {@link Collection} fails. - * */ public abstract class AbstractCollectionTest extends AbstractObjectTest { @@ -571,43 +572,27 @@ public void testUnsupportedAdd() { } resetEmpty(); - try { - getCollection().add(getFullNonNullElements()[0]); - fail("Empty collection should not support add."); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().add(getFullNonNullElements()[0]), + "Empty collection should not support add."); // make sure things didn't change even if the expected exception was // thrown. verify(); - try { - getCollection().addAll(Arrays.asList(getFullElements())); - fail("Empty collection should not support addAll."); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().addAll(Arrays.asList(getFullElements())), + "Empty collection should not support addAll."); // make sure things didn't change even if the expected exception was // thrown. verify(); resetFull(); - try { - getCollection().add(getFullNonNullElements()[0]); - fail("Full collection should not support add."); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().add(getFullNonNullElements()[0]), + "Full collection should not support add."); // make sure things didn't change even if the expected exception was // thrown. verify(); - try { - getCollection().addAll(Arrays.asList(getOtherElements())); - fail("Full collection should not support addAll."); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().addAll(Arrays.asList(getOtherElements())), + "Full collection should not support addAll."); // make sure things didn't change even if the expected exception was // thrown. verify(); @@ -733,13 +718,9 @@ public void testCollectionIterator() { resetEmpty(); Iterator it1 = getCollection().iterator(); assertFalse("Iterator for empty Collection shouldn't have next.", it1.hasNext()); - try { - it1.next(); - fail("Iterator at end of Collection should throw " - + "NoSuchElementException when next is called."); - } catch (final NoSuchElementException e) { - // expected - } + Iterator finalIt1 = it1; + assertThrows(NoSuchElementException.class, () -> finalIt1.next(), + "Iterator at end of Collection should throw NoSuchElementException when next is called."); // make sure nothing has changed after non-modification verify(); @@ -759,12 +740,9 @@ public void testCollectionIterator() { getCollection().contains(next)); list.add(next); } - try { - it1.next(); - fail("iterator.next() should raise NoSuchElementException after it finishes"); - } catch (final NoSuchElementException e) { - // expected - } + Iterator finalIt2 = it1; + assertThrows(NoSuchElementException.class, () -> finalIt2.next(), + "iterator.next() should raise NoSuchElementException after it finishes"); // make sure nothing has changed after non-modification verify(); } @@ -779,22 +757,14 @@ public void testCollectionIteratorRemove() { } resetEmpty(); - try { - getCollection().iterator().remove(); - fail("New iterator.remove should raise IllegalState"); - } catch (final IllegalStateException e) { - // expected - } + assertThrows(IllegalStateException.class, () -> getCollection().iterator().remove(), + "New iterator.remove should raise IllegalState"); verify(); - try { - final Iterator iter = getCollection().iterator(); - iter.hasNext(); - iter.remove(); - fail("New iterator.remove should raise IllegalState even after hasNext"); - } catch (final IllegalStateException e) { - // expected - } + final Iterator iter0 = getCollection().iterator(); + iter0.hasNext(); + assertThrows(IllegalStateException.class, () -> iter0.remove(), + "New iterator.remove should raise IllegalState even after hasNext"); verify(); resetFull(); @@ -832,12 +802,9 @@ public void testCollectionIteratorRemove() { iter = getCollection().iterator(); iter.next(); iter.remove(); - try { - iter.remove(); - fail("Second iter.remove should raise IllegalState"); - } catch (final IllegalStateException e) { - // expected - } + Iterator finalIter = iter; + assertThrows(IllegalStateException.class, () -> finalIter.remove(), + "Second iter.remove should raise IllegalState"); } /** @@ -1100,21 +1067,13 @@ public void testCollectionToArray2() { verify(); resetFull(); - try { - array = getCollection().toArray(new Void[0]); - fail("toArray(new Void[0]) should raise ArrayStore"); - } catch (final ArrayStoreException e) { - // expected - } + assertThrows(ArrayStoreException.class, () -> getCollection().toArray(new Void[0]), + "toArray(new Void[0]) should raise ArrayStore"); verify(); - try { - // Casting to Object[] allows compilation on Java 11. - array = getCollection().toArray((Object[]) null); - fail("toArray(null) should raise NPE"); - } catch (final NullPointerException e) { - // expected - } + // Casting to Object[] allows compilation on Java 11. + assertThrows(NullPointerException.class, () -> getCollection().toArray((Object[]) null), + "toArray(null) should raise NPE"); verify(); array = getCollection().toArray(new Object[0]); @@ -1167,55 +1126,31 @@ public void testUnsupportedRemove() { } resetEmpty(); - try { - getCollection().clear(); - fail("clear should raise UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().clear(), + "clear should raise UnsupportedOperationException"); verify(); - try { - getCollection().remove(null); - fail("remove should raise UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().remove(null), + "remove should raise UnsupportedOperationException"); verify(); - try { - getCollection().removeIf(e -> true); - fail("removeIf should raise UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().removeIf(e -> true), + "removeIf should raise UnsupportedOperationException"); verify(); - try { - getCollection().removeAll(null); - fail("removeAll should raise UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().removeAll(null), + "removeAll should raise UnsupportedOperationException"); verify(); - try { - getCollection().retainAll(null); - fail("retainAll should raise UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().retainAll(null), + "retainAll should raise UnsupportedOperationException"); verify(); resetFull(); - try { - final Iterator iterator = getCollection().iterator(); - iterator.next(); - iterator.remove(); - fail("iterator.remove should raise UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // expected - } + final Iterator iterator = getCollection().iterator(); + iterator.next(); + assertThrows(UnsupportedOperationException.class, () -> iterator.remove(), + "iterator.remove should raise UnsupportedOperationException"); verify(); } @@ -1230,28 +1165,20 @@ public void testCollectionIteratorFailFast() { if (isAddSupported()) { resetFull(); - try { - final Iterator iter = getCollection().iterator(); - final E o = getOtherElements()[0]; - getCollection().add(o); - getConfirmed().add(o); - iter.next(); - fail("next after add should raise ConcurrentModification"); - } catch (final ConcurrentModificationException e) { - // expected - } + final Iterator iter0 = getCollection().iterator(); + final E o = getOtherElements()[0]; + getCollection().add(o); + getConfirmed().add(o); + assertThrows(ConcurrentModificationException.class, () -> iter0.next(), + "next after add should raise ConcurrentModification"); verify(); resetFull(); - try { - final Iterator iter = getCollection().iterator(); - getCollection().addAll(Arrays.asList(getOtherElements())); - getConfirmed().addAll(Arrays.asList(getOtherElements())); - iter.next(); - fail("next after addAll should raise ConcurrentModification"); - } catch (final ConcurrentModificationException e) { - // expected - } + final Iterator iter = getCollection().iterator(); + getCollection().addAll(Arrays.asList(getOtherElements())); + getConfirmed().addAll(Arrays.asList(getOtherElements())); + assertThrows(ConcurrentModificationException.class, () -> iter.next(), + "next after addAll should raise ConcurrentModification"); verify(); } @@ -1271,46 +1198,30 @@ public void testCollectionIteratorFailFast() { } resetFull(); - try { - final Iterator iter = getCollection().iterator(); - getCollection().remove(getFullElements()[0]); - iter.next(); - fail("next after remove should raise ConcurrentModification"); - } catch (final ConcurrentModificationException e) { - // expected - } + final Iterator iter0 = getCollection().iterator(); + getCollection().remove(getFullElements()[0]); + assertThrows(ConcurrentModificationException.class, () -> iter0.next(), + "next after remove should raise ConcurrentModification"); resetFull(); - try { - final Iterator iter = getCollection().iterator(); - getCollection().removeIf(e -> false); - iter.next(); - fail("next after removeIf should raise ConcurrentModification"); - } catch (final ConcurrentModificationException e) { - // expected - } + final Iterator iter1 = getCollection().iterator(); + getCollection().removeIf(e -> false); + assertThrows(ConcurrentModificationException.class, () -> iter1.next(), + "next after removeIf should raise ConcurrentModification"); resetFull(); - try { - final Iterator iter = getCollection().iterator(); - final List sublist = Arrays.asList(getFullElements()).subList(2, 5); - getCollection().removeAll(sublist); - iter.next(); - fail("next after removeAll should raise ConcurrentModification"); - } catch (final ConcurrentModificationException e) { - // expected - } + final Iterator iter2 = getCollection().iterator(); + final List sublist = Arrays.asList(getFullElements()).subList(2, 5); + getCollection().removeAll(sublist); + assertThrows(ConcurrentModificationException.class, () -> iter2.next(), + "next after removeAll should raise ConcurrentModification"); resetFull(); - try { - final Iterator iter = getCollection().iterator(); - final List sublist = Arrays.asList(getFullElements()).subList(2, 5); - getCollection().retainAll(sublist); - iter.next(); - fail("next after retainAll should raise ConcurrentModification"); - } catch (final ConcurrentModificationException e) { - // expected - } + final Iterator iter3 = getCollection().iterator(); + final List sublist3 = Arrays.asList(getFullElements()).subList(2, 5); + getCollection().retainAll(sublist3); + assertThrows(ConcurrentModificationException.class, () -> iter3.next(), + "next after retainAll should raise ConcurrentModification"); } @Override @@ -1420,4 +1331,5 @@ protected static void assertNotRemoveAllFromCollection(final Collection coll, //apparently not } } + } 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 94963de1c2..247ac6bf82 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractIteratorTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Iterator; import java.util.NoSuchElementException; @@ -108,11 +110,8 @@ public void testEmptyIterator() { assertFalse("hasNext() should return false for empty iterators", it.hasNext()); // next() should throw a NoSuchElementException - try { - it.next(); - fail("NoSuchElementException must be thrown when Iterator is exhausted"); - } catch (final NoSuchElementException e) { - } + assertThrows(NoSuchElementException.class, () -> it.next(), + "NoSuchElementException must be thrown when Iterator is exhausted"); verify(); assertNotNull(it.toString()); @@ -145,11 +144,8 @@ public void testFullIterator() { } // next() must throw NoSuchElementException now - try { - it.next(); - fail("NoSuchElementException must be thrown when Iterator is exhausted"); - } catch (final NoSuchElementException e) { - } + assertThrows(NoSuchElementException.class, () -> it.next(), + "NoSuchElementException must be thrown when Iterator is exhausted"); assertNotNull(it.toString()); } @@ -169,10 +165,7 @@ public void testRemove() { } // should throw IllegalStateException before next() called - try { - it.remove(); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.remove()); verify(); // remove after next should be fine @@ -180,10 +173,7 @@ public void testRemove() { it.remove(); // should throw IllegalStateException for second remove() - try { - it.remove(); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.remove()); } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java index 5c1608b93f..2a1a53b565 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractListIteratorTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ArrayList; import java.util.ListIterator; import java.util.NoSuchElementException; @@ -101,18 +103,12 @@ public void testEmptyListIteratorIsIndeedEmpty() { assertEquals(-1, it.previousIndex()); // next() should throw a NoSuchElementException - try { - it.next(); - fail("NoSuchElementException must be thrown from empty ListIterator"); - } catch (final NoSuchElementException e) { - } + assertThrows(NoSuchElementException.class, () -> it.next(), + "NoSuchElementException must be thrown from empty ListIterator"); // previous() should throw a NoSuchElementException - try { - it.previous(); - fail("NoSuchElementException must be thrown from empty ListIterator"); - } catch (final NoSuchElementException e) { - } + assertThrows(NoSuchElementException.class, () -> it.previous(), + "NoSuchElementException must be thrown from empty ListIterator"); } /** @@ -128,11 +124,8 @@ public void testWalkForwardAndBack() { // check state at end assertFalse(it.hasNext()); assertTrue(it.hasPrevious()); - try { - it.next(); - fail("NoSuchElementException must be thrown from next at end of ListIterator"); - } catch (final NoSuchElementException e) { - } + assertThrows(NoSuchElementException.class, () -> it.next(), + "NoSuchElementException must be thrown from next at end of ListIterator"); // loop back through comparing for (int i = list.size() - 1; i >= 0; i--) { @@ -146,11 +139,8 @@ public void testWalkForwardAndBack() { // check state at start assertTrue(it.hasNext()); assertFalse(it.hasPrevious()); - try { - it.previous(); - fail("NoSuchElementException must be thrown from previous at start of ListIterator"); - } catch (final NoSuchElementException e) { - } + assertThrows(NoSuchElementException.class, () -> it.previous(), + "NoSuchElementException must be thrown from previous at start of ListIterator"); } /** @@ -162,10 +152,9 @@ public void testAdd() { final E addValue = addSetValue(); if (!supportsAdd()) { // check for UnsupportedOperationException if not supported - try { - it.add(addValue); - fail("UnsupportedOperationException must be thrown from add of " + it.getClass().getSimpleName()); - } catch (final UnsupportedOperationException ex) {} + ListIterator finalIt0 = it; + assertThrows(UnsupportedOperationException.class, () -> finalIt0.add(addValue), + "UnsupportedOperationException must be thrown from add of " + it.getClass().getSimpleName()); return; } @@ -198,18 +187,13 @@ public void testSet() { if (!supportsSet()) { // check for UnsupportedOperationException if not supported - try { - it.set(addSetValue()); - fail("UnsupportedOperationException must be thrown from set in " + it.getClass().getSimpleName()); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> it.set(addSetValue()), + "UnsupportedOperationException must be thrown from set in " + it.getClass().getSimpleName()); return; } // should throw IllegalStateException before next() called - try { - it.set(addSetValue()); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.set(addSetValue())); // set after next should be fine it.next(); @@ -225,11 +209,8 @@ public void testRemoveThenSet() { if (supportsRemove() && supportsSet()) { it.next(); it.remove(); - try { - it.set(addSetValue()); - fail("IllegalStateException must be thrown from set after remove"); - } catch (final IllegalStateException e) { - } + assertThrows(IllegalStateException.class, () -> it.set(addSetValue()), + "IllegalStateException must be thrown from set after remove"); } } @@ -239,11 +220,8 @@ public void testAddThenSet() { if (supportsAdd() && supportsSet()) { it.next(); it.add(addSetValue()); - try { - it.set(addSetValue()); - fail("IllegalStateException must be thrown from set after add"); - } catch (final IllegalStateException e) { - } + assertThrows(IllegalStateException.class, () -> it.set(addSetValue()), + "IllegalStateException must be thrown from set after add"); } } @@ -257,11 +235,8 @@ public void testAddThenRemove() { if (supportsAdd() && supportsRemove()) { it.next(); it.add(addSetValue()); - try { - it.remove(); - fail("IllegalStateException must be thrown from remove after add"); - } catch (final IllegalStateException e) { - } + assertThrows(IllegalStateException.class, () -> it.remove(), + "IllegalStateException must be thrown from remove after add"); } } 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 7e5b3ef0e7..320c704a8e 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/AbstractMapIteratorTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.iterators; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.HashSet; import java.util.Map; import java.util.NoSuchElementException; @@ -117,22 +119,13 @@ public void testEmptyMapIterator() { assertFalse(it.hasNext()); // next() should throw a NoSuchElementException - try { - it.next(); - fail(); - } catch (final NoSuchElementException ex) {} + assertThrows(NoSuchElementException.class, () -> it.next()); // getKey() should throw an IllegalStateException - try { - it.getKey(); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.getKey()); // getValue() should throw an IllegalStateException - try { - it.getValue(); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.getValue()); if (!supportsSetValue()) { // setValue() should throw an UnsupportedOperationException/IllegalStateException @@ -144,10 +137,7 @@ public void testEmptyMapIterator() { } } else { // setValue() should throw an IllegalStateException - try { - it.setValue(addSetValues()[0]); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.setValue(addSetValues()[0])); } } @@ -198,10 +188,7 @@ public void testMapIteratorSet() { final V value = it.getValue(); if (!supportsSetValue()) { - try { - it.setValue(newValue); - fail(); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> it.setValue(newValue)); return; } final V old = it.setValue(newValue); @@ -238,11 +225,7 @@ public void testRemove() { // override final K key = it.next(); if (!supportsRemove()) { - try { - it.remove(); - fail(); - } catch (final UnsupportedOperationException ex) { - } + assertThrows(UnsupportedOperationException.class, () -> it.remove()); return; } @@ -274,10 +257,7 @@ public void testMapIteratorSetRemoveSet() { confirmed.remove(key); verify(); - try { - it.setValue(newValue); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.setValue(newValue)); verify(); } @@ -295,10 +275,7 @@ public void testMapIteratorRemoveGetKey() { confirmed.remove(key); verify(); - try { - it.getKey(); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.getKey()); verify(); } @@ -316,10 +293,7 @@ public void testMapIteratorRemoveGetValue() { confirmed.remove(key); verify(); - try { - it.getValue(); - fail(); - } catch (final IllegalStateException ex) {} + assertThrows(IllegalStateException.class, () -> it.getValue()); verify(); } 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 f3eeede242..6f5b57ec9d 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/BoundedIteratorTest.java @@ -118,7 +118,7 @@ public void testSameAsDecorated() { assertFalse(iter.hasNext()); - assertThrows(NoSuchElementException.class, ()->iter.next()); + assertThrows(NoSuchElementException.class, () -> iter.next()); } /** diff --git a/src/test/java/org/apache/commons/collections4/list/AbstractLinkedListTest.java b/src/test/java/org/apache/commons/collections4/list/AbstractLinkedListTest.java index 825f1f5e63..5853867156 100644 --- a/src/test/java/org/apache/commons/collections4/list/AbstractLinkedListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/AbstractLinkedListTest.java @@ -16,11 +16,12 @@ */ package org.apache.commons.collections4.list; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; /** * Test case for {@link AbstractLinkedList}. - * */ public abstract class AbstractLinkedListTest extends AbstractListTest { @@ -138,36 +139,20 @@ public void testGetNode() { final AbstractLinkedList list = getCollection(); // get marker assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next); - try { - list.getNode(0, false); - fail("Expecting IndexOutOfBoundsException."); - } catch (final IndexOutOfBoundsException ex) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(0, false), + "Expecting IndexOutOfBoundsException."); list.addAll( Arrays.asList((E[]) new String[]{"value1", "value2"})); checkNodes(); list.addFirst((E) "value0"); checkNodes(); list.removeNode(list.getNode(1, false)); checkNodes(); - try { - list.getNode(2, false); - fail("Expecting IndexOutOfBoundsException."); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - list.getNode(-1, false); - fail("Expecting IndexOutOfBoundsException."); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - list.getNode(3, true); - fail("Expecting IndexOutOfBoundsException."); - } catch (final IndexOutOfBoundsException ex) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(2, false), + "Expecting IndexOutOfBoundsException."); + assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(-1, false), + "Expecting IndexOutOfBoundsException."); + assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(3, true), + "Expecting IndexOutOfBoundsException."); } protected void checkNodes() { @@ -188,4 +173,5 @@ protected void checkNodes() { public AbstractLinkedList getCollection() { return (AbstractLinkedList) super.getCollection(); } + } diff --git a/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java b/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java index 1fad15fd7a..ce464cdf03 100644 --- a/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.list; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.IOException; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; @@ -34,6 +36,7 @@ import org.apache.commons.collections4.BulkTest; import org.apache.commons.collections4.collection.AbstractCollectionTest; import org.apache.commons.collections4.iterators.AbstractListIteratorTest; +import org.junit.jupiter.api.Assertions; /** * Abstract test class for {@link java.util.List} methods and contracts. @@ -45,7 +48,6 @@ * you may still use this base set of cases. Simply override the * test case (method) your {@link List} fails or override one of the * protected methods from AbstractCollectionTest. - * */ public abstract class AbstractListTest extends AbstractCollectionTest { @@ -176,40 +178,23 @@ public void testListAddByIndexBoundsChecking() { return; } - List list; final E element = getOtherElements()[0]; - try { - list = makeObject(); - list.add(Integer.MIN_VALUE, element); - fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList0 = makeObject(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList0.add(Integer.MIN_VALUE, element), + "List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list = makeObject(); - list.add(-1, element); - fail("List.add should throw IndexOutOfBoundsException [-1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList1 = makeObject(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList1.add(-1, element), + "List.add should throw IndexOutOfBoundsException [-1]"); - try { - list = makeObject(); - list.add(1, element); - fail("List.add should throw IndexOutOfBoundsException [1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList2 = makeObject(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList2.add(1, element), + "List.add should throw IndexOutOfBoundsException [1]"); - try { - list = makeObject(); - list.add(Integer.MAX_VALUE, element); - fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList3 = makeObject(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList3.add(Integer.MAX_VALUE, element), + "List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } /** @@ -221,40 +206,23 @@ public void testListAddByIndexBoundsChecking2() { return; } - List list; final E element = getOtherElements()[0]; - try { - list = makeFullCollection(); - list.add(Integer.MIN_VALUE, element); - fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList0 = makeFullCollection(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList0.add(Integer.MIN_VALUE, element), + "List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list = makeFullCollection(); - list.add(-1, element); - fail("List.add should throw IndexOutOfBoundsException [-1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList1 = makeFullCollection(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList1.add(-1, element), + "List.add should throw IndexOutOfBoundsException [-1]"); - try { - list = makeFullCollection(); - list.add(list.size() + 1, element); - fail("List.add should throw IndexOutOfBoundsException [size + 1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList2 = makeFullCollection(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList2.add(finalList2.size() + 1, element), + "List.add should throw IndexOutOfBoundsException [size + 1]"); - try { - list = makeFullCollection(); - list.add(Integer.MAX_VALUE, element); - fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + List finalList3 = makeFullCollection(); + assertThrows(IndexOutOfBoundsException.class, () -> finalList3.add(Integer.MAX_VALUE, element), + "List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } /** @@ -383,40 +351,20 @@ public void testListGetByIndex() { public void testListGetByIndexBoundsChecking() { final List list = makeObject(); - try { - list.get(Integer.MIN_VALUE); - fail("List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MIN_VALUE), + "List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list.get(-1); - fail("List.get should throw IndexOutOfBoundsException [-1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1), + "List.get should throw IndexOutOfBoundsException [-1]"); - try { - list.get(0); - fail("List.get should throw IndexOutOfBoundsException [0]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(0), + "List.get should throw IndexOutOfBoundsException [0]"); - try { - list.get(1); - fail("List.get should throw IndexOutOfBoundsException [1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(1), + "List.get should throw IndexOutOfBoundsException [1]"); - try { - list.get(Integer.MAX_VALUE); - fail("List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MAX_VALUE), + "List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } /** @@ -426,33 +374,17 @@ public void testListGetByIndexBoundsChecking() { public void testListGetByIndexBoundsChecking2() { final List list = makeFullCollection(); - try { - list.get(Integer.MIN_VALUE); - fail("List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MIN_VALUE), + "List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list.get(-1); - fail("List.get should throw IndexOutOfBoundsException [-1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1), + "List.get should throw IndexOutOfBoundsException [-1]"); - try { - list.get(getFullElements().length); - fail("List.get should throw IndexOutOfBoundsException [size]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(getFullElements().length), + "List.get should throw IndexOutOfBoundsException [size]"); - try { - list.get(Integer.MAX_VALUE); - fail("List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MAX_VALUE), + "List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } /** @@ -513,43 +445,22 @@ public void testListSetByIndexBoundsChecking() { final List list = makeObject(); final E element = getOtherElements()[0]; - try { - list.set(Integer.MIN_VALUE, element); - fail("List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MIN_VALUE, element), + "List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list.set(-1, element); - fail("List.set should throw IndexOutOfBoundsException [-1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element), + "List.set should throw IndexOutOfBoundsException [-1]"); - try { - list.set(0, element); - fail("List.set should throw IndexOutOfBoundsException [0]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(0, element), + "List.set should throw IndexOutOfBoundsException [0]"); - try { - list.set(1, element); - fail("List.set should throw IndexOutOfBoundsException [1]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(1, element), + "List.set should throw IndexOutOfBoundsException [1]"); - try { - list.set(Integer.MAX_VALUE, element); - fail("List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MAX_VALUE, element), + "List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } - /** * Tests bounds checking for {@link List#set(int,Object)} on a * full list. @@ -562,38 +473,19 @@ public void testListSetByIndexBoundsChecking2() { final List list = makeFullCollection(); final E element = getOtherElements()[0]; - try { - list.set(Integer.MIN_VALUE, element); - fail("List.set should throw IndexOutOfBoundsException " + - "[Integer.MIN_VALUE]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MIN_VALUE, element), + "List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list.set(-1, element); - fail("List.set should throw IndexOutOfBoundsException [-1]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element), + "List.set should throw IndexOutOfBoundsException [-1]"); - try { - list.set(getFullElements().length, element); - fail("List.set should throw IndexOutOfBoundsException [size]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(getFullElements().length, element), + "List.set should throw IndexOutOfBoundsException [size]"); - try { - list.set(Integer.MAX_VALUE, element); - fail("List.set should throw IndexOutOfBoundsException " + - "[Integer.MAX_VALUE]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MAX_VALUE, element), + "List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } - /** * Test {@link List#set(int,Object)}. */ @@ -625,12 +517,8 @@ public void testUnsupportedSet() { } resetFull(); - try { - getCollection().set(0, getFullElements()[0]); - fail("Empty collection should not support set."); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> getCollection().set(0, getFullElements()[0]), + "Empty collection should not support set."); // make sure things didn't change even if the expected exception was // thrown. verify(); @@ -647,40 +535,20 @@ public void testListRemoveByIndexBoundsChecking() { final List list = makeObject(); - try { - list.remove(Integer.MIN_VALUE); - fail("List.remove should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MIN_VALUE), + "List.remove should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list.remove(-1); - fail("List.remove should throw IndexOutOfBoundsException [-1]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1), + "List.remove should throw IndexOutOfBoundsException [-1]"); - try { - list.remove(0); - fail("List.remove should throw IndexOutOfBoundsException [0]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0), + "List.remove should throw IndexOutOfBoundsException [0]"); - try { - list.remove(1); - fail("List.remove should throw IndexOutOfBoundsException [1]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(1), + "List.remove should throw IndexOutOfBoundsException [1]"); - try { - list.remove(Integer.MAX_VALUE); - fail("List.remove should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MAX_VALUE), + "List.remove should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } /** @@ -694,38 +562,19 @@ public void testListRemoveByIndexBoundsChecking2() { final List list = makeFullCollection(); - try { - list.remove(Integer.MIN_VALUE); - fail("List.remove should throw IndexOutOfBoundsException " + - "[Integer.MIN_VALUE]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MIN_VALUE), + "List.remove should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); - try { - list.remove(-1); - fail("List.remove should throw IndexOutOfBoundsException [-1]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1), + "List.remove should throw IndexOutOfBoundsException [-1]"); - try { - list.remove(getFullElements().length); - fail("List.remove should throw IndexOutOfBoundsException [size]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(getFullElements().length), + "List.remove should throw IndexOutOfBoundsException [size]"); - try { - list.remove(Integer.MAX_VALUE); - fail("List.remove should throw IndexOutOfBoundsException " + - "[Integer.MAX_VALUE]"); - } catch(final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MAX_VALUE), + "List.remove should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); } - /** * Tests {@link List#remove(int)}. */ @@ -922,12 +771,8 @@ private void forwardTest(final ListIterator iter, int i) { assertEquals("nextIndex should be size", max, iter.nextIndex()); assertEquals("previousIndex should be size - 1", max - 1, iter.previousIndex()); - try { - iter.next(); - fail("Exhausted iterator should raise NoSuchElement"); - } catch (final NoSuchElementException e) { - // expected - } + assertThrows(NoSuchElementException.class, () -> iter.next(), + "Exhausted iterator should raise NoSuchElement"); } /** @@ -958,13 +803,8 @@ private void backwardTest(final ListIterator iter, int i) { final int prevIndex = iter.previousIndex(); assertEquals("previousIndex should be -1", -1, prevIndex); - try { - iter.previous(); - fail("Exhausted iterator should raise NoSuchElement"); - } catch (final NoSuchElementException e) { - // expected - } - + assertThrows(NoSuchElementException.class, () -> iter.previous(), + "Exhausted iterator should raise NoSuchElement"); } @@ -1315,19 +1155,10 @@ protected void failFastMethod(final List list, final Method m) { } } - try { - m.invoke(list, params); - fail(m.getName() + " should raise ConcurrentModification"); - } catch (final IllegalAccessException e) { - // impossible - } catch (final InvocationTargetException e) { - final Throwable t = e.getTargetException(); - if (t instanceof ConcurrentModificationException) { - // expected - return; - } - fail(m.getName() + " raised unexpected " + e); - } + final InvocationTargetException thrown = assertThrows(InvocationTargetException.class, () -> m.invoke(list, params), + m.getName() + " should raise ConcurrentModification"); + Assertions.assertTrue(thrown.getTargetException() instanceof ConcurrentModificationException, + m.getName() + " raised unexpected " + thrown.getTargetException()); } // ----------------------------------------------------------------------- @@ -1372,4 +1203,5 @@ public ListIterator makeObject() { return AbstractListTest.this.getCollection().listIterator(); } } + } diff --git a/src/test/java/org/apache/commons/collections4/map/AbstractIterableMapTest.java b/src/test/java/org/apache/commons/collections4/map/AbstractIterableMapTest.java index 2670e8dc99..f11d681c8d 100644 --- a/src/test/java/org/apache/commons/collections4/map/AbstractIterableMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/AbstractIterableMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.Map; @@ -27,7 +29,6 @@ /** * Abstract test class for {@link IterableMap} methods and contracts. - * */ public abstract class AbstractIterableMapTest extends AbstractMapTest { @@ -65,19 +66,15 @@ public void testFailFastEntrySet() { Iterator> it = getMap().entrySet().iterator(); final Map.Entry val = it.next(); getMap().remove(val.getKey()); - try { - it.next(); - fail(); - } catch (final ConcurrentModificationException ex) {} + Iterator> finalIt0 = it; + assertThrows(ConcurrentModificationException.class, () -> finalIt0.next()); resetFull(); it = getMap().entrySet().iterator(); it.next(); getMap().clear(); - try { - it.next(); - fail(); - } catch (final ConcurrentModificationException ex) {} + Iterator> finalIt1 = it; + assertThrows(ConcurrentModificationException.class, () -> finalIt1.next()); } public void testFailFastKeySet() { @@ -91,19 +88,15 @@ public void testFailFastKeySet() { Iterator it = getMap().keySet().iterator(); final K val = it.next(); getMap().remove(val); - try { - it.next(); - fail(); - } catch (final ConcurrentModificationException ex) {} + Iterator finalIt0 = it; + assertThrows(ConcurrentModificationException.class, () -> finalIt0.next()); resetFull(); it = getMap().keySet().iterator(); it.next(); getMap().clear(); - try { - it.next(); - fail(); - } catch (final ConcurrentModificationException ex) {} + Iterator finalIt1 = it; + assertThrows(ConcurrentModificationException.class, () -> finalIt1.next()); } public void testFailFastValues() { @@ -117,19 +110,15 @@ public void testFailFastValues() { Iterator it = getMap().values().iterator(); it.next(); getMap().remove(getMap().keySet().iterator().next()); - try { - it.next(); - fail(); - } catch (final ConcurrentModificationException ex) {} + Iterator finalIt0 = it; + assertThrows(ConcurrentModificationException.class, () -> finalIt0.next()); resetFull(); it = getMap().values().iterator(); it.next(); getMap().clear(); - try { - it.next(); - fail(); - } catch (final ConcurrentModificationException ex) {} + Iterator finalIt1 = it; + assertThrows(ConcurrentModificationException.class, () -> finalIt1.next()); } public BulkTest bulkTestMapIterator() { @@ -206,4 +195,5 @@ public void verify() { public IterableMap getMap() { return (IterableMap) super.getMap(); } + } diff --git a/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java b/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java index e30fef4397..69be394f67 100644 --- a/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; @@ -118,7 +120,6 @@ * fails and/or the methods that define the assumptions used by the test * cases. For example, if your map does not allow duplicate values, override * {@link #isAllowDuplicateValues()} and have it return {@code false} - * */ public abstract class AbstractMapTest extends AbstractObjectTest { @@ -588,11 +589,9 @@ public void testMapSize() { */ public void testMapClear() { if (!isRemoveSupported()) { - try { - resetFull(); - getMap().clear(); - fail("Expected UnsupportedOperationException on clear"); - } catch (final UnsupportedOperationException ex) {} + resetFull(); + assertThrows(UnsupportedOperationException.class, () -> getMap().clear(), + "Expected UnsupportedOperationException on clear"); return; } @@ -849,10 +848,8 @@ public void testMapPut() { } } } else { - try { - getMap().put(keys[0], values[0]); - fail("Expected UnsupportedOperationException on put (add)"); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> getMap().put(keys[0], values[0]), + "Expected UnsupportedOperationException on put (add)"); } } @@ -902,10 +899,8 @@ public void testMapPutAll() { if (!isPutChangeSupported()) { final Map temp = makeFullMap(); resetEmpty(); - try { - getMap().putAll(temp); - fail("Expected UnsupportedOperationException on putAll"); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> getMap().putAll(temp), + "Expected UnsupportedOperationException on putAll"); } return; } @@ -960,11 +955,9 @@ public void testMapPutAll() { */ public void testMapRemove() { if (!isRemoveSupported()) { - try { - resetFull(); - getMap().remove(getMap().keySet().iterator().next()); - fail("Expected UnsupportedOperationException on remove"); - } catch (final UnsupportedOperationException ex) {} + resetFull(); + assertThrows(UnsupportedOperationException.class, () -> getMap().remove(getMap().keySet().iterator().next()), + "Expected UnsupportedOperationException on remove"); return; } @@ -2031,4 +2024,5 @@ public Map getMap() { public Map getConfirmed() { return confirmed; } + } diff --git a/src/test/java/org/apache/commons/collections4/map/AbstractOrderedMapTest.java b/src/test/java/org/apache/commons/collections4/map/AbstractOrderedMapTest.java index aa7eee1f91..ece717f9e8 100644 --- a/src/test/java/org/apache/commons/collections4/map/AbstractOrderedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/AbstractOrderedMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -33,7 +35,6 @@ /** * Abstract test class for {@link OrderedMap} methods and contracts. - * */ public abstract class AbstractOrderedMapTest extends AbstractIterableMapTest { @@ -85,10 +86,8 @@ public K[] getSampleKeys() { public void testFirstKey() { resetEmpty(); OrderedMap ordered = getMap(); - try { - ordered.firstKey(); - fail(); - } catch (final NoSuchElementException ex) {} + OrderedMap finalOrdered = ordered; + assertThrows(NoSuchElementException.class, () -> finalOrdered.firstKey()); resetFull(); ordered = getMap(); @@ -99,10 +98,8 @@ public void testFirstKey() { public void testLastKey() { resetEmpty(); OrderedMap ordered = getMap(); - try { - ordered.lastKey(); - fail(); - } catch (final NoSuchElementException ex) {} + OrderedMap finalOrdered = ordered; + assertThrows(NoSuchElementException.class, () -> finalOrdered.lastKey()); resetFull(); ordered = getMap(); @@ -137,10 +134,8 @@ public void testNextKey() { assertNull(ordered.nextKey(confirmedLast)); if (!isAllowNullKey()) { - try { - ordered.nextKey(null); - fail(); - } catch (final NullPointerException ex) {} + OrderedMap finalOrdered = ordered; + assertThrows(NullPointerException.class, () -> finalOrdered.nextKey(null)); } else { assertNull(ordered.nextKey(null)); } @@ -172,10 +167,8 @@ public void testPreviousKey() { assertNull(ordered.previousKey(confirmedLast)); if (!isAllowNullKey()) { - try { - ordered.previousKey(null); - fail(); - } catch (final NullPointerException ex) {} + OrderedMap finalOrdered = ordered; + assertThrows(NullPointerException.class, () -> finalOrdered.previousKey(null)); } else { if (!isAllowNullKey()) { assertNull(ordered.previousKey(null)); @@ -245,4 +238,5 @@ public void verify() { public OrderedMap getMap() { return (OrderedMap) super.getMap(); } + } diff --git a/src/test/java/org/apache/commons/collections4/map/AbstractSortedMapTest.java b/src/test/java/org/apache/commons/collections4/map/AbstractSortedMapTest.java index 2b6d96f495..051289d80c 100644 --- a/src/test/java/org/apache/commons/collections4/map/AbstractSortedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/AbstractSortedMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -28,7 +30,6 @@ /** * Abstract test class for {@link java.util.SortedMap} methods and contracts. - * */ public abstract class AbstractSortedMapTest extends AbstractMapTest { @@ -235,10 +236,7 @@ public void testHeadMapOutOfRange() { return; } resetEmpty(); - try { - getMap().put(toKey, subSortedValues.get(0)); - fail(); - } catch (final IllegalArgumentException ex) {} + assertThrows(IllegalArgumentException.class, () -> getMap().put(toKey, subSortedValues.get(0))); verify(); } @Override @@ -290,10 +288,7 @@ public void testTailMapOutOfRange() { return; } resetEmpty(); - try { - getMap().put(invalidKey, subSortedValues.get(0)); - fail(); - } catch (final IllegalArgumentException ex) {} + assertThrows(IllegalArgumentException.class, () -> getMap().put(invalidKey, subSortedValues.get(0))); verify(); } @Override @@ -352,10 +347,7 @@ public void testSubMapOutOfRange() { return; } resetEmpty(); - try { - getMap().put(toKey, subSortedValues.get(0)); - fail(); - } catch (final IllegalArgumentException ex) {} + assertThrows(IllegalArgumentException.class, () -> getMap().put(toKey, subSortedValues.get(0))); verify(); } @Override @@ -390,4 +382,5 @@ public SortedMap getMap() { public SortedMap getConfirmed() { return (SortedMap) super.getConfirmed(); } + } diff --git a/src/test/java/org/apache/commons/collections4/map/PredicatedMapTest.java b/src/test/java/org/apache/commons/collections4/map/PredicatedMapTest.java index 9a046800ce..0feb6399a2 100644 --- a/src/test/java/org/apache/commons/collections4/map/PredicatedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/PredicatedMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -67,19 +69,11 @@ public void testEntrySet() { @SuppressWarnings("unchecked") public void testPut() { final Map map = makeTestMap(); - try { - map.put((K) "Hi", (V) Integer.valueOf(3)); - fail("Illegal value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } - - try { - map.put((K) Integer.valueOf(3), (V) "Hi"); - fail("Illegal key should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)), + "Illegal value should raise IllegalArgument"); + + assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"), + "Illegal key should raise IllegalArgument"); assertFalse(map.containsKey(Integer.valueOf(3))); assertFalse(map.containsValue(Integer.valueOf(3))); @@ -90,28 +84,20 @@ public void testPut() { map2.put((K) "C", (V) "c"); map2.put((K) "c", (V) Integer.valueOf(3)); - try { - map.putAll(map2); - fail("Illegal value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.putAll(map2), + "Illegal value should raise IllegalArgument"); map.put((K) "E", (V) "e"); Iterator> iterator = map.entrySet().iterator(); - try { - final Map.Entry entry = iterator.next(); - entry.setValue((V) Integer.valueOf(3)); - fail("Illegal value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + Map.Entry entry = iterator.next(); + Map.Entry finalEntry = entry; + assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)), + "Illegal value should raise IllegalArgument"); map.put((K) "F", (V) "f"); iterator = map.entrySet().iterator(); - final Map.Entry entry = iterator.next(); + entry = iterator.next(); entry.setValue((V) "x"); - } @Override diff --git a/src/test/java/org/apache/commons/collections4/map/PredicatedSortedMapTest.java b/src/test/java/org/apache/commons/collections4/map/PredicatedSortedMapTest.java index dd2700083f..29a732d16e 100644 --- a/src/test/java/org/apache/commons/collections4/map/PredicatedSortedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/PredicatedSortedMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; @@ -95,19 +97,11 @@ public void testEntrySet() { @SuppressWarnings("unchecked") public void testPut() { final Map map = makeTestMap(); - try { - map.put((K) "Hi", (V) Integer.valueOf(3)); - fail("Illegal value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)), + "Illegal value should raise IllegalArgument"); - try { - map.put((K) Integer.valueOf(3), (V) "Hi"); - fail("Illegal key should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"), + "Illegal key should raise IllegalArgument"); assertFalse(map.containsKey(Integer.valueOf(3))); assertFalse(map.containsValue(Integer.valueOf(3))); @@ -118,26 +112,19 @@ public void testPut() { map2.put((K) "C", (V) "c"); map2.put((K) "c", (V) Integer.valueOf(3)); - try { - map.putAll(map2); - fail("Illegal value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.putAll(map2), + "Illegal value should raise IllegalArgument"); map.put((K) "E", (V) "e"); Iterator> iterator = map.entrySet().iterator(); - try { - final Map.Entry entry = iterator.next(); - entry.setValue((V) Integer.valueOf(3)); - fail("Illegal value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + Map.Entry entry = iterator.next(); + Map.Entry finalEntry = entry; + assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)), + "Illegal value should raise IllegalArgument"); map.put((K) "F", (V) "f"); iterator = map.entrySet().iterator(); - final Map.Entry entry = iterator.next(); + entry = iterator.next(); entry.setValue((V) "x"); } @@ -147,19 +134,11 @@ public void testSortOrder() { final SortedMap map = makeTestMap(); map.put((K) "A", (V) "a"); map.put((K) "B", (V) "b"); - try { - map.put(null, (V) "c"); - fail("Null key should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.put(null, (V) "c"), + "Null key should raise IllegalArgument"); map.put((K) "C", (V) "c"); - try { - map.put((K) "D", null); - fail("Null value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.put((K) "D", null), + "Null value should raise IllegalArgument"); assertEquals("First key should be A", "A", map.firstKey()); assertEquals("Last key should be C", "C", map.lastKey()); assertEquals("First key in tail map should be B", @@ -178,19 +157,11 @@ public void testReverseSortOrder() { final SortedMap map = makeTestMapWithComparator(); map.put((K) "A", (V) "a"); map.put((K) "B", (V) "b"); - try { - map.put(null, (V) "c"); - fail("Null key should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.put(null, (V) "c"), + "Null key should raise IllegalArgument"); map.put((K) "C", (V) "c"); - try { - map.put((K) "D", null); - fail("Null value should raise IllegalArgument"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> map.put((K) "D", null), + "Null value should raise IllegalArgument"); assertEquals("Last key should be A", "A", map.lastKey()); assertEquals("First key should be C", "C", map.firstKey()); assertEquals("First key in tail map should be B", diff --git a/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java b/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java index d392cb8782..a35e2b0fa7 100644 --- a/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/ReferenceIdentityMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.lang.ref.WeakReference; import java.util.Iterator; import java.util.Map; @@ -28,7 +30,6 @@ /** * Tests for ReferenceIdentityMap. - * */ public class ReferenceIdentityMapTest extends AbstractIterableMapTest { @@ -286,18 +287,9 @@ public void testNullHandling() { assertFalse(getMap().entrySet().contains(null)); assertFalse(getMap().keySet().contains(null)); assertFalse(getMap().values().contains(null)); - try { - getMap().put(null, null); - fail(); - } catch (final NullPointerException ex) {} - try { - getMap().put((K) new Object(), null); - fail(); - } catch (final NullPointerException ex) {} - try { - getMap().put(null, (V) new Object()); - fail(); - } catch (final NullPointerException ex) {} + assertThrows(NullPointerException.class, () -> getMap().put(null, null)); + assertThrows(NullPointerException.class, () -> getMap().put((K) new Object(), null)); + assertThrows(NullPointerException.class, () -> getMap().put(null, (V) new Object())); } /** Tests whether purge values setting works */ diff --git a/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java b/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java index 34121022df..a2ea3f0f5c 100644 --- a/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -37,7 +39,6 @@ /** * Tests for ReferenceMap. - * */ public class ReferenceMapTest extends AbstractIterableMapTest { @@ -90,18 +91,9 @@ public void testNullHandling() { assertFalse(map.entrySet().contains(null)); assertFalse(map.containsKey(null)); assertFalse(map.containsValue(null)); - try { - map.put(null, null); - fail(); - } catch (final NullPointerException ex) {} - try { - map.put((K) new Object(), null); - fail(); - } catch (final NullPointerException ex) {} - try { - map.put(null, (V) new Object()); - fail(); - } catch (final NullPointerException ex) {} + assertThrows(NullPointerException.class, () -> map.put(null, null)); + assertThrows(NullPointerException.class, () -> map.put((K) new Object(), null)); + assertThrows(NullPointerException.class, () -> map.put(null, (V) new Object())); } /* @@ -345,4 +337,5 @@ protected void onPurge() { } } } + } diff --git a/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java b/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java index e5979ac3bf..a3ba3342d2 100644 --- a/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/TransformedSortedMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.map; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Map; import java.util.Set; import java.util.SortedMap; @@ -77,18 +79,15 @@ public void testTransformedMap() { map.put((K) els[i], (V) els[i]); assertEquals(i + 1, map.size()); assertTrue(map.containsKey(Integer.valueOf((String) els[i]))); - try { - map.containsKey(els[i]); - fail(); - } catch (final ClassCastException ex) {} + SortedMap finalMap1 = map; + int finalI = i; + assertThrows(ClassCastException.class, () -> finalMap1.containsKey(els[finalI])); assertTrue(map.containsValue(els[i])); assertEquals(els[i], map.get(Integer.valueOf((String) els[i]))); } - try { - map.remove(els[0]); - fail(); - } catch (final ClassCastException ex) {} + SortedMap finalMap = map; + assertThrows(ClassCastException.class, () -> finalMap.remove(els[0])); assertEquals(els[0], map.remove(Integer.valueOf((String) els[0]))); map = TransformedSortedMap diff --git a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java index f40776b26a..45d426a37d 100644 --- a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.multimap; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -555,13 +557,9 @@ public void testPutAll_Map1() { test.put((K) "key", (V) "object0"); test.putAll(original); - try { - final MultiValuedMap originalNull = null; - test.putAll(originalNull); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } + final MultiValuedMap originalNull = null; + assertThrows(NullPointerException.class, () -> test.putAll(originalNull), + "expecting NullPointerException"); assertEquals(2, test.keySet().size()); assertEquals(4, test.size()); @@ -587,13 +585,9 @@ public void testPutAll_Map2() { test.put((K) "keyX", (V) "object0"); test.putAll(original); - try { - final Map originalNull = null; - test.putAll(originalNull); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } + final Map originalNull = null; + assertThrows(NullPointerException.class, () -> test.putAll(originalNull), + "expecting NullPointerException"); assertEquals(3, test.keySet().size()); assertEquals(4, test.size()); @@ -620,12 +614,8 @@ public void testPutAll_KeyIterable() { assertTrue(map.containsMapping("A", "Y")); assertTrue(map.containsMapping("A", "Z")); - try { - map.putAll((K) "A", null); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } + assertThrows(NullPointerException.class, () -> map.putAll((K) "A", null), + "expecting NullPointerException"); assertEquals(3, map.get((K) "A").size()); assertTrue(map.containsMapping("A", "X")); @@ -680,13 +670,9 @@ public void testToString(){ "{B=[U, V, W], A=[X, Y, Z]}".equals(map.toString()) ); - try { - final MultiValuedMap originalNull = null; - map.putAll(originalNull); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } + final MultiValuedMap originalNull = null; + assertThrows(NullPointerException.class, () -> map.putAll(originalNull), + "expecting NullPointerException"); assertTrue( "{A=[X, Y, Z], B=[U, V, W]}".equals(map.toString()) || "{B=[U, V, W], A=[X, Y, Z]}".equals(map.toString()) @@ -786,33 +772,17 @@ public void testMapIteratorUnsupportedSet() { resetFull(); final MapIterator mapIt = getMap().mapIterator(); mapIt.next(); - try { - mapIt.setValue((V) "some value"); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> mapIt.setValue((V) "some value")); } public void testMultiValuedMapIterator() { final MultiValuedMap map = makeFullMap(); final MapIterator it = map.mapIterator(); - try { - it.getKey(); - fail(); - } catch (final IllegalStateException ise) { - } - try { - it.getValue(); - fail(); - } catch (final IllegalStateException ise) { - } + assertThrows(IllegalStateException.class, () -> it.getKey()); + assertThrows(IllegalStateException.class, () -> it.getValue()); if (isAddSupported()) { - try { - it.setValue((V) "V"); - fail(); - } catch (final IllegalStateException ise) { - } + assertThrows(IllegalStateException.class, () -> it.setValue((V) "V")); } if (!isHashSetValue() && isAddSupported()) { @@ -835,11 +805,7 @@ public void testMultiValuedMapIterator() { assertEquals("three", it.next()); assertEquals("three", it.getKey()); assertEquals("trois", it.getValue()); - try { - it.setValue((V) "threetrois"); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> it.setValue((V) "threetrois")); } } @@ -1259,4 +1225,5 @@ public boolean isTestSerialization() { return false; } } + } diff --git a/src/test/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMapTest.java b/src/test/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMapTest.java index c7aa751523..efbce78531 100644 --- a/src/test/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.multimap; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Set; import java.util.Map; import java.util.Map.Entry; @@ -93,57 +95,34 @@ public void testDecorateFactory() { } public void testDecoratorFactoryNullMap() { - try { - UnmodifiableMultiValuedMap.unmodifiableMultiValuedMap(null); - fail("map must not be null"); - } catch (final NullPointerException e) { - // expected - } + assertThrows(NullPointerException.class, () -> UnmodifiableMultiValuedMap.unmodifiableMultiValuedMap(null), + "map must not be null"); } @SuppressWarnings("unchecked") public void testAddException() { final MultiValuedMap map = makeObject(); - try { - map.put((K) "one", (V) "uno"); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> map.put((K) "one", (V) "uno")); } public void testRemoveException() { final MultiValuedMap map = makeFullMap(); - try { - map.remove("one"); - fail(); - } catch (final UnsupportedOperationException e) { - // expected, not support remove() method - // UnmodifiableMultiValuedMap does not support change - } + assertThrows(UnsupportedOperationException.class, () -> map.remove("one"), + "not support remove() method UnmodifiableMultiValuedMap does not support change"); this.assertMapContainsAllValues(map); } public void testRemoveMappingException() { final MultiValuedMap map = makeFullMap(); - try { - map.removeMapping("one", "uno"); - fail(); - } catch (final UnsupportedOperationException e) { - // expected, not support removeMapping() method - // UnmodifiableMultiValuedMap does not support change - } + assertThrows(UnsupportedOperationException.class, () -> map.removeMapping("one", "uno"), + "expected, not support removeMapping() method UnmodifiableMultiValuedMap does not support change"); this.assertMapContainsAllValues(map); } public void testClearException() { final MultiValuedMap map = makeFullMap(); - try { - map.clear(); - fail(); - } catch (final UnsupportedOperationException e) { - // expected, not support clear() method - // UnmodifiableMultiValuedMap does not support change - } + assertThrows(UnsupportedOperationException.class, () -> map.clear(), + "expected, not support clear() method UnmodifiableMultiValuedMap does not support change"); this.assertMapContainsAllValues(map); } @@ -157,29 +136,14 @@ public void testPutAllException() { originalMap.put((K) "keyX", (V) "object1"); originalMap.put((K) "keyY", (V) "object2"); - try { - map.putAll(original); - fail(); - } catch (final UnsupportedOperationException e) { - // expected, not support putAll() method - // UnmodifiableMultiValuedMap does not support change - } + assertThrows(UnsupportedOperationException.class, () -> map.putAll(original), + "expected, not support putAll() method UnmodifiableMultiValuedMap does not support change"); assertEquals("{}", map.toString()); - try { - map.putAll(originalMap); - fail(); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> map.putAll(originalMap)); assertEquals("{}", map.toString()); - try { - map.putAll((K) "A", coll); - fail(); - } catch (final UnsupportedOperationException e) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> map.putAll((K) "A", coll)); assertEquals("{}", map.toString()); } @@ -187,161 +151,77 @@ public void testPutAllException() { public void testUnmodifiableEntries() { resetFull(); final Collection> entries = getMap().entries(); - try { - entries.clear(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> entries.clear()); final Iterator> it = entries.iterator(); final Entry entry = it.next(); - try { - it.remove(); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - entry.setValue((V) "three"); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> it.remove()); + + assertThrows(UnsupportedOperationException.class, () -> entry.setValue((V) "three")); } @SuppressWarnings("unchecked") public void testUnmodifiableMapIterator() { resetFull(); final MapIterator mapIt = getMap().mapIterator(); - try { - mapIt.remove(); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - mapIt.setValue((V) "three"); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> mapIt.remove()); + + assertThrows(UnsupportedOperationException.class, () -> mapIt.setValue((V) "three")); } @SuppressWarnings("unchecked") public void testUnmodifiableKeySet() { resetFull(); final Set keySet = getMap().keySet(); - try { - keySet.add((K) "four"); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - keySet.remove("four"); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - keySet.clear(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> keySet.add((K) "four")); + + assertThrows(UnsupportedOperationException.class, () -> keySet.remove("four")); + + assertThrows(UnsupportedOperationException.class, () -> keySet.clear()); final Iterator it = keySet.iterator(); - try { - it.remove(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> it.remove()); } @SuppressWarnings("unchecked") public void testUnmodifiableValues() { resetFull(); final Collection values = getMap().values(); - try { - values.add((V) "four"); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - values.remove("four"); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - values.clear(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> values.add((V) "four")); + + assertThrows(UnsupportedOperationException.class, () -> values.remove("four")); + + assertThrows(UnsupportedOperationException.class, () -> values.clear()); final Iterator it = values.iterator(); - try { - it.remove(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> it.remove()); } @SuppressWarnings("unchecked") public void testUnmodifiableAsMap() { resetFull(); final Map> mapCol = getMap().asMap(); - try { - mapCol.put((K) "four", (Collection) Arrays.asList("four")); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - mapCol.remove("four"); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - mapCol.clear(); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - mapCol.clear(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> mapCol.put((K) "four", (Collection) Arrays.asList("four"))); + + assertThrows(UnsupportedOperationException.class, () -> mapCol.remove("four")); + + assertThrows(UnsupportedOperationException.class, () -> mapCol.clear()); + + assertThrows(UnsupportedOperationException.class, () -> mapCol.clear()); } @SuppressWarnings("unchecked") public void testUnmodifiableKeys() { resetFull(); final MultiSet keys = getMap().keys(); - try { - keys.add((K) "four"); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - keys.remove("four"); - fail(); - } catch (final UnsupportedOperationException e) { - } - - try { - keys.clear(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> keys.add((K) "four")); + + assertThrows(UnsupportedOperationException.class, () -> keys.remove("four")); + + assertThrows(UnsupportedOperationException.class, () -> keys.clear()); final Iterator it = keys.iterator(); - try { - it.remove(); - fail(); - } catch (final UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, () -> it.remove()); } // public void testCreate() throws Exception { diff --git a/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java index 5296bc317a..8463c754e9 100644 --- a/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java +++ b/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.multiset; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; @@ -374,12 +376,8 @@ public void testMultiSetIteratorFail() { final Iterator it = multiset.iterator(); it.next(); multiset.remove("A"); - try { - it.next(); - fail("Should throw ConcurrentModificationException"); - } catch (final ConcurrentModificationException e) { - // expected - } + assertThrows(ConcurrentModificationException.class, () -> it.next(), + "Should throw ConcurrentModificationException"); } @SuppressWarnings("unchecked") @@ -396,12 +394,8 @@ public void testMultiSetIteratorFailNoMore() { it.next(); it.next(); it.next(); - try { - it.next(); - fail("Should throw NoSuchElementException"); - } catch (final NoSuchElementException ex) { - // expected - } + assertThrows(NoSuchElementException.class, () -> it.next(), + "Should throw NoSuchElementException"); } @SuppressWarnings("unchecked") @@ -420,12 +414,8 @@ public void testMultiSetIteratorFailDoubleRemove() { assertEquals(3, multiset.size()); it.remove(); assertEquals(2, multiset.size()); - try { - it.remove(); - fail("Should throw IllegalStateException"); - } catch (final IllegalStateException ex) { - // expected - } + assertThrows(IllegalStateException.class, () -> it.remove(), + "Should throw IllegalStateException"); assertEquals(2, multiset.size()); it.next(); it.remove(); @@ -707,4 +697,5 @@ public void testFullMultiSetCompatibility() throws IOException, ClassNotFoundExc assertEquals(multiset, multiset2); } } + } diff --git a/src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java index 2a52962a53..4dca4d990b 100644 --- a/src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java +++ b/src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.multiset; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Set; import junit.framework.Test; @@ -86,12 +88,8 @@ public void testLegalAddRemove() { public void testIllegalAdd() { final MultiSet multiset = makeTestMultiSet(); final Integer i = Integer.valueOf(3); - try { - multiset.add((T) i); - fail("Integer should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> multiset.add((T) i), + "Integer should fail string predicate."); assertFalse("Collection shouldn't contain illegal element", multiset.contains(i)); } @@ -102,18 +100,10 @@ public void testIllegalDecorate() { elements.add("two"); elements.add(Integer.valueOf(3)); elements.add("four"); - try { - decorateMultiSet((HashMultiSet) elements, stringPredicate()); - fail("MultiSet contains an element that should fail the predicate."); - } catch (final IllegalArgumentException e) { - // expected - } - try { - decorateMultiSet(new HashMultiSet(), null); - fail("Expecting NullPointerException for null predicate."); - } catch (final NullPointerException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> decorateMultiSet((HashMultiSet) elements, stringPredicate()), + "MultiSet contains an element that should fail the predicate."); + assertThrows(NullPointerException.class, () -> decorateMultiSet(new HashMultiSet(), null), + "Expecting NullPointerException for null predicate."); } @Override diff --git a/src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java index 599f0be0b0..84f1d4fa53 100644 --- a/src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java +++ b/src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.multiset; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; import junit.framework.Test; @@ -82,38 +84,26 @@ public void testDecorateFactory() { final MultiSet multiset = makeFullCollection(); assertSame(multiset, UnmodifiableMultiSet.unmodifiableMultiSet(multiset)); - try { - UnmodifiableMultiSet.unmodifiableMultiSet(null); - fail(); - } catch (final NullPointerException ex) {} + assertThrows(NullPointerException.class, () -> UnmodifiableMultiSet.unmodifiableMultiSet(null)); } public void testAdd() { final MultiSet multiset = makeFullCollection(); final MultiSet unmodifiableMultiSet = UnmodifiableMultiSet.unmodifiableMultiSet(multiset); - try { - unmodifiableMultiSet.add((E) "One", 1); - fail(); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> unmodifiableMultiSet.add((E) "One", 1)); } public void testRemove() { final MultiSet multiset = makeFullCollection(); final MultiSet unmodifiableMultiSet = UnmodifiableMultiSet.unmodifiableMultiSet(multiset); - try { - unmodifiableMultiSet.remove("One", 1); - fail(); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> unmodifiableMultiSet.remove("One", 1)); } public void testSetCount() { final MultiSet multiset = makeFullCollection(); final MultiSet unmodifiableMultiSet = UnmodifiableMultiSet.unmodifiableMultiSet(multiset); - try { - unmodifiableMultiSet.setCount((E) "One", 2); - fail(); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> unmodifiableMultiSet.setCount((E) "One", 2)); } public void testEntrySet() { diff --git a/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java index 93a136dd2b..273cfd3d30 100644 --- a/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/AbstractQueueTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.queue; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; @@ -161,12 +163,8 @@ public void testQueueOffer() { public void testQueueElement() { resetEmpty(); - try { - getCollection().element(); - fail("Queue.element should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected - } + assertThrows(NoSuchElementException.class, () -> getCollection().element(), + "Queue.element should throw NoSuchElementException"); resetFull(); @@ -192,12 +190,8 @@ public void testQueueElement() { verify(); } - try { - getCollection().element(); - fail("Queue.element should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected - } + assertThrows(NoSuchElementException.class, () -> getCollection().element(), + "Queue.element should throw NoSuchElementException"); } /** @@ -245,12 +239,8 @@ public void testQueueRemove() { resetEmpty(); - try { - getCollection().remove(); - fail("Queue.remove should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected - } + assertThrows(NoSuchElementException.class, () -> getCollection().remove(), + "Queue.remove should throw NoSuchElementException"); resetFull(); @@ -262,12 +252,8 @@ public void testQueueRemove() { verify(); } - try { - getCollection().element(); - fail("Queue.remove should throw NoSuchElementException"); - } catch (final NoSuchElementException e) { - // expected - } + assertThrows(NoSuchElementException.class, () -> getCollection().element(), + "Queue.remove should throw NoSuchElementException"); } /** diff --git a/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java index b1cb67a957..4980cbb1f0 100644 --- a/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/CircularFifoQueueTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.queue; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; @@ -146,48 +148,29 @@ public void testCircularFifoQueueRemove() { verify(); } - try { - getCollection().remove(); - fail("Empty queue should raise Underflow."); - } catch (final NoSuchElementException e) { - // expected - } + assertThrows(NoSuchElementException.class, () -> getCollection().remove(), + "Empty queue should raise Underflow."); } /** * Tests that the constructor correctly throws an exception. */ public void testConstructorException1() { - try { - new CircularFifoQueue(0); - } catch (final IllegalArgumentException ex) { - return; - } - fail(); + assertThrows(IllegalArgumentException.class, () -> new CircularFifoQueue(0)); } /** * Tests that the constructor correctly throws an exception. */ public void testConstructorException2() { - try { - new CircularFifoQueue(-20); - } catch (final IllegalArgumentException ex) { - return; - } - fail(); + assertThrows(IllegalArgumentException.class, () -> new CircularFifoQueue(-20)); } /** * Tests that the constructor correctly throws an exception. */ public void testConstructorException3() { - try { - new CircularFifoQueue(null); - } catch (final NullPointerException ex) { - return; - } - fail(); + assertThrows(NullPointerException.class, () -> new CircularFifoQueue(null)); } @SuppressWarnings("unchecked") @@ -419,13 +402,7 @@ public void testGetIndex() { public void testAddNull() { final CircularFifoQueue b = new CircularFifoQueue<>(2); - try { - b.add(null); - fail(); - } catch (final NullPointerException ex) { - return; - } - fail(); + assertThrows(NullPointerException.class, () -> b.add(null)); } public void testDefaultSizeAndGetError1() { @@ -437,12 +414,7 @@ public void testDefaultSizeAndGetError1() { fifo.add((E) "4"); fifo.add((E) "5"); assertEquals(5, fifo.size()); - try { - fifo.get(5); - } catch (final NoSuchElementException ex) { - return; - } - fail(); + assertThrows(NoSuchElementException.class, () -> fifo.get(5)); } public void testDefaultSizeAndGetError2() { @@ -454,12 +426,7 @@ public void testDefaultSizeAndGetError2() { fifo.add((E) "4"); fifo.add((E) "5"); assertEquals(5, fifo.size()); - try { - fifo.get(-2); - } catch (final NoSuchElementException ex) { - return; - } - fail(); + assertThrows(NoSuchElementException.class, () -> fifo.get(-2)); } @Override diff --git a/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java index 4ab8e3e11f..5c99ff3070 100644 --- a/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java +++ b/src/test/java/org/apache/commons/collections4/queue/UnmodifiableQueueTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.queue; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; @@ -80,10 +82,7 @@ public boolean isNullSupported() { @Override public void testQueueRemove() { resetEmpty(); - try { - getCollection().remove(); - fail(); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> getCollection().remove()); } public void testUnmodifiable() { @@ -95,27 +94,18 @@ public void testDecorateFactory() { final Queue queue = makeFullCollection(); assertSame(queue, UnmodifiableQueue.unmodifiableQueue(queue)); - try { - UnmodifiableQueue.unmodifiableQueue(null); - fail(); - } catch (final NullPointerException ex) {} + assertThrows(NullPointerException.class, () -> UnmodifiableQueue.unmodifiableQueue(null)); } public void testOffer() { final Queue queue = makeFullCollection(); final E e = null; - try { - queue.offer(e); - fail(); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> queue.offer(e)); } public void testPoll() { final Queue queue = makeFullCollection(); - try { - queue.poll(); - fail(); - } catch (final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> queue.poll()); } diff --git a/src/test/java/org/apache/commons/collections4/set/CompositeSetTest.java b/src/test/java/org/apache/commons/collections4/set/CompositeSetTest.java index 55e35901a6..26ce6c79b5 100644 --- a/src/test/java/org/apache/commons/collections4/set/CompositeSetTest.java +++ b/src/test/java/org/apache/commons/collections4/set/CompositeSetTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.set; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -30,6 +32,7 @@ * @since 3.0 */ public class CompositeSetTest extends AbstractSetTest { + public CompositeSetTest(final String name) { super(name); } @@ -129,12 +132,8 @@ public boolean addAll(final CompositeSet composite, final HashSet three = new HashSet<>(); three.add((E) "1"); - try { - set.addComposited(three); - fail("IllegalArgumentException should have been thrown"); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> set.addComposited(three), + "IllegalArgumentException should have been thrown"); } @SuppressWarnings("unchecked") @@ -159,12 +158,8 @@ public void testAddComposited() { final CompositeSet set5 = new CompositeSet<>(set3); set5.addComposited(set4); assertEquals(set, set5); - try { - set.addComposited(set3); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException ex) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> set.addComposited(set3), + "Expecting UnsupportedOperationException."); } @SuppressWarnings("unchecked") @@ -176,18 +171,10 @@ public void testAddCompositedCollision() { final HashSet set2 = new HashSet<>(); set2.add((E) "4"); final CompositeSet set3 = new CompositeSet<>(set1); - try { - set3.addComposited(set1, buildOne()); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException ex) { - // expected - } - try { - set3.addComposited(set1, buildOne(), buildTwo()); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException ex) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> set3.addComposited(set1, buildOne()), + "Expecting UnsupportedOperationException."); + assertThrows(UnsupportedOperationException.class, () -> set3.addComposited(set1, buildOne(), buildTwo()), + "Expecting UnsupportedOperationException."); } @Override diff --git a/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java b/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java index d5547d9fc6..53c35890a1 100644 --- a/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java +++ b/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java @@ -16,6 +16,9 @@ */ package org.apache.commons.collections4.set; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -249,31 +252,13 @@ public int hashCode() { } public void testDecorator() { - try { - ListOrderedSet.listOrderedSet((List) null); - fail(); - } catch (final NullPointerException ex) { - } - try { - ListOrderedSet.listOrderedSet((Set) null); - fail(); - } catch (final NullPointerException ex) { - } - try { - ListOrderedSet.listOrderedSet(null, null); - fail(); - } catch (final NullPointerException ex) { - } - try { - ListOrderedSet.listOrderedSet(new HashSet(), null); - fail(); - } catch (final NullPointerException ex) { - } - try { - ListOrderedSet.listOrderedSet(null, new ArrayList()); - fail(); - } catch (final NullPointerException ex) { - } + assertAll( + () -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet((List) null)), + () -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet((Set) null)), + () -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet(null, null)), + () -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet(new HashSet(), null)), + () -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet(null, new ArrayList())) + ); } @Override diff --git a/src/test/java/org/apache/commons/collections4/set/PredicatedNavigableSetTest.java b/src/test/java/org/apache/commons/collections4/set/PredicatedNavigableSetTest.java index 320d25b185..908c99be1f 100644 --- a/src/test/java/org/apache/commons/collections4/set/PredicatedNavigableSetTest.java +++ b/src/test/java/org/apache/commons/collections4/set/PredicatedNavigableSetTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.set; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; import java.util.Comparator; import java.util.NavigableSet; @@ -76,12 +78,8 @@ public void testGetSet() { public void testIllegalAdd() { final NavigableSet set = makeTestSet(); final String testString = "B"; - try { - set.add((E) testString); - fail("Should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> set.add((E) testString), + "Should fail string predicate."); assertFalse("Collection shouldn't contain illegal element", set.contains(testString)); } @@ -93,12 +91,8 @@ public void testIllegalAddAll() { elements.add((E) "Atwo"); elements.add((E) "Bthree"); elements.add((E) "Afour"); - try { - set.addAll(elements); - fail("Should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> set.addAll(elements), + "Should fail string predicate."); assertFalse("Set shouldn't contain illegal element", set.contains("Aone")); assertFalse("Set shouldn't contain illegal element", set.contains("Atwo")); assertFalse("Set shouldn't contain illegal element", set.contains("Bthree")); diff --git a/src/test/java/org/apache/commons/collections4/set/PredicatedSetTest.java b/src/test/java/org/apache/commons/collections4/set/PredicatedSetTest.java index c2a6515d2e..0df42430c7 100644 --- a/src/test/java/org/apache/commons/collections4/set/PredicatedSetTest.java +++ b/src/test/java/org/apache/commons/collections4/set/PredicatedSetTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.set; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.HashSet; import java.util.Set; @@ -71,12 +73,8 @@ public void testGetSet() { public void testIllegalAdd() { final Set set = makeTestSet(); final Integer i = Integer.valueOf(3); - try { - set.add((E) i); - fail("Integer should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> set.add((E) i), + "Integer should fail string predicate."); assertFalse("Collection shouldn't contain illegal element", set.contains(i)); } @@ -88,12 +86,8 @@ public void testIllegalAddAll() { elements.add((E) "two"); elements.add((E) Integer.valueOf(3)); elements.add((E) "four"); - try { - set.addAll(elements); - fail("Integer should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> set.addAll(elements), + "Integer should fail string predicate."); assertFalse("Set shouldn't contain illegal element", set.contains("one")); assertFalse("Set shouldn't contain illegal element", set.contains("two")); assertFalse("Set shouldn't contain illegal element", set.contains(Integer.valueOf(3))); diff --git a/src/test/java/org/apache/commons/collections4/set/PredicatedSortedSetTest.java b/src/test/java/org/apache/commons/collections4/set/PredicatedSortedSetTest.java index 1100ddfab3..97fcfc4d6b 100644 --- a/src/test/java/org/apache/commons/collections4/set/PredicatedSortedSetTest.java +++ b/src/test/java/org/apache/commons/collections4/set/PredicatedSortedSetTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.set; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; import java.util.Comparator; import java.util.Set; @@ -73,12 +75,8 @@ public void testGetSet() { public void testIllegalAdd() { final SortedSet set = makeTestSet(); final String testString = "B"; - try { - set.add((E) testString); - fail("Should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> set.add((E) testString), + "Should fail string predicate."); assertFalse("Collection shouldn't contain illegal element", set.contains(testString)); } @@ -90,12 +88,8 @@ public void testIllegalAddAll() { elements.add((E) "Atwo"); elements.add((E) "Bthree"); elements.add((E) "Afour"); - try { - set.addAll(elements); - fail("Should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> set.addAll(elements), + "Should fail string predicate."); assertFalse("Set shouldn't contain illegal element", set.contains("Aone")); assertFalse("Set shouldn't contain illegal element", set.contains("Atwo")); assertFalse("Set shouldn't contain illegal element", set.contains("Bthree")); diff --git a/src/test/java/org/apache/commons/collections4/set/UnmodifiableSortedSetTest.java b/src/test/java/org/apache/commons/collections4/set/UnmodifiableSortedSetTest.java index 177046f4fb..39400f8707 100644 --- a/src/test/java/org/apache/commons/collections4/set/UnmodifiableSortedSetTest.java +++ b/src/test/java/org/apache/commons/collections4/set/UnmodifiableSortedSetTest.java @@ -16,6 +16,9 @@ */ package org.apache.commons.collections4.set; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -91,10 +94,7 @@ public void testDecorateFactory() { final SortedSet set = makeFullCollection(); assertSame(set, UnmodifiableSortedSet.unmodifiableSortedSet(set)); - try { - UnmodifiableSortedSet.unmodifiableSortedSet(null); - fail(); - } catch (final NullPointerException ex) {} + assertThrows(NullPointerException.class, () -> UnmodifiableSortedSet.unmodifiableSortedSet(null)); } /** @@ -102,42 +102,20 @@ public void testDecorateFactory() { */ @SuppressWarnings("unchecked") public void verifyUnmodifiable(final Set set) { - try { - set.add((E) "value"); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.addAll(new TreeSet()); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.clear(); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.remove("x"); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.removeAll(array); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } - try { - set.retainAll(array); - fail("Expecting UnsupportedOperationException."); - } catch (final UnsupportedOperationException e) { - // expected - } + assertAll( + () -> assertThrows(UnsupportedOperationException.class, () -> set.add((E) "value"), + "Expecting UnsupportedOperationException."), + () -> assertThrows(UnsupportedOperationException.class, () -> set.addAll(new TreeSet()), + "Expecting UnsupportedOperationException."), + () -> assertThrows(UnsupportedOperationException.class, () -> set.clear(), + "Expecting UnsupportedOperationException."), + () -> assertThrows(UnsupportedOperationException.class, () -> set.remove("x"), + "Expecting UnsupportedOperationException."), + () -> assertThrows(UnsupportedOperationException.class, () -> set.removeAll(array), + "Expecting UnsupportedOperationException."), + () -> assertThrows(UnsupportedOperationException.class, () -> set.retainAll(array), + "Expecting UnsupportedOperationException.") + ); } public void testComparator() {