Skip to content
Permalink
Browse files
COLLECTIONS-777 JUnit v5 (#283)
JUnit v5 assertThrows PredicatedBagTest

JUnit v5 assertThrows PredicatedSortedBagTest

JUnit v5 assertThrows TreeBagTest

JUnit v5 assertThrows AbstractBagTest

JUnit v5 assertThrows AbstractBidiMapTest

JUnit v5 assertThrows AbstractOrderedBidiMapTest

JUnit v5 assertThrows PredicateUtilsTest

JUnit v5 assertThrows MultiMapUtilsTest

JUnit v5 assertThrows EnumerationUtilsTest

JUnit v5 assertThrows ArrayStackTest
  • Loading branch information
nhojpatrick committed Mar 4, 2022
1 parent e113297 commit 4d06b47d5527c5242a177b7ddc88a19aa0e05adc
Showing 10 changed files with 91 additions and 166 deletions.
@@ -16,13 +16,14 @@
*/
package org.apache.commons.collections4;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.EmptyStackException;

import junit.framework.Test;

/**
* Tests ArrayStack.
*
*/
@SuppressWarnings("deprecation") // we test a deprecated class
public class ArrayStackTest<E> extends AbstractArrayListTest<E> {
@@ -45,20 +46,9 @@ public void testNewStack() {
assertTrue("New stack is empty", stack.empty());
assertEquals("New stack has size zero", 0, stack.size());

try {
stack.peek();
fail("peek() should have thrown EmptyStackException");
} catch (final EmptyStackException e) {
// Expected result
}

try {
stack.pop();
fail("pop() should have thrown EmptyStackException");
} catch (final EmptyStackException e) {
// Expected result
}
assertThrows(EmptyStackException.class, () -> stack.peek());

assertThrows(EmptyStackException.class, () -> stack.pop());
}

@SuppressWarnings("unchecked")
@@ -87,7 +77,6 @@ public void testPushPeekPop() {
assertEquals("Popped item is 'First Item'",
"First Item", (String) stack.pop());
assertEquals("Stack size is zero", 0, stack.size());

}

@Override
@@ -103,7 +92,6 @@ public void testSearch() {
2, stack.search("First Item"));
assertEquals("Cannot find 'Missing Item'",
-1, stack.search("Missing Item"));

}

@Override
@@ -17,9 +17,9 @@
package org.apache.commons.collections4;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.Enumeration;
@@ -33,7 +33,6 @@

/**
* Tests EnumerationUtils.
*
*/
public class EnumerationUtilsTest {

@@ -51,12 +50,9 @@ public void getFromEnumeration() throws Exception {
assertEquals("one", EnumerationUtils.get(en, 1));

// Enumerator, non-existent entry
try {
EnumerationUtils.get(en, 3);
fail("Expecting IndexOutOfBoundsException.");
} catch (final IndexOutOfBoundsException e) {
// expected
}
Enumeration<String> finalEn = en;
assertThrows(IndexOutOfBoundsException.class, () -> EnumerationUtils.get(finalEn, 3));

assertFalse(en.hasMoreElements());
}

@@ -76,12 +72,7 @@ public void testAsIterableFor() {

@Test
public void testAsIterableForNull() {
try {
EnumerationUtils.asIterable((Enumeration) null).iterator().next();
fail("Expecting NullPointerException");
} catch (final NullPointerException ex) {
// success
}
assertThrows(NullPointerException.class, () -> EnumerationUtils.asIterable((Enumeration) null).iterator().next());
}

@Test
@@ -19,8 +19,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Arrays;
import java.util.Collection;
@@ -43,22 +43,16 @@ public class MultiMapUtilsTest {
public void testEmptyUnmodifiableMultiValuedMap() {
final MultiValuedMap map = MultiMapUtils.EMPTY_MULTI_VALUED_MAP;
assertTrue(map.isEmpty());
try {
map.put("key", "value");
fail("Should throw UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
}

assertThrows(UnsupportedOperationException.class, () -> map.put("key", "value"));
}

@Test
public void testTypeSafeEmptyMultiValuedMap() {
final MultiValuedMap<String, String> map = MultiMapUtils.<String, String>emptyMultiValuedMap();
assertTrue(map.isEmpty());
try {
map.put("key", "value");
fail("Should throw UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
}

assertThrows(UnsupportedOperationException.class, () -> map.put("key", "value"));
}

@Test
@@ -23,7 +23,6 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.ArrayList;
import java.util.Arrays;
@@ -58,16 +57,10 @@ public class PredicateUtilsTest extends AbstractPredicateTest {
public void testExceptionPredicate() {
assertNotNull(PredicateUtils.exceptionPredicate());
assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils.exceptionPredicate());
try {
PredicateUtils.exceptionPredicate().evaluate(null);
} catch (final FunctorException ex) {
try {
PredicateUtils.exceptionPredicate().evaluate(cString);
} catch (final FunctorException ex2) {
return;
}
}
fail();

assertThrows(FunctorException.class, () -> PredicateUtils.exceptionPredicate().evaluate(null));

assertThrows(FunctorException.class, () -> PredicateUtils.exceptionPredicate().evaluate(cString));
}

// notNullPredicate
@@ -666,10 +659,8 @@ public void testTransformedPredicate() {
final Predicate<Object> p = EqualPredicate.<Object>equalPredicate("Hello");
assertFalse(PredicateUtils.transformedPredicate(t, p).evaluate(null));
assertTrue(PredicateUtils.transformedPredicate(t, p).evaluate(Boolean.TRUE));
try {
PredicateUtils.transformedPredicate(null, null);
fail();
} catch (final NullPointerException ex) {}

assertThrows(NullPointerException.class, () -> PredicateUtils.transformedPredicate(null, null));
}

// misc tests
@@ -701,6 +692,5 @@ public void testSingletonPatternInSerialization() {
protected Predicate<?> generatePredicate() {
return truePredicate(); //Just return something to satisfy super class.
}
}


}
@@ -34,6 +34,7 @@
import org.apache.commons.collections4.set.AbstractSetTest;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Abstract test class for {@link org.apache.commons.collections4.Bag Bag} methods and contracts.
@@ -59,7 +60,6 @@
* interface (prefix testBag). For Bag specific tests use the {@link #makeObject()} and
* {@link #makeFullCollection()} methods instead of {@link #resetEmpty()} and resetFull(),
* otherwise the collection will be wrapped by a {@link CollectionBag} decorator.
*
*/
public abstract class AbstractBagTest<T> extends AbstractCollectionTest<T> {

@@ -108,7 +108,6 @@ public Bag<T> makeFullCollection() {
return bag;
}


@Override
public void resetEmpty() {
this.setCollection(CollectionBag.collectionBag(makeObject()));
@@ -379,12 +378,8 @@ public void testBagIteratorFail() {
final Iterator<T> it = bag.iterator();
it.next();
bag.remove("A");
try {
it.next();
fail("Should throw ConcurrentModificationException");
} catch (final ConcurrentModificationException e) {
// expected
}

assertThrows(ConcurrentModificationException.class, () -> it.next());
}

@SuppressWarnings("unchecked")
@@ -401,12 +396,8 @@ public void testBagIteratorFailNoMore() {
it.next();
it.next();
it.next();
try {
it.next();
fail("Should throw NoSuchElementException");
} catch (final NoSuchElementException ex) {
// expected
}

assertThrows(NoSuchElementException.class, () -> it.next());
}

@SuppressWarnings("unchecked")
@@ -425,12 +416,9 @@ public void testBagIteratorFailDoubleRemove() {
assertEquals(3, bag.size());
it.remove();
assertEquals(2, bag.size());
try {
it.remove();
fail("Should throw IllegalStateException");
} catch (final IllegalStateException ex) {
// expected
}

assertThrows(IllegalStateException.class, () -> it.remove());

assertEquals(2, bag.size());
it.next();
it.remove();
@@ -590,7 +578,6 @@ public void testBagHashCode() {
assertEquals(total, bag2.hashCode());
}


/**
* Bulk test {@link Bag#uniqueSet()}. This method runs through all of
* the tests in {@link AbstractSetTest}.
@@ -604,6 +591,7 @@ public BulkTest bulkTestBagUniqueSet() {
}

public class TestBagUniqueSet extends AbstractSetTest<T> {

public TestBagUniqueSet() {
super("");
}
@@ -666,8 +654,8 @@ public void resetFull() {
public void verify() {
super.verify();
}
}

}

/**
* Compare the current serialized form of the Bag
@@ -696,4 +684,5 @@ public void testFullBagCompatibility() throws IOException, ClassNotFoundExceptio
assertEquals(bag, bag2);
}
}

}
@@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.bag;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Set;

import junit.framework.Test;
@@ -85,12 +87,9 @@ public void testlegalAddRemove() {
public void testIllegalAdd() {
final Bag<T> bag = makeTestBag();
final Integer i = 3;
try {
bag.add((T) i);
fail("Integer should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}

assertThrows(IllegalArgumentException.class, () -> bag.add((T) i));

assertFalse("Collection shouldn't contain illegal element", bag.contains(i));
}

@@ -101,18 +100,10 @@ public void testIllegalDecorate() {
elements.add("two");
elements.add(3);
elements.add("four");
try {
decorateBag((HashBag<T>) elements, stringPredicate());
fail("Bag contains an element that should fail the predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
try {
decorateBag(new HashBag<T>(), null);
fail("Expecting NullPointerException for null predicate.");
} catch (final NullPointerException e) {
// expected
}

assertThrows(IllegalArgumentException.class, () -> decorateBag((HashBag<T>) elements, stringPredicate()));

assertThrows(NullPointerException.class, () -> decorateBag(new HashBag<T>(), null));
}

@Override
@@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.bag;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Comparator;

import junit.framework.Test;
@@ -69,14 +71,10 @@ protected SortedBag<T> makeTestBag() {
public void testDecorate() {
final SortedBag<T> bag = decorateBag(new TreeBag<T>(), stringPredicate());
((PredicatedSortedBag<T>) bag).decorated();
try {
decorateBag(new TreeBag<T>(), null);
fail("Expecting NullPointerException for null predicate");
} catch (final NullPointerException e) {}
try {
decorateBag(nullBag, stringPredicate());
fail("Expecting NullPointerException for null bag");
} catch (final NullPointerException e) {}

assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag<T>(), null));

assertThrows(NullPointerException.class, () -> decorateBag(nullBag, stringPredicate()));
}

@SuppressWarnings("unchecked")

0 comments on commit 4d06b47

Please sign in to comment.