Skip to content

Commit

Permalink
[COLLECTIONS-777] Migrate to JUnit 5
Browse files Browse the repository at this point in the history
[COLLECTIONS-809] Update try/catch/fail logic to assertThrows
  • Loading branch information
garydgregory committed Oct 20, 2024
1 parent 4754807 commit 0ea3686
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 249 deletions.
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix generics in org.apache.commons.collections4.IteratorUtils.chainedIterator(Collection).</action>
<action type="fix" dev="ggregory" due-to="Benjamin Confino, Gary Gregory" issue="COLLECTIONS-856">Javadoc: Document interaction between peek and filter iterator #515.</action>
<action type="fix" dev="ggregory" due-to="Elia Bertolina, Gary Gregory" issue="COLLECTIONS-815">Javadoc: Update ClosureUtils Javadoc to match runtime.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory" issue="COLLECTIONS-815">Javadoc: Update ClosureUtils Javadoc to match runtime.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory" issue="COLLECTIONS-777">Migrate to JUnit 5.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">LayerManager.Builder implements Supplier.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory, hemanth0525">Add CollectionUtils.duplicateList(Collection).</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.apache.commons.collections4.bag;

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

import java.io.IOException;
import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package org.apache.commons.collections4.iterators;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
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.Iterator;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -132,11 +132,7 @@ public void testFullIterator() {
assertTrue(it.hasNext(), "hasNext() should return true for at least one element");

// next() must not throw exception (ensure makeFullIterator is correct!)
try {
it.next();
} catch (final NoSuchElementException e) {
fail("Full iterators must have at least one element");
}
assertDoesNotThrow(it::next, "Full iterators must have at least one element");

// iterate through
while (it.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,12 @@ public void testRemove() { // override
assertThrows(UnsupportedOperationException.class, () -> it.remove());
return;
}

it.remove();
confirmed.remove(key);
assertFalse(map.containsKey(key));
verify();

try {
it.remove(); // second remove fails
} catch (final IllegalStateException ex) {
}
// second remove fails
assertThrows(NoSuchElementException.class, it::remove, "Full iterators must have at least one element");
verify();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Iterator;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -85,10 +84,7 @@ public void testIndexedArray() {
iter.next();
}

assertEquals(
count,
testArray.length - 2,
"the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ");
assertEquals(count, testArray.length - 2, "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ");
assertAll(
() -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, -1),
"new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException"),
Expand All @@ -99,17 +95,11 @@ public void testIndexedArray() {
() -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, 0, testArray.length + 1),
"new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException"),
() -> assertThrows(IllegalArgumentException.class, () -> makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2),
"new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException")
);

try {
iter = makeArrayIterator(testArray, 1, 1);
// expected not to fail
} catch (final IllegalArgumentException iae) {
// MODIFIED: an iterator over a zero-length section of array
// should be perfectly legal behavior
fail("new ArrayIterator(Object,1,1) should NOT throw an IllegalArgumentException");
}
"new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException"));

iter = makeArrayIterator(testArray, 1, 1);
// MODIFIED: an iterator over a zero-length section of array
// should be perfectly legal behavior
}

