[COLLECTIONS-809] JUnit v5 assertThrows (#291)

Refactor PredicatedMapTest

Refactor UnmodifiableSortedSetTest

Refactor PredicatedSortedSetTest

Refactor PredicatedSetTest

Refactor PredicatedNavigableSetTest

Refactor ListOrderedSetTest

Refactor PredicatedSortedMapTest

Refactor ReferenceIdentityMapTest

Refactor ReferenceMapTest

Refactor TransformedSortedMapTest

Refactor PredicatedMapTest

Refactor UnmodifiableSortedSetTest

Refactor PredicatedSortedSetTest

Refactor PredicatedSetTest

Refactor PredicatedNavigableSetTest

Refactor ListOrderedSetTest

Refactor PredicatedSortedMapTest

Refactor ReferenceIdentityMapTest

Refactor ReferenceMapTest

Refactor TransformedSortedMapTest

Refactor UnmodifiableMultiValuedMapTest

Refactor PredicatedMultiSetTest

Refactor UnmodifiableMultiSetTest

Refactor CircularFifoQueueTest

Refactor UnmodifiableQueueTest

Refactor CompositeSetTest

Refactor AbstractQueueTest

Refactor AbstractMultiSetTest

Refactor AbstractMultiValuedMapTest

Refactor AbstractBidiMapTest

Refactor AbstractBloomFilterTest

Refactor AbstractIteratorTest

Refactor AbstractLinkedListTest

Refactor AbstractListTest

Refactor AbstractSortedMapTest

Refactor AbstractOrderedMapTest

Refactor AbstractIterableMapTest

Refactor AbstractMapTest

Refactor AbstractMapIteratorTest

Refactor AbstractListIteratorTest

Refactor AbstractCollectionTest
This commit is contained in:
John Patrick 2022-03-16 19:19:09 +00:00 committed by GitHub
parent eab0aa0b09
commit f55268b092
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 520 additions and 1268 deletions

View File

@ -556,27 +556,23 @@ public abstract class AbstractBidiMapTest<K, V> extends AbstractIterableMapTest<
// at this point
// key1=newValue1, key2=newValue2
try {
it.setValue(newValue1); // should remove key1
fail();
} catch (final IllegalArgumentException ex) {
return; // simplest way of dealing with tricky situation
}
confirmed.put(key2, newValue1);
AbstractBidiMapTest.this.getConfirmed().remove(key1);
assertEquals(newValue1, it.getValue());
assertTrue(bidi.containsKey(it.getKey()));
assertTrue(bidi.containsValue(newValue1));
assertEquals(newValue1, bidi.get(it.getKey()));
assertFalse(bidi.containsKey(key1));
assertFalse(bidi.containsValue(newValue2));
verify();
// check for ConcurrentModification
it.next(); // if you fail here, maybe you should be throwing an IAE, see above
if (isRemoveSupported()) {
it.remove();
}
assertThrows(IllegalArgumentException.class, () -> it.setValue(newValue1)); // should remove key1
// below code was previously never executed
// confirmed.put(key2, newValue1);
// AbstractBidiMapTest.this.getConfirmed().remove(key1);
// assertEquals(newValue1, it.getValue());
// assertTrue(bidi.containsKey(it.getKey()));
// assertTrue(bidi.containsValue(newValue1));
// assertEquals(newValue1, bidi.get(it.getKey()));
// assertFalse(bidi.containsKey(key1));
// assertFalse(bidi.containsValue(newValue2));
// verify();
//
// // check for ConcurrentModification
// it.next(); // if you fail here, maybe you should be throwing an IAE, see above
// if (isRemoveSupported()) {
// it.remove();
// }
}
}

View File

