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 9f37668 commit b68fc69
Show file tree
Hide file tree
Showing 122 changed files with 3,751 additions and 3,955 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public boolean add(final E object) {
*/
@Override
public void add(final int index, final E object) {
checkInterval(index, 0, size());
// adds element if it is not contained already
if (set.contains(object) == false) {
set.add(object);
Expand Down Expand Up @@ -429,5 +430,17 @@ public void set(final E object) {
throw new UnsupportedOperationException("ListIterator does not support set");
}
}

/**
* Checks whether the index is valid.
*
* @param index the index to check
* @param startIndex the first allowed index
* @param endIndex the last allowed index
* @throws IndexOutOfBoundsException if the index is invalid
*/
private void checkInterval(final int index, final int startIndex, final int endIndex) {
if (index < startIndex || index > endIndex) {
throw new IndexOutOfBoundsException("Invalid index:" + index + ", size=" + size());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
package org.apache.commons.collections4;

import static org.junit.jupiter.api.Assertions.assertNotNull;
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 org.apache.commons.collections4.list.AbstractListTest;
Expand All @@ -42,11 +47,14 @@ public void testNewArrayList() {
assertTrue("New list is empty", list.isEmpty());
assertEquals("New list has size zero", 0, list.size());

try {
Exception exception = assertThrows(IndexOutOfBoundsException.class, () -> {
list.get(1);
fail("get(int i) should have thrown IndexOutOfBoundsException");
} catch (final IndexOutOfBoundsException e) {
// Expected result
});
String actualMessage = exception.getMessage();
if (null == actualMessage) {
assertNull(actualMessage);
} else {
assertNotNull(actualMessage);
}
}

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

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

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
Expand Down Expand Up @@ -110,13 +113,10 @@ public void testLinkedListAddLast() {
*/
public void testLinkedListGetFirst() {
resetEmpty();
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
getCollection().getFirst();
fail("getFirst() should throw a NoSuchElementException for an " +
"empty list.");
} catch (final NoSuchElementException e) {
// This is correct
}
});
assertNull(exception.getMessage());
verify();

resetFull();
Expand All @@ -132,13 +132,10 @@ public void testLinkedListGetFirst() {
*/
public void testLinkedListGetLast() {
resetEmpty();
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
getCollection().getLast();
fail("getLast() should throw a NoSuchElementException for an " +
"empty list.");
} catch (final NoSuchElementException e) {
// This is correct
}
});
assertNull(exception.getMessage());
verify();

resetFull();
Expand All @@ -158,13 +155,10 @@ public void testLinkedListRemoveFirst() {
}

resetEmpty();
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
getCollection().removeFirst();
fail("removeFirst() should throw a NoSuchElementException for " +
"an empty list.");
} catch (final NoSuchElementException e) {
// This is correct
}
});
assertNull(exception.getMessage());
verify();

resetFull();
Expand All @@ -184,13 +178,10 @@ public void testLinkedListRemoveLast() {
}

resetEmpty();
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
getCollection().removeLast();
fail("removeLast() should throw a NoSuchElementException for " +
"an empty list.");
} catch (final NoSuchElementException e) {
// This is correct
}
});
assertNull(exception.getMessage());
verify();

resetFull();
Expand Down
20 changes: 9 additions & 11 deletions src/test/java/org/apache/commons/collections4/ArrayStackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.commons.collections4;

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

import java.util.EmptyStackException;

import junit.framework.Test;
Expand Down Expand Up @@ -46,20 +49,15 @@ public void testNewStack() {
assertTrue("New stack is empty", stack.empty());
assertEquals("New stack has size zero", 0, stack.size());

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

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

});
assertNull(exception.getMessage());
}

@SuppressWarnings("unchecked")
Expand Down
140 changes: 63 additions & 77 deletions src/test/java/org/apache/commons/collections4/BagUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.apache.commons.collections4;