@Test
Expand All @@ -118,17 +108,10 @@ public void testIterator() {
for (final int element : testArray) {
final Integer testValue = Integer.valueOf(element);
final Number iterValue = (Number) iter.next();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

assertFalse(iter.hasNext(), "Iterator should now be empty");

try {
iter.next();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}
assertThrows(NoSuchElementException.class, iter::next);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,19 @@ public boolean supportsRemove() {
@Test
public void testListIterator() {
final ListIterator<E> iter = makeObject();

// TestArrayIterator#testIterator() has already tested the iterator forward,
// now we need to test it in reverse

// fast-forward the iterator to the end...
while (iter.hasNext()) {
iter.next();
}

for (int x = testArray.length - 1; x >= 0; x--) {
final Object testValue = testArray[x];
final Object iterValue = iter.previous();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

assertFalse(iter.hasPrevious(), "Iterator should now be empty");

try {
iter.previous();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}

assertThrows(NoSuchElementException.class, iter::previous);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

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.assertNull;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
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.ArrayList;
Expand Down Expand Up @@ -224,11 +225,7 @@ public void testNullComparator() {
final List<Integer> l2 = Arrays.asList(2, 4, 6);

final CollatingIterator<Integer> collatingIterator1 = new CollatingIterator<>(null, l1.iterator(), l2.iterator());
try {
collatingIterator1.next();
} catch (final NullPointerException e) {
assertTrue(e.getMessage().startsWith("You must invoke setComparator"));
}
assertThrows(NullPointerException.class, collatingIterator1::next, "You must invoke setComparator");

int i = 0;
final CollatingIterator<Integer> collatingIterator2 = new CollatingIterator<>(null, l1.iterator(), l2.iterator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ public void testIterator() {
assertEquals(testValue, iterValue, "Iteration value is correct");
}
assertFalse(iter.hasNext(), "Iterator should now be empty");
try {
iter.next();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}
assertThrows(NoSuchElementException.class, iter::next);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,10 @@ public void testIterator() {
final Iterator<String> iter = makeObject();
for (final String testValue : testArray) {
final Object iterValue = iter.next();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

assertFalse(iter.hasNext(), "Iterator should now be empty");

try {
iter.next();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}
assertThrows(NoSuchElementException.class, iter::next);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,39 +74,22 @@ public void testIterator() {
final ListIterator<E> iter = makeObject();
for (final String testValue : testArray) {
final Object iterValue = iter.next();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

assertFalse(iter.hasNext(), "Iterator should now be empty");

try {
iter.next();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}

assertThrows(NoSuchElementException.class, iter::next);
// now, read it backwards
for (int i = testArray.length - 1; i > -1; --i) {
final Object testValue = testArray[i];
final E iterValue = iter.previous();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

try {
iter.previous();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}

assertThrows(NoSuchElementException.class, iter::previous);
// now, read it forwards again
for (final String testValue : testArray) {
final Object iterValue = iter.next();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,39 +75,22 @@ public void testIterator() {
final ListIterator<E> iter = makeObject();
for (final String testValue : testArray) {
final Object iterValue = iter.next();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

assertFalse(iter.hasNext(), "Iterator should now be empty");

try {
iter.next();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}

assertThrows(NoSuchElementException.class, iter::next);
// now, read it backwards
for (int i = testArray.length - 1; i > -1; --i) {
final Object testValue = testArray[i];
final E iterValue = iter.previous();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

try {
iter.previous();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}

assertThrows(NoSuchElementException.class, iter::previous);
// now, read it forwards again
for (final String testValue : testArray) {
final Object iterValue = iter.next();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,10 @@ public void testIterator() {
final Iterator<E> iter = makeObject();
for (final String testValue : testArray) {
final E iterValue = iter.next();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

assertFalse(iter.hasNext(), "Iterator should now be empty");

try {
iter.next();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}
assertThrows(NoSuchElementException.class, iter::next);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,19 @@ public ObjectArrayListIterator<E> makeObject() {
@Test
public void testListIterator() {
final ListIterator<E> iter = makeObject();

// TestArrayIterator#testIterator() has already tested the iterator forward,
// now we need to test it in reverse

// fast-forward the iterator to the end...
while (iter.hasNext()) {
iter.next();
}

for (int x = testArray.length - 1; x >= 0; x--) {
final Object testValue = testArray[x];
final Object iterValue = iter.previous();

assertEquals(testValue, iterValue, "Iteration value is correct");
}

assertFalse(iter.hasPrevious(), "Iterator should now be empty");

try {
iter.previous();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}

assertThrows(NoSuchElementException.class, iter::previous);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

import java.util.Iterator;
Expand Down Expand Up @@ -70,17 +71,10 @@ public boolean supportsRemove() {
public void testIterator() {
final Iterator<E> iter = makeObject();
assertTrue(iter.hasNext(), "Iterator has a first item");

final E iterValue = iter.next();
assertEquals(testValue, iterValue, "Iteration value is correct");

assertFalse(iter.hasNext(), "Iterator should now be empty");

try {
iter.next();
} catch (final Exception e) {
assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
}
assertThrows(NoSuchElementException.class, iter::next);
}

@Test
Expand Down
Loading

0 comments on commit 0ea3686

Please sign in to comment.