You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2021/01/15 22:01:30 UTC

[commons-collections] 01/03: [COLLECTIONS-780] Use assertThrows V2

This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit 230076f1f4df9c861eeb080198a909429d48c269
Author: Arturo Bernal <ar...@gmail.com>
AuthorDate: Thu Jan 14 08:20:32 2021 +0100

    [COLLECTIONS-780] Use assertThrows V2
---
 .../commons/collections4/FactoryUtilsTest.java     |  15 +-
 .../commons/collections4/PredicateUtilsTest.java   | 155 +++++++++++----------
 .../hasher/HashFunctionValidatorTest.java          |   7 +-
 .../functors/AbstractPredicateTest.java            |   6 +-
 .../collections4/functors/NullPredicateTest.java   |   2 +-
 5 files changed, 95 insertions(+), 90 deletions(-)

diff --git a/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java b/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java
index d57cfa8..df8e9a7 100644
--- a/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
 import java.io.Serializable;
@@ -31,7 +32,7 @@ import java.util.TimeZone;
 
 import org.apache.commons.collections4.functors.ConstantFactory;
 import org.apache.commons.collections4.functors.ExceptionFactory;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
 /**
  * Tests the org.apache.commons.collections.FactoryUtils class.
@@ -214,9 +215,9 @@ public class FactoryUtilsTest {
     // instantiateFactory
     //------------------------------------------------------------------
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void instantiateFactoryNull() {
-        FactoryUtils.instantiateFactory(null);
+        assertThrows(NullPointerException.class, () -> FactoryUtils.instantiateFactory(null));
     }
 
     @Test
@@ -229,14 +230,14 @@ public class FactoryUtilsTest {
         assertEquals(1, created.getValue());
     }
 
-    @Test(expected=IllegalArgumentException.class)
+    @Test
     public void instantiateFactoryMismatch() {
-        FactoryUtils.instantiateFactory(Date.class, null, new Object[] {null});
+        assertThrows(IllegalArgumentException.class, () -> FactoryUtils.instantiateFactory(Date.class, null, new Object[] {null}));
     }
 
-    @Test(expected=IllegalArgumentException.class)
+    @Test
     public void instantiateFactoryNoConstructor() {
-        FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class}, new Object[] {null});
+        assertThrows(IllegalArgumentException.class, () -> FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class}, new Object[] {null}));
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java b/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java
index af558a6..087fa74 100644
--- a/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java
@@ -36,7 +36,7 @@ import org.apache.commons.collections4.functors.FalsePredicate;
 import org.apache.commons.collections4.functors.NotNullPredicate;
 import org.apache.commons.collections4.functors.NullPredicate;
 import org.apache.commons.collections4.functors.TruePredicate;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
 /**
  * Tests the PredicateUtils class.
@@ -130,9 +130,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertFalse(PredicateUtils.notPredicate(truePredicate()).evaluate(cInteger));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNotPredicateEx() {
-        PredicateUtils.notPredicate(null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.notPredicate(null));
     }
 
     // andPredicate
@@ -146,9 +146,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertFalse(PredicateUtils.andPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAndPredicateEx() {
-        PredicateUtils.andPredicate(null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.andPredicate(null, null));
     }
 
     // allPredicate
@@ -192,25 +192,25 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertPredicateTrue(AllPredicate.allPredicate(coll), null);
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAllPredicateEx1() {
-        AllPredicate.allPredicate((Predicate<Object>[]) null);
+        assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate((Predicate<Object>[]) null));
     }
 
     @SuppressWarnings("unchecked")
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAllPredicateEx2() {
-        AllPredicate.<Object>allPredicate(new Predicate[] { null });
+        assertThrows(NullPointerException.class, () -> AllPredicate.<Object>allPredicate(new Predicate[] { null }));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAllPredicateEx3() {
-        AllPredicate.allPredicate(null, null);
+        assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate(null, null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAllPredicateEx4() {
-        AllPredicate.allPredicate((Collection<Predicate<Object>>) null);
+        assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate((Collection<Predicate<Object>>) null));
     }
 
     @Test
@@ -218,12 +218,12 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         AllPredicate.allPredicate(Collections.emptyList());
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAllPredicateEx6() {
         final Collection<Predicate<Object>> coll = new ArrayList<>();
         coll.add(null);
         coll.add(null);
-        AllPredicate.allPredicate(coll);
+        assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate(coll));
     }
 
     // orPredicate
@@ -237,9 +237,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertFalse(PredicateUtils.orPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testOrPredicateEx() {
-        PredicateUtils.orPredicate(null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.orPredicate(null, null));
     }
 
     // anyPredicate
@@ -284,25 +284,25 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertPredicateFalse(PredicateUtils.anyPredicate(coll), null);
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAnyPredicateEx1() {
-        PredicateUtils.anyPredicate((Predicate<Object>[]) null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate((Predicate<Object>[]) null));
     }
 
     @SuppressWarnings("unchecked")
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAnyPredicateEx2() {
-        PredicateUtils.anyPredicate(new Predicate[] {null});
+        assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate(new Predicate[] {null}));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAnyPredicateEx3() {
-        PredicateUtils.anyPredicate(null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate(null, null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAnyPredicateEx4() {
-        PredicateUtils.anyPredicate((Collection<Predicate<Object>>) null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate((Collection<Predicate<Object>>) null));
     }
 
     @Test
@@ -310,12 +310,12 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         PredicateUtils.anyPredicate(Collections.emptyList());
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAnyPredicateEx6() {
         final Collection<Predicate<Object>> coll = new ArrayList<>();
         coll.add(null);
         coll.add(null);
-        PredicateUtils.anyPredicate(coll);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate(coll));
     }
 
     // eitherPredicate
@@ -329,9 +329,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertFalse(PredicateUtils.eitherPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testEitherPredicateEx() {
-        PredicateUtils.eitherPredicate(null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.eitherPredicate(null, null));
     }
 
     // onePredicate
@@ -377,25 +377,25 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertPredicateFalse(PredicateUtils.onePredicate(coll), null);
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testOnePredicateEx1() {
-        PredicateUtils.onePredicate((Predicate<Object>[]) null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate((Predicate<Object>[]) null));
     }
 
     @SuppressWarnings("unchecked")
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testOnePredicateEx2() {
-        PredicateUtils.onePredicate(new Predicate[] {null});
+        assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate(new Predicate[] {null}));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testOnePredicateEx3() {
-        PredicateUtils.onePredicate(null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate(null, null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testOnePredicateEx4() {
-        PredicateUtils.onePredicate((Collection<Predicate<Object>>) null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate((Collection<Predicate<Object>>) null));
     }
 
     @SuppressWarnings("unchecked")
@@ -404,9 +404,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         PredicateUtils.onePredicate(Collections.EMPTY_LIST);
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testOnePredicateEx6() {
-        PredicateUtils.onePredicate(Arrays.asList(null, null));
+        assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate(Arrays.asList(null, null)));
     }
 
     // neitherPredicate
@@ -420,9 +420,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertTrue(PredicateUtils.neitherPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNeitherPredicateEx() {
-        PredicateUtils.neitherPredicate(null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.neitherPredicate(null, null));
     }
 
     // nonePredicate
@@ -466,26 +466,26 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertPredicateTrue(PredicateUtils.nonePredicate(coll), null);
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNonePredicateEx1() {
-        PredicateUtils.nonePredicate((Predicate<Object>[]) null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate((Predicate<Object>[]) null));
     }
 
     @SuppressWarnings("unchecked")
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNonePredicateEx2() {
-        PredicateUtils.nonePredicate(new Predicate[] {null});
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate(new Predicate[] {null}));
     }
 
     @SuppressWarnings("unchecked")
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNonePredicateEx3() {
-        PredicateUtils.nonePredicate(null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate(null, null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNonePredicateEx4() {
-        PredicateUtils.nonePredicate((Collection<Predicate<Object>>) null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate((Collection<Predicate<Object>>) null));
     }
 
     @Test
@@ -493,12 +493,12 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         PredicateUtils.nonePredicate(Collections.emptyList());
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNonePredicateEx6() {
         final Collection<Predicate<Object>> coll = new ArrayList<>();
         coll.add(null);
         coll.add(null);
-        PredicateUtils.nonePredicate(coll);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate(coll));
     }
 
     // instanceofPredicate
@@ -536,14 +536,14 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertTrue(PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(true));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testAsPredicateTransformerEx1() {
-        PredicateUtils.asPredicate(null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.asPredicate(null));
     }
 
-    @Test(expected=FunctorException.class)
+    @Test
     public void testAsPredicateTransformerEx2() {
-        PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(null);
+        assertThrows(FunctorException.class, () -> PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(null));
     }
 
     // invokerPredicate
@@ -557,19 +557,19 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertFalse(PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testInvokerPredicateEx1() {
-        PredicateUtils.invokerPredicate(null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.invokerPredicate(null));
     }
 
-    @Test(expected=FunctorException.class)
+    @Test
     public void testInvokerPredicateEx2() {
-        PredicateUtils.invokerPredicate("isEmpty").evaluate(null);
+        assertThrows(FunctorException.class, () -> PredicateUtils.invokerPredicate("isEmpty").evaluate(null));
     }
 
-    @Test(expected=FunctorException.class)
+    @Test
     public void testInvokerPredicateEx3() {
-        PredicateUtils.invokerPredicate("noSuchMethod").evaluate(new Object());
+        assertThrows(FunctorException.class, () -> PredicateUtils.invokerPredicate("noSuchMethod").evaluate(new Object()));
     }
 
     // invokerPredicate2
@@ -585,34 +585,35 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
                 "contains", new Class[]{Object.class}, new Object[]{cString}).evaluate(list));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testInvokerPredicate2Ex1() {
-        PredicateUtils.invokerPredicate(null, null, null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.invokerPredicate(null, null, null));
     }
 
-    @Test(expected=FunctorException.class)
+    @Test
     public void testInvokerPredicate2Ex2() {
-        PredicateUtils.invokerPredicate("contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(null);
+        assertThrows(FunctorException.class, () -> PredicateUtils.
+                invokerPredicate("contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(null));
     }
 
-    @Test(expected=FunctorException.class)
+    @Test
     public void testInvokerPredicate2Ex3() {
-        PredicateUtils.invokerPredicate(
-                "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).evaluate(new Object());
+        assertThrows(FunctorException.class, () -> PredicateUtils.invokerPredicate(
+                "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).evaluate(new Object()));
     }
 
     // nullIsException
     //------------------------------------------------------------------
 
-    @Test(expected=FunctorException.class)
+    @Test
     public void testNullIsExceptionPredicate() {
         assertTrue(PredicateUtils.nullIsExceptionPredicate(truePredicate()).evaluate(new Object()));
-        PredicateUtils.nullIsExceptionPredicate(TruePredicate.truePredicate()).evaluate(null);
+        assertThrows(FunctorException.class, () -> PredicateUtils.nullIsExceptionPredicate(TruePredicate.truePredicate()).evaluate(null));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNullIsExceptionPredicateEx1() {
-        PredicateUtils.nullIsExceptionPredicate(null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nullIsExceptionPredicate(null));
     }
 
     // nullIsTrue
@@ -625,9 +626,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertFalse(PredicateUtils.nullIsTruePredicate(FalsePredicate.falsePredicate()).evaluate(new Object()));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNullIsTruePredicateEx1() {
-        PredicateUtils.nullIsTruePredicate(null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nullIsTruePredicate(null));
     }
 
     // nullIsFalse
@@ -640,9 +641,9 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         assertFalse(PredicateUtils.nullIsFalsePredicate(FalsePredicate.falsePredicate()).evaluate(new Object()));
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test
     public void testNullIsFalsePredicateEx1() {
-        PredicateUtils.nullIsFalsePredicate(null);
+        assertThrows(NullPointerException.class, () -> PredicateUtils.nullIsFalsePredicate(null));
     }
 
     // transformed
@@ -696,3 +697,5 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
         return truePredicate();  //Just return something to satisfy super class.
     }
 }
+
+
diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/HashFunctionValidatorTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/HashFunctionValidatorTest.java
index 625cb64..bd9532a 100644
--- a/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/HashFunctionValidatorTest.java
+++ b/src/test/java/org/apache/commons/collections4/bloomfilter/hasher/HashFunctionValidatorTest.java
@@ -17,11 +17,12 @@
 package org.apache.commons.collections4.bloomfilter.hasher;
 
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity.ProcessType;
 import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity.Signedness;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
 /**
  * Tests of the {@link HashFunctionValidator}.
@@ -108,12 +109,12 @@ public class HashFunctionValidatorTest {
     /**
      * Test the check method throws when the two hash functions are not equal.
      */
