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
This commit is contained in:
John Patrick 2022-03-04 13:44:25 +00:00 committed by GitHub
parent 4d06b47d55
commit 2a2a534c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 192 additions and 365 deletions

View File

@ -16,9 +16,10 @@
*/ */
package org.apache.commons.collections4; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.Comparator; import java.util.Comparator;
@ -26,9 +27,9 @@ import org.junit.jupiter.api.Test;
/** /**
* Tests ComparatorUtils. * Tests ComparatorUtils.
*
*/ */
public class ComparatorUtilsTest { public class ComparatorUtilsTest {
@Test @Test
public void booleanComparator() { public void booleanComparator() {
Comparator<Boolean> comp = ComparatorUtils.booleanComparator(true); 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(1), ComparatorUtils.max(1, 10, reversed));
assertEquals(Integer.valueOf(-10), ComparatorUtils.max(10, -10, reversed)); assertEquals(Integer.valueOf(-10), ComparatorUtils.max(10, -10, reversed));
try { assertAll(
ComparatorUtils.max(1, null, null); () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.max(1, null, null),
fail("expecting NullPointerException"); "expecting NullPointerException"),
} catch (final NullPointerException npe) {
// expected
}
try { () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.max(null, 10, null),
ComparatorUtils.max(null, 10, null); "expecting NullPointerException")
fail("expecting NullPointerException"); );
} catch (final NullPointerException npe) {
// expected
}
} }
@Test @Test
@ -89,19 +84,13 @@ public class ComparatorUtilsTest {
assertEquals(Integer.valueOf(10), ComparatorUtils.min(1, 10, reversed)); assertEquals(Integer.valueOf(10), ComparatorUtils.min(1, 10, reversed));
assertEquals(Integer.valueOf(10), ComparatorUtils.min(10, -10, reversed)); assertEquals(Integer.valueOf(10), ComparatorUtils.min(10, -10, reversed));
try { assertAll(
ComparatorUtils.min(1, null, null); () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.min(1, null, null),
fail("expecting NullPointerException"); "expecting NullPointerException"),
} catch (final NullPointerException npe) {
// expected
}
try { () -> assertThrows(NullPointerException.class, () -> ComparatorUtils.min(null, 10, null),
ComparatorUtils.min(null, 10, null); "expecting NullPointerException")
fail("expecting NullPointerException"); );
} catch (final NullPointerException npe) {
// expected
}
} }
@Test @Test

View File

