Skip to content
Permalink
Browse files
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
  • Loading branch information
nhojpatrick committed Mar 4, 2022
1 parent f4c041b commit e113297085e163003ab65c25bf674e608f03acee
Showing 15 changed files with 128 additions and 184 deletions.
@@ -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 void testDifferentShapesThrows() {
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 final void jaccardSimilarityTest_NoValues() {
assertEquals(1.0, SetOperations.jaccardSimilarity(filter1, filter3), 0.0001);
assertEquals(1.0, SetOperations.jaccardSimilarity(filter3, filter1), 0.0001);
}

}
@@ -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 void testToCollection() {
@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();
}

@@ -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 Collection<E> makeTestCollection() {
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 void testIllegalAddAll() {
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));
@@ -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 @@

/**
* Tests for ComparatorChain.
*
*/
public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainTest.PseudoRow> {

@@ -67,11 +68,8 @@ public void testBadNoopComparatorChain() {
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 void testBadListComparatorChain() {
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 boolean equals(final Object that) {
return that instanceof ColumnComparator && colIndex == ((ColumnComparator) that).colIndex;
}
}

}
@@ -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 @@ protected <T> Closure<T> generateClosure() {
return generateNoExceptionClosure();
}

@Test
public void testThrowingClosure() {
Closure<Integer> closure = generateNoExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
} catch (final RuntimeException ex) {
fail();
}

closure = generateIOExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
fail();
} catch (final FunctorException ex) {
assertTrue(ex.getCause() instanceof IOException);
} catch (final RuntimeException ex) {
fail();
}

closure = generateNullPointerExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
fail();
} catch (final FunctorException ex) {
fail();
} catch (final RuntimeException ex) {
assertTrue(ex instanceof NullPointerException);
}
@TestFactory
public Collection<DynamicTest> testThrowingClosure() {

return Arrays.asList(

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)));
})

);
}

}
@@ -20,6 +20,7 @@
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 void testListIteratorSet() {
// 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.");
}

}
@@ -23,9 +23,10 @@

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 void testRemove() {
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 void testRemove() {
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());
@@ -20,6 +20,7 @@
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 void testListIteratorSet() {
// 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.");
}

}
@@ -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 void testIteratorExhausted() {
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 void testIllegalRemove() {
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) {

0 comments on commit e113297

Please sign in to comment.