This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push: new 2a2a534 COLLECTIONS-777 JUnit v5 (#284) 2a2a534 is described below commit 2a2a534c02ac6c400f0c6d16c349a319e83d6d0f Author: John Patrick <142304+nhojpatr...@users.noreply.github.com> AuthorDate: Fri Mar 4 13:44:25 2022 +0000 COLLECTIONS-777 JUnit v5 (#284) JUnit v5 assertThrows TransformerUtilsTest JUnit v5 assertThrows DynamicHasherBuilderTest JUnit v5 assertThrows DynamicHasherTest JUnit v5 assertThrows IndexFilterTest JUnit v5 assertThrows BooleanComparatorTest JUnit v5 assertThrows FixedOrderComparatorTest JUnit v5 assertThrows ComparatorUtilsTest JUnit v5 assertThrows FactoryUtilsTest JUnit v5 assertThrows MultiSetUtilsTest JUnit v5 assertThrows QueueUtilsTest --- .../commons/collections4/ComparatorUtilsTest.java | 45 ++-- .../commons/collections4/FactoryUtilsTest.java | 32 +-- .../commons/collections4/MultiSetUtilsTest.java | 52 ++--- .../commons/collections4/QueueUtilsTest.java | 65 ++---- .../commons/collections4/TransformerUtilsTest.java | 228 ++++++++------------- .../collections4/bloomfilter/IndexFilterTest.java | 26 +-- .../hasher/DynamicHasherBuilderTest.java | 11 +- .../bloomfilter/hasher/DynamicHasherTest.java | 20 +- .../comparators/BooleanComparatorTest.java | 46 ++--- .../comparators/FixedOrderComparatorTest.java | 42 ++-- 10 files changed, 197 insertions(+), 370 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/ComparatorUtilsTest.java b/src/test/java/org/apache/commons/collections4/ComparatorUtilsTest.java index f66e279..5af0625 100644 --- a/src/test/java/org/apache/commons/collections4/ComparatorUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/ComparatorUtilsTest.java @@ -16,9 +16,10 @@ */ package org.apache.commons.collections4; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +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.Comparator; @@ -26,9 +27,9 @@ import org.junit.jupiter.api.Test; /** * Tests ComparatorUtils. - * */ public class ComparatorUtilsTest { + @Test public void booleanComparator() { Comparator<Boolean> comp = ComparatorUtils.booleanComparator(true); @@ -63,19 +64,13 @@ public class ComparatorUtilsTest { assertEquals(Integer.valueOf(1), ComparatorUtils.max(1, 10, reversed)); assertEquals(Integer.valueOf(-10), ComparatorUtils.max(10, -10, reversed)); - try { - ComparatorUtils.max(1, null, null); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } - - try { - ComparatorUtils.max(null, 10, null); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } + assertAll( + () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.max(1, null, null), + "expecting NullPointerException"), + + () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.max(null, 10, null), + "expecting NullPointerException") + ); } @Test @@ -89,19 +84,13 @@ public class ComparatorUtilsTest { assertEquals(Integer.valueOf(10), ComparatorUtils.min(1, 10, reversed)); assertEquals(Integer.valueOf(10), ComparatorUtils.min(10, -10, reversed)); - try { - ComparatorUtils.min(1, null, null); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } - - try { - ComparatorUtils.min(null, 10, null); - fail("expecting NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } + assertAll( + () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.min(1, null, null), + "expecting NullPointerException"), + + () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.min(null, 10, null), + "expecting NullPointerException") + ); } @Test diff --git a/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java b/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java index d5fa0cb..0e4c85a 100644 --- a/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java @@ -21,9 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -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.io.IOException; import java.io.Serializable; @@ -48,16 +47,8 @@ public class FactoryUtilsTest { public void testExceptionFactory() { assertNotNull(FactoryUtils.exceptionFactory()); assertSame(FactoryUtils.exceptionFactory(), FactoryUtils.exceptionFactory()); - try { - FactoryUtils.exceptionFactory().create(); - } catch (final FunctorException ex) { - try { - FactoryUtils.exceptionFactory().create(); - } catch (final FunctorException ex2) { - return; - } - } - fail(); + + assertThrows(FunctorException.class, () -> FactoryUtils.exceptionFactory().create()); } // nullFactory @@ -134,24 +125,15 @@ public class FactoryUtilsTest { final Mock2 proto = new Mock2(new Object()); final Factory<Object> factory = FactoryUtils.<Object>prototypeFactory(proto); assertNotNull(factory); - try { - factory.create(); - } catch (final FunctorException ex) { - assertTrue(ex.getCause() instanceof IOException); - return; - } - fail(); + + final FunctorException thrown = assertThrows(FunctorException.class, () -> factory.create()); + assertTrue(thrown.getCause() instanceof IOException); } @Test public void testPrototypeFactoryPublicBad() { final Object proto = new Object(); - try { - FactoryUtils.prototypeFactory(proto); - } catch (final IllegalArgumentException ex) { - return; - } - fail(); + assertThrows(IllegalArgumentException.class, () -> FactoryUtils.prototypeFactory(proto)); } public static class Mock1 { diff --git a/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java b/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java index 95bc6b6..7f7da61 100644 --- a/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java @@ -17,7 +17,7 @@ package org.apache.commons.collections4; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Arrays; @@ -49,11 +49,9 @@ public class MultiSetUtilsTest { public void testEmptyMultiSet() { final MultiSet<Integer> empty = MultiSetUtils.emptyMultiSet(); assertEquals(0, empty.size()); - try { - empty.add(55); - fail("Empty multi set must be read-only"); - } catch (final UnsupportedOperationException e) { - } + + assertThrows(UnsupportedOperationException.class, () -> empty.add(55), + "Empty multi set must be read-only"); } /** @@ -64,17 +62,11 @@ public class MultiSetUtilsTest { final MultiSet<String> unmodifiable = MultiSetUtils.unmodifiableMultiSet(multiSet); assertEquals(multiSet, unmodifiable); - try { - unmodifiable.add("a"); - fail("Empty multi set must be read-only"); - } catch (final UnsupportedOperationException e) { - } - - try { - MultiSetUtils.unmodifiableMultiSet(null); - fail("Expecting NPE"); - } catch (final NullPointerException e) { - } + assertThrows(UnsupportedOperationException.class, () -> unmodifiable.add("a"), + "Empty multi set must be read-only"); + + assertThrows(NullPointerException.class, () -> MultiSetUtils.unmodifiableMultiSet(null), + "Expecting NPE"); } /** @@ -97,22 +89,14 @@ public class MultiSetUtilsTest { assertEquals(multiSet.size(), predicated.size()); assertEquals(multiSet.getCount("a"), predicated.getCount("a")); - try { - MultiSetUtils.predicatedMultiSet(null, predicate); - fail("Expecting NPE"); - } catch (final NullPointerException e) { - } - - try { - MultiSetUtils.predicatedMultiSet(multiSet, null); - fail("Expecting NPE"); - } catch (final NullPointerException e) { - } - - try { - MultiSetUtils.predicatedMultiSet(multiSet, object -> object.equals("a")); - fail("Predicate is violated for all elements not being 'a'"); - } catch (final IllegalArgumentException iae) { - } + assertThrows(NullPointerException.class, () -> MultiSetUtils.predicatedMultiSet(null, predicate), + "Expecting NPE"); + + assertThrows(NullPointerException.class, () -> MultiSetUtils.predicatedMultiSet(multiSet, null), + "Expecting NPE"); + + assertThrows(IllegalArgumentException.class, () -> MultiSetUtils.predicatedMultiSet(multiSet, object -> object.equals("a")), + "Predicate is violated for all elements not being 'a'"); } + } diff --git a/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java b/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java index 09d5f71..4b86ae1 100644 --- a/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java @@ -17,8 +17,8 @@ package org.apache.commons.collections4; 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.LinkedList; import java.util.Queue; @@ -32,7 +32,6 @@ import org.junit.jupiter.api.Test; /** * Tests for QueueUtils factory methods. - * */ public class QueueUtilsTest { @@ -45,24 +44,18 @@ public class QueueUtilsTest { public void testSynchronizedQueue() { final Queue<Object> queue = QueueUtils.synchronizedQueue(new LinkedList<>()); assertTrue(queue instanceof SynchronizedQueue, "Returned object should be a SynchronizedQueue."); - try { - QueueUtils.synchronizedQueue(null); - fail("Expecting NullPointerException for null queue."); - } catch (final NullPointerException ex) { - // expected - } + + assertThrows(NullPointerException.class, () -> QueueUtils.synchronizedQueue(null), + "Expecting NullPointerException for null queue."); } @Test public void testUnmodifiableQueue() { final Queue<Object> queue = QueueUtils.unmodifiableQueue(new LinkedList<>()); assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue."); - try { - QueueUtils.unmodifiableQueue(null); - fail("Expecting NullPointerException for null queue."); - } catch (final NullPointerException ex) { - // expected - } + + assertThrows(NullPointerException.class, () -> QueueUtils.unmodifiableQueue(null), + "Expecting NullPointerException for null queue."); assertSame(queue, QueueUtils.unmodifiableQueue(queue), "UnmodifiableQueue shall not be decorated"); } @@ -71,36 +64,24 @@ public class QueueUtilsTest { public void testPredicatedQueue() { final Queue<Object> queue = QueueUtils.predicatedQueue(new LinkedList<>(), truePredicate); assertTrue(queue instanceof PredicatedQueue, "Returned object should be a PredicatedQueue."); - try { - QueueUtils.predicatedQueue(null, truePredicate); - fail("Expecting NullPointerException for null queue."); - } catch (final NullPointerException ex) { - // expected - } - try { - QueueUtils.predicatedQueue(new LinkedList<>(), null); - fail("Expecting NullPointerException for null predicate."); - } catch (final NullPointerException ex) { - // expected - } + + assertThrows(NullPointerException.class, () -> QueueUtils.predicatedQueue(null, truePredicate), + "Expecting NullPointerException for null queue."); + + assertThrows(NullPointerException.class, () -> QueueUtils.predicatedQueue(new LinkedList<>(), null), + "Expecting NullPointerException for null predicate."); } @Test public void testTransformedQueue() { final Queue<Object> queue = QueueUtils.transformingQueue(new LinkedList<>(), nopTransformer); assertTrue(queue instanceof TransformedQueue, "Returned object should be an TransformedQueue."); - try { - QueueUtils.transformingQueue(null, nopTransformer); - fail("Expecting NullPointerException for null queue."); - } catch (final NullPointerException ex) { - // expected - } - try { - QueueUtils.transformingQueue(new LinkedList<>(), null); - fail("Expecting NullPointerException for null transformer."); - } catch (final NullPointerException ex) { - // expected - } + + assertThrows(NullPointerException.class, () -> QueueUtils.transformingQueue(null, nopTransformer), + "Expecting NullPointerException for null queue."); + + assertThrows(NullPointerException.class, () -> QueueUtils.transformingQueue(new LinkedList<>(), null), + "Expecting NullPointerException for null transformer."); } @Test @@ -109,12 +90,8 @@ public class QueueUtilsTest { assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue."); assertTrue(queue.isEmpty(), "Returned queue is not empty."); - try { - queue.add(new Object()); - fail("Expecting UnsupportedOperationException for empty queue."); - } catch (final UnsupportedOperationException ex) { - // expected - } + assertThrows(UnsupportedOperationException.class, () -> queue.add(new Object()), + "Expecting UnsupportedOperationException for empty queue."); } } diff --git a/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java b/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java index 5ab67c3..3d303ca 100644 --- a/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java @@ -16,11 +16,12 @@ */ package org.apache.commons.collections4; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.Collection; @@ -55,18 +56,15 @@ public class TransformerUtilsTest { @Test public void testExceptionTransformer() { - assertNotNull(TransformerUtils.exceptionTransformer()); - assertSame(TransformerUtils.exceptionTransformer(), TransformerUtils.exceptionTransformer()); - try { - TransformerUtils.exceptionTransformer().transform(null); - } catch (final FunctorException ex) { - try { - TransformerUtils.exceptionTransformer().transform(cString); - } catch (final FunctorException ex2) { - return; - } - } - fail(); + assertAll( + () -> assertNotNull(TransformerUtils.exceptionTransformer()), + + () -> assertSame(TransformerUtils.exceptionTransformer(), TransformerUtils.exceptionTransformer()), + + () -> assertThrows(FunctorException.class, () -> TransformerUtils.exceptionTransformer().transform(null)), + + () -> assertThrows(FunctorException.class, () -> TransformerUtils.exceptionTransformer().transform(cString)) + ); } // nullTransformer @@ -115,12 +113,8 @@ public class TransformerUtilsTest { assertNull(TransformerUtils.cloneTransformer().transform(null)); assertEquals(cString, TransformerUtils.cloneTransformer().transform(cString)); assertEquals(cInteger, TransformerUtils.cloneTransformer().transform(cInteger)); - try { - assertEquals(cObject, TransformerUtils.cloneTransformer().transform(cObject)); - } catch (final IllegalArgumentException ex) { - return; - } - fail(); + + assertThrows(IllegalArgumentException.class, () -> assertEquals(cObject, TransformerUtils.cloneTransformer().transform(cObject))); } // mapTransformer @@ -149,12 +143,8 @@ public class TransformerUtilsTest { assertEquals(cObject, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cObject)); assertEquals(cString, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cString)); assertEquals(cInteger, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cInteger)); - try { - TransformerUtils.asTransformer((Closure<Object>) null); - } catch (final NullPointerException ex) { - return; - } - fail(); + + assertThrows(NullPointerException.class, () -> TransformerUtils.asTransformer((Closure<Object>) null)); } // predicateTransformer @@ -166,12 +156,8 @@ public class TransformerUtilsTest { assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cObject)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cString)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cInteger)); - try { - TransformerUtils.asTransformer((Predicate<Object>) null); - } catch (final IllegalArgumentException ex) { - return; - } - fail(); + + assertThrows(IllegalArgumentException.class, () -> TransformerUtils.asTransformer((Predicate<Object>) null)); } // factoryTransformer @@ -183,12 +169,8 @@ public class TransformerUtilsTest { assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cObject)); assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cString)); assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cInteger)); - try { - TransformerUtils.asTransformer((Factory<Object>) null); - } catch (final NullPointerException ex) { - return; - } - fail(); + + assertThrows(NullPointerException.class, () -> TransformerUtils.asTransformer((Factory<Object>) null)); } // chainedTransformer @@ -211,29 +193,22 @@ public class TransformerUtilsTest { assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer()); assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(Collections.<Transformer<Object, Object>>emptyList())); - try { - TransformerUtils.chainedTransformer(null, null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.chainedTransformer((Transformer[]) null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.chainedTransformer((Collection<Transformer<Object, Object>>) null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.chainedTransformer(null, null); - fail(); - } catch (final NullPointerException ex) {} - try { - coll = new ArrayList<>(); - coll.add(null); - coll.add(null); - TransformerUtils.chainedTransformer(coll); - fail(); - } catch (final NullPointerException ex) {} + assertAll( + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer(null, null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer((Transformer[]) null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer((Collection<Transformer<Object, Object>>) null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer(null, null)), + + () -> assertThrows(NullPointerException.class, () -> { + Collection<Transformer<Object, Object>> coll1 = new ArrayList<>(); + coll1.add(null); + coll1.add(null); + TransformerUtils.chainedTransformer(coll1); + }) + ); } // ifTransformer @@ -258,22 +233,15 @@ public class TransformerUtilsTest { assertEquals("C", TransformerUtils.ifTransformer(equalsAPredicate, c).transform("A")); assertEquals("B", TransformerUtils.ifTransformer(equalsAPredicate, c).transform("B")); - try { - TransformerUtils.ifTransformer(null, null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.ifTransformer(TruePredicate.truePredicate(), null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.ifTransformer(null, ConstantTransformer.constantTransformer("A")); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.ifTransformer(null, null, null); - fail(); - } catch (final NullPointerException ex) {} + assertAll( + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(null, null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(TruePredicate.truePredicate(), null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(null, ConstantTransformer.constantTransformer("A"))), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(null, null, null)) + ); } // switchTransformer @@ -318,28 +286,19 @@ public class TransformerUtilsTest { map.put(null, null); assertEquals(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(map)); - try { - TransformerUtils.switchTransformer(null, null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.switchTransformer(null, (Transformer[]) null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.switchTransformer(null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.switchTransformer(new Predicate[2], new Transformer[2]); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.switchTransformer( - new Predicate[] { TruePredicate.truePredicate() }, - new Transformer[] { a, b }); - fail(); - } catch (final IllegalArgumentException ex) {} + assertAll( + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(null, null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(null, (Transformer[]) null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(null)), + + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(new Predicate[2], new Transformer[2])), + + () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.switchTransformer( + new Predicate[]{TruePredicate.truePredicate()}, + new Transformer[]{a, b})) + ); } // switchMapTransformer @@ -365,10 +324,7 @@ public class TransformerUtilsTest { map.put(null, null); assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(map)); - try { - TransformerUtils.switchMapTransformer(null); - fail(); - } catch (final NullPointerException ex) {} + assertThrows(NullPointerException.class, () -> TransformerUtils.switchMapTransformer(null)); } // invokerTransformer @@ -382,14 +338,11 @@ public class TransformerUtilsTest { assertEquals(1, TransformerUtils.invokerTransformer("size").transform(list)); assertNull(TransformerUtils.invokerTransformer("size").transform(null)); - try { - TransformerUtils.invokerTransformer(null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object()); - fail(); - } catch (final FunctorException ex) {} + assertAll( + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.invokerTransformer(null)), + + () -> assertThrows(FunctorException.class, () -> TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object())) + ); } // invokerTransformer2 @@ -406,27 +359,18 @@ public class TransformerUtilsTest { assertNull(TransformerUtils.invokerTransformer("contains", new Class[]{Object.class}, new Object[]{cString}).transform(null)); - try { - TransformerUtils.invokerTransformer(null, null, null); - fail(); - } catch (final NullPointerException ex) {} - try { - TransformerUtils.invokerTransformer("noSuchMethod", new Class[] { Object.class }, - new Object[] { cString }).transform(new Object()); - fail(); - } catch (final FunctorException ex) {} - try { - TransformerUtils.invokerTransformer("badArgs", null, new Object[] { cString }); - fail(); - } catch (final IllegalArgumentException ex) {} - try { - TransformerUtils.invokerTransformer("badArgs", new Class[] { Object.class }, null); - fail(); - } catch (final IllegalArgumentException ex) {} - try { - TransformerUtils.invokerTransformer("badArgs", new Class[] {}, new Object[] { cString }); - fail(); - } catch (final IllegalArgumentException ex) {} + assertAll( + () -> assertThrows(NullPointerException.class, () -> TransformerUtils.invokerTransformer(null, null, null)), + + () -> assertThrows(FunctorException.class, () -> TransformerUtils.invokerTransformer("noSuchMethod", new Class[]{Object.class}, + new Object[]{cString}).transform(new Object())), + + () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.invokerTransformer("badArgs", null, new Object[]{cString})), + + () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.invokerTransformer("badArgs", new Class[]{Object.class}, null)), + + () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.invokerTransformer("badArgs", new Class[]{}, new Object[]{cString})) + ); } // stringValueTransformer @@ -447,20 +391,17 @@ public class TransformerUtilsTest { @Test public void testInstantiateTransformerNull() { - try { - TransformerUtils.instantiateTransformer(null, new Object[] { "str" }); - fail(); - } catch (final IllegalArgumentException ex) {} - try { - TransformerUtils.instantiateTransformer(new Class[] {}, new Object[] { "str" }); - fail(); - } catch (final IllegalArgumentException ex) {} + + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.instantiateTransformer(null, new Object[]{"str"})), + + () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.instantiateTransformer(new Class[]{}, new Object[]{"str"})) + ); Transformer<Class<?>, Object> trans = TransformerUtils.instantiateTransformer(new Class[] { Long.class }, new Object[] { null }); - try { - trans.transform(String.class); - fail(); - } catch (final FunctorException ex) {} + + Transformer<Class<?>, Object> finalTrans = trans; + assertThrows(FunctorException.class, () -> finalTrans.transform(String.class)); trans = TransformerUtils.instantiateTransformer(); assertEquals("", trans.transform(String.class)); @@ -488,4 +429,5 @@ public class TransformerUtilsTest { TestUtils.assertSameAfterSerialization("Singleton pattern broken for " + original.getClass(), original); } } + } diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java index c6c6a03..8e87708 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java @@ -28,9 +28,10 @@ import java.util.Set; import java.util.function.IntConsumer; import java.util.stream.Collectors; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; /** * Tests for the {@link IndexFilters}. @@ -53,26 +54,13 @@ public class IndexFilterTest { final ArrayList<Integer> actual = new ArrayList<>(); final IntConsumer consumer = actual::add; - try { - IndexFilters.distinctIndexes(null, shape, consumer); - fail("null hasher"); - } catch (final NullPointerException expected) { - // Ignore - } + assertAll( + () -> assertThrows(NullPointerException.class, () -> IndexFilters.distinctIndexes(null, shape, consumer), "null hasher"), - try { - IndexFilters.distinctIndexes(hasher, null, consumer); - fail("null shape"); - } catch (final NullPointerException expected) { - // Ignore - } + () -> assertThrows(NullPointerException.class, () -> IndexFilters.distinctIndexes(hasher, null, consumer), "null shape"), - try { - IndexFilters.distinctIndexes(hasher, shape, null); - fail("null consumer"); - } catch (final NullPointerException expected) { - // Ignore - } + () -> assertThrows(NullPointerException.class, () -> IndexFilters.distinctIndexes(hasher, shape, null), "null consumer") + ); // All OK together IndexFilters.distinctIndexes(hasher, shape, consumer); diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherBuilderTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherBuilderTest.java index afbd6d8..5e925b9 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherBuilderTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherBuilderTest.java @@ -18,8 +18,8 @@ package org.apache.commons.collections4.bloomfilter.hasher; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.nio.charset.StandardCharsets; import java.util.NoSuchElementException; @@ -65,12 +65,8 @@ public class DynamicHasherBuilderTest { final OfInt iter = hasher.iterator(shape); assertFalse(iter.hasNext()); - try { - iter.nextInt(); - fail("Should have thrown NoSuchElementException"); - } catch (final NoSuchElementException ignore) { - // do nothing - } + + assertThrows(NoSuchElementException.class, () -> iter.nextInt(), "Should have thrown NoSuchElementException"); } /** @@ -129,4 +125,5 @@ public class DynamicHasherBuilderTest { public void setup() { builder = new DynamicHasher.Builder(hf); } + } diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherTest.java index 7b2bbba..10de0b5 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/DynamicHasherTest.java @@ -18,8 +18,8 @@ package org.apache.commons.collections4.bloomfilter.hasher; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.nio.charset.StandardCharsets; import java.util.NoSuchElementException; @@ -34,6 +34,7 @@ import org.junit.jupiter.api.Test; * Tests the {@link DynamicHasher}. */ public class DynamicHasherTest { + private DynamicHasher.Builder builder; private Shape shape; @@ -63,6 +64,7 @@ public class DynamicHasherTest { public Signedness getSignedness() { return Signedness.SIGNED; } + }; /** @@ -110,12 +112,8 @@ public class DynamicHasherTest { assertEquals(element, iter.nextInt()); } assertFalse(iter.hasNext()); - try { - iter.next(); - fail("Should have thrown NoSuchElementException"); - } catch (final NoSuchElementException ignore) { - // do nothing - } + + assertThrows(NoSuchElementException.class, () -> iter.next(), "Should have thrown NoSuchElementException"); } /** @@ -126,11 +124,7 @@ public class DynamicHasherTest { final Hasher hasher = builder.with("Hello", StandardCharsets.UTF_8).build(); - try { - hasher.iterator(new Shape(testFunction, 3, 72, 17)); - fail("Should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // do nothing - } + assertThrows(IllegalArgumentException.class, () -> hasher.iterator(new Shape(testFunction, 3, 72, 17)), "Should have thrown IllegalArgumentException"); } + } diff --git a/src/test/java/org/apache/commons/collections4/comparators/BooleanComparatorTest.java b/src/test/java/org/apache/commons/collections4/comparators/BooleanComparatorTest.java index 387b514..f24244e 100644 --- a/src/test/java/org/apache/commons/collections4/comparators/BooleanComparatorTest.java +++ b/src/test/java/org/apache/commons/collections4/comparators/BooleanComparatorTest.java @@ -23,11 +23,12 @@ import java.util.List; import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Tests for {@link BooleanComparator}. - * */ @SuppressWarnings("boxing") public class BooleanComparatorTest extends AbstractComparatorTest<Boolean> { @@ -130,35 +131,18 @@ public class BooleanComparatorTest extends AbstractComparatorTest<Boolean> { protected void nullArgumentTests(final BooleanComparator comp) { assertNotNull(comp); - try { - comp.compare(null, null); - fail("Expected NullPointerException"); - } catch (final NullPointerException e) { - // expected - } - try { - comp.compare(Boolean.TRUE, null); - fail("Expected NullPointerException"); - } catch (final NullPointerException e) { - // expected - } - try { - comp.compare(Boolean.FALSE, null); - fail("Expected NullPointerException"); - } catch (final NullPointerException e) { - // expected - } - try { - comp.compare(null, Boolean.TRUE); - fail("Expected NullPointerException"); - } catch (final NullPointerException e) { - // expected - } - try { - comp.compare(null, Boolean.FALSE); - fail("Expected NullPointerException"); - } catch (final NullPointerException e) { - // expected - } + + assertAll( + () -> assertThrows(NullPointerException.class, () -> comp.compare(null, null), "Expected NullPointerException"), + + () -> assertThrows(NullPointerException.class, () -> comp.compare(Boolean.TRUE, null), "Expected NullPointerException"), + + () -> assertThrows(NullPointerException.class, () -> comp.compare(Boolean.FALSE, null), "Expected NullPointerException"), + + () -> assertThrows(NullPointerException.class, () -> comp.compare(null, Boolean.TRUE), "Expected NullPointerException"), + + () -> assertThrows(NullPointerException.class, () -> comp.compare(null, Boolean.FALSE), "Expected NullPointerException") + ); } + } diff --git a/src/test/java/org/apache/commons/collections4/comparators/FixedOrderComparatorTest.java b/src/test/java/org/apache/commons/collections4/comparators/FixedOrderComparatorTest.java index 193569c..db24b1b 100644 --- a/src/test/java/org/apache/commons/collections4/comparators/FixedOrderComparatorTest.java +++ b/src/test/java/org/apache/commons/collections4/comparators/FixedOrderComparatorTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.comparators; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; @@ -26,7 +28,6 @@ import org.junit.Test; /** * Test class for FixedOrderComparator. - * */ public class FixedOrderComparatorTest extends AbstractComparatorTest<String> { @@ -143,36 +144,25 @@ public class FixedOrderComparatorTest extends AbstractComparatorTest<String> { assertFalse(comparator.isLocked()); comparator.compare("New York", "Tokyo"); assertTrue(comparator.isLocked()); - try { - comparator.add("Minneapolis"); - fail("Should have thrown an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // success -- ignore - } - try { - comparator.addAsEqual("New York", "Minneapolis"); - fail("Should have thrown an UnsupportedOperationException"); - } catch (final UnsupportedOperationException e) { - // success -- ignore - } + assertThrows(UnsupportedOperationException.class, () -> comparator.add("Minneapolis"), + "Should have thrown an UnsupportedOperationException"); + + assertThrows(UnsupportedOperationException.class, () -> comparator.addAsEqual("New York", "Minneapolis"), + "Should have thrown an UnsupportedOperationException"); } @Test public void testUnknownObjectBehavior() { FixedOrderComparator<String> comparator = new FixedOrderComparator<>(topCities); - try { - comparator.compare("New York", "Minneapolis"); - fail("Should have thrown a IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - // success-- ignore - } - try { - comparator.compare("Minneapolis", "New York"); - fail("Should have thrown a IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - // success-- ignore - } + + FixedOrderComparator<String> finalComparator = comparator; + assertThrows(IllegalArgumentException.class, () -> finalComparator.compare("New York", "Minneapolis"), + "Should have thrown a IllegalArgumentException"); + + assertThrows(IllegalArgumentException.class, () -> finalComparator.compare("Minneapolis", "New York"), + "Should have thrown a IllegalArgumentException"); + assertEquals(FixedOrderComparator.UnknownObjectBehavior.EXCEPTION, comparator.getUnknownObjectBehavior()); comparator = new FixedOrderComparator<>(topCities); @@ -195,7 +185,6 @@ public class FixedOrderComparatorTest extends AbstractComparatorTest<String> { assertEquals( 1, comparator.compare("Minneapolis", "New York")); assertEquals(-1, comparator.compare("New York", "Minneapolis")); assertEquals( 0, comparator.compare("Minneapolis", "St Paul")); - } // @@ -240,4 +229,5 @@ public class FixedOrderComparatorTest extends AbstractComparatorTest<String> { assertEquals(orderedObjects[i], keys[i]); } } + }