Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the fail() exceptions with Junit5. #159

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 @@
*/
package org.apache.commons.collections4;

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

import org.apache.commons.collections4.list.AbstractListTest;
Expand All @@ -42,11 +46,11 @@ 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
});
if (null != exception.getMessage()) {
assertNotNull(exception.getMessage());
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public boolean isEqualsCheckable() {
//-----------------------------------------------------------------------
public void testObjectEqualsSelf() {
final Object obj = makeObject();
assertEquals("A Object should equal itself", obj, obj);
assertEquals(obj, obj);
}

public void testEqualsNull() {
Expand All @@ -111,24 +111,18 @@ public void testEqualsNull() {

public void testObjectHashCodeEqualsSelfHashCode() {
final Object obj = makeObject();
assertEquals("hashCode should be repeatable", obj.hashCode(), obj.hashCode());
assertEquals(obj.hashCode(), obj.hashCode());
}

public void testObjectHashCodeEqualsContract() {
final Object obj1 = makeObject();
if (obj1.equals(obj1)) {
assertEquals(
"[1] When two objects are equal, their hashCodes should be also.",
obj1.hashCode(), obj1.hashCode());
assertEquals(obj1.hashCode(), obj1.hashCode());
}
final Object obj2 = makeObject();
if (obj1.equals(obj2)) {
assertEquals(
"[2] When two objects are equal, their hashCodes should be also.",
obj1.hashCode(), obj2.hashCode());
assertTrue(
"When obj1.equals(obj2) is true, then obj2.equals(obj1) should also be true",
obj2.equals(obj1));
assertEquals(obj1.hashCode(), obj2.hashCode());
assertTrue(obj2.equals(obj1));
}
}

Expand All @@ -150,7 +144,7 @@ public void testSerializeDeserializeThenCompare() throws Exception {
if (obj instanceof Serializable && isTestSerialization()) {
final Object dest = serializeDeserialize(obj);
if (isEqualsCheckable()) {
assertEquals("obj != deserialize(serialize(obj))", obj, dest);
assertEquals(obj, dest);
}
}
}
Expand Down Expand Up @@ -180,9 +174,7 @@ public void testCanonicalEmptyCollectionExists() {
final Object object = makeObject();
if (object instanceof Serializable) {
final String name = getCanonicalEmptyCollectionName(object);
assertTrue(
"Canonical empty collection (" + name + ") is not in SCM",
new File(name).exists());
assertTrue(new File(name).exists());
}
}
}
Expand All @@ -196,9 +188,7 @@ public void testCanonicalFullCollectionExists() {
final Object object = makeObject();
if (object instanceof Serializable) {
final String name = getCanonicalFullCollectionName(object);
assertTrue(
"Canonical full collection (" + name + ") is not in SCM",
new File(name).exists());
assertTrue(new File(name).exists());
}
}
}
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
Loading