diff --git a/src/test/java/org/apache/commons/collections4/ArrayStackTest.java b/src/test/java/org/apache/commons/collections4/ArrayStackTest.java index e96940ab5..8f1ce5d81 100644 --- a/src/test/java/org/apache/commons/collections4/ArrayStackTest.java +++ b/src/test/java/org/apache/commons/collections4/ArrayStackTest.java @@ -16,13 +16,14 @@ */ package org.apache.commons.collections4; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.EmptyStackException; import junit.framework.Test; /** * Tests ArrayStack. - * */ @SuppressWarnings("deprecation") // we test a deprecated class public class ArrayStackTest extends AbstractArrayListTest { @@ -45,20 +46,9 @@ public class ArrayStackTest extends AbstractArrayListTest { assertTrue("New stack is empty", stack.empty()); assertEquals("New stack has size zero", 0, stack.size()); - try { - stack.peek(); - fail("peek() should have thrown EmptyStackException"); - } catch (final EmptyStackException e) { - // Expected result - } - - try { - stack.pop(); - fail("pop() should have thrown EmptyStackException"); - } catch (final EmptyStackException e) { - // Expected result - } + assertThrows(EmptyStackException.class, () -> stack.peek()); + assertThrows(EmptyStackException.class, () -> stack.pop()); } @SuppressWarnings("unchecked") @@ -87,7 +77,6 @@ public class ArrayStackTest extends AbstractArrayListTest { assertEquals("Popped item is 'First Item'", "First Item", (String) stack.pop()); assertEquals("Stack size is zero", 0, stack.size()); - } @Override @@ -103,7 +92,6 @@ public class ArrayStackTest extends AbstractArrayListTest { 2, stack.search("First Item")); assertEquals("Cannot find 'Missing Item'", -1, stack.search("Missing Item")); - } @Override diff --git a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java index 48975f09e..9c8dca49d 100644 --- a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java @@ -17,9 +17,9 @@ package org.apache.commons.collections4; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.Enumeration; @@ -33,7 +33,6 @@ import org.junit.jupiter.api.Test; /** * Tests EnumerationUtils. - * */ public class EnumerationUtilsTest { @@ -51,12 +50,9 @@ public class EnumerationUtilsTest { assertEquals("one", EnumerationUtils.get(en, 1)); // Enumerator, non-existent entry - try { - EnumerationUtils.get(en, 3); - fail("Expecting IndexOutOfBoundsException."); - } catch (final IndexOutOfBoundsException e) { - // expected - } + Enumeration finalEn = en; + assertThrows(IndexOutOfBoundsException.class, () -> EnumerationUtils.get(finalEn, 3)); + assertFalse(en.hasMoreElements()); } @@ -76,12 +72,7 @@ public class EnumerationUtilsTest { @Test public void testAsIterableForNull() { - try { - EnumerationUtils.asIterable((Enumeration) null).iterator().next(); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - // success - } + assertThrows(NullPointerException.class, () -> EnumerationUtils.asIterable((Enumeration) null).iterator().next()); } @Test diff --git a/src/test/java/org/apache/commons/collections4/MultiMapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MultiMapUtilsTest.java index c2125ad25..39a6c2bfe 100644 --- a/src/test/java/org/apache/commons/collections4/MultiMapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MultiMapUtilsTest.java @@ -19,8 +19,8 @@ package org.apache.commons.collections4; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.util.Arrays; import java.util.Collection; @@ -43,22 +43,16 @@ public class MultiMapUtilsTest { public void testEmptyUnmodifiableMultiValuedMap() { final MultiValuedMap map = MultiMapUtils.EMPTY_MULTI_VALUED_MAP; assertTrue(map.isEmpty()); - try { - map.put("key", "value"); - fail("Should throw UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - } + + assertThrows(UnsupportedOperationException.class, () -> map.put("key", "value")); } @Test public void testTypeSafeEmptyMultiValuedMap() { final MultiValuedMap map = MultiMapUtils.emptyMultiValuedMap(); assertTrue(map.isEmpty()); - try { - map.put("key", "value"); - fail("Should throw UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - } + + assertThrows(UnsupportedOperationException.class, () -> map.put("key", "value")); } @Test diff --git a/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java b/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java index ec8057ff4..bd30e5605 100644 --- a/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java @@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.util.ArrayList; import java.util.Arrays; @@ -58,16 +57,10 @@ public class PredicateUtilsTest extends AbstractPredicateTest { public void testExceptionPredicate() { assertNotNull(PredicateUtils.exceptionPredicate()); assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils.exceptionPredicate()); - try { - PredicateUtils.exceptionPredicate().evaluate(null); - } catch (final FunctorException ex) { - try { - PredicateUtils.exceptionPredicate().evaluate(cString); - } catch (final FunctorException ex2) { - return; - } - } - fail(); + + assertThrows(FunctorException.class, () -> PredicateUtils.exceptionPredicate().evaluate(null)); + + assertThrows(FunctorException.class, () -> PredicateUtils.exceptionPredicate().evaluate(cString)); } // notNullPredicate @@ -666,10 +659,8 @@ public class PredicateUtilsTest extends AbstractPredicateTest { final Predicate p = EqualPredicate.equalPredicate("Hello"); assertFalse(PredicateUtils.transformedPredicate(t, p).evaluate(null)); assertTrue(PredicateUtils.transformedPredicate(t, p).evaluate(Boolean.TRUE)); - try { - PredicateUtils.transformedPredicate(null, null); - fail(); - } catch (final NullPointerException ex) {} + + assertThrows(NullPointerException.class, () -> PredicateUtils.transformedPredicate(null, null)); } // misc tests @@ -701,6 +692,5 @@ public class PredicateUtilsTest extends AbstractPredicateTest { protected Predicate generatePredicate() { return truePredicate(); //Just return something to satisfy super class. } + } - - diff --git a/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java b/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java index 214b72702..b6cd0adaa 100644 --- a/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java +++ b/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java @@ -34,6 +34,7 @@ import org.apache.commons.collections4.collection.AbstractCollectionTest; import org.apache.commons.collections4.set.AbstractSetTest; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Abstract test class for {@link org.apache.commons.collections4.Bag Bag} methods and contracts. @@ -59,7 +60,6 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; * interface (prefix testBag). For Bag specific tests use the {@link #makeObject()} and * {@link #makeFullCollection()} methods instead of {@link #resetEmpty()} and resetFull(), * otherwise the collection will be wrapped by a {@link CollectionBag} decorator. - * */ public abstract class AbstractBagTest extends AbstractCollectionTest { @@ -108,7 +108,6 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { return bag; } - @Override public void resetEmpty() { this.setCollection(CollectionBag.collectionBag(makeObject())); @@ -379,12 +378,8 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { final Iterator it = bag.iterator(); it.next(); bag.remove("A"); - try { - it.next(); - fail("Should throw ConcurrentModificationException"); - } catch (final ConcurrentModificationException e) { - // expected - } + + assertThrows(ConcurrentModificationException.class, () -> it.next()); } @SuppressWarnings("unchecked") @@ -401,12 +396,8 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { it.next(); it.next(); it.next(); - try { - it.next(); - fail("Should throw NoSuchElementException"); - } catch (final NoSuchElementException ex) { - // expected - } + + assertThrows(NoSuchElementException.class, () -> it.next()); } @SuppressWarnings("unchecked") @@ -425,12 +416,9 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { assertEquals(3, bag.size()); it.remove(); assertEquals(2, bag.size()); - try { - it.remove(); - fail("Should throw IllegalStateException"); - } catch (final IllegalStateException ex) { - // expected - } + + assertThrows(IllegalStateException.class, () -> it.remove()); + assertEquals(2, bag.size()); it.next(); it.remove(); @@ -590,7 +578,6 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { assertEquals(total, bag2.hashCode()); } - /** * Bulk test {@link Bag#uniqueSet()}. This method runs through all of * the tests in {@link AbstractSetTest}. @@ -604,6 +591,7 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { } public class TestBagUniqueSet extends AbstractSetTest { + public TestBagUniqueSet() { super(""); } @@ -666,8 +654,8 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { public void verify() { super.verify(); } - } + } /** * Compare the current serialized form of the Bag @@ -696,4 +684,5 @@ public abstract class AbstractBagTest extends AbstractCollectionTest { assertEquals(bag, bag2); } } + } diff --git a/src/test/java/org/apache/commons/collections4/bag/PredicatedBagTest.java b/src/test/java/org/apache/commons/collections4/bag/PredicatedBagTest.java index 0aed058ff..9f97050e0 100644 --- a/src/test/java/org/apache/commons/collections4/bag/PredicatedBagTest.java +++ b/src/test/java/org/apache/commons/collections4/bag/PredicatedBagTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.bag; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Set; import junit.framework.Test; @@ -85,12 +87,9 @@ public class PredicatedBagTest extends AbstractBagTest { public void testIllegalAdd() { final Bag bag = makeTestBag(); final Integer i = 3; - try { - bag.add((T) i); - fail("Integer should fail string predicate."); - } catch (final IllegalArgumentException e) { - // expected - } + + assertThrows(IllegalArgumentException.class, () -> bag.add((T) i)); + assertFalse("Collection shouldn't contain illegal element", bag.contains(i)); } @@ -101,18 +100,10 @@ public class PredicatedBagTest extends AbstractBagTest { elements.add("two"); elements.add(3); elements.add("four"); - try { - decorateBag((HashBag) elements, stringPredicate()); - fail("Bag contains an element that should fail the predicate."); - } catch (final IllegalArgumentException e) { - // expected - } - try { - decorateBag(new HashBag(), null); - fail("Expecting NullPointerException for null predicate."); - } catch (final NullPointerException e) { - // expected - } + + assertThrows(IllegalArgumentException.class, () -> decorateBag((HashBag) elements, stringPredicate())); + + assertThrows(NullPointerException.class, () -> decorateBag(new HashBag(), null)); } @Override diff --git a/src/test/java/org/apache/commons/collections4/bag/PredicatedSortedBagTest.java b/src/test/java/org/apache/commons/collections4/bag/PredicatedSortedBagTest.java index ae1e1608e..1e409743a 100644 --- a/src/test/java/org/apache/commons/collections4/bag/PredicatedSortedBagTest.java +++ b/src/test/java/org/apache/commons/collections4/bag/PredicatedSortedBagTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.bag; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Comparator; import junit.framework.Test; @@ -69,14 +71,10 @@ public class PredicatedSortedBagTest extends AbstractSortedBagTest { public void testDecorate() { final SortedBag bag = decorateBag(new TreeBag(), stringPredicate()); ((PredicatedSortedBag) bag).decorated(); - try { - decorateBag(new TreeBag(), null); - fail("Expecting NullPointerException for null predicate"); - } catch (final NullPointerException e) {} - try { - decorateBag(nullBag, stringPredicate()); - fail("Expecting NullPointerException for null bag"); - } catch (final NullPointerException e) {} + + assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag(), null)); + + assertThrows(NullPointerException.class, () -> decorateBag(nullBag, stringPredicate())); } @SuppressWarnings("unchecked") diff --git a/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java b/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java index 2e96061dd..297444f3d 100644 --- a/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java +++ b/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.bag; +import static org.junit.jupiter.api.Assertions.assertThrows; + import junit.framework.Test; import org.apache.commons.collections4.Bag; @@ -25,7 +27,6 @@ import org.apache.commons.collections4.SortedBag; /** * Extension of {@link AbstractBagTest} for exercising the {@link TreeBag} * implementation. - * */ public class TreeBagTest extends AbstractSortedBagTest { @@ -37,7 +38,6 @@ public class TreeBagTest extends AbstractSortedBagTest { return BulkTest.makeSuite(TreeBagTest.class); } - @Override public SortedBag makeObject() { return new TreeBag<>(); @@ -55,33 +55,21 @@ public class TreeBagTest extends AbstractSortedBagTest { public void testCollections265() { final Bag bag = new TreeBag<>(); - try { - bag.add(new Object()); - fail("IllegalArgumentException expected"); - } catch(final IllegalArgumentException iae) { - // expected; - } + + assertThrows(IllegalArgumentException.class, () -> bag.add(new Object())); } public void testCollections555() { final Bag bag = new TreeBag<>(); - try { - bag.add(null); - fail("NullPointerException expected"); - } catch(final NullPointerException npe) { - // expected; - } + + assertThrows(NullPointerException.class, () -> bag.add(null)); final Bag bag2 = new TreeBag<>(String::compareTo); - try { - // jdk bug: adding null to an empty TreeMap works - // thus ensure that the bag is not empty before adding null - bag2.add("a"); - bag2.add(null); - fail("NullPointerException expected"); - } catch(final NullPointerException npe) { - // expected; - } + // jdk bug: adding null to an empty TreeMap works + // thus ensure that the bag is not empty before adding null + bag2.add("a"); + + assertThrows(NullPointerException.class, () -> bag2.add(null)); } public void testOrdering() { @@ -104,4 +92,5 @@ public class TreeBagTest extends AbstractSortedBagTest { // bag = makeFullCollection(); // writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TreeBag.fullCollection.version4.obj"); // } + } 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 ea8b28469..8a01b65b7 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/AbstractBidiMapTest.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/AbstractBidiMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.bidimap; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -30,7 +32,6 @@ import org.apache.commons.collections4.map.AbstractIterableMapTest; /** * Abstract test class for {@link BidiMap} methods and contracts. - * */ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest { @@ -208,10 +209,7 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< public void testBidiClear() { if (!isRemoveSupported()) { - try { - makeFullMap().clear(); - fail(); - } catch(final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> makeFullMap().clear()); return; } @@ -225,19 +223,14 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< map.clear(); assertTrue("Map was not cleared.", map.isEmpty()); assertTrue("Inverse map was not cleared.", map.inverseBidiMap().isEmpty()); - } public void testBidiRemove() { if (!isRemoveSupported()) { - try { - makeFullMap().remove(getSampleKeys()[0]); - fail(); - } catch(final UnsupportedOperationException ex) {} - try { - makeFullMap().removeValue(getSampleValues()[0]); - fail(); - } catch(final UnsupportedOperationException ex) {} + assertThrows(UnsupportedOperationException.class, () -> makeFullMap().remove(getSampleKeys()[0])); + + assertThrows(UnsupportedOperationException.class, () -> makeFullMap().removeValue(getSampleValues()[0])); + return; } @@ -329,8 +322,10 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< } public class TestBidiMapEntrySet extends TestMapEntrySet { + public TestBidiMapEntrySet() { } + public void testMapEntrySetIteratorEntrySetValueCrossCheck() { final K key1 = getSampleKeys()[0]; final K key2 = getSampleKeys()[1]; @@ -387,6 +382,7 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< it.remove(); } } + } public BulkTest bulkTestInverseMap() { @@ -394,6 +390,7 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< } public class TestInverseBidiMap extends AbstractBidiMapTest { + final AbstractBidiMapTest main; public TestInverseBidiMap(final AbstractBidiMapTest main) { @@ -414,6 +411,7 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< public V[] getSampleKeys() { return main.getSampleValues(); } + @Override public K[] getSampleValues() { return main.getSampleKeys(); @@ -461,6 +459,7 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< } public class TestBidiMapIterator extends AbstractMapIteratorTest { + public TestBidiMapIterator() { super("TestBidiMapIterator"); } @@ -509,6 +508,7 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< super.verify(); AbstractBidiMapTest.this.verify(); } + } public void testBidiMapIteratorSet() { @@ -522,11 +522,7 @@ public abstract class AbstractBidiMapTest extends AbstractIterableMapTest< final K key1 = it.next(); if (!isSetValueSupported()) { - try { - it.setValue(newValue1); - fail(); - } catch (final UnsupportedOperationException ex) { - } + assertThrows(UnsupportedOperationException.class, () -> it.setValue(newValue1)); return; } diff --git a/src/test/java/org/apache/commons/collections4/bidimap/AbstractOrderedBidiMapTest.java b/src/test/java/org/apache/commons/collections4/bidimap/AbstractOrderedBidiMapTest.java index f662401eb..bab1837a9 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/AbstractOrderedBidiMapTest.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/AbstractOrderedBidiMapTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.bidimap; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -30,7 +32,6 @@ import org.apache.commons.collections4.iterators.AbstractMapIteratorTest; /** * Abstract test class for {@link OrderedBidiMap} methods and contracts. - * */ public abstract class AbstractOrderedBidiMapTest extends AbstractBidiMapTest { @@ -44,10 +45,9 @@ public abstract class AbstractOrderedBidiMapTest extends AbstractBidiMapTe public void testFirstKey() { resetEmpty(); OrderedBidiMap bidi = getMap(); - try { - bidi.firstKey(); - fail(); - } catch (final NoSuchElementException ex) {} + + OrderedBidiMap finalBidi = bidi; + assertThrows(NoSuchElementException.class, () -> finalBidi.firstKey()); resetFull(); bidi = getMap(); @@ -58,10 +58,9 @@ public abstract class AbstractOrderedBidiMapTest extends AbstractBidiMapTe public void testLastKey() { resetEmpty(); OrderedBidiMap bidi = getMap(); - try { - bidi.lastKey(); - fail(); - } catch (final NoSuchElementException ex) {} + + OrderedBidiMap finalBidi = bidi; + assertThrows(NoSuchElementException.class, () -> finalBidi.lastKey()); resetFull(); bidi = getMap(); @@ -96,10 +95,9 @@ public abstract class AbstractOrderedBidiMapTest extends AbstractBidiMapTe assertNull(bidi.nextKey(confirmedLast)); if (!isAllowNullKey()) { - try { - bidi.nextKey(null); - fail(); - } catch (final NullPointerException ex) {} + OrderedBidiMap finalBidi = bidi; + assertThrows(NullPointerException.class, () -> finalBidi.nextKey(null)); + } else { assertNull(bidi.nextKey(null)); } @@ -131,10 +129,9 @@ public abstract class AbstractOrderedBidiMapTest extends AbstractBidiMapTe assertNull(bidi.previousKey(confirmedLast)); if (!isAllowNullKey()) { - try { - bidi.previousKey(null); - fail(); - } catch (final NullPointerException ex) {} + OrderedBidiMap finalBidi = bidi; + assertThrows(NullPointerException.class, () -> finalBidi.previousKey(null)); + } else { assertNull(bidi.previousKey(null)); } @@ -153,6 +150,7 @@ public abstract class AbstractOrderedBidiMapTest extends AbstractBidiMapTe } public class TestBidiOrderedMapIterator extends AbstractMapIteratorTest { + public TestBidiOrderedMapIterator() { super("TestBidiOrderedMapIterator"); } @@ -201,6 +199,7 @@ public abstract class AbstractOrderedBidiMapTest extends AbstractBidiMapTe super.verify(); AbstractOrderedBidiMapTest.this.verify(); } + } }