COLLECTIONS-777 JUnit v5 (#282)

JUnit v5 assertThrows SetOperationsTest

JUnit v5 assertThrows CompositeCollectionTest

JUnit v5 assertThrows MultiValueMapTest

JUnit v5 assertThrows UnmodifiableMapTest

JUnit v5 assertThrows UnmodifiableOrderedMapTest

JUnit v5 assertThrows UnmodifiableSortedMapTest

JUnit v5 assertThrows CatchAndRethrowClosureTest

JUnit v5 assertThrows PredicatedCollectionTest

JUnit v5 assertThrows ComparatorChainTest

JUnit v5 assertThrows ArrayListIteratorTest

JUnit v5 assertThrows ListIteratorWrapper2Test

JUnit v5 assertThrows ObjectArrayListIteratorTest

JUnit v5 assertThrows PeekingIteratorTest

JUnit v5 assertThrows DefaultKeyValueTest

JUnit v5 assertThrows MultiKeyTest
This commit is contained in:
John Patrick 2022-03-04 13:40:31 +00:00 committed by GitHub
parent f4c041ba94
commit e113297085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 126 additions and 182 deletions

View File

@ -17,7 +17,7 @@
package org.apache.commons.collections4.bloomfilter;
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.List;
import java.util.Arrays;
@ -73,12 +73,7 @@ public class SetOperationsTest {
final Hasher hasher2 = new StaticHasher(lst2.iterator(), shape2);
final BloomFilter filter2 = new HasherBloomFilter(hasher2, shape2);
try {
SetOperations.cosineDistance(filter1, filter2);
fail("Expected an IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// Ignore
}
assertThrows(IllegalArgumentException.class, () -> SetOperations.cosineDistance(filter1, filter2));
}
/**
@ -351,4 +346,5 @@ public class SetOperationsTest {
assertEquals(1.0, SetOperations.jaccardSimilarity(filter1, filter3), 0.0001);
assertEquals(1.0, SetOperations.jaccardSimilarity(filter3, filter1), 0.0001);
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.collection;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -426,12 +428,9 @@ public class CompositeCollectionTest<E> extends AbstractCollectionTest<E> {
@Override
public void testUnsupportedRemove() {
resetFull();
try {
getCollection().remove(null);
fail("remove should raise UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().remove(null));
verify();
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.collection;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -82,12 +84,9 @@ public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
public void testIllegalAdd() {
final Collection<E> c = makeTestCollection();
final Integer i = 3;
try {
c.add((E) i);
fail("Integer should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> c.add((E) i), "Integer should fail string predicate.");
assertFalse("Collection shouldn't contain illegal element", c.contains(i));
}
@ -99,12 +98,9 @@ public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
elements.add((E) "two");
elements.add((E) Integer.valueOf(3));
elements.add((E) "four");
try {
c.addAll(elements);
fail("Integer should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> c.addAll(elements), "Integer should fail string predicate.");
assertFalse("Collection shouldn't contain illegal element", c.contains("one"));
assertFalse("Collection shouldn't contain illegal element", c.contains("two"));
assertFalse("Collection shouldn't contain illegal element", c.contains(3));

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.comparators;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
@ -26,7 +28,6 @@ import org.junit.Test;
/**
* Tests for ComparatorChain.
*
*/
public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainTest.PseudoRow> {
@ -67,11 +68,8 @@ public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainT
final ComparatorChain<Integer> chain = new ComparatorChain<>();
final Integer i1 = 4;
final Integer i2 = 6;
try {
chain.compare(i1, i2);
fail("An exception should be thrown when a chain contains zero comparators.");
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> chain.compare(i1, i2), "An exception should be thrown when a chain contains zero comparators.");
}
@Test
@ -92,11 +90,8 @@ public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainT
final ComparatorChain<Integer> chain = new ComparatorChain<>(list);
final Integer i1 = 4;
final Integer i2 = 6;
try {
chain.compare(i1, i2);
fail("An exception should be thrown when a chain contains zero comparators.");
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> chain.compare(i1, i2));
}
@Test
@ -188,4 +183,5 @@ public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainT
return that instanceof ColumnComparator && colIndex == ((ColumnComparator) that).colIndex;
}
}
}

View File