@ -22,7 +22,6 @@ import org.apache.commons.collections4.BulkTest;
/**
* JUnit tests.
*
*/
public class DualHashBidiMapTest<K, V> extends AbstractBidiMapTest<K, V> {
@ -56,4 +55,5 @@ public class DualHashBidiMapTest<K, V> extends AbstractBidiMapTest<K, V> {
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/DualHashBidiMap.fullCollection.version4.obj");
// }
}

View File

@ -16,10 +16,11 @@
*/
package org.apache.commons.collections4.bloomfilter;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.List;
import java.util.PrimitiveIterator.OfInt;
@ -261,12 +262,8 @@ public abstract class AbstractBloomFilterTest {
final List<Integer> lst = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
final Hasher hasher = new StaticHasher(lst.iterator(), anotherShape);
try {
createFilter(hasher, shape);
fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
assertThrows(IllegalArgumentException.class, () -> createFilter(hasher, shape),
"Should throw IllegalArgumentException");
}
/**
@ -297,12 +294,8 @@ public abstract class AbstractBloomFilterTest {
final Shape anotherShape = new Shape(testFunctionX, 3, 72, 17);
final Hasher hasher2 = new StaticHasher(lst.iterator(), anotherShape);
final BloomFilter bf2 = createFilter(hasher2, anotherShape);
try {
bf.contains(bf2);
fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
assertThrows(IllegalArgumentException.class, () -> bf.contains(bf2),
"Should throw IllegalArgumentException");
}
/**
@ -340,12 +333,8 @@ public abstract class AbstractBloomFilterTest {
final List<Integer> lst2 = Arrays.asList(4, 5, 6, 7, 8, 9, 10);
final Hasher hasher2 = new StaticHasher(lst2.iterator(), anotherShape);
try {
bf.contains(hasher2);
fail("Should have thrown IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing
}
assertThrows(IllegalArgumentException.class, () -> bf.contains(hasher2),
"Should have thrown IllegalArgumentException");
}
/**
@ -485,12 +474,8 @@ public abstract class AbstractBloomFilterTest {
final Hasher hasher2 = new StaticHasher(lst2.iterator(), anotherShape);
final BloomFilter bf2 = createFilter(hasher2, anotherShape);
try {
bf.merge(bf2);
fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
assertThrows(IllegalArgumentException.class, () -> bf.merge(bf2),
"Should throw IllegalArgumentException");
}
/**
@ -524,12 +509,8 @@ public abstract class AbstractBloomFilterTest {
final List<Integer> lst2 = Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
final Hasher hasher2 = new StaticHasher(lst2.iterator(), anotherShape);
try {
bf.merge(hasher2);
fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException expected) {
// do nothing.
}
assertThrows(IllegalArgumentException.class, () -> bf.merge(hasher2),
"Should throw IllegalArgumentException");
}
/**
@ -638,4 +619,5 @@ public abstract class AbstractBloomFilterTest {
assertEquals(20, bf.xorCardinality(bf2));
assertEquals(20, bf2.xorCardinality(bf));
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.collection;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
@ -118,7 +120,6 @@ import org.apache.commons.collections4.AbstractObjectTest;
* If your {@link Collection} fails one of these tests by design,
* you may still use this base set of cases. Simply override the
* test case (method) your {@link Collection} fails.
*
*/
public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
@ -571,43 +572,27 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
}
resetEmpty();
try {
getCollection().add(getFullNonNullElements()[0]);
fail("Empty collection should not support add.");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().add(getFullNonNullElements()[0]),
"Empty collection should not support add.");
// make sure things didn't change even if the expected exception was
// thrown.
verify();
try {
getCollection().addAll(Arrays.asList(getFullElements()));
fail("Empty collection should not support addAll.");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().addAll(Arrays.asList(getFullElements())),
"Empty collection should not support addAll.");
// make sure things didn't change even if the expected exception was
// thrown.
verify();
resetFull();
try {
getCollection().add(getFullNonNullElements()[0]);
fail("Full collection should not support add.");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().add(getFullNonNullElements()[0]),
"Full collection should not support add.");
// make sure things didn't change even if the expected exception was
// thrown.
verify();
try {
getCollection().addAll(Arrays.asList(getOtherElements()));
fail("Full collection should not support addAll.");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().addAll(Arrays.asList(getOtherElements())),
"Full collection should not support addAll.");
// make sure things didn't change even if the expected exception was
// thrown.
verify();
@ -733,13 +718,9 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
resetEmpty();
Iterator<E> it1 = getCollection().iterator();
assertFalse("Iterator for empty Collection shouldn't have next.", it1.hasNext());
try {
it1.next();
fail("Iterator at end of Collection should throw "
+ "NoSuchElementException when next is called.");
} catch (final NoSuchElementException e) {
// expected
}
Iterator<E> finalIt1 = it1;
assertThrows(NoSuchElementException.class, () -> finalIt1.next(),
"Iterator at end of Collection should throw NoSuchElementException when next is called.");
// make sure nothing has changed after non-modification
verify();
@ -759,12 +740,9 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
getCollection().contains(next));
list.add(next);
}
try {
it1.next();
fail("iterator.next() should raise NoSuchElementException after it finishes");
} catch (final NoSuchElementException e) {
// expected
}
Iterator<E> finalIt2 = it1;
assertThrows(NoSuchElementException.class, () -> finalIt2.next(),
"iterator.next() should raise NoSuchElementException after it finishes");
// make sure nothing has changed after non-modification
verify();
}
@ -779,22 +757,14 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
}
resetEmpty();
try {
getCollection().iterator().remove();
fail("New iterator.remove should raise IllegalState");
} catch (final IllegalStateException e) {
// expected
}
assertThrows(IllegalStateException.class, () -> getCollection().iterator().remove(),
"New iterator.remove should raise IllegalState");
verify();
try {
final Iterator<E> iter = getCollection().iterator();
iter.hasNext();
iter.remove();
fail("New iterator.remove should raise IllegalState even after hasNext");
} catch (final IllegalStateException e) {
// expected
}
final Iterator<E> iter0 = getCollection().iterator();
iter0.hasNext();
assertThrows(IllegalStateException.class, () -> iter0.remove(),
"New iterator.remove should raise IllegalState even after hasNext");
verify();
resetFull();
@ -832,12 +802,9 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
iter = getCollection().iterator();
iter.next();
iter.remove();
try {
iter.remove();
fail("Second iter.remove should raise IllegalState");
} catch (final IllegalStateException e) {
// expected
}
Iterator<E> finalIter = iter;
assertThrows(IllegalStateException.class, () -> finalIter.remove(),
"Second iter.remove should raise IllegalState");
}
/**
@ -1100,21 +1067,13 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
verify();
resetFull();
try {
array = getCollection().toArray(new Void[0]);
fail("toArray(new Void[0]) should raise ArrayStore");
} catch (final ArrayStoreException e) {
// expected
}
assertThrows(ArrayStoreException.class, () -> getCollection().toArray(new Void[0]),
"toArray(new Void[0]) should raise ArrayStore");
verify();
try {
// Casting to Object[] allows compilation on Java 11.
array = getCollection().toArray((Object[]) null);
fail("toArray(null) should raise NPE");
} catch (final NullPointerException e) {
// expected
}
assertThrows(NullPointerException.class, () -> getCollection().toArray((Object[]) null),
"toArray(null) should raise NPE");
verify();
array = getCollection().toArray(new Object[0]);
@ -1167,55 +1126,31 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
}
resetEmpty();
try {
getCollection().clear();
fail("clear should raise UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().clear(),
"clear should raise UnsupportedOperationException");
verify();
try {
getCollection().remove(null);
fail("remove should raise UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().remove(null),
"remove should raise UnsupportedOperationException");
verify();
try {
getCollection().removeIf(e -> true);
fail("removeIf should raise UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().removeIf(e -> true),
"removeIf should raise UnsupportedOperationException");
verify();
try {
getCollection().removeAll(null);
fail("removeAll should raise UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().removeAll(null),
"removeAll should raise UnsupportedOperationException");
verify();
try {
getCollection().retainAll(null);
fail("retainAll should raise UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().retainAll(null),
"retainAll should raise UnsupportedOperationException");
verify();
resetFull();
try {
final Iterator<E> iterator = getCollection().iterator();
iterator.next();
iterator.remove();
fail("iterator.remove should raise UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> iterator.remove(),
"iterator.remove should raise UnsupportedOperationException");
verify();
}
@ -1230,28 +1165,20 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
if (isAddSupported()) {
resetFull();
try {
final Iterator<E> iter = getCollection().iterator();
final Iterator<E> iter0 = getCollection().iterator();
final E o = getOtherElements()[0];
getCollection().add(o);
getConfirmed().add(o);
iter.next();
fail("next after add should raise ConcurrentModification");
} catch (final ConcurrentModificationException e) {
// expected
}
assertThrows(ConcurrentModificationException.class, () -> iter0.next(),
"next after add should raise ConcurrentModification");
verify();
resetFull();
try {
final Iterator<E> iter = getCollection().iterator();
getCollection().addAll(Arrays.asList(getOtherElements()));
getConfirmed().addAll(Arrays.asList(getOtherElements()));
iter.next();
fail("next after addAll should raise ConcurrentModification");
} catch (final ConcurrentModificationException e) {
// expected
}
assertThrows(ConcurrentModificationException.class, () -> iter.next(),
"next after addAll should raise ConcurrentModification");
verify();
}
@ -1271,46 +1198,30 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
}
resetFull();
try {
final Iterator<E> iter = getCollection().iterator();
final Iterator<E> iter0 = getCollection().iterator();
getCollection().remove(getFullElements()[0]);
iter.next();
fail("next after remove should raise ConcurrentModification");
} catch (final ConcurrentModificationException e) {
// expected
}
assertThrows(ConcurrentModificationException.class, () -> iter0.next(),
"next after remove should raise ConcurrentModification");
resetFull();
try {
final Iterator<E> iter = getCollection().iterator();
final Iterator<E> iter1 = getCollection().iterator();
getCollection().removeIf(e -> false);
iter.next();
fail("next after removeIf should raise ConcurrentModification");
} catch (final ConcurrentModificationException e) {
// expected
}
assertThrows(ConcurrentModificationException.class, () -> iter1.next(),
"next after removeIf should raise ConcurrentModification");
resetFull();
try {
final Iterator<E> iter = getCollection().iterator();
final Iterator<E> iter2 = getCollection().iterator();
final List<E> sublist = Arrays.asList(getFullElements()).subList(2, 5);
getCollection().removeAll(sublist);
iter.next();
fail("next after removeAll should raise ConcurrentModification");
} catch (final ConcurrentModificationException e) {
// expected
}
assertThrows(ConcurrentModificationException.class, () -> iter2.next(),
"next after removeAll should raise ConcurrentModification");
resetFull();
try {
final Iterator<E> iter = getCollection().iterator();
final List<E> sublist = Arrays.asList(getFullElements()).subList(2, 5);
getCollection().retainAll(sublist);
iter.next();
fail("next after retainAll should raise ConcurrentModification");
} catch (final ConcurrentModificationException e) {
// expected
}
final Iterator<E> iter3 = getCollection().iterator();
final List<E> sublist3 = Arrays.asList(getFullElements()).subList(2, 5);
getCollection().retainAll(sublist3);
assertThrows(ConcurrentModificationException.class, () -> iter3.next(),
"next after retainAll should raise ConcurrentModification");
}
@Override
@ -1420,4 +1331,5 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
//apparently not
}
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.iterators;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Iterator;
import java.util.NoSuchElementException;
@ -108,11 +110,8 @@ public abstract class AbstractIteratorTest<E> extends AbstractObjectTest {
assertFalse("hasNext() should return false for empty iterators", it.hasNext());
// next() should throw a NoSuchElementException
try {
it.next();
fail("NoSuchElementException must be thrown when Iterator is exhausted");
} catch (final NoSuchElementException e) {
}
assertThrows(NoSuchElementException.class, () -> it.next(),
"NoSuchElementException must be thrown when Iterator is exhausted");
verify();
assertNotNull(it.toString());
@ -145,11 +144,8 @@ public abstract class AbstractIteratorTest<E> extends AbstractObjectTest {
}
// next() must throw NoSuchElementException now
try {
it.next();
fail("NoSuchElementException must be thrown when Iterator is exhausted");
} catch (final NoSuchElementException e) {
}
assertThrows(NoSuchElementException.class, () -> it.next(),
"NoSuchElementException must be thrown when Iterator is exhausted");
assertNotNull(it.toString());
}
@ -169,10 +165,7 @@ public abstract class AbstractIteratorTest<E> extends AbstractObjectTest {
}
// should throw IllegalStateException before next() called
try {
it.remove();
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.remove());
verify();
// remove after next should be fine
@ -180,10 +173,7 @@ public abstract class AbstractIteratorTest<E> extends AbstractObjectTest {
it.remove();
// should throw IllegalStateException for second remove()
try {
it.remove();
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.remove());
}
}

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.ListIterator;
import java.util.NoSuchElementException;
@ -101,18 +103,12 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
assertEquals(-1, it.previousIndex());
// next() should throw a NoSuchElementException
try {
it.next();
fail("NoSuchElementException must be thrown from empty ListIterator");
} catch (final NoSuchElementException e) {
}
assertThrows(NoSuchElementException.class, () -> it.next(),
"NoSuchElementException must be thrown from empty ListIterator");
// previous() should throw a NoSuchElementException
try {
it.previous();
fail("NoSuchElementException must be thrown from empty ListIterator");
} catch (final NoSuchElementException e) {
}
assertThrows(NoSuchElementException.class, () -> it.previous(),
"NoSuchElementException must be thrown from empty ListIterator");
}
/**
@ -128,11 +124,8 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
// check state at end
assertFalse(it.hasNext());
assertTrue(it.hasPrevious());
try {
it.next();
fail("NoSuchElementException must be thrown from next at end of ListIterator");
} catch (final NoSuchElementException e) {
}
assertThrows(NoSuchElementException.class, () -> it.next(),
"NoSuchElementException must be thrown from next at end of ListIterator");
// loop back through comparing
for (int i = list.size() - 1; i >= 0; i--) {
@ -146,11 +139,8 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
// check state at start
assertTrue(it.hasNext());
assertFalse(it.hasPrevious());
try {
it.previous();
fail("NoSuchElementException must be thrown from previous at start of ListIterator");
} catch (final NoSuchElementException e) {
}
assertThrows(NoSuchElementException.class, () -> it.previous(),
"NoSuchElementException must be thrown from previous at start of ListIterator");
}
/**
@ -162,10 +152,9 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
final E addValue = addSetValue();
if (!supportsAdd()) {
// check for UnsupportedOperationException if not supported
try {
it.add(addValue);
fail("UnsupportedOperationException must be thrown from add of " + it.getClass().getSimpleName());
} catch (final UnsupportedOperationException ex) {}
ListIterator<E> finalIt0 = it;
assertThrows(UnsupportedOperationException.class, () -> finalIt0.add(addValue),
"UnsupportedOperationException must be thrown from add of " + it.getClass().getSimpleName());
return;
}
@ -198,18 +187,13 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
if (!supportsSet()) {
// check for UnsupportedOperationException if not supported
try {
it.set(addSetValue());
fail("UnsupportedOperationException must be thrown from set in " + it.getClass().getSimpleName());
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> it.set(addSetValue()),
"UnsupportedOperationException must be thrown from set in " + it.getClass().getSimpleName());
return;
}
// should throw IllegalStateException before next() called
try {
it.set(addSetValue());
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.set(addSetValue()));
// set after next should be fine
it.next();
@ -225,11 +209,8 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
if (supportsRemove() && supportsSet()) {
it.next();
it.remove();
try {
it.set(addSetValue());
fail("IllegalStateException must be thrown from set after remove");
} catch (final IllegalStateException e) {
}
assertThrows(IllegalStateException.class, () -> it.set(addSetValue()),
"IllegalStateException must be thrown from set after remove");
}
}
@ -239,11 +220,8 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
if (supportsAdd() && supportsSet()) {
it.next();
it.add(addSetValue());
try {
it.set(addSetValue());
fail("IllegalStateException must be thrown from set after add");
} catch (final IllegalStateException e) {
}
assertThrows(IllegalStateException.class, () -> it.set(addSetValue()),
"IllegalStateException must be thrown from set after add");
}
}
@ -257,11 +235,8 @@ public abstract class AbstractListIteratorTest<E> extends AbstractIteratorTest<E
if (supportsAdd() && supportsRemove()) {
it.next();
it.add(addSetValue());
try {
it.remove();
fail("IllegalStateException must be thrown from remove after add");
} catch (final IllegalStateException e) {
}
assertThrows(IllegalStateException.class, () -> it.remove(),
"IllegalStateException must be thrown from remove after add");
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.iterators;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.HashSet;
import java.util.Map;
import java.util.NoSuchElementException;
@ -117,22 +119,13 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest
assertFalse(it.hasNext());
// next() should throw a NoSuchElementException
try {
it.next();
fail();
} catch (final NoSuchElementException ex) {}
assertThrows(NoSuchElementException.class, () -> it.next());
// getKey() should throw an IllegalStateException
try {
it.getKey();
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.getKey());
// getValue() should throw an IllegalStateException
try {
it.getValue();
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.getValue());
if (!supportsSetValue()) {
// setValue() should throw an UnsupportedOperationException/IllegalStateException
@ -144,10 +137,7 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest
}
} else {
// setValue() should throw an IllegalStateException
try {
it.setValue(addSetValues()[0]);
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.setValue(addSetValues()[0]));
}
}
@ -198,10 +188,7 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest
final V value = it.getValue();
if (!supportsSetValue()) {
try {
it.setValue(newValue);
fail();
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> it.setValue(newValue));
return;
}
final V old = it.setValue(newValue);
@ -238,11 +225,7 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest
final K key = it.next();
if (!supportsRemove()) {
try {
it.remove();
fail();
} catch (final UnsupportedOperationException ex) {
}
assertThrows(UnsupportedOperationException.class, () -> it.remove());
return;
}
@ -274,10 +257,7 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest
confirmed.remove(key);
verify();
try {
it.setValue(newValue);
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.setValue(newValue));
verify();
}
@ -295,10 +275,7 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest
confirmed.remove(key);
verify();
try {
it.getKey();
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.getKey());
verify();
}
@ -316,10 +293,7 @@ public abstract class AbstractMapIteratorTest<K, V> extends AbstractIteratorTest
confirmed.remove(key);
verify();
try {
it.getValue();
fail();
} catch (final IllegalStateException ex) {}
assertThrows(IllegalStateException.class, () -> it.getValue());
verify();
}

View File

@ -16,11 +16,12 @@
*/
package org.apache.commons.collections4.list;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
/**
* Test case for {@link AbstractLinkedList}.
*
*/
public abstract class AbstractLinkedListTest<E> extends AbstractListTest<E> {
@ -138,36 +139,20 @@ public abstract class AbstractLinkedListTest<E> extends AbstractListTest<E> {
final AbstractLinkedList<E> list = getCollection();
// get marker
assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next);
try {
list.getNode(0, false);
fail("Expecting IndexOutOfBoundsException.");
} catch (final IndexOutOfBoundsException ex) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(0, false),
"Expecting IndexOutOfBoundsException.");
list.addAll( Arrays.asList((E[]) new String[]{"value1", "value2"}));
checkNodes();
list.addFirst((E) "value0");
checkNodes();
list.removeNode(list.getNode(1, false));
checkNodes();
try {
list.getNode(2, false);
fail("Expecting IndexOutOfBoundsException.");
} catch (final IndexOutOfBoundsException ex) {
// expected
}
try {
list.getNode(-1, false);
fail("Expecting IndexOutOfBoundsException.");
} catch (final IndexOutOfBoundsException ex) {
// expected
}
try {
list.getNode(3, true);
fail("Expecting IndexOutOfBoundsException.");
} catch (final IndexOutOfBoundsException ex) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(2, false),
"Expecting IndexOutOfBoundsException.");
assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(-1, false),
"Expecting IndexOutOfBoundsException.");
assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(3, true),
"Expecting IndexOutOfBoundsException.");
}
protected void checkNodes() {
@ -188,4 +173,5 @@ public abstract class AbstractLinkedListTest<E> extends AbstractListTest<E> {
public AbstractLinkedList<E> getCollection() {
return (AbstractLinkedList<E>) super.getCollection();
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.list;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
@ -34,6 +36,7 @@ import java.util.NoSuchElementException;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.collection.AbstractCollectionTest;
import org.apache.commons.collections4.iterators.AbstractListIteratorTest;
import org.junit.jupiter.api.Assertions;
/**
* Abstract test class for {@link java.util.List} methods and contracts.
@ -45,7 +48,6 @@ import org.apache.commons.collections4.iterators.AbstractListIteratorTest;
* you may still use this base set of cases. Simply override the
* test case (method) your {@link List} fails or override one of the
* protected methods from AbstractCollectionTest.
*
*/
public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
@ -176,40 +178,23 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
return;
}
List<E> list;
final E element = getOtherElements()[0];
try {
list = makeObject();
list.add(Integer.MIN_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList0 = makeObject();
assertThrows(IndexOutOfBoundsException.class, () -> finalList0.add(Integer.MIN_VALUE, element),
"List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list = makeObject();
list.add(-1, element);
fail("List.add should throw IndexOutOfBoundsException [-1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList1 = makeObject();
assertThrows(IndexOutOfBoundsException.class, () -> finalList1.add(-1, element),
"List.add should throw IndexOutOfBoundsException [-1]");
try {
list = makeObject();
list.add(1, element);
fail("List.add should throw IndexOutOfBoundsException [1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList2 = makeObject();
assertThrows(IndexOutOfBoundsException.class, () -> finalList2.add(1, element),
"List.add should throw IndexOutOfBoundsException [1]");
try {
list = makeObject();
list.add(Integer.MAX_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList3 = makeObject();
assertThrows(IndexOutOfBoundsException.class, () -> finalList3.add(Integer.MAX_VALUE, element),
"List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
/**
@ -221,40 +206,23 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
return;
}
List<E> list;
final E element = getOtherElements()[0];
try {
list = makeFullCollection();
list.add(Integer.MIN_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList0 = makeFullCollection();
assertThrows(IndexOutOfBoundsException.class, () -> finalList0.add(Integer.MIN_VALUE, element),
"List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list = makeFullCollection();
list.add(-1, element);
fail("List.add should throw IndexOutOfBoundsException [-1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList1 = makeFullCollection();
assertThrows(IndexOutOfBoundsException.class, () -> finalList1.add(-1, element),
"List.add should throw IndexOutOfBoundsException [-1]");
try {
list = makeFullCollection();
list.add(list.size() + 1, element);
fail("List.add should throw IndexOutOfBoundsException [size + 1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList2 = makeFullCollection();
assertThrows(IndexOutOfBoundsException.class, () -> finalList2.add(finalList2.size() + 1, element),
"List.add should throw IndexOutOfBoundsException [size + 1]");
try {
list = makeFullCollection();
list.add(Integer.MAX_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
List<E> finalList3 = makeFullCollection();
assertThrows(IndexOutOfBoundsException.class, () -> finalList3.add(Integer.MAX_VALUE, element),
"List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
/**
@ -383,40 +351,20 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
public void testListGetByIndexBoundsChecking() {
final List<E> list = makeObject();
try {
list.get(Integer.MIN_VALUE);
fail("List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MIN_VALUE),
"List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list.get(-1);
fail("List.get should throw IndexOutOfBoundsException [-1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1),
"List.get should throw IndexOutOfBoundsException [-1]");
try {
list.get(0);
fail("List.get should throw IndexOutOfBoundsException [0]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(0),
"List.get should throw IndexOutOfBoundsException [0]");
try {
list.get(1);
fail("List.get should throw IndexOutOfBoundsException [1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(1),
"List.get should throw IndexOutOfBoundsException [1]");
try {
list.get(Integer.MAX_VALUE);
fail("List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MAX_VALUE),
"List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
/**
@ -426,33 +374,17 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
public void testListGetByIndexBoundsChecking2() {
final List<E> list = makeFullCollection();
try {
list.get(Integer.MIN_VALUE);
fail("List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MIN_VALUE),
"List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list.get(-1);
fail("List.get should throw IndexOutOfBoundsException [-1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1),
"List.get should throw IndexOutOfBoundsException [-1]");
try {
list.get(getFullElements().length);
fail("List.get should throw IndexOutOfBoundsException [size]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(getFullElements().length),
"List.get should throw IndexOutOfBoundsException [size]");
try {
list.get(Integer.MAX_VALUE);
fail("List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(Integer.MAX_VALUE),
"List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
/**
@ -513,42 +445,21 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
final List<E> list = makeObject();
final E element = getOtherElements()[0];
try {
list.set(Integer.MIN_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MIN_VALUE, element),
"List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list.set(-1, element);
fail("List.set should throw IndexOutOfBoundsException [-1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element),
"List.set should throw IndexOutOfBoundsException [-1]");
try {
list.set(0, element);
fail("List.set should throw IndexOutOfBoundsException [0]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(0, element),
"List.set should throw IndexOutOfBoundsException [0]");
try {
list.set(1, element);
fail("List.set should throw IndexOutOfBoundsException [1]");
} catch (final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(1, element),
"List.set should throw IndexOutOfBoundsException [1]");
try {
list.set(Integer.MAX_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (final IndexOutOfBoundsException e) {
// expected
assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MAX_VALUE, element),
"List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
}
/**
* Tests bounds checking for {@link List#set(int,Object)} on a
@ -562,37 +473,18 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
final List<E> list = makeFullCollection();
final E element = getOtherElements()[0];
try {
list.set(Integer.MIN_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException " +
"[Integer.MIN_VALUE]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MIN_VALUE, element),
"List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list.set(-1, element);
fail("List.set should throw IndexOutOfBoundsException [-1]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element),
"List.set should throw IndexOutOfBoundsException [-1]");
try {
list.set(getFullElements().length, element);
fail("List.set should throw IndexOutOfBoundsException [size]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(getFullElements().length, element),
"List.set should throw IndexOutOfBoundsException [size]");
try {
list.set(Integer.MAX_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException " +
"[Integer.MAX_VALUE]");
} catch(final IndexOutOfBoundsException e) {
// expected
assertThrows(IndexOutOfBoundsException.class, () -> list.set(Integer.MAX_VALUE, element),
"List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
}
/**
* Test {@link List#set(int,Object)}.
@ -625,12 +517,8 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
}
resetFull();
try {
getCollection().set(0, getFullElements()[0]);
fail("Empty collection should not support set.");
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> getCollection().set(0, getFullElements()[0]),
"Empty collection should not support set.");
// make sure things didn't change even if the expected exception was
// thrown.
verify();
@ -647,40 +535,20 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
final List<E> list = makeObject();
try {
list.remove(Integer.MIN_VALUE);
fail("List.remove should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MIN_VALUE),
"List.remove should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list.remove(-1);
fail("List.remove should throw IndexOutOfBoundsException [-1]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1),
"List.remove should throw IndexOutOfBoundsException [-1]");
try {
list.remove(0);
fail("List.remove should throw IndexOutOfBoundsException [0]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0),
"List.remove should throw IndexOutOfBoundsException [0]");
try {
list.remove(1);
fail("List.remove should throw IndexOutOfBoundsException [1]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(1),
"List.remove should throw IndexOutOfBoundsException [1]");
try {
list.remove(Integer.MAX_VALUE);
fail("List.remove should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MAX_VALUE),
"List.remove should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
/**
@ -694,37 +562,18 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
final List<E> list = makeFullCollection();
try {
list.remove(Integer.MIN_VALUE);
fail("List.remove should throw IndexOutOfBoundsException " +
"[Integer.MIN_VALUE]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MIN_VALUE),
"List.remove should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
try {
list.remove(-1);
fail("List.remove should throw IndexOutOfBoundsException [-1]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1),
"List.remove should throw IndexOutOfBoundsException [-1]");
try {
list.remove(getFullElements().length);
fail("List.remove should throw IndexOutOfBoundsException [size]");
} catch(final IndexOutOfBoundsException e) {
// expected
}
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(getFullElements().length),
"List.remove should throw IndexOutOfBoundsException [size]");
try {
list.remove(Integer.MAX_VALUE);
fail("List.remove should throw IndexOutOfBoundsException " +
"[Integer.MAX_VALUE]");
} catch(final IndexOutOfBoundsException e) {
// expected
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(Integer.MAX_VALUE),
"List.remove should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
}
}
/**
* Tests {@link List#remove(int)}.
@ -922,12 +771,8 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
assertEquals("nextIndex should be size", max, iter.nextIndex());
assertEquals("previousIndex should be size - 1", max - 1, iter.previousIndex());
try {
iter.next();
fail("Exhausted iterator should raise NoSuchElement");
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> iter.next(),
"Exhausted iterator should raise NoSuchElement");
}
/**
@ -958,13 +803,8 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
final int prevIndex = iter.previousIndex();
assertEquals("previousIndex should be -1", -1, prevIndex);
try {
iter.previous();
fail("Exhausted iterator should raise NoSuchElement");
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> iter.previous(),
"Exhausted iterator should raise NoSuchElement");
}
@ -1315,19 +1155,10 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
}
}
try {
m.invoke(list, params);
fail(m.getName() + " should raise ConcurrentModification");
} catch (final IllegalAccessException e) {
// impossible
} catch (final InvocationTargetException e) {
final Throwable t = e.getTargetException();
if (t instanceof ConcurrentModificationException) {
// expected
return;
}
fail(m.getName() + " raised unexpected " + e);
}
final InvocationTargetException thrown = assertThrows(InvocationTargetException.class, () -> m.invoke(list, params),
m.getName() + " should raise ConcurrentModification");
Assertions.assertTrue(thrown.getTargetException() instanceof ConcurrentModificationException,
m.getName() + " raised unexpected " + thrown.getTargetException());
}
// -----------------------------------------------------------------------
@ -1372,4 +1203,5 @@ public abstract class AbstractListTest<E> extends AbstractCollectionTest<E> {
return AbstractListTest.this.getCollection().listIterator();
}
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Map;
@ -27,7 +29,6 @@ import org.apache.commons.collections4.iterators.AbstractMapIteratorTest;
/**
* Abstract test class for {@link IterableMap} methods and contracts.
*
*/
public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<K, V> {
@ -65,19 +66,15 @@ public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<K, V
Iterator<Map.Entry<K, V>> it = getMap().entrySet().iterator();
final Map.Entry<K, V> val = it.next();
getMap().remove(val.getKey());
try {
it.next();
fail();
} catch (final ConcurrentModificationException ex) {}
Iterator<Map.Entry<K, V>> finalIt0 = it;
assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
resetFull();
it = getMap().entrySet().iterator();
it.next();
getMap().clear();
try {
it.next();
fail();
} catch (final ConcurrentModificationException ex) {}
Iterator<Map.Entry<K, V>> finalIt1 = it;
assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
}
public void testFailFastKeySet() {
@ -91,19 +88,15 @@ public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<K, V
Iterator<K> it = getMap().keySet().iterator();
final K val = it.next();
getMap().remove(val);
try {
it.next();
fail();
} catch (final ConcurrentModificationException ex) {}
Iterator<K> finalIt0 = it;
assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
resetFull();
it = getMap().keySet().iterator();
it.next();
getMap().clear();
try {
it.next();
fail();
} catch (final ConcurrentModificationException ex) {}
Iterator<K> finalIt1 = it;
assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
}
public void testFailFastValues() {
@ -117,19 +110,15 @@ public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<K, V
Iterator<V> it = getMap().values().iterator();
it.next();
getMap().remove(getMap().keySet().iterator().next());
try {
it.next();
fail();
} catch (final ConcurrentModificationException ex) {}
Iterator<V> finalIt0 = it;
assertThrows(ConcurrentModificationException.class, () -> finalIt0.next());
resetFull();
it = getMap().values().iterator();
it.next();
getMap().clear();
try {
it.next();
fail();
} catch (final ConcurrentModificationException ex) {}
Iterator<V> finalIt1 = it;
assertThrows(ConcurrentModificationException.class, () -> finalIt1.next());
}
public BulkTest bulkTestMapIterator() {
@ -206,4 +195,5 @@ public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<K, V
public IterableMap<K, V> getMap() {
return (IterableMap<K, V>) super.getMap();
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
@ -118,7 +120,6 @@ import org.apache.commons.collections4.set.AbstractSetTest;
* fails and/or the methods that define the assumptions used by the test
* cases. For example, if your map does not allow duplicate values, override
* {@link #isAllowDuplicateValues()} and have it return {@code false}
*
*/
public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
@ -588,11 +589,9 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
*/
public void testMapClear() {
if (!isRemoveSupported()) {
try {
resetFull();
getMap().clear();
fail("Expected UnsupportedOperationException on clear");
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> getMap().clear(),
"Expected UnsupportedOperationException on clear");
return;
}
@ -849,10 +848,8 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
}
}
} else {
try {
getMap().put(keys[0], values[0]);
fail("Expected UnsupportedOperationException on put (add)");
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> getMap().put(keys[0], values[0]),
"Expected UnsupportedOperationException on put (add)");
}
}
@ -902,10 +899,8 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
if (!isPutChangeSupported()) {
final Map<K, V> temp = makeFullMap();
resetEmpty();
try {
getMap().putAll(temp);
fail("Expected UnsupportedOperationException on putAll");
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> getMap().putAll(temp),
"Expected UnsupportedOperationException on putAll");
}
return;
}
@ -960,11 +955,9 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
*/
public void testMapRemove() {
if (!isRemoveSupported()) {
try {
resetFull();
getMap().remove(getMap().keySet().iterator().next());
fail("Expected UnsupportedOperationException on remove");
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> getMap().remove(getMap().keySet().iterator().next()),
"Expected UnsupportedOperationException on remove");
return;
}
@ -2031,4 +2024,5 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
public Map<K, V> getConfirmed() {
return confirmed;
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -33,7 +35,6 @@ import org.apache.commons.collections4.iterators.AbstractOrderedMapIteratorTest;
/**
* Abstract test class for {@link OrderedMap} methods and contracts.
*
*/
public abstract class AbstractOrderedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
@ -85,10 +86,8 @@ public abstract class AbstractOrderedMapTest<K, V> extends AbstractIterableMapTe
public void testFirstKey() {
resetEmpty();
OrderedMap<K, V> ordered = getMap();
try {
ordered.firstKey();
fail();
} catch (final NoSuchElementException ex) {}
OrderedMap<K, V> finalOrdered = ordered;
assertThrows(NoSuchElementException.class, () -> finalOrdered.firstKey());
resetFull();
ordered = getMap();
@ -99,10 +98,8 @@ public abstract class AbstractOrderedMapTest<K, V> extends AbstractIterableMapTe
public void testLastKey() {
resetEmpty();
OrderedMap<K, V> ordered = getMap();
try {
ordered.lastKey();
fail();
} catch (final NoSuchElementException ex) {}
OrderedMap<K, V> finalOrdered = ordered;
assertThrows(NoSuchElementException.class, () -> finalOrdered.lastKey());
resetFull();
ordered = getMap();
@ -137,10 +134,8 @@ public abstract class AbstractOrderedMapTest<K, V> extends AbstractIterableMapTe
assertNull(ordered.nextKey(confirmedLast));
if (!isAllowNullKey()) {
try {
ordered.nextKey(null);
fail();
} catch (final NullPointerException ex) {}
OrderedMap<K, V> finalOrdered = ordered;
assertThrows(NullPointerException.class, () -> finalOrdered.nextKey(null));
} else {
assertNull(ordered.nextKey(null));
}
@ -172,10 +167,8 @@ public abstract class AbstractOrderedMapTest<K, V> extends AbstractIterableMapTe
assertNull(ordered.previousKey(confirmedLast));
if (!isAllowNullKey()) {
try {
ordered.previousKey(null);
fail();
} catch (final NullPointerException ex) {}
OrderedMap<K, V> finalOrdered = ordered;
assertThrows(NullPointerException.class, () -> finalOrdered.previousKey(null));
} else {
if (!isAllowNullKey()) {
assertNull(ordered.previousKey(null));
@ -245,4 +238,5 @@ public abstract class AbstractOrderedMapTest<K, V> extends AbstractIterableMapTe
public OrderedMap<K, V> getMap() {
return (OrderedMap<K, V>) super.getMap();
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -28,7 +30,6 @@ import org.apache.commons.collections4.BulkTest;
/**
* Abstract test class for {@link java.util.SortedMap} methods and contracts.
*
*/
public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V> {
@ -235,10 +236,7 @@ public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V>
return;
}
resetEmpty();
try {
getMap().put(toKey, subSortedValues.get(0));
fail();
} catch (final IllegalArgumentException ex) {}
assertThrows(IllegalArgumentException.class, () -> getMap().put(toKey, subSortedValues.get(0)));
verify();
}
@Override
@ -290,10 +288,7 @@ public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V>
return;
}
resetEmpty();
try {
getMap().put(invalidKey, subSortedValues.get(0));
fail();
} catch (final IllegalArgumentException ex) {}
assertThrows(IllegalArgumentException.class, () -> getMap().put(invalidKey, subSortedValues.get(0)));
verify();
}
@Override
@ -352,10 +347,7 @@ public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V>
return;
}
resetEmpty();
try {
getMap().put(toKey, subSortedValues.get(0));
fail();
} catch (final IllegalArgumentException ex) {}
assertThrows(IllegalArgumentException.class, () -> getMap().put(toKey, subSortedValues.get(0)));
verify();
}
@Override
@ -390,4 +382,5 @@ public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V>
public SortedMap<K, V> getConfirmed() {
return (SortedMap<K, V>) super.getConfirmed();
}
}

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.Iterator;
import java.util.Map;
@ -67,19 +69,11 @@ public class PredicatedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
@SuppressWarnings("unchecked")
public void testPut() {
final Map<K, V> map = makeTestMap();
try {
map.put((K) "Hi", (V) Integer.valueOf(3));
fail("Illegal value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)),
"Illegal value should raise IllegalArgument");
try {
map.put((K) Integer.valueOf(3), (V) "Hi");
fail("Illegal key should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"),
"Illegal key should raise IllegalArgument");
assertFalse(map.containsKey(Integer.valueOf(3)));
assertFalse(map.containsValue(Integer.valueOf(3)));
@ -90,28 +84,20 @@ public class PredicatedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
map2.put((K) "C", (V) "c");
map2.put((K) "c", (V) Integer.valueOf(3));
try {
map.putAll(map2);
fail("Illegal value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.putAll(map2),
"Illegal value should raise IllegalArgument");
map.put((K) "E", (V) "e");
Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
try {
final Map.Entry<K, V> entry = iterator.next();
entry.setValue((V) Integer.valueOf(3));
fail("Illegal value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
Map.Entry<K, V> entry = iterator.next();
Map.Entry<K, V> finalEntry = entry;
assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)),
"Illegal value should raise IllegalArgument");
map.put((K) "F", (V) "f");
iterator = map.entrySet().iterator();
final Map.Entry<K, V> entry = iterator.next();
entry = iterator.next();
entry.setValue((V) "x");
}
@Override

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
@ -95,19 +97,11 @@ public class PredicatedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
@SuppressWarnings("unchecked")
public void testPut() {
final Map<K, V> map = makeTestMap();
try {
map.put((K) "Hi", (V) Integer.valueOf(3));
fail("Illegal value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)),
"Illegal value should raise IllegalArgument");
try {
map.put((K) Integer.valueOf(3), (V) "Hi");
fail("Illegal key should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"),
"Illegal key should raise IllegalArgument");
assertFalse(map.containsKey(Integer.valueOf(3)));
assertFalse(map.containsValue(Integer.valueOf(3)));
@ -118,26 +112,19 @@ public class PredicatedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
map2.put((K) "C", (V) "c");
map2.put((K) "c", (V) Integer.valueOf(3));
try {
map.putAll(map2);
fail("Illegal value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.putAll(map2),
"Illegal value should raise IllegalArgument");
map.put((K) "E", (V) "e");
Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
try {
final Map.Entry<K, V> entry = iterator.next();
entry.setValue((V) Integer.valueOf(3));
fail("Illegal value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
Map.Entry<K, V> entry = iterator.next();
Map.Entry<K, V> finalEntry = entry;
assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)),
"Illegal value should raise IllegalArgument");
map.put((K) "F", (V) "f");
iterator = map.entrySet().iterator();
final Map.Entry<K, V> entry = iterator.next();
entry = iterator.next();
entry.setValue((V) "x");
}
@ -147,19 +134,11 @@ public class PredicatedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
final SortedMap<K, V> map = makeTestMap();
map.put((K) "A", (V) "a");
map.put((K) "B", (V) "b");
try {
map.put(null, (V) "c");
fail("Null key should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put(null, (V) "c"),
"Null key should raise IllegalArgument");
map.put((K) "C", (V) "c");
try {
map.put((K) "D", null);
fail("Null value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put((K) "D", null),
"Null value should raise IllegalArgument");
assertEquals("First key should be A", "A", map.firstKey());
assertEquals("Last key should be C", "C", map.lastKey());
assertEquals("First key in tail map should be B",
@ -178,19 +157,11 @@ public class PredicatedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
final SortedMap<K, V> map = makeTestMapWithComparator();
map.put((K) "A", (V) "a");
map.put((K) "B", (V) "b");
try {
map.put(null, (V) "c");
fail("Null key should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put(null, (V) "c"),
"Null key should raise IllegalArgument");
map.put((K) "C", (V) "c");
try {
map.put((K) "D", null);
fail("Null value should raise IllegalArgument");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> map.put((K) "D", null),
"Null value should raise IllegalArgument");
assertEquals("Last key should be A", "A", map.lastKey());
assertEquals("First key should be C", "C", map.firstKey());
assertEquals("First key in tail map should be B",

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Map;
@ -28,7 +30,6 @@ import org.apache.commons.collections4.map.AbstractReferenceMap.ReferenceStrengt
/**
* Tests for ReferenceIdentityMap.
*
*/
public class ReferenceIdentityMapTest<K, V> extends AbstractIterableMapTest<K, V> {
@ -286,18 +287,9 @@ public class ReferenceIdentityMapTest<K, V> extends AbstractIterableMapTest<K, V
assertFalse(getMap().entrySet().contains(null));
assertFalse(getMap().keySet().contains(null));
assertFalse(getMap().values().contains(null));
try {
getMap().put(null, null);
fail();
} catch (final NullPointerException ex) {}
try {
getMap().put((K) new Object(), null);
fail();
} catch (final NullPointerException ex) {}
try {
getMap().put(null, (V) new Object());
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> getMap().put(null, null));
assertThrows(NullPointerException.class, () -> getMap().put((K) new Object(), null));
assertThrows(NullPointerException.class, () -> getMap().put(null, (V) new Object()));
}
/** Tests whether purge values setting works */

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;
@ -37,7 +39,6 @@ import junit.framework.Test;
/**
* Tests for ReferenceMap.
*
*/
public class ReferenceMapTest<K, V> extends AbstractIterableMapTest<K, V> {
@ -90,18 +91,9 @@ public class ReferenceMapTest<K, V> extends AbstractIterableMapTest<K, V> {
assertFalse(map.entrySet().contains(null));
assertFalse(map.containsKey(null));
assertFalse(map.containsValue(null));
try {
map.put(null, null);
fail();
} catch (final NullPointerException ex) {}
try {
map.put((K) new Object(), null);
fail();
} catch (final NullPointerException ex) {}
try {
map.put(null, (V) new Object());
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> map.put(null, null));
assertThrows(NullPointerException.class, () -> map.put((K) new Object(), null));
assertThrows(NullPointerException.class, () -> map.put(null, (V) new Object()));
}
/*
@ -345,4 +337,5 @@ public class ReferenceMapTest<K, V> extends AbstractIterableMapTest<K, V> {
}
}
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
@ -77,18 +79,15 @@ public class TransformedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V>
map.put((K) els[i], (V) els[i]);
assertEquals(i + 1, map.size());
assertTrue(map.containsKey(Integer.valueOf((String) els[i])));
try {
map.containsKey(els[i]);
fail();
} catch (final ClassCastException ex) {}
SortedMap<K, V> finalMap1 = map;
int finalI = i;
assertThrows(ClassCastException.class, () -> finalMap1.containsKey(els[finalI]));
assertTrue(map.containsValue(els[i]));
assertEquals(els[i], map.get(Integer.valueOf((String) els[i])));
}
try {
map.remove(els[0]);
fail();
} catch (final ClassCastException ex) {}
SortedMap<K, V> finalMap = map;
assertThrows(ClassCastException.class, () -> finalMap.remove(els[0]));
assertEquals(els[0], map.remove(Integer.valueOf((String) els[0])));
map = TransformedSortedMap

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.multimap;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -555,13 +557,9 @@ public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTes
test.put((K) "key", (V) "object0");
test.putAll(original);
try {
final MultiValuedMap<K, V> originalNull = null;
test.putAll(originalNull);
fail("expecting NullPointerException");
} catch (final NullPointerException npe) {
// expected
}
assertThrows(NullPointerException.class, () -> test.putAll(originalNull),
"expecting NullPointerException");
assertEquals(2, test.keySet().size());
assertEquals(4, test.size());
@ -587,13 +585,9 @@ public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTes
test.put((K) "keyX", (V) "object0");
test.putAll(original);
try {
final Map<K, V> originalNull = null;
test.putAll(originalNull);
fail("expecting NullPointerException");
} catch (final NullPointerException npe) {
// expected
}
assertThrows(NullPointerException.class, () -> test.putAll(originalNull),
"expecting NullPointerException");
assertEquals(3, test.keySet().size());
assertEquals(4, test.size());
@ -620,12 +614,8 @@ public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTes
assertTrue(map.containsMapping("A", "Y"));
assertTrue(map.containsMapping("A", "Z"));
try {
map.putAll((K) "A", null);
fail("expecting NullPointerException");
} catch (final NullPointerException npe) {
// expected
}
assertThrows(NullPointerException.class, () -> map.putAll((K) "A", null),
"expecting NullPointerException");
assertEquals(3, map.get((K) "A").size());
assertTrue(map.containsMapping("A", "X"));
@ -680,13 +670,9 @@ public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTes
"{B=[U, V, W], A=[X, Y, Z]}".equals(map.toString())
);
try {
final MultiValuedMap<K, V> originalNull = null;
map.putAll(originalNull);
fail("expecting NullPointerException");
} catch (final NullPointerException npe) {
// expected
}
assertThrows(NullPointerException.class, () -> map.putAll(originalNull),
"expecting NullPointerException");
assertTrue(
"{A=[X, Y, Z], B=[U, V, W]}".equals(map.toString()) ||
"{B=[U, V, W], A=[X, Y, Z]}".equals(map.toString())
@ -786,33 +772,17 @@ public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTes
resetFull();
final MapIterator<K, V> mapIt = getMap().mapIterator();
mapIt.next();
try {
mapIt.setValue((V) "some value");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> mapIt.setValue((V) "some value"));
}
public void testMultiValuedMapIterator() {
final MultiValuedMap<K, V> map = makeFullMap();
final MapIterator<K, V> it = map.mapIterator();
try {
it.getKey();
fail();
} catch (final IllegalStateException ise) {
}
try {
it.getValue();
fail();
} catch (final IllegalStateException ise) {
}
assertThrows(IllegalStateException.class, () -> it.getKey());
assertThrows(IllegalStateException.class, () -> it.getValue());
if (isAddSupported()) {
try {
it.setValue((V) "V");
fail();
} catch (final IllegalStateException ise) {
}
assertThrows(IllegalStateException.class, () -> it.setValue((V) "V"));
}
if (!isHashSetValue() && isAddSupported()) {
@ -835,11 +805,7 @@ public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTes
assertEquals("three", it.next());
assertEquals("three", it.getKey());
assertEquals("trois", it.getValue());
try {
it.setValue((V) "threetrois");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> it.setValue((V) "threetrois"));
}
}
@ -1259,4 +1225,5 @@ public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTes
return false;
}
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.multimap;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Set;
import java.util.Map;
import java.util.Map.Entry;
@ -93,57 +95,34 @@ public class UnmodifiableMultiValuedMapTest<K, V> extends AbstractMultiValuedMap
}
public void testDecoratorFactoryNullMap() {
try {
UnmodifiableMultiValuedMap.unmodifiableMultiValuedMap(null);
fail("map must not be null");
} catch (final NullPointerException e) {
// expected
}
assertThrows(NullPointerException.class, () -> UnmodifiableMultiValuedMap.unmodifiableMultiValuedMap(null),
"map must not be null");
}
@SuppressWarnings("unchecked")
public void testAddException() {
final MultiValuedMap<K, V> map = makeObject();
try {
map.put((K) "one", (V) "uno");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> map.put((K) "one", (V) "uno"));
}
public void testRemoveException() {
final MultiValuedMap<K, V> map = makeFullMap();
try {
map.remove("one");
fail();
} catch (final UnsupportedOperationException e) {
// expected, not support remove() method
// UnmodifiableMultiValuedMap does not support change
}
assertThrows(UnsupportedOperationException.class, () -> map.remove("one"),
"not support remove() method UnmodifiableMultiValuedMap does not support change");
this.assertMapContainsAllValues(map);
}
public void testRemoveMappingException() {
final MultiValuedMap<K, V> map = makeFullMap();
try {
map.removeMapping("one", "uno");
fail();
} catch (final UnsupportedOperationException e) {
// expected, not support removeMapping() method
// UnmodifiableMultiValuedMap does not support change
}
assertThrows(UnsupportedOperationException.class, () -> map.removeMapping("one", "uno"),
"expected, not support removeMapping() method UnmodifiableMultiValuedMap does not support change");
this.assertMapContainsAllValues(map);
}
public void testClearException() {
final MultiValuedMap<K, V> map = makeFullMap();
try {
map.clear();
fail();
} catch (final UnsupportedOperationException e) {
// expected, not support clear() method
// UnmodifiableMultiValuedMap does not support change
}
assertThrows(UnsupportedOperationException.class, () -> map.clear(),
"expected, not support clear() method UnmodifiableMultiValuedMap does not support change");
this.assertMapContainsAllValues(map);
}
@ -157,29 +136,14 @@ public class UnmodifiableMultiValuedMapTest<K, V> extends AbstractMultiValuedMap
originalMap.put((K) "keyX", (V) "object1");
originalMap.put((K) "keyY", (V) "object2");
try {
map.putAll(original);
fail();
} catch (final UnsupportedOperationException e) {
// expected, not support putAll() method
// UnmodifiableMultiValuedMap does not support change
}
assertThrows(UnsupportedOperationException.class, () -> map.putAll(original),
"expected, not support putAll() method UnmodifiableMultiValuedMap does not support change");
assertEquals("{}", map.toString());
try {
map.putAll(originalMap);
fail();
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> map.putAll(originalMap));
assertEquals("{}", map.toString());
try {
map.putAll((K) "A", coll);
fail();
} catch (final UnsupportedOperationException e) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> map.putAll((K) "A", coll));
assertEquals("{}", map.toString());
}
@ -187,161 +151,77 @@ public class UnmodifiableMultiValuedMapTest<K, V> extends AbstractMultiValuedMap
public void testUnmodifiableEntries() {
resetFull();
final Collection<Entry<K, V>> entries = getMap().entries();
try {
entries.clear();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> entries.clear());
final Iterator<Entry<K, V>> it = entries.iterator();
final Entry<K, V> entry = it.next();
try {
it.remove();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> it.remove());
try {
entry.setValue((V) "three");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> entry.setValue((V) "three"));
}
@SuppressWarnings("unchecked")
public void testUnmodifiableMapIterator() {
resetFull();
final MapIterator<K, V> mapIt = getMap().mapIterator();
try {
mapIt.remove();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> mapIt.remove());
try {
mapIt.setValue((V) "three");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> mapIt.setValue((V) "three"));
}
@SuppressWarnings("unchecked")
public void testUnmodifiableKeySet() {
resetFull();
final Set<K> keySet = getMap().keySet();
try {
keySet.add((K) "four");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> keySet.add((K) "four"));
try {
keySet.remove("four");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> keySet.remove("four"));
try {
keySet.clear();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> keySet.clear());
final Iterator<K> it = keySet.iterator();
try {
it.remove();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> it.remove());
}
@SuppressWarnings("unchecked")
public void testUnmodifiableValues() {
resetFull();
final Collection<V> values = getMap().values();
try {
values.add((V) "four");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> values.add((V) "four"));
try {
values.remove("four");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> values.remove("four"));
try {
values.clear();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> values.clear());
final Iterator<V> it = values.iterator();
try {
it.remove();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> it.remove());
}
@SuppressWarnings("unchecked")
public void testUnmodifiableAsMap() {
resetFull();
final Map<K, Collection<V>> mapCol = getMap().asMap();
try {
mapCol.put((K) "four", (Collection<V>) Arrays.asList("four"));
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> mapCol.put((K) "four", (Collection<V>) Arrays.asList("four")));
try {
mapCol.remove("four");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> mapCol.remove("four"));
try {
mapCol.clear();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> mapCol.clear());
try {
mapCol.clear();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> mapCol.clear());
}
@SuppressWarnings("unchecked")
public void testUnmodifiableKeys() {
resetFull();
final MultiSet<K> keys = getMap().keys();
try {
keys.add((K) "four");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> keys.add((K) "four"));
try {
keys.remove("four");
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> keys.remove("four"));
try {
keys.clear();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> keys.clear());
final Iterator<K> it = keys.iterator();
try {
it.remove();
fail();
} catch (final UnsupportedOperationException e) {
}
assertThrows(UnsupportedOperationException.class, () -> it.remove());
}
// public void testCreate() throws Exception {

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.multiset;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
@ -374,12 +376,8 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
final Iterator<T> it = multiset.iterator();
it.next();
multiset.remove("A");
try {
it.next();
fail("Should throw ConcurrentModificationException");
} catch (final ConcurrentModificationException e) {
// expected
}
assertThrows(ConcurrentModificationException.class, () -> it.next(),
"Should throw ConcurrentModificationException");
}
@SuppressWarnings("unchecked")
@ -396,12 +394,8 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
it.next();
it.next();
it.next();
try {
it.next();
fail("Should throw NoSuchElementException");
} catch (final NoSuchElementException ex) {
// expected
}
assertThrows(NoSuchElementException.class, () -> it.next(),
"Should throw NoSuchElementException");
}
@SuppressWarnings("unchecked")
@ -420,12 +414,8 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
assertEquals(3, multiset.size());
it.remove();
assertEquals(2, multiset.size());
try {
it.remove();
fail("Should throw IllegalStateException");
} catch (final IllegalStateException ex) {
// expected
}
assertThrows(IllegalStateException.class, () -> it.remove(),
"Should throw IllegalStateException");
assertEquals(2, multiset.size());
it.next();
it.remove();
@ -707,4 +697,5 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
assertEquals(multiset, multiset2);
}
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.multiset;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Set;
import junit.framework.Test;
@ -86,12 +88,8 @@ public class PredicatedMultiSetTest<T> extends AbstractMultiSetTest<T> {
public void testIllegalAdd() {
final MultiSet<T> multiset = makeTestMultiSet();
final Integer i = Integer.valueOf(3);
try {
multiset.add((T) i);
fail("Integer should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> multiset.add((T) i),
"Integer should fail string predicate.");
assertFalse("Collection shouldn't contain illegal element", multiset.contains(i));
}
@ -102,18 +100,10 @@ public class PredicatedMultiSetTest<T> extends AbstractMultiSetTest<T> {
elements.add("two");
elements.add(Integer.valueOf(3));
elements.add("four");
try {
decorateMultiSet((HashMultiSet<T>) elements, stringPredicate());
fail("MultiSet contains an element that should fail the predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
try {
decorateMultiSet(new HashMultiSet<T>(), null);
fail("Expecting NullPointerException for null predicate.");
} catch (final NullPointerException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> decorateMultiSet((HashMultiSet<T>) elements, stringPredicate()),
"MultiSet contains an element that should fail the predicate.");
assertThrows(NullPointerException.class, () -> decorateMultiSet(new HashMultiSet<T>(), null),
"Expecting NullPointerException for null predicate.");
}
@Override

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.multiset;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import junit.framework.Test;
@ -82,38 +84,26 @@ public class UnmodifiableMultiSetTest<E> extends AbstractMultiSetTest<E> {
final MultiSet<E> multiset = makeFullCollection();
assertSame(multiset, UnmodifiableMultiSet.unmodifiableMultiSet(multiset));
try {
UnmodifiableMultiSet.unmodifiableMultiSet(null);
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> UnmodifiableMultiSet.unmodifiableMultiSet(null));
}
public void testAdd() {
final MultiSet<E> multiset = makeFullCollection();
final MultiSet<E> unmodifiableMultiSet = UnmodifiableMultiSet.unmodifiableMultiSet(multiset);
try {
unmodifiableMultiSet.add((E) "One", 1);
fail();
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> unmodifiableMultiSet.add((E) "One", 1));
}
public void testRemove() {
final MultiSet<E> multiset = makeFullCollection();
final MultiSet<E> unmodifiableMultiSet = UnmodifiableMultiSet.unmodifiableMultiSet(multiset);
try {
unmodifiableMultiSet.remove("One", 1);
fail();
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> unmodifiableMultiSet.remove("One", 1));
}
public void testSetCount() {
final MultiSet<E> multiset = makeFullCollection();
final MultiSet<E> unmodifiableMultiSet = UnmodifiableMultiSet.unmodifiableMultiSet(multiset);
try {
unmodifiableMultiSet.setCount((E) "One", 2);
fail();
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> unmodifiableMultiSet.setCount((E) "One", 2));
}
public void testEntrySet() {

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.queue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
@ -161,12 +163,8 @@ public abstract class AbstractQueueTest<E> extends AbstractCollectionTest<E> {
public void testQueueElement() {
resetEmpty();
try {
getCollection().element();
fail("Queue.element should throw NoSuchElementException");
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> getCollection().element(),
"Queue.element should throw NoSuchElementException");
resetFull();
@ -192,12 +190,8 @@ public abstract class AbstractQueueTest<E> extends AbstractCollectionTest<E> {
verify();
}
try {
getCollection().element();
fail("Queue.element should throw NoSuchElementException");
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> getCollection().element(),
"Queue.element should throw NoSuchElementException");
}
/**
@ -245,12 +239,8 @@ public abstract class AbstractQueueTest<E> extends AbstractCollectionTest<E> {
resetEmpty();
try {
getCollection().remove();
fail("Queue.remove should throw NoSuchElementException");
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> getCollection().remove(),
"Queue.remove should throw NoSuchElementException");
resetFull();
@ -262,12 +252,8 @@ public abstract class AbstractQueueTest<E> extends AbstractCollectionTest<E> {
verify();
}
try {
getCollection().element();
fail("Queue.remove should throw NoSuchElementException");
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> getCollection().element(),
"Queue.remove should throw NoSuchElementException");
}
/**

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.queue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
@ -146,48 +148,29 @@ public class CircularFifoQueueTest<E> extends AbstractQueueTest<E> {
verify();
}
try {
getCollection().remove();
fail("Empty queue should raise Underflow.");
} catch (final NoSuchElementException e) {
// expected
}
assertThrows(NoSuchElementException.class, () -> getCollection().remove(),
"Empty queue should raise Underflow.");
}
/**
* Tests that the constructor correctly throws an exception.
*/
public void testConstructorException1() {
try {
new CircularFifoQueue<E>(0);
} catch (final IllegalArgumentException ex) {
return;
}
fail();
assertThrows(IllegalArgumentException.class, () -> new CircularFifoQueue<E>(0));
}
/**
* Tests that the constructor correctly throws an exception.
*/
public void testConstructorException2() {
try {
new CircularFifoQueue<E>(-20);
} catch (final IllegalArgumentException ex) {
return;
}
fail();
assertThrows(IllegalArgumentException.class, () -> new CircularFifoQueue<E>(-20));
}
/**
* Tests that the constructor correctly throws an exception.
*/
public void testConstructorException3() {
try {
new CircularFifoQueue<E>(null);
} catch (final NullPointerException ex) {
return;
}
fail();
assertThrows(NullPointerException.class, () -> new CircularFifoQueue<E>(null));
}
@SuppressWarnings("unchecked")
@ -419,13 +402,7 @@ public class CircularFifoQueueTest<E> extends AbstractQueueTest<E> {
public void testAddNull() {
final CircularFifoQueue<E> b = new CircularFifoQueue<>(2);
try {
b.add(null);
fail();
} catch (final NullPointerException ex) {
return;
}
fail();
assertThrows(NullPointerException.class, () -> b.add(null));
}
public void testDefaultSizeAndGetError1() {
@ -437,12 +414,7 @@ public class CircularFifoQueueTest<E> extends AbstractQueueTest<E> {
fifo.add((E) "4");
fifo.add((E) "5");
assertEquals(5, fifo.size());
try {
fifo.get(5);
} catch (final NoSuchElementException ex) {
return;
}
fail();
assertThrows(NoSuchElementException.class, () -> fifo.get(5));
}
public void testDefaultSizeAndGetError2() {
@ -454,12 +426,7 @@ public class CircularFifoQueueTest<E> extends AbstractQueueTest<E> {
fifo.add((E) "4");
fifo.add((E) "5");
assertEquals(5, fifo.size());
try {
fifo.get(-2);
} catch (final NoSuchElementException ex) {
return;
}
fail();
assertThrows(NoSuchElementException.class, () -> fifo.get(-2));
}
@Override

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.queue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
@ -80,10 +82,7 @@ public class UnmodifiableQueueTest<E> extends AbstractQueueTest<E> {
@Override
public void testQueueRemove() {
resetEmpty();
try {
getCollection().remove();
fail();
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> getCollection().remove());
}
public void testUnmodifiable() {
@ -95,27 +94,18 @@ public class UnmodifiableQueueTest<E> extends AbstractQueueTest<E> {
final Queue<E> queue = makeFullCollection();
assertSame(queue, UnmodifiableQueue.unmodifiableQueue(queue));
try {
UnmodifiableQueue.unmodifiableQueue(null);
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> UnmodifiableQueue.unmodifiableQueue(null));
}
public void testOffer() {
final Queue<E> queue = makeFullCollection();
final E e = null;
try {
queue.offer(e);
fail();
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> queue.offer(e));
}
public void testPoll() {
final Queue<E> queue = makeFullCollection();
try {
queue.poll();
fail();
} catch (final UnsupportedOperationException ex) {}
assertThrows(UnsupportedOperationException.class, () -> queue.poll());
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.set;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@ -30,6 +32,7 @@ import org.apache.commons.collections4.set.CompositeSet.SetMutator;
* @since 3.0
*/
public class CompositeSetTest<E> extends AbstractSetTest<E> {
public CompositeSetTest(final String name) {
super(name);
}
@ -129,12 +132,8 @@ public class CompositeSetTest<E> extends AbstractSetTest<E> {
final HashSet<E> three = new HashSet<>();
three.add((E) "1");
try {
set.addComposited(three);
fail("IllegalArgumentException should have been thrown");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> set.addComposited(three),
"IllegalArgumentException should have been thrown");
}
@SuppressWarnings("unchecked")
@ -159,12 +158,8 @@ public class CompositeSetTest<E> extends AbstractSetTest<E> {
final CompositeSet<E> set5 = new CompositeSet<>(set3);
set5.addComposited(set4);
assertEquals(set, set5);
try {
set.addComposited(set3);
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException ex) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> set.addComposited(set3),
"Expecting UnsupportedOperationException.");
}
@SuppressWarnings("unchecked")
@ -176,18 +171,10 @@ public class CompositeSetTest<E> extends AbstractSetTest<E> {
final HashSet<E> set2 = new HashSet<>();
set2.add((E) "4");
final CompositeSet<E> set3 = new CompositeSet<>(set1);
try {
set3.addComposited(set1, buildOne());
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException ex) {
// expected
}
try {
set3.addComposited(set1, buildOne(), buildTwo());
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException ex) {
// expected
}
assertThrows(UnsupportedOperationException.class, () -> set3.addComposited(set1, buildOne()),
"Expecting UnsupportedOperationException.");
assertThrows(UnsupportedOperationException.class, () -> set3.addComposited(set1, buildOne(), buildTwo()),
"Expecting UnsupportedOperationException.");
}
@Override

View File

@ -16,6 +16,9 @@
*/
package org.apache.commons.collections4.set;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@ -249,31 +252,13 @@ public class ListOrderedSetTest<E>
}
public void testDecorator() {
try {
ListOrderedSet.listOrderedSet((List<E>) null);
fail();
} catch (final NullPointerException ex) {
}
try {
ListOrderedSet.listOrderedSet((Set<E>) null);
fail();
} catch (final NullPointerException ex) {
}
try {
ListOrderedSet.listOrderedSet(null, null);
fail();
} catch (final NullPointerException ex) {
}
try {
ListOrderedSet.listOrderedSet(new HashSet<E>(), null);
fail();
} catch (final NullPointerException ex) {
}
try {
ListOrderedSet.listOrderedSet(null, new ArrayList<E>());
fail();
} catch (final NullPointerException ex) {
}
assertAll(
() -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet((List<E>) null)),
() -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet((Set<E>) null)),
() -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet(null, null)),
() -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet(new HashSet<E>(), null)),
() -> assertThrows(NullPointerException.class, () -> ListOrderedSet.listOrderedSet(null, new ArrayList<E>()))
);
}
@Override

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.set;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NavigableSet;
@ -76,12 +78,8 @@ public class PredicatedNavigableSetTest<E> extends AbstractNavigableSetTest<E> {
public void testIllegalAdd() {
final NavigableSet<E> set = makeTestSet();
final String testString = "B";
try {
set.add((E) testString);
fail("Should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> set.add((E) testString),
"Should fail string predicate.");
assertFalse("Collection shouldn't contain illegal element", set.contains(testString));
}
@ -93,12 +91,8 @@ public class PredicatedNavigableSetTest<E> extends AbstractNavigableSetTest<E> {
elements.add((E) "Atwo");
elements.add((E) "Bthree");
elements.add((E) "Afour");
try {
set.addAll(elements);
fail("Should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> set.addAll(elements),
"Should fail string predicate.");
assertFalse("Set shouldn't contain illegal element", set.contains("Aone"));
assertFalse("Set shouldn't contain illegal element", set.contains("Atwo"));
assertFalse("Set shouldn't contain illegal element", set.contains("Bthree"));

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.set;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.HashSet;
import java.util.Set;
@ -71,12 +73,8 @@ public class PredicatedSetTest<E> extends AbstractSetTest<E> {
public void testIllegalAdd() {
final Set<E> set = makeTestSet();
final Integer i = Integer.valueOf(3);
try {
set.add((E) i);
fail("Integer should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> set.add((E) i),
"Integer should fail string predicate.");
assertFalse("Collection shouldn't contain illegal element", set.contains(i));
}
@ -88,12 +86,8 @@ public class PredicatedSetTest<E> extends AbstractSetTest<E> {
elements.add((E) "two");
elements.add((E) Integer.valueOf(3));
elements.add((E) "four");
try {
set.addAll(elements);
fail("Integer should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> set.addAll(elements),
"Integer should fail string predicate.");
assertFalse("Set shouldn't contain illegal element", set.contains("one"));
assertFalse("Set shouldn't contain illegal element", set.contains("two"));
assertFalse("Set shouldn't contain illegal element", set.contains(Integer.valueOf(3)));

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.set;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
@ -73,12 +75,8 @@ public class PredicatedSortedSetTest<E> extends AbstractSortedSetTest<E> {
public void testIllegalAdd() {
final SortedSet<E> set = makeTestSet();
final String testString = "B";
try {
set.add((E) testString);
fail("Should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> set.add((E) testString),
"Should fail string predicate.");
assertFalse("Collection shouldn't contain illegal element", set.contains(testString));
}
@ -90,12 +88,8 @@ public class PredicatedSortedSetTest<E> extends AbstractSortedSetTest<E> {
elements.add((E) "Atwo");
elements.add((E) "Bthree");
elements.add((E) "Afour");
try {
set.addAll(elements);
fail("Should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> set.addAll(elements),
"Should fail string predicate.");
assertFalse("Set shouldn't contain illegal element", set.contains("Aone"));
assertFalse("Set shouldn't contain illegal element", set.contains("Atwo"));
assertFalse("Set shouldn't contain illegal element", set.contains("Bthree"));

View File

@ -16,6 +16,9 @@
*/
package org.apache.commons.collections4.set;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@ -91,10 +94,7 @@ public class UnmodifiableSortedSetTest<E> extends AbstractSortedSetTest<E> {
final SortedSet<E> set = makeFullCollection();
assertSame(set, UnmodifiableSortedSet.unmodifiableSortedSet(set));
try {
UnmodifiableSortedSet.unmodifiableSortedSet(null);
fail();
} catch (final NullPointerException ex) {}
assertThrows(NullPointerException.class, () -> UnmodifiableSortedSet.unmodifiableSortedSet(null));
}
/**
@ -102,42 +102,20 @@ public class UnmodifiableSortedSetTest<E> extends AbstractSortedSetTest<E> {
*/
@SuppressWarnings("unchecked")
public void verifyUnmodifiable(final Set<E> set) {
try {
set.add((E) "value");
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException e) {
// expected
}
try {
set.addAll(new TreeSet<E>());
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException e) {
// expected
}
try {
set.clear();
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException e) {
// expected
}
try {
set.remove("x");
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException e) {
// expected
}
try {
set.removeAll(array);
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException e) {
// expected
}
try {
set.retainAll(array);
fail("Expecting UnsupportedOperationException.");
} catch (final UnsupportedOperationException e) {
// expected
}
assertAll(
() -> assertThrows(UnsupportedOperationException.class, () -> set.add((E) "value"),
"Expecting UnsupportedOperationException."),
() -> assertThrows(UnsupportedOperationException.class, () -> set.addAll(new TreeSet<E>()),
"Expecting UnsupportedOperationException."),
() -> assertThrows(UnsupportedOperationException.class, () -> set.clear(),
"Expecting UnsupportedOperationException."),
() -> assertThrows(UnsupportedOperationException.class, () -> set.remove("x"),
"Expecting UnsupportedOperationException."),
() -> assertThrows(UnsupportedOperationException.class, () -> set.removeAll(array),
"Expecting UnsupportedOperationException."),
() -> assertThrows(UnsupportedOperationException.class, () -> set.retainAll(array),
"Expecting UnsupportedOperationException.")
);
}
public void testComparator() {