@ -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.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; 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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
@ -48,16 +47,8 @@ public class FactoryUtilsTest {
public void testExceptionFactory() { public void testExceptionFactory() {
assertNotNull(FactoryUtils.exceptionFactory()); assertNotNull(FactoryUtils.exceptionFactory());
assertSame(FactoryUtils.exceptionFactory(), FactoryUtils.exceptionFactory()); assertSame(FactoryUtils.exceptionFactory(), FactoryUtils.exceptionFactory());
try {
FactoryUtils.exceptionFactory().create(); assertThrows(FunctorException.class, () -> FactoryUtils.exceptionFactory().create());
} catch (final FunctorException ex) {
try {
FactoryUtils.exceptionFactory().create();
} catch (final FunctorException ex2) {
return;
}
}
fail();
} }
// nullFactory // nullFactory
@ -134,24 +125,15 @@ public class FactoryUtilsTest {
final Mock2 proto = new Mock2(new Object()); final Mock2 proto = new Mock2(new Object());
final Factory<Object> factory = FactoryUtils.<Object>prototypeFactory(proto); final Factory<Object> factory = FactoryUtils.<Object>prototypeFactory(proto);
assertNotNull(factory); assertNotNull(factory);
try {
factory.create(); final FunctorException thrown = assertThrows(FunctorException.class, () -> factory.create());
} catch (final FunctorException ex) { assertTrue(thrown.getCause() instanceof IOException);
assertTrue(ex.getCause() instanceof IOException);
return;
}
fail();
} }
@Test @Test
public void testPrototypeFactoryPublicBad() { public void testPrototypeFactoryPublicBad() {
final Object proto = new Object(); final Object proto = new Object();
try { assertThrows(IllegalArgumentException.class, () -> FactoryUtils.prototypeFactory(proto));
FactoryUtils.prototypeFactory(proto);
} catch (final IllegalArgumentException ex) {
return;
}
fail();
} }
public static class Mock1 { public static class Mock1 {

View File

@ -17,7 +17,7 @@
package org.apache.commons.collections4; package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertEquals; 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; import java.util.Arrays;
@ -49,11 +49,9 @@ public class MultiSetUtilsTest {
public void testEmptyMultiSet() { public void testEmptyMultiSet() {
final MultiSet<Integer> empty = MultiSetUtils.emptyMultiSet(); final MultiSet<Integer> empty = MultiSetUtils.emptyMultiSet();
assertEquals(0, empty.size()); assertEquals(0, empty.size());
try {
empty.add(55); assertThrows(UnsupportedOperationException.class, () -> empty.add(55),
fail("Empty multi set must be read-only"); "Empty multi set must be read-only");
} catch (final UnsupportedOperationException e) {
}
} }
/** /**
@ -64,17 +62,11 @@ public class MultiSetUtilsTest {
final MultiSet<String> unmodifiable = MultiSetUtils.unmodifiableMultiSet(multiSet); final MultiSet<String> unmodifiable = MultiSetUtils.unmodifiableMultiSet(multiSet);
assertEquals(multiSet, unmodifiable); assertEquals(multiSet, unmodifiable);
try { assertThrows(UnsupportedOperationException.class, () -> unmodifiable.add("a"),
unmodifiable.add("a"); "Empty multi set must be read-only");
fail("Empty multi set must be read-only");
} catch (final UnsupportedOperationException e) {
}
try { assertThrows(NullPointerException.class, () -> MultiSetUtils.unmodifiableMultiSet(null),
MultiSetUtils.unmodifiableMultiSet(null); "Expecting NPE");
fail("Expecting NPE");
} catch (final NullPointerException e) {
}
} }
/** /**
@ -97,22 +89,14 @@ public class MultiSetUtilsTest {
assertEquals(multiSet.size(), predicated.size()); assertEquals(multiSet.size(), predicated.size());
assertEquals(multiSet.getCount("a"), predicated.getCount("a")); assertEquals(multiSet.getCount("a"), predicated.getCount("a"));
try { assertThrows(NullPointerException.class, () -> MultiSetUtils.predicatedMultiSet(null, predicate),
MultiSetUtils.predicatedMultiSet(null, predicate); "Expecting NPE");
fail("Expecting NPE");
} catch (final NullPointerException e) {
}
try { assertThrows(NullPointerException.class, () -> MultiSetUtils.predicatedMultiSet(multiSet, null),
MultiSetUtils.predicatedMultiSet(multiSet, null); "Expecting NPE");
fail("Expecting NPE");
} catch (final NullPointerException e) {
}
try { assertThrows(IllegalArgumentException.class, () -> MultiSetUtils.predicatedMultiSet(multiSet, object -> object.equals("a")),
MultiSetUtils.predicatedMultiSet(multiSet, object -> object.equals("a")); "Predicate is violated for all elements not being 'a'");
fail("Predicate is violated for all elements not being 'a'");
} catch (final IllegalArgumentException iae) {
}
} }
} }

View File

@ -17,8 +17,8 @@
package org.apache.commons.collections4; package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertSame; 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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
@ -32,7 +32,6 @@ import org.junit.jupiter.api.Test;
/** /**
* Tests for QueueUtils factory methods. * Tests for QueueUtils factory methods.
*
*/ */
public class QueueUtilsTest { public class QueueUtilsTest {
@ -45,24 +44,18 @@ public class QueueUtilsTest {
public void testSynchronizedQueue() { public void testSynchronizedQueue() {
final Queue<Object> queue = QueueUtils.synchronizedQueue(new LinkedList<>()); final Queue<Object> queue = QueueUtils.synchronizedQueue(new LinkedList<>());
assertTrue(queue instanceof SynchronizedQueue, "Returned object should be a SynchronizedQueue."); assertTrue(queue instanceof SynchronizedQueue, "Returned object should be a SynchronizedQueue.");
try {
QueueUtils.synchronizedQueue(null); assertThrows(NullPointerException.class, () -> QueueUtils.synchronizedQueue(null),
fail("Expecting NullPointerException for null queue."); "Expecting NullPointerException for null queue.");
} catch (final NullPointerException ex) {
// expected
}
} }
@Test @Test
public void testUnmodifiableQueue() { public void testUnmodifiableQueue() {
final Queue<Object> queue = QueueUtils.unmodifiableQueue(new LinkedList<>()); final Queue<Object> queue = QueueUtils.unmodifiableQueue(new LinkedList<>());
assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue."); assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue.");
try {
QueueUtils.unmodifiableQueue(null); assertThrows(NullPointerException.class, () -> QueueUtils.unmodifiableQueue(null),
fail("Expecting NullPointerException for null queue."); "Expecting NullPointerException for null queue.");
} catch (final NullPointerException ex) {
// expected
}
assertSame(queue, QueueUtils.unmodifiableQueue(queue), "UnmodifiableQueue shall not be decorated"); assertSame(queue, QueueUtils.unmodifiableQueue(queue), "UnmodifiableQueue shall not be decorated");
} }
@ -71,36 +64,24 @@ public class QueueUtilsTest {
public void testPredicatedQueue() { public void testPredicatedQueue() {
final Queue<Object> queue = QueueUtils.predicatedQueue(new LinkedList<>(), truePredicate); final Queue<Object> queue = QueueUtils.predicatedQueue(new LinkedList<>(), truePredicate);
assertTrue(queue instanceof PredicatedQueue, "Returned object should be a PredicatedQueue."); assertTrue(queue instanceof PredicatedQueue, "Returned object should be a PredicatedQueue.");
try {
QueueUtils.predicatedQueue(null, truePredicate); assertThrows(NullPointerException.class, () -> QueueUtils.predicatedQueue(null, truePredicate),
fail("Expecting NullPointerException for null queue."); "Expecting NullPointerException for null queue.");
} catch (final NullPointerException ex) {
// expected assertThrows(NullPointerException.class, () -> QueueUtils.predicatedQueue(new LinkedList<>(), null),
} "Expecting NullPointerException for null predicate.");
try {
QueueUtils.predicatedQueue(new LinkedList<>(), null);
fail("Expecting NullPointerException for null predicate.");
} catch (final NullPointerException ex) {
// expected
}
} }
@Test @Test
public void testTransformedQueue() { public void testTransformedQueue() {
final Queue<Object> queue = QueueUtils.transformingQueue(new LinkedList<>(), nopTransformer); final Queue<Object> queue = QueueUtils.transformingQueue(new LinkedList<>(), nopTransformer);
assertTrue(queue instanceof TransformedQueue, "Returned object should be an TransformedQueue."); assertTrue(queue instanceof TransformedQueue, "Returned object should be an TransformedQueue.");
try {
QueueUtils.transformingQueue(null, nopTransformer); assertThrows(NullPointerException.class, () -> QueueUtils.transformingQueue(null, nopTransformer),
fail("Expecting NullPointerException for null queue."); "Expecting NullPointerException for null queue.");
} catch (final NullPointerException ex) {
// expected assertThrows(NullPointerException.class, () -> QueueUtils.transformingQueue(new LinkedList<>(), null),
} "Expecting NullPointerException for null transformer.");
try {
QueueUtils.transformingQueue(new LinkedList<>(), null);
fail("Expecting NullPointerException for null transformer.");
} catch (final NullPointerException ex) {
// expected
}
} }
@Test @Test
@ -109,12 +90,8 @@ public class QueueUtilsTest {
assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue."); assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue.");
assertTrue(queue.isEmpty(), "Returned queue is not empty."); assertTrue(queue.isEmpty(), "Returned queue is not empty.");
try { assertThrows(UnsupportedOperationException.class, () -> queue.add(new Object()),
queue.add(new Object()); "Expecting UnsupportedOperationException for empty queue.");
fail("Expecting UnsupportedOperationException for empty queue.");
} catch (final UnsupportedOperationException ex) {
// expected
}
} }
} }