@ -17,13 +17,17 @@
package org.apache.commons.collections4.functors;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.FunctorException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
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 static org.junit.jupiter.api.DynamicTest.dynamicTest;
public class CatchAndRethrowClosureTest extends AbstractClosureTest {
@ -61,33 +65,28 @@ public class CatchAndRethrowClosureTest extends AbstractClosureTest {
return generateNoExceptionClosure();
}
@Test
public void testThrowingClosure() {
Closure<Integer> closure = generateNoExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
} catch (final RuntimeException ex) {
fail();
}
@TestFactory
public Collection<DynamicTest> testThrowingClosure() {
closure = generateIOExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
fail();
} catch (final FunctorException ex) {
assertTrue(ex.getCause() instanceof IOException);
} catch (final RuntimeException ex) {
fail();
}
return Arrays.asList(
closure = generateNullPointerExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
fail();
} catch (final FunctorException ex) {
fail();
} catch (final RuntimeException ex) {
assertTrue(ex instanceof NullPointerException);
}
dynamicTest("Closure NoException", () -> {
Closure<Integer> closure = generateNoExceptionClosure();
closure.execute(Integer.valueOf(0));
}),
dynamicTest("Closure IOException", () -> {
Closure<Integer> closure = generateIOExceptionClosure();
final FunctorException thrown = assertThrows(FunctorException.class, () -> closure.execute(Integer.valueOf(0)));
assertTrue(thrown.getCause() instanceof IOException);
}),
dynamicTest("Closure NullPointerException", () -> {
Closure<Integer> closure = generateNullPointerExceptionClosure();
assertThrows(NullPointerException.class, () -> closure.execute(Integer.valueOf(0)));
})
);
}
}

View File

@ -20,6 +20,7 @@ import java.util.ListIterator;
import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Test the ArrayListIterator class.
@ -104,15 +105,8 @@ public class ArrayListIteratorTest<E> extends ArrayIteratorTest<E> {
// a call to set() before a call to next() or previous() should throw an IllegalStateException
iter = makeArrayListIterator(testArray);
try {
iter.set((E) "should fail");
fail("ListIterator#set should fail if next() or previous() have not yet been called.");
} catch (final IllegalStateException e) {
// expected
} catch (final Throwable t) { // should never happen
fail(t.toString());
}
ListIterator<E> finalIter = iter;
assertThrows(IllegalStateException.class, () -> finalIter.set((E) "should fail"), "ListIterator#set should fail if next() or previous() have not yet been called.");
}
}

View File

