Use Assertions.assertInstanceOf()

This commit is contained in:
Gary Gregory 2024-09-01 19:08:34 -04:00
parent e205e071bb
commit 03e5b0ee3b
12 changed files with 42 additions and 35 deletions

View File

@ -17,9 +17,9 @@
package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.commons.collections4.bag.HashBag;
import org.apache.commons.collections4.bag.PredicatedBag;
@ -45,7 +45,7 @@ public class BagUtilsTest {
@Test
public void testPredicatedBag() {
final Bag<Object> bag = BagUtils.predicatedBag(new HashBag<>(), truePredicate);
assertTrue(bag instanceof PredicatedBag, "Returned object should be a PredicatedBag.");
assertInstanceOf(PredicatedBag.class, bag, "Returned object should be a PredicatedBag.");
assertAll(
() -> assertThrows(NullPointerException.class, () -> BagUtils.predicatedBag(null, truePredicate),
"Expecting NullPointerException for null bag."),
@ -57,7 +57,7 @@ public class BagUtilsTest {
@Test
public void testPredicatedSortedBag() {
final Bag<Object> bag = BagUtils.predicatedSortedBag(new TreeBag<>(), truePredicate);
assertTrue(bag instanceof PredicatedSortedBag, "Returned object should be a PredicatedSortedBag.");
assertInstanceOf(PredicatedSortedBag.class, bag, "Returned object should be a PredicatedSortedBag.");
assertAll(
() -> assertThrows(NullPointerException.class, () -> BagUtils.predicatedSortedBag(null, truePredicate),
"Expecting NullPointerException for null bag."),
@ -69,7 +69,7 @@ public class BagUtilsTest {
@Test
public void testSynchronizedBag() {
final Bag<Object> bag = BagUtils.synchronizedBag(new HashBag<>());
assertTrue(bag instanceof SynchronizedBag, "Returned object should be a SynchronizedBag.");
assertInstanceOf(SynchronizedBag.class, bag, "Returned object should be a SynchronizedBag.");
assertThrows(NullPointerException.class, () -> BagUtils.synchronizedBag(null),
"Expecting NullPointerException for null bag.");
}
@ -77,7 +77,7 @@ public class BagUtilsTest {
@Test
public void testSynchronizedSortedBag() {
final Bag<Object> bag = BagUtils.synchronizedSortedBag(new TreeBag<>());
assertTrue(bag instanceof SynchronizedSortedBag, "Returned object should be a SynchronizedSortedBag.");
assertInstanceOf(SynchronizedSortedBag.class, bag, "Returned object should be a SynchronizedSortedBag.");
assertThrows(NullPointerException.class, () -> BagUtils.synchronizedSortedBag(null),
"Expecting NullPointerException for null bag.");
}
@ -85,7 +85,7 @@ public class BagUtilsTest {
@Test
public void testTransformedBag() {
final Bag<Object> bag = BagUtils.transformingBag(new HashBag<>(), nopTransformer);
assertTrue(bag instanceof TransformedBag, "Returned object should be an TransformedBag.");
assertInstanceOf(TransformedBag.class, bag, "Returned object should be an TransformedBag.");
assertAll(
() -> assertThrows(NullPointerException.class, () -> BagUtils.transformingBag(null, nopTransformer),
"Expecting NullPointerException for null bag."),
@ -97,7 +97,7 @@ public class BagUtilsTest {
@Test
public void testTransformedSortedBag() {
final Bag<Object> bag = BagUtils.transformingSortedBag(new TreeBag<>(), nopTransformer);
assertTrue(bag instanceof TransformedSortedBag, "Returned object should be an TransformedSortedBag");
assertInstanceOf(TransformedSortedBag.class, bag, "Returned object should be an TransformedSortedBag");
assertAll(
() -> assertThrows(NullPointerException.class, () -> BagUtils.transformingSortedBag(null, nopTransformer),
"Expecting NullPointerException for null bag."),
@ -109,7 +109,7 @@ public class BagUtilsTest {
@Test
public void testUnmodifiableBag() {
final Bag<Object> bag = BagUtils.unmodifiableBag(new HashBag<>());
assertTrue(bag instanceof UnmodifiableBag, "Returned object should be an UnmodifiableBag.");
assertInstanceOf(UnmodifiableBag.class, bag, "Returned object should be an UnmodifiableBag.");
assertThrows(NullPointerException.class, () -> BagUtils.unmodifiableBag(null),
"Expecting NullPointerException for null bag.");
assertSame(bag, BagUtils.unmodifiableBag(bag), "UnmodifiableBag shall not be decorated");
@ -118,7 +118,7 @@ public class BagUtilsTest {
@Test
public void testUnmodifiableSortedBag() {
final SortedBag<Object> bag = BagUtils.unmodifiableSortedBag(new TreeBag<>());
assertTrue(bag instanceof UnmodifiableSortedBag, "Returned object should be an UnmodifiableSortedBag.");
assertInstanceOf(UnmodifiableSortedBag.class, bag, "Returned object should be an UnmodifiableSortedBag.");
assertThrows(NullPointerException.class, () -> BagUtils.unmodifiableSortedBag(null),
"Expecting NullPointerException for null bag.");
assertSame(bag, BagUtils.unmodifiableSortedBag(bag), "UnmodifiableSortedBag shall not be decorated");

View File

@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -1746,7 +1747,7 @@ public class CollectionUtilsTest extends MockTestCase {
public void testPredicatedCollection() {
final Predicate<Object> predicate = PredicateUtils.instanceofPredicate(Integer.class);
final Collection<Number> collection = CollectionUtils.predicatedCollection(new ArrayList<>(), predicate);
assertTrue(collection instanceof PredicatedCollection, "returned object should be a PredicatedCollection");
assertInstanceOf(PredicatedCollection.class, collection, "returned object should be a PredicatedCollection");
}
@Test
@ -2321,7 +2322,7 @@ public class CollectionUtilsTest extends MockTestCase {
@Deprecated
public void testSynchronizedCollection() {
final Collection<Object> col = CollectionUtils.synchronizedCollection(new ArrayList<>());
assertTrue(col instanceof SynchronizedCollection, "Returned object should be a SynchronizedCollection.");
assertInstanceOf(SynchronizedCollection.class, col, "Returned object should be a SynchronizedCollection.");
assertThrows(NullPointerException.class, () -> CollectionUtils.synchronizedCollection(null),
"Expecting NullPointerException for null collection.");
@ -2366,7 +2367,7 @@ public class CollectionUtilsTest extends MockTestCase {
public void testTransformedCollection() {
final Transformer<Object, Object> transformer = TransformerUtils.nopTransformer();
final Collection<Object> collection = CollectionUtils.transformingCollection(new ArrayList<>(), transformer);
assertTrue(collection instanceof TransformedCollection, "returned object should be a TransformedCollection");
assertInstanceOf(TransformedCollection.class, collection, "returned object should be a TransformedCollection");
}
@Test
@ -2430,7 +2431,7 @@ public class CollectionUtilsTest extends MockTestCase {
@Deprecated
public void testUnmodifiableCollection() {
final Collection<Object> col = CollectionUtils.unmodifiableCollection(new ArrayList<>());
assertTrue(col instanceof UnmodifiableCollection, "Returned object should be a UnmodifiableCollection.");
assertInstanceOf(UnmodifiableCollection.class, col, "Returned object should be a UnmodifiableCollection.");
assertThrows(NullPointerException.class, () -> CollectionUtils.unmodifiableCollection(null),
"Expecting NullPointerException for null collection.");

View File

@ -19,6 +19,7 @@ package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@ -365,7 +366,7 @@ public class ListUtilsTest {
public void testPredicatedList() {
final Predicate<Object> predicate = String.class::isInstance;
final List<Object> list = ListUtils.predicatedList(new ArrayList<>(), predicate);
assertTrue(list instanceof PredicatedList, "returned object should be a PredicatedList");
assertInstanceOf(PredicatedList.class, list, "returned object should be a PredicatedList");
assertAll(
() -> assertThrows(NullPointerException.class, () -> ListUtils.predicatedList(new ArrayList<>(), null),
"Expecting IllegalArgumentException for null predicate."),

View File

@ -19,6 +19,7 @@ package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@ -737,7 +738,7 @@ public class MapUtilsTest {
public void testLazyMapFactory() {
final Factory<Integer> factory = FactoryUtils.constantFactory(Integer.valueOf(5));
Map<Object, Object> map = MapUtils.lazyMap(new HashMap<>(), factory);
assertTrue(map instanceof LazyMap);
assertInstanceOf(LazyMap.class, map);
assertAll(
() -> assertThrows(NullPointerException.class, () -> MapUtils.lazyMap(new HashMap<>(), (Factory<Object>) null),
"Expecting NullPointerException for null factory"),
@ -747,7 +748,7 @@ public class MapUtilsTest {
final Transformer<Object, Integer> transformer = TransformerUtils.asTransformer(factory);
map = MapUtils.lazyMap(new HashMap<>(), transformer);
assertTrue(map instanceof LazyMap);
assertInstanceOf(LazyMap.class, map);
assertAll(
() -> assertThrows(NullPointerException.class, () -> MapUtils.lazyMap(new HashMap<>(), (Transformer<Object, Object>) null),
"Expecting NullPointerException for null transformer"),
@ -811,7 +812,7 @@ public class MapUtilsTest {
inMap.put("key1", "value1");
inMap.put("key2", "value2");
final Map<String, String> map = MapUtils.orderedMap(inMap);
assertTrue(map instanceof OrderedMap);
assertInstanceOf(OrderedMap.class, map);
}
@Test
@ -876,7 +877,7 @@ public class MapUtilsTest {
public void testPredicatedMap() {
final Predicate<Object> p = getPredicate();
final Map<Object, Object> map = MapUtils.predicatedMap(new HashMap<>(), p, p);
assertTrue(map instanceof PredicatedMap);
assertInstanceOf(PredicatedMap.class, map);
assertThrows(NullPointerException.class, () -> MapUtils.predicatedMap(null, p, p),
"Expecting NullPointerException for null map.");

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -41,7 +42,7 @@ public class QueueUtilsTest {
@Test
public void testEmptyQueue() {
final Queue<Object> queue = QueueUtils.emptyQueue();
assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue.");
assertInstanceOf(UnmodifiableQueue.class, queue, "Returned object should be an UnmodifiableQueue.");
assertTrue(queue.isEmpty(), "Returned queue is not empty.");
assertThrows(UnsupportedOperationException.class, () -> queue.add(new Object()),
@ -51,7 +52,7 @@ public class QueueUtilsTest {
@Test
public void testPredicatedQueue() {
final Queue<Object> queue = QueueUtils.predicatedQueue(new LinkedList<>(), truePredicate);
assertTrue(queue instanceof PredicatedQueue, "Returned object should be a PredicatedQueue.");
assertInstanceOf(PredicatedQueue.class, queue, "Returned object should be a PredicatedQueue.");
assertThrows(NullPointerException.class, () -> QueueUtils.predicatedQueue(null, truePredicate),
"Expecting NullPointerException for null queue.");
@ -63,7 +64,7 @@ public class QueueUtilsTest {
@Test
public void testSynchronizedQueue() {
final Queue<Object> queue = QueueUtils.synchronizedQueue(new LinkedList<>());
assertTrue(queue instanceof SynchronizedQueue, "Returned object should be a SynchronizedQueue.");
assertInstanceOf(SynchronizedQueue.class, queue, "Returned object should be a SynchronizedQueue.");
assertThrows(NullPointerException.class, () -> QueueUtils.synchronizedQueue(null),
"Expecting NullPointerException for null queue.");
@ -72,7 +73,7 @@ public class QueueUtilsTest {
@Test
public void testTransformedQueue() {
final Queue<Object> queue = QueueUtils.transformingQueue(new LinkedList<>(), nopTransformer);
assertTrue(queue instanceof TransformedQueue, "Returned object should be an TransformedQueue.");
assertInstanceOf(TransformedQueue.class, queue, "Returned object should be an TransformedQueue.");
assertThrows(NullPointerException.class, () -> QueueUtils.transformingQueue(null, nopTransformer),
"Expecting NullPointerException for null queue.");
@ -84,7 +85,7 @@ public class QueueUtilsTest {
@Test
public void testUnmodifiableQueue() {
final Queue<Object> queue = QueueUtils.unmodifiableQueue(new LinkedList<>());
assertTrue(queue instanceof UnmodifiableQueue, "Returned object should be an UnmodifiableQueue.");
assertInstanceOf(UnmodifiableQueue.class, queue, "Returned object should be an UnmodifiableQueue.");
assertThrows(NullPointerException.class, () -> QueueUtils.unmodifiableQueue(null),
"Expecting NullPointerException for null queue.");

View File

@ -19,6 +19,7 @@ package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@ -210,7 +211,7 @@ public class SetUtilsTest {
public void testpredicatedSet() {
final Predicate<Object> predicate = String.class::isInstance;
final Set<Object> set = SetUtils.predicatedSet(new HashSet<>(), predicate);
assertTrue(set instanceof PredicatedSet, "returned object should be a PredicatedSet");
assertInstanceOf(PredicatedSet.class, set, "returned object should be a PredicatedSet");
assertAll(
() -> assertThrows(NullPointerException.class, () -> SetUtils.predicatedSet(new HashSet<>(), null),
"Expecting NullPointerException for null predicate."),

View File

@ -18,6 +18,7 @@ package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -93,7 +94,7 @@ public class SplitMapUtilsTest {
}
// unmodifiable
assertTrue(map instanceof Unmodifiable);
assertInstanceOf(Unmodifiable.class, map);
// check individual operations
int sz = map.size();

View File

@ -16,9 +16,9 @@
*/
package org.apache.commons.collections4;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.commons.collections4.trie.PatriciaTrie;
import org.apache.commons.collections4.trie.UnmodifiableTrie;
@ -32,7 +32,7 @@ public class TrieUtilsTest {
@Test
public void testUnmodifiableTrie() {
final Trie<String, Object> trie = TrieUtils.unmodifiableTrie(new PatriciaTrie<>());
assertTrue(trie instanceof UnmodifiableTrie, "Returned object should be an UnmodifiableTrie.");
assertInstanceOf(UnmodifiableTrie.class, trie, "Returned object should be an UnmodifiableTrie.");
assertThrows(NullPointerException.class, () -> TrieUtils.unmodifiableTrie(null));

View File

@ -17,7 +17,7 @@
package org.apache.commons.collections4.comparators;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.FileNotFoundException;
@ -159,7 +159,7 @@ public abstract class AbstractComparatorTest<T> extends AbstractObjectTest {
@Test
public void testComparatorIsSerializable() {
final Comparator<T> comparator = makeObject();
assertTrue(comparator instanceof Serializable,
assertInstanceOf(Serializable.class, comparator,
"This comparator should be Serializable.");
}

View File

@ -16,9 +16,9 @@
*/
package org.apache.commons.collections4.keyvalue;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Map;
@ -89,7 +89,7 @@ public class UnmodifiableMapEntryTest<K, V> extends AbstractMapEntryTest<K, V> {
assertSame(key, entry2.getKey());
assertSame(value, entry2.getValue());
assertTrue(entry instanceof Unmodifiable);
assertInstanceOf(Unmodifiable.class, entry);
}
@Test

View File

@ -19,10 +19,10 @@ package org.apache.commons.collections4.map;
import static org.apache.commons.collections4.map.LazySortedMap.lazySortedMap;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Comparator;
import java.util.Map;
@ -133,7 +133,7 @@ public class LazySortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
public void testTransformerDecorate() {
final Transformer<Object, Integer> transformer = TransformerUtils.asTransformer(oneFactory);
final SortedMap<Integer, Number> map = lazySortedMap(new TreeMap<>(), transformer);
assertTrue(map instanceof LazySortedMap);
assertInstanceOf(LazySortedMap.class, map);
assertAll(
() -> assertThrows(NullPointerException.class, () -> lazySortedMap(new TreeMap<>(), (Transformer<Integer, Number>) null),
"Expecting NullPointerException for null transformer"),

View File

@ -17,6 +17,7 @@
package org.apache.commons.collections4.map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashMap;
@ -101,7 +102,7 @@ public class SingletonMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
assertEquals(1, map.size());
assertTrue(map.isFull());
assertEquals(1, map.maxSize());
assertTrue(map instanceof BoundedMap);
assertInstanceOf(BoundedMap.class, map);
}
@Test
@ -167,7 +168,7 @@ public class SingletonMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
assertEquals(1, map.size());
assertEquals(ONE, map.getKey());
assertEquals(TWO, map.getValue());
assertTrue(map instanceof KeyValue);
assertInstanceOf(KeyValue.class, map);
}
// public void testCreate() throws Exception {