import static org.junit.Assert.*;
import static org.junit.Assert.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;
Expand Down Expand Up @@ -45,133 +47,117 @@ public class BagUtilsTest {
@Test
public void testSynchronizedBag() {
final Bag<Object> bag = BagUtils.synchronizedBag(new HashBag<>());
assertTrue("Returned object should be a SynchronizedBag.",
bag instanceof SynchronizedBag);
try {
assertTrue(bag instanceof SynchronizedBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.synchronizedBag(null);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));
}

@Test
public void testUnmodifiableBag() {
final Bag<Object> bag = BagUtils.unmodifiableBag(new HashBag<>());
assertTrue("Returned object should be an UnmodifiableBag.",
bag instanceof UnmodifiableBag);
try {
assertTrue(bag instanceof UnmodifiableBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.unmodifiableBag(null);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));

assertSame("UnmodifiableBag shall not be decorated", bag, BagUtils.unmodifiableBag(bag));
}

@Test
public void testPredicatedBag() {
final Bag<Object> bag = BagUtils.predicatedBag(new HashBag<>(), truePredicate);
assertTrue("Returned object should be a PredicatedBag.",
bag instanceof PredicatedBag);
try {
assertTrue(bag instanceof PredicatedBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.predicatedBag(null, truePredicate);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
try {
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));

exception = assertThrows(NullPointerException.class, () -> {
BagUtils.predicatedBag(new HashBag<>(), null);
fail("Expecting NullPointerException for null predicate.");
} catch (final NullPointerException ex) {
// expected
}
});
actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("predicate"));
}

@Test
public void testTransformedBag() {
final Bag<Object> bag = BagUtils.transformingBag(new HashBag<>(), nopTransformer);
assertTrue("Returned object should be an TransformedBag.",
bag instanceof TransformedBag);
try {
assertTrue(bag instanceof TransformedBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.transformingBag(null, nopTransformer);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
try {
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));

exception = assertThrows(NullPointerException.class, () -> {
BagUtils.transformingBag(new HashBag<>(), null);
fail("Expecting NullPointerException for null transformer.");
} catch (final NullPointerException ex) {
// expected
}
});
actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("transformer"));
}

@Test
public void testSynchronizedSortedBag() {
final Bag<Object> bag = BagUtils.synchronizedSortedBag(new TreeBag<>());
assertTrue("Returned object should be a SynchronizedSortedBag.",
bag instanceof SynchronizedSortedBag);
try {
assertTrue(bag instanceof SynchronizedSortedBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.synchronizedSortedBag(null);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));
}

@Test
public void testUnmodifiableSortedBag() {
final SortedBag<Object> bag = BagUtils.unmodifiableSortedBag(new TreeBag<>());
assertTrue("Returned object should be an UnmodifiableSortedBag.",
bag instanceof UnmodifiableSortedBag);
try {
assertTrue(bag instanceof UnmodifiableSortedBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.unmodifiableSortedBag(null);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));

assertSame("UnmodifiableSortedBag shall not be decorated", bag, BagUtils.unmodifiableSortedBag(bag));
}

@Test
public void testPredicatedSortedBag() {
final Bag<Object> bag = BagUtils.predicatedSortedBag(new TreeBag<>(), truePredicate);
assertTrue("Returned object should be a PredicatedSortedBag.",
bag instanceof PredicatedSortedBag);
try {
assertTrue(bag instanceof PredicatedSortedBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.predicatedSortedBag(null, truePredicate);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
try {
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));

exception = assertThrows(NullPointerException.class, () -> {
BagUtils.predicatedSortedBag(new TreeBag<>(), null);
fail("Expecting NullPointerException for null predicate.");
} catch (final NullPointerException ex) {
// expected
}
});
actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("predicate"));
}

@Test
public void testTransformedSortedBag() {
final Bag<Object> bag = BagUtils.transformingSortedBag(new TreeBag<>(), nopTransformer);
assertTrue("Returned object should be an TransformedSortedBag",
bag instanceof TransformedSortedBag);
try {
assertTrue(bag instanceof TransformedSortedBag);
Exception exception = assertThrows(NullPointerException.class, () -> {
BagUtils.transformingSortedBag(null, nopTransformer);
fail("Expecting NullPointerException for null bag.");
} catch (final NullPointerException ex) {
// expected
}
try {
});
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("collection"));

exception = assertThrows(NullPointerException.class, () -> {
BagUtils.transformingSortedBag(new TreeBag<>(), null);
fail("Expecting NullPointerException for null transformer.");
} catch (final NullPointerException ex) {
// expected
}
});
actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("transformer"));
}
}

Expand Down
Loading

0 comments on commit b68fc69

Please sign in to comment.