View File

@ -16,11 +16,12 @@
*/ */
package org.apache.commons.collections4; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; 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.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -55,18 +56,15 @@ public class TransformerUtilsTest {
@Test @Test
public void testExceptionTransformer() { public void testExceptionTransformer() {
assertNotNull(TransformerUtils.exceptionTransformer()); assertAll(
assertSame(TransformerUtils.exceptionTransformer(), TransformerUtils.exceptionTransformer()); () -> assertNotNull(TransformerUtils.exceptionTransformer()),
try {
TransformerUtils.exceptionTransformer().transform(null); () -> assertSame(TransformerUtils.exceptionTransformer(), TransformerUtils.exceptionTransformer()),
} catch (final FunctorException ex) {
try { () -> assertThrows(FunctorException.class, () -> TransformerUtils.exceptionTransformer().transform(null)),
TransformerUtils.exceptionTransformer().transform(cString);
} catch (final FunctorException ex2) { () -> assertThrows(FunctorException.class, () -> TransformerUtils.exceptionTransformer().transform(cString))
return; );
}
}
fail();
} }
// nullTransformer // nullTransformer
@ -115,12 +113,8 @@ public class TransformerUtilsTest {
assertNull(TransformerUtils.cloneTransformer().transform(null)); assertNull(TransformerUtils.cloneTransformer().transform(null));
assertEquals(cString, TransformerUtils.cloneTransformer().transform(cString)); assertEquals(cString, TransformerUtils.cloneTransformer().transform(cString));
assertEquals(cInteger, TransformerUtils.cloneTransformer().transform(cInteger)); assertEquals(cInteger, TransformerUtils.cloneTransformer().transform(cInteger));
try {
assertEquals(cObject, TransformerUtils.cloneTransformer().transform(cObject)); assertThrows(IllegalArgumentException.class, () -> assertEquals(cObject, TransformerUtils.cloneTransformer().transform(cObject)));
} catch (final IllegalArgumentException ex) {
return;
}
fail();
} }
// mapTransformer // mapTransformer
@ -149,12 +143,8 @@ public class TransformerUtilsTest {
assertEquals(cObject, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cObject)); assertEquals(cObject, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cObject));
assertEquals(cString, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cString)); assertEquals(cString, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cString));
assertEquals(cInteger, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cInteger)); assertEquals(cInteger, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cInteger));
try {
TransformerUtils.asTransformer((Closure<Object>) null); assertThrows(NullPointerException.class, () -> TransformerUtils.asTransformer((Closure<Object>) null));
} catch (final NullPointerException ex) {
return;
}
fail();
} }
// predicateTransformer // 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(cObject));
assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cString)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cString));
assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cInteger)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cInteger));
try {
TransformerUtils.asTransformer((Predicate<Object>) null); assertThrows(IllegalArgumentException.class, () -> TransformerUtils.asTransformer((Predicate<Object>) null));
} catch (final IllegalArgumentException ex) {
return;
}
fail();
} }
// factoryTransformer // factoryTransformer
@ -183,12 +169,8 @@ public class TransformerUtilsTest {
assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cObject)); assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cObject));
assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cString)); assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cString));
assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cInteger)); assertNull(TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cInteger));
try {
TransformerUtils.asTransformer((Factory<Object>) null); assertThrows(NullPointerException.class, () -> TransformerUtils.asTransformer((Factory<Object>) null));
} catch (final NullPointerException ex) {
return;
}
fail();
} }
// chainedTransformer // chainedTransformer
@ -211,29 +193,22 @@ public class TransformerUtilsTest {
assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer()); assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer());
assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(Collections.<Transformer<Object, Object>>emptyList())); assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(Collections.<Transformer<Object, Object>>emptyList()));
try { assertAll(
TransformerUtils.chainedTransformer(null, null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer(null, null)),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer((Transformer[]) null)),
try {
TransformerUtils.chainedTransformer((Transformer[]) null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer((Collection<Transformer<Object, Object>>) null)),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(NullPointerException.class, () -> TransformerUtils.chainedTransformer(null, null)),
try {
TransformerUtils.chainedTransformer((Collection<Transformer<Object, Object>>) null); () -> assertThrows(NullPointerException.class, () -> {
fail(); Collection<Transformer<Object, Object>> coll1 = new ArrayList<>();
} catch (final NullPointerException ex) {} coll1.add(null);
try { coll1.add(null);
TransformerUtils.chainedTransformer(null, null); TransformerUtils.chainedTransformer(coll1);
fail(); })
} catch (final NullPointerException ex) {} );
try {
coll = new ArrayList<>();
coll.add(null);
coll.add(null);
TransformerUtils.chainedTransformer(coll);
fail();
} catch (final NullPointerException ex) {}
} }
// ifTransformer // ifTransformer
@ -258,22 +233,15 @@ public class TransformerUtilsTest {
assertEquals("C", TransformerUtils.ifTransformer(equalsAPredicate, c).transform("A")); assertEquals("C", TransformerUtils.ifTransformer(equalsAPredicate, c).transform("A"));
assertEquals("B", TransformerUtils.ifTransformer(equalsAPredicate, c).transform("B")); assertEquals("B", TransformerUtils.ifTransformer(equalsAPredicate, c).transform("B"));
try { assertAll(
TransformerUtils.ifTransformer(null, null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(null, null)),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(TruePredicate.truePredicate(), null)),
try {
TransformerUtils.ifTransformer(TruePredicate.truePredicate(), null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(null, ConstantTransformer.constantTransformer("A"))),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(NullPointerException.class, () -> TransformerUtils.ifTransformer(null, null, null))
try { );
TransformerUtils.ifTransformer(null, ConstantTransformer.constantTransformer("A"));
fail();
} catch (final NullPointerException ex) {}
try {
TransformerUtils.ifTransformer(null, null, null);
fail();
} catch (final NullPointerException ex) {}
} }
// switchTransformer // switchTransformer
@ -318,28 +286,19 @@ public class TransformerUtilsTest {
map.put(null, null); map.put(null, null);
assertEquals(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(map)); assertEquals(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(map));
try { assertAll(
TransformerUtils.switchTransformer(null, null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(null, null)),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(null, (Transformer[]) null)),
try {
TransformerUtils.switchTransformer(null, (Transformer[]) null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(null)),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(NullPointerException.class, () -> TransformerUtils.switchTransformer(new Predicate[2], new Transformer[2])),
try {
TransformerUtils.switchTransformer(null); () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.switchTransformer(
fail(); new Predicate[]{TruePredicate.truePredicate()},
} catch (final NullPointerException ex) {} new Transformer[]{a, b}))
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) {}
} }
// switchMapTransformer // switchMapTransformer
@ -365,10 +324,7 @@ public class TransformerUtilsTest {
map.put(null, null); map.put(null, null);
assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(map)); assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(map));
try { assertThrows(NullPointerException.class, () -> TransformerUtils.switchMapTransformer(null));
TransformerUtils.switchMapTransformer(null);
fail();
} catch (final NullPointerException ex) {}
} }
// invokerTransformer // invokerTransformer
@ -382,14 +338,11 @@ public class TransformerUtilsTest {
assertEquals(1, TransformerUtils.invokerTransformer("size").transform(list)); assertEquals(1, TransformerUtils.invokerTransformer("size").transform(list));
assertNull(TransformerUtils.invokerTransformer("size").transform(null)); assertNull(TransformerUtils.invokerTransformer("size").transform(null));
try { assertAll(
TransformerUtils.invokerTransformer(null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.invokerTransformer(null)),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(FunctorException.class, () -> TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object()))
try { );
TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object());
fail();
} catch (final FunctorException ex) {}
} }
// invokerTransformer2 // invokerTransformer2
@ -406,27 +359,18 @@ public class TransformerUtilsTest {
assertNull(TransformerUtils.invokerTransformer("contains", assertNull(TransformerUtils.invokerTransformer("contains",
new Class[]{Object.class}, new Object[]{cString}).transform(null)); new Class[]{Object.class}, new Object[]{cString}).transform(null));
try { assertAll(
TransformerUtils.invokerTransformer(null, null, null); () -> assertThrows(NullPointerException.class, () -> TransformerUtils.invokerTransformer(null, null, null)),
fail();
} catch (final NullPointerException ex) {} () -> assertThrows(FunctorException.class, () -> TransformerUtils.invokerTransformer("noSuchMethod", new Class[]{Object.class},
try { new Object[]{cString}).transform(new Object())),
TransformerUtils.invokerTransformer("noSuchMethod", new Class[] { Object.class },
new Object[] { cString }).transform(new Object()); () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.invokerTransformer("badArgs", null, new Object[]{cString})),
fail();
} catch (final FunctorException ex) {} () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.invokerTransformer("badArgs", new Class[]{Object.class}, null)),
try {
TransformerUtils.invokerTransformer("badArgs", null, new Object[] { cString }); () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.invokerTransformer("badArgs", new Class[]{}, 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) {}
} }
// stringValueTransformer // stringValueTransformer
@ -447,20 +391,17 @@ public class TransformerUtilsTest {
@Test @Test
public void testInstantiateTransformerNull() { public void testInstantiateTransformerNull() {
try {
TransformerUtils.instantiateTransformer(null, new Object[] { "str" }); assertAll(
fail(); () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.instantiateTransformer(null, new Object[]{"str"})),
} catch (final IllegalArgumentException ex) {}
try { () -> assertThrows(IllegalArgumentException.class, () -> TransformerUtils.instantiateTransformer(new Class[]{}, new Object[]{"str"}))
TransformerUtils.instantiateTransformer(new Class[] {}, new Object[] { "str" }); );
fail();
} catch (final IllegalArgumentException ex) {}
Transformer<Class<?>, Object> trans = TransformerUtils.instantiateTransformer(new Class[] { Long.class }, new Object[] { null }); Transformer<Class<?>, Object> trans = TransformerUtils.instantiateTransformer(new Class[] { Long.class }, new Object[] { null });
try {
trans.transform(String.class); Transformer<Class<?>, Object> finalTrans = trans;
fail(); assertThrows(FunctorException.class, () -> finalTrans.transform(String.class));
} catch (final FunctorException ex) {}
trans = TransformerUtils.instantiateTransformer(); trans = TransformerUtils.instantiateTransformer();
assertEquals("", trans.transform(String.class)); assertEquals("", trans.transform(String.class));
@ -488,4 +429,5 @@ public class TransformerUtilsTest {
TestUtils.assertSameAfterSerialization("Singleton pattern broken for " + original.getClass(), original); TestUtils.assertSameAfterSerialization("Singleton pattern broken for " + original.getClass(), original);
} }
} }
} }