-    @Test(expected=IllegalArgumentException.class)
+    @Test
     public void testCheckThrows() {
         final HashFunctionIdentityImpl impl1 = new HashFunctionIdentityImpl("Testing Suite", "impl1", Signedness.SIGNED,
             ProcessType.CYCLIC, 300L);
         final HashFunctionIdentityImpl impl2 = new HashFunctionIdentityImpl("Testing Suite", "impl1", Signedness.UNSIGNED,
             ProcessType.CYCLIC, 300L);
-        HashFunctionValidator.checkAreEqual(impl1, impl2);
+        assertThrows(IllegalArgumentException.class, () -> HashFunctionValidator.checkAreEqual(impl1, impl2));
     }
 }
diff --git a/src/test/java/org/apache/commons/collections4/functors/AbstractPredicateTest.java b/src/test/java/org/apache/commons/collections4/functors/AbstractPredicateTest.java
index e26564c..68e46e9 100644
--- a/src/test/java/org/apache/commons/collections4/functors/AbstractPredicateTest.java
+++ b/src/test/java/org/apache/commons/collections4/functors/AbstractPredicateTest.java
@@ -18,15 +18,15 @@ package org.apache.commons.collections4.functors;
 
 import org.apache.commons.collections4.Predicate;
 import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 
 public abstract class AbstractPredicateTest {
     protected Object cObject;
     protected String cString;
     protected Integer cInteger;
 
-    @Before
+    @BeforeEach
     public void initializeTestObjects() throws Exception {
         cObject = new Object();
         cString = "Hello";
diff --git a/src/test/java/org/apache/commons/collections4/functors/NullPredicateTest.java b/src/test/java/org/apache/commons/collections4/functors/NullPredicateTest.java
index 652f7c8..15abe48 100644
--- a/src/test/java/org/apache/commons/collections4/functors/NullPredicateTest.java
+++ b/src/test/java/org/apache/commons/collections4/functors/NullPredicateTest.java
@@ -20,7 +20,7 @@ import static org.apache.commons.collections4.functors.NullPredicate.nullPredica
 import static org.junit.Assert.assertSame;
 
 import org.apache.commons.collections4.Predicate;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
 public class NullPredicateTest extends AbstractPredicateTest {
     @Test