Skip to content

Commit

Permalink
Updated the fail() exceptions with Junit5.
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed May 19, 2020
1 parent 6d30bbe commit da7b8ba
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
package org.apache.commons.collections4.iterators;

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 java.util.Iterator;
import java.util.NoSuchElementException;

Expand Down Expand Up @@ -107,13 +112,14 @@ public void testEmptyIterator() {
final Iterator<E> it = makeEmptyIterator();

// hasNext() should return false
assertEquals("hasNext() should return false for empty iterators", false, it.hasNext());
assertFalse(it.hasNext());

// next() should throw a NoSuchElementException
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
it.next();
fail("NoSuchElementException must be thrown when Iterator is exhausted");
} catch (final NoSuchElementException e) {
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
verify();

Expand All @@ -131,14 +137,10 @@ public void testFullIterator() {
final Iterator<E> it = makeObject();

// hasNext() must be true (ensure makeFullIterator is correct!)
assertEquals("hasNext() should return true for at least one element", true, it.hasNext());
assertTrue(it.hasNext());

// 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");
}
it.next();

// iterate through
while (it.hasNext()) {
Expand All @@ -147,10 +149,11 @@ public void testFullIterator() {
}

// next() must throw NoSuchElementException now
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
it.next();
fail("NoSuchElementException must be thrown when Iterator is exhausted");
} catch (final NoSuchElementException e) {
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}

assertNotNull(it.toString());
Expand All @@ -164,28 +167,35 @@ public void testRemove() {

if (!supportsRemove()) {
// check for UnsupportedOperationException if not supported
try {
Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
it.remove();
} catch (final UnsupportedOperationException ex) {}
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
return;
}

// should throw IllegalStateException before next() called
try {
Exception exception = assertThrows(IllegalStateException.class, () -> {
it.remove();
fail();
} catch (final IllegalStateException ex) {}
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
verify();

// remove after next should be fine
it.next();
it.remove();

// should throw IllegalStateException for second remove()
try {
exception = assertThrows(IllegalStateException.class, () -> {
it.remove();
fail();
} catch (final IllegalStateException ex) {}
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
*/
package org.apache.commons.collections4.iterators;

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 java.util.ArrayList;
import java.util.ListIterator;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -97,23 +103,25 @@ public void testEmptyListIteratorIsIndeedEmpty() {

final ListIterator<E> it = makeEmptyIterator();

assertEquals(false, it.hasNext());
assertFalse(it.hasNext());
assertEquals(0, it.nextIndex());
assertEquals(false, it.hasPrevious());
assertFalse(it.hasPrevious());
assertEquals(-1, it.previousIndex());

// next() should throw a NoSuchElementException
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
it.next();
fail("NoSuchElementException must be thrown from empty ListIterator");
} catch (final NoSuchElementException e) {
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}

// previous() should throw a NoSuchElementException
try {
exception = assertThrows(NoSuchElementException.class, () -> {
it.previous();
fail("NoSuchElementException must be thrown from empty ListIterator");
} catch (final NoSuchElementException e) {
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
}

Expand All @@ -128,12 +136,13 @@ public void testWalkForwardAndBack() {
}

// check state at end
assertEquals(false, it.hasNext());
assertEquals(true, it.hasPrevious());
try {
assertFalse(it.hasNext());
assertTrue(it.hasPrevious());
Exception exception = assertThrows(NoSuchElementException.class, () -> {
it.next();
fail("NoSuchElementException must be thrown from next at end of ListIterator");
} catch (final NoSuchElementException e) {
});
if (null != exception.getMessage()) {
assertTrue(exception.getMessage().contains("element"));
}

// loop back through comparing
Expand All @@ -146,33 +155,36 @@ public void testWalkForwardAndBack() {
}

// check state at start
assertEquals(true, it.hasNext());
assertEquals(false, it.hasPrevious());
try {
assertTrue(it.hasNext());
assertFalse(it.hasPrevious());
exception = assertThrows(NoSuchElementException.class, () -> {
it.previous();
fail("NoSuchElementException must be thrown from previous at start of ListIterator");
} catch (final NoSuchElementException e) {
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
}

/**
* Test add behavior.
*/
public void testAdd() {
ListIterator<E> it = makeObject();
final ListIterator<E> it1 = makeObject();

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) {}
Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
it1.add(addValue);
});
if (null != exception.getMessage()) {
assertTrue(exception.getMessage().contains("not support"));
}
return;
}

// add at start should be OK, added should be previous
it = makeObject();
ListIterator<E> it = makeObject();
it.add(addValue);
assertEquals(addValue, it.previous());

Expand Down Expand Up @@ -200,18 +212,22 @@ public void testSet() {

if (!supportsSet()) {
// check for UnsupportedOperationException if not supported
try {
Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
it.set(addSetValue());
fail("UnsupportedOperationException must be thrown from set in " + it.getClass().getSimpleName());
} catch (final UnsupportedOperationException ex) {}
});
if (null != exception.getMessage()) {
assertTrue(exception.getMessage().contains("not support"));
}
return;
}

// should throw IllegalStateException before next() called
try {
Exception exception = assertThrows(IllegalStateException.class, () -> {
it.set(addSetValue());
fail();
} catch (final IllegalStateException ex) {}
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}

// set after next should be fine
it.next();
Expand All @@ -227,11 +243,10 @@ public void testRemoveThenSet() {
if (supportsRemove() && supportsSet()) {
it.next();
it.remove();
try {
Exception exception = assertThrows(IllegalStateException.class, () -> {
it.set(addSetValue());
fail("IllegalStateException must be thrown from set after remove");
} catch (final IllegalStateException e) {
}
});
assertNull(exception.getMessage());
}
}

Expand All @@ -241,10 +256,11 @@ public void testAddThenSet() {
if (supportsAdd() && supportsSet()) {
it.next();
it.add(addSetValue());
try {
Exception exception = assertThrows(IllegalStateException.class, () -> {
it.set(addSetValue());
fail("IllegalStateException must be thrown from set after add");
} catch (final IllegalStateException e) {
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
}
}
Expand All @@ -259,10 +275,11 @@ public void testAddThenRemove() {
if (supportsAdd() && supportsRemove()) {
it.next();
it.add(addSetValue());
try {
Exception exception = assertThrows(IllegalStateException.class, () -> {
it.remove();
fail("IllegalStateException must be thrown from remove after add");
} catch (final IllegalStateException e) {
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
}
}
Expand Down
Loading

0 comments on commit da7b8ba

Please sign in to comment.