@ -23,9 +23,10 @@ import java.util.NoSuchElementException;
import org.apache.commons.collections4.ResettableListIterator;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Tests the ListIteratorWrapper to insure that it behaves as expected when wrapping a ListIterator.
*
*/
public class ListIteratorWrapper2Test<E> extends AbstractIteratorTest<E> {
@ -109,11 +110,7 @@ public class ListIteratorWrapper2Test<E> extends AbstractIteratorTest<E> {
assertEquals(-1, iter.previousIndex());
assertEquals(0, iter.nextIndex());
try {
iter.remove();
fail("ListIteratorWrapper#remove() should fail; must be initially positioned first");
} catch (final IllegalStateException e) {
}
assertThrows(IllegalStateException.class, () -> iter.remove(), "ListIteratorWrapper#remove() should fail; must be initially positioned first");
//no change from invalid op:
assertEquals(-1, iter.previousIndex());
@ -134,11 +131,7 @@ public class ListIteratorWrapper2Test<E> extends AbstractIteratorTest<E> {
assertEquals(-1, iter.previousIndex());
assertEquals(0, iter.nextIndex());
try {
iter.remove();
fail("ListIteratorWrapper#remove() should fail; must be repositioned first");
} catch (final IllegalStateException e) {
}
assertThrows(IllegalStateException.class, () -> iter.remove(), "ListIteratorWrapper#remove() should fail; must be repositioned first");
//no change from invalid op:
assertEquals(-1, iter.previousIndex());

View File

@ -20,6 +20,7 @@ import java.util.ListIterator;
import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Tests the ObjectArrayListIterator class.
@ -101,15 +102,8 @@ public class ObjectArrayListIteratorTest<E> extends ObjectArrayIteratorTest<E> {
// a call to set() before a call to next() or previous() should throw an IllegalStateException
iter = makeArrayListIterator((E[]) testArray);
try {
iter.set((E) "should fail");
fail("ListIterator#set should fail if next() or previous() have not yet been called.");
} catch (final IllegalStateException e) {
// expected
} catch (final Throwable t) { // should never happen
fail(t.toString());
}
ListIterator<E> finalIter = iter;
assertThrows(IllegalStateException.class, () -> finalIter.set((E) "should fail"), "ListIterator#set should fail if next() or previous() have not yet been called.");
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.iterators;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -105,12 +107,7 @@ public class PeekingIteratorTest<E> extends AbstractIteratorTest<E> {
assertFalse(it.hasNext());
assertNull(it.peek());
try {
it.element();
fail();
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> it.element());
}
@Test
@ -122,12 +119,7 @@ public class PeekingIteratorTest<E> extends AbstractIteratorTest<E> {
assertTrue(it.hasNext());
assertEquals("b", it.peek());
try {
it.remove();
fail();
} catch (final IllegalStateException e) {
// expected
}
assertThrows(IllegalStateException.class, () -> it.remove());
}
private void validate(final Iterator<E> iter, final E... items) {

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.keyvalue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
@ -25,7 +27,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
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;
/**
* Test the DefaultKeyValue class.
@ -72,7 +73,6 @@ public class DefaultKeyValueTest<K, V> {
kv.setValue(null);
assertNull(kv.getValue());
}
@SuppressWarnings("unchecked")
@ -84,25 +84,9 @@ public class DefaultKeyValueTest<K, V> {
final DefaultKeyValue<K, V> kv = makeDefaultKeyValue();
try {
kv.setKey((K) kv);
fail("Should throw an IllegalArgumentException");
} catch (final IllegalArgumentException iae) {
// expected to happen...
// check that the KVP's state has not changed
assertTrue(kv.getKey() == null && kv.getValue() == null);
}
try {
kv.setValue((V) kv);
fail("Should throw an IllegalArgumentException");
} catch (final IllegalArgumentException iae) {
// expected to happen...
// check that the KVP's state has not changed
assertTrue(kv.getKey() == null && kv.getValue() == null);
}
assertThrows(IllegalArgumentException.class, () -> kv.setKey((K) kv));
// check that the KVP's state has not changed
assertTrue(kv.getKey() == null && kv.getValue() == null);
}
/**
@ -140,7 +124,6 @@ public class DefaultKeyValueTest<K, V> {
// test that the KVP is independent of the Map.Entry
entry.setValue(null);
assertSame(value, kv.getValue());
}
@SuppressWarnings("unchecked")

View File

@ -16,7 +16,9 @@
*/
package org.apache.commons.collections4.keyvalue;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -24,6 +26,8 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@ -33,12 +37,12 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
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 static org.junit.jupiter.api.DynamicTest.dynamicTest;
/**
* Unit tests for {@link org.apache.commons.collections4.keyvalue.MultiKey}.
*
*/
public class MultiKeyTest {
@ -86,12 +90,14 @@ public class MultiKeyTest {
return this;
}
}
Integer ONE = Integer.valueOf(1);
Integer TWO = Integer.valueOf(2);
Integer THREE = Integer.valueOf(3);
Integer FOUR = Integer.valueOf(4);
Integer FIVE = Integer.valueOf(5);
@Test
public void testConstructors() throws Exception {
MultiKey<Integer> mk;
@ -139,21 +145,22 @@ public class MultiKeyTest {
assertArrayEquals(new Object[]{THREE, FOUR, ONE, FIVE}, mk.getKeys());
}
@Test
public void testConstructorsByArrayNull() throws Exception {
@TestFactory
public Collection<DynamicTest> testConstructorsByArrayNull() {
final Integer[] keys = null;
try {
new MultiKey<>(keys);
fail();
} catch (final NullPointerException ex) {}
try {
new MultiKey<>(keys, true);
fail();
} catch (final NullPointerException ex) {}
try {
new MultiKey<>(keys, false);
fail();
} catch (final NullPointerException ex) {}
return Arrays.asList(
dynamicTest("Integer[] null", () -> {
assertThrows(NullPointerException.class, () -> new MultiKey<>(keys));
}),
dynamicTest("Integer[] null + makeClone true", () -> {
assertThrows(NullPointerException.class, () -> new MultiKey<>(keys, true));
}),
dynamicTest("Integer[] null + makeClone false", () -> {
assertThrows(NullPointerException.class, () -> new MultiKey<>(keys, false));
})
);
}
@Test
@ -215,19 +222,26 @@ public class MultiKeyTest {
assertEquals(mk.hashCode(), mk2.hashCode());
}
@Test
public void testGetIndexed() {
@TestFactory
public Collection<DynamicTest> testGetIndexed() {
final MultiKey<Integer> mk = new MultiKey<>(ONE, TWO);
assertSame(ONE, mk.getKey(0));
assertSame(TWO, mk.getKey(1));
try {
mk.getKey(-1);
fail();
} catch (final IndexOutOfBoundsException ex) {}
try {
mk.getKey(2);
fail();
} catch (final IndexOutOfBoundsException ex) {}
return Arrays.asList(
dynamicTest("0", () -> {
assertSame(ONE, mk.getKey(0));
}),
dynamicTest("1", () -> {
assertSame(TWO, mk.getKey(1));
}),
dynamicTest("-1", () -> {
assertThrows(IndexOutOfBoundsException.class, () -> mk.getKey(-1));
}),
dynamicTest("2", () -> {
assertThrows(IndexOutOfBoundsException.class, () -> mk.getKey(2));
})
);
}
@Test

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -397,12 +399,9 @@ public class MultiValueMapTest<K, V> extends AbstractObjectTest {
final MultiValueMap map2 = MultiValueMap.multiValueMap(new HashMap(), (Class) String.class);
bytes = serialize(map2);
try {
result = deserialize(bytes);
fail("unsafe clazz accepted when de-serializing MultiValueMap");
} catch (final UnsupportedOperationException ex) {
// expected
}
byte[] finalBytes = bytes;
assertThrows(UnsupportedOperationException.class, () -> deserialize(finalBytes));
}
private byte[] serialize(final Object object) throws IOException {
@ -453,6 +452,7 @@ public class MultiValueMapTest<K, V> extends AbstractObjectTest {
final Map<?, ?> map2 = (Map<?, ?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(map));
assertEquals("Map is empty", 0, map2.size());
}
public void testFullMapCompatibility() throws Exception {
final Map<?, ?> map = (Map<?, ?>) makeObject();
final Map<?, ?> map2 = (Map<?, ?>) readExternalFormFromDisk(getCanonicalFullCollectionName(map));

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.HashMap;
import java.util.Map;
@ -63,7 +65,6 @@ public class UnmodifiableMapTest<K, V> extends AbstractIterableMapTest<K, V> {
return (IterableMap<K, V>) UnmodifiableMap.unmodifiableMap(m);
}
public void testUnmodifiable() {
assertTrue(makeObject() instanceof Unmodifiable);
assertTrue(makeFullMap() instanceof Unmodifiable);
@ -73,13 +74,9 @@ public class UnmodifiableMapTest<K, V> extends AbstractIterableMapTest<K, V> {
final Map<K, V> map = makeFullMap();
assertSame(map, UnmodifiableMap.unmodifiableMap(map));
try {
UnmodifiableMap.unmodifiableMap(null);
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> UnmodifiableMap.unmodifiableMap(null));
}
@Override
public String getCompatibilityVersion() {
return "4";

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.HashMap;
import org.apache.commons.collections4.OrderedMap;
@ -62,7 +64,6 @@ public class UnmodifiableOrderedMapTest<K, V> extends AbstractOrderedMapTest<K,
return UnmodifiableOrderedMap.unmodifiableOrderedMap(m);
}
public void testUnmodifiable() {
assertTrue(makeObject() instanceof Unmodifiable);
assertTrue(makeFullMap() instanceof Unmodifiable);
@ -72,13 +73,9 @@ public class UnmodifiableOrderedMapTest<K, V> extends AbstractOrderedMapTest<K,
final OrderedMap<K, V> map = makeFullMap();
assertSame(map, UnmodifiableOrderedMap.unmodifiableOrderedMap(map));
try {
UnmodifiableOrderedMap.unmodifiableOrderedMap(null);
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> UnmodifiableOrderedMap.unmodifiableOrderedMap(null));
}
@Override
public String getCompatibilityVersion() {
return "4";

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.SortedMap;
import java.util.TreeMap;
@ -62,7 +64,6 @@ public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<K, V>
return UnmodifiableSortedMap.unmodifiableSortedMap(m);
}
public void testUnmodifiable() {
assertTrue(makeObject() instanceof Unmodifiable);
assertTrue(makeFullMap() instanceof Unmodifiable);
@ -72,13 +73,9 @@ public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<K, V>
final SortedMap<K, V> map = makeFullMap();
assertSame(map, UnmodifiableSortedMap.unmodifiableSortedMap(map));
try {
UnmodifiableSortedMap.unmodifiableSortedMap(null);
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> UnmodifiableSortedMap.unmodifiableSortedMap(null));
}
public void testHeadMap() {
final SortedMap<K, V> map = makeFullMap();
final SortedMap<K, V> m = new TreeMap<>();
@ -91,7 +88,6 @@ public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<K, V>
assertSame(16, map.headMap((K) "we'll").size());
}
public void testTailMap() {
final SortedMap<K, V> map = makeFullMap();
@ -104,7 +100,6 @@ public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<K, V>
assertSame(18, map.tailMap((K) "again").size());
}
public void testSubMap() {
final SortedMap<K, V> map = makeFullMap();
@ -119,7 +114,6 @@ public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<K, V>
assertSame(map.headMap((K) "you").size(), map.subMap((K) "again", (K) "you").size());
}
@Override
public String getCompatibilityVersion() {
return "4";