View File

@ -28,9 +28,10 @@ import java.util.Set;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
import java.util.stream.Collectors; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Tests for the {@link IndexFilters}. * Tests for the {@link IndexFilters}.
@ -53,26 +54,13 @@ public class IndexFilterTest {
final ArrayList<Integer> actual = new ArrayList<>(); final ArrayList<Integer> actual = new ArrayList<>();
final IntConsumer consumer = actual::add; final IntConsumer consumer = actual::add;
try { assertAll(
IndexFilters.distinctIndexes(null, shape, consumer); () -> assertThrows(NullPointerException.class, () -> IndexFilters.distinctIndexes(null, shape, consumer), "null hasher"),
fail("null hasher");
} catch (final NullPointerException expected) {
// Ignore
}
try { () -> assertThrows(NullPointerException.class, () -> IndexFilters.distinctIndexes(hasher, null, consumer), "null shape"),
IndexFilters.distinctIndexes(hasher, null, consumer);
fail("null shape");
} catch (final NullPointerException expected) {
// Ignore
}
try { () -> assertThrows(NullPointerException.class, () -> IndexFilters.distinctIndexes(hasher, shape, null), "null consumer")
IndexFilters.distinctIndexes(hasher, shape, null); );
fail("null consumer");
} catch (final NullPointerException expected) {
// Ignore
}
// All OK together // All OK together
IndexFilters.distinctIndexes(hasher, shape, consumer); IndexFilters.distinctIndexes(hasher, shape, consumer);

View File

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; 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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -65,12 +65,8 @@ public class DynamicHasherBuilderTest {
final OfInt iter = hasher.iterator(shape); final OfInt iter = hasher.iterator(shape);
assertFalse(iter.hasNext()); assertFalse(iter.hasNext());
try {
iter.nextInt(); assertThrows(NoSuchElementException.class, () -> iter.nextInt(), "Should have thrown NoSuchElementException");
fail("Should have thrown NoSuchElementException");
} catch (final NoSuchElementException ignore) {
// do nothing
}
} }
/** /**
@ -129,4 +125,5 @@ public class DynamicHasherBuilderTest {
public void setup() { public void setup() {
builder = new DynamicHasher.Builder(hf); builder = new DynamicHasher.Builder(hf);
} }
} }

View File

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; 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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -34,6 +34,7 @@ import org.junit.jupiter.api.Test;
* Tests the {@link DynamicHasher}. * Tests the {@link DynamicHasher}.
*/ */
public class DynamicHasherTest { public class DynamicHasherTest {
private DynamicHasher.Builder builder; private DynamicHasher.Builder builder;
private Shape shape; private Shape shape;
@ -63,6 +64,7 @@ public class DynamicHasherTest {
public Signedness getSignedness() { public Signedness getSignedness() {
return Signedness.SIGNED; return Signedness.SIGNED;
} }
}; };
/** /**
@ -110,12 +112,8 @@ public class DynamicHasherTest {
assertEquals(element, iter.nextInt()); assertEquals(element, iter.nextInt());
} }
assertFalse(iter.hasNext()); assertFalse(iter.hasNext());
try {
iter.next(); assertThrows(NoSuchElementException.class, () -> iter.next(), "Should have thrown NoSuchElementException");
fail("Should have thrown NoSuchElementException");
} catch (final NoSuchElementException ignore) {
// do nothing
}
} }
/** /**
@ -126,11 +124,7 @@ public class DynamicHasherTest {
final Hasher hasher = builder.with("Hello", StandardCharsets.UTF_8).build(); final Hasher hasher = builder.with("Hello", StandardCharsets.UTF_8).build();
try { assertThrows(IllegalArgumentException.class, () -> hasher.iterator(new Shape(testFunction, 3, 72, 17)), "Should have thrown IllegalArgumentException");
hasher.iterator(new Shape(testFunction, 3, 72, 17));
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing
}
} }
} }

View File

@ -23,11 +23,12 @@ import java.util.List;
import org.junit.Test; 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.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** /**
* Tests for {@link BooleanComparator}. * Tests for {@link BooleanComparator}.
*
*/ */
@SuppressWarnings("boxing") @SuppressWarnings("boxing")
public class BooleanComparatorTest extends AbstractComparatorTest<Boolean> { public class BooleanComparatorTest extends AbstractComparatorTest<Boolean> {
@ -130,35 +131,18 @@ public class BooleanComparatorTest extends AbstractComparatorTest<Boolean> {
protected void nullArgumentTests(final BooleanComparator comp) { protected void nullArgumentTests(final BooleanComparator comp) {
assertNotNull(comp); assertNotNull(comp);
try {
comp.compare(null, null); assertAll(
fail("Expected NullPointerException"); () -> assertThrows(NullPointerException.class, () -> comp.compare(null, null), "Expected NullPointerException"),
} catch (final NullPointerException e) {
// expected () -> assertThrows(NullPointerException.class, () -> comp.compare(Boolean.TRUE, null), "Expected NullPointerException"),
}
try { () -> assertThrows(NullPointerException.class, () -> comp.compare(Boolean.FALSE, null), "Expected NullPointerException"),
comp.compare(Boolean.TRUE, null);
fail("Expected NullPointerException"); () -> assertThrows(NullPointerException.class, () -> comp.compare(null, Boolean.TRUE), "Expected NullPointerException"),
} catch (final NullPointerException e) {
// expected () -> assertThrows(NullPointerException.class, () -> comp.compare(null, Boolean.FALSE), "Expected NullPointerException")
} );
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
}
} }
} }

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.commons.collections4.comparators; package org.apache.commons.collections4.comparators;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
@ -26,7 +28,6 @@ import org.junit.Test;
/** /**
* Test class for FixedOrderComparator. * Test class for FixedOrderComparator.
*
*/ */
public class FixedOrderComparatorTest extends AbstractComparatorTest<String> { public class FixedOrderComparatorTest extends AbstractComparatorTest<String> {
@ -143,36 +144,25 @@ public class FixedOrderComparatorTest extends AbstractComparatorTest<String> {
assertFalse(comparator.isLocked()); assertFalse(comparator.isLocked());
comparator.compare("New York", "Tokyo"); comparator.compare("New York", "Tokyo");
assertTrue(comparator.isLocked()); assertTrue(comparator.isLocked());
try {
comparator.add("Minneapolis");
fail("Should have thrown an UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// success -- ignore
}
try { assertThrows(UnsupportedOperationException.class, () -> comparator.add("Minneapolis"),
comparator.addAsEqual("New York", "Minneapolis"); "Should have thrown an UnsupportedOperationException");
fail("Should have thrown an UnsupportedOperationException");
} catch (final UnsupportedOperationException e) { assertThrows(UnsupportedOperationException.class, () -> comparator.addAsEqual("New York", "Minneapolis"),
// success -- ignore "Should have thrown an UnsupportedOperationException");
}
} }
@Test @Test
public void testUnknownObjectBehavior() { public void testUnknownObjectBehavior() {
FixedOrderComparator<String> comparator = new FixedOrderComparator<>(topCities); FixedOrderComparator<String> comparator = new FixedOrderComparator<>(topCities);
try {
comparator.compare("New York", "Minneapolis"); FixedOrderComparator<String> finalComparator = comparator;
fail("Should have thrown a IllegalArgumentException"); assertThrows(IllegalArgumentException.class, () -> finalComparator.compare("New York", "Minneapolis"),
} catch (final IllegalArgumentException e) { "Should have thrown a IllegalArgumentException");
// success-- ignore
} assertThrows(IllegalArgumentException.class, () -> finalComparator.compare("Minneapolis", "New York"),
try { "Should have thrown a IllegalArgumentException");
comparator.compare("Minneapolis", "New York");
fail("Should have thrown a IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// success-- ignore
}
assertEquals(FixedOrderComparator.UnknownObjectBehavior.EXCEPTION, comparator.getUnknownObjectBehavior()); assertEquals(FixedOrderComparator.UnknownObjectBehavior.EXCEPTION, comparator.getUnknownObjectBehavior());
comparator = new FixedOrderComparator<>(topCities); 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("Minneapolis", "New York"));
assertEquals(-1, comparator.compare("New York", "Minneapolis")); assertEquals(-1, comparator.compare("New York", "Minneapolis"));
assertEquals( 0, comparator.compare("Minneapolis", "St Paul")); assertEquals( 0, comparator.compare("Minneapolis", "St Paul"));
} }
// //
@ -240,4 +229,5 @@ public class FixedOrderComparatorTest extends AbstractComparatorTest<String> {
assertEquals(orderedObjects[i], keys[i]); assertEquals(orderedObjects[i], keys[i]);
} }
} }
} }