Skip to content

Commit

Permalink
Updated the exceptions with Junit5.
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed May 18, 2020
1 parent b0317fd commit 4e8ca01
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 355 deletions.
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.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -67,11 +73,13 @@ public void testEmptyMapIterator() {
super.testEmptyMapIterator();

final OrderedMapIterator<K, V> it = makeEmptyIterator();
assertEquals(false, it.hasPrevious());
try {
assertFalse(it.hasPrevious());
Exception exception = assertThrows(NoSuchElementException.class, () -> {
it.previous();
fail();
} catch (final NoSuchElementException ex) {}
});
if (null != exception.getMessage()) {
assertTrue(exception.getMessage().contains("Iterator contains no elements"));
}
}

//-----------------------------------------------------------------------
Expand All @@ -89,42 +97,42 @@ public void testFullMapIterator() {
final OrderedMapIterator<K, V> it = makeObject();
final Map<K, V> map = getMap();

assertEquals(true, it.hasNext());
assertEquals(false, it.hasPrevious());
assertTrue(it.hasNext());
assertFalse(it.hasPrevious());
final Set<K> set = new HashSet<>();
while (it.hasNext()) {
// getKey
final K key = it.next();
assertSame("it.next() should equals getKey()", key, it.getKey());
assertTrue("Key must be in map", map.containsKey(key));
assertTrue("Key must be unique", set.add(key));
assertTrue(map.containsKey(key));
assertTrue(set.add(key));

// getValue
final V value = it.getValue();
if (!isGetStructuralModify()) {
assertSame("Value must be mapped to key", map.get(key), value);
}
assertTrue("Value must be in map", map.containsValue(value));
assertTrue(map.containsValue(value));

assertEquals(true, it.hasPrevious());
assertTrue(it.hasPrevious());

verify();
}
while (it.hasPrevious()) {
// getKey
final Object key = it.previous();
assertSame("it.previous() should equals getKey()", key, it.getKey());
assertTrue("Key must be in map", map.containsKey(key));
assertTrue("Key must be unique", set.remove(key));
assertTrue(map.containsKey(key));
assertTrue(set.remove(key));

// getValue
final Object value = it.getValue();
if (!isGetStructuralModify()) {
assertSame("Value must be mapped to key", map.get(key), value);
}
assertTrue("Value must be in map", map.containsValue(value));
assertTrue(map.containsValue(value));

assertEquals(true, it.hasNext());
assertTrue(it.hasNext());

verify();
}
Expand All @@ -145,8 +153,8 @@ public void testMapIteratorOrder() {
assertEquals("keySet() not consistent", new ArrayList<>(map.keySet()), new ArrayList<>(map.keySet()));

final Iterator<K> it2 = map.keySet().iterator();
assertEquals(true, it.hasNext());
assertEquals(true, it2.hasNext());
assertTrue(it.hasNext());
assertTrue(it2.hasNext());
final List<K> list = new ArrayList<>();
while (it.hasNext()) {
final K key = it.next();
Expand Down
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.assertEquals;
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.Iterator;
import java.util.NoSuchElementException;

Expand Down Expand Up @@ -56,24 +61,19 @@ public void testIterator() {
assertEquals("Iteration value is correct", testValue, iterValue);
}

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

try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
} catch (final Exception e) {
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals(new NoSuchElementException().getClass()));
}
});
assertNull(exception.getMessage());
}

public void testNullArray() {
try {
Exception exception = assertThrows(NullPointerException.class, () -> {
new ArrayIterator<>(null);
fail("Constructor should throw a NullPointerException when constructed with a null array");
} catch (final NullPointerException e) {
// expected
}
});
assertNull(exception.getMessage());
}

public void testReset() {
Expand Down
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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
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.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -81,11 +87,10 @@ public void testBounded() {
assertEquals("f", iter.next());

assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand Down Expand Up @@ -114,11 +119,10 @@ public void testSameAsDecorated() {
assertEquals("g", iter.next());

assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand All @@ -130,11 +134,10 @@ public void testSameAsDecorated() {
public void testEmptyBounded() {
final Iterator<E> iter = new BoundedIterator<>(testList.iterator(), 3, 0);
assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand All @@ -143,11 +146,10 @@ public void testEmptyBounded() {
*/
@Test
public void testNegativeOffset() {
try {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
new BoundedIterator<>(testList.iterator(), -1, 4);
fail("Expected IllegalArgumentException.");
} catch (final IllegalArgumentException iae) { /* Success case */
}
});
assertTrue(exception.getMessage().contains("Offset parameter must not be negative"));
}

/**
Expand All @@ -156,11 +158,10 @@ public void testNegativeOffset() {
*/
@Test
public void testNegativeMax() {
try {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
new BoundedIterator<>(testList.iterator(), 3, -1);
fail("Expected IllegalArgumentException.");
} catch (final IllegalArgumentException iae) { /* Success case */
}
});
assertTrue(exception.getMessage().contains("Max parameter must not be negative."));
}

/**
Expand All @@ -172,11 +173,10 @@ public void testNegativeMax() {
public void testOffsetGreaterThanSize() {
final Iterator<E> iter = new BoundedIterator<>(testList.iterator(), 10, 4);
assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand All @@ -203,11 +203,10 @@ public void testMaxGreaterThanSize() {
assertEquals("g", iter.next());

assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand All @@ -219,11 +218,10 @@ public void testRemoveWithoutCallingNext() {
final List<E> testListCopy = new ArrayList<>(testList);
final Iterator<E> iter = new BoundedIterator<>(testListCopy.iterator(), 1, 5);

try {
Exception exception = assertThrows(IllegalStateException.class, () -> {
iter.remove();
fail("Expected IllegalStateException.");
} catch (final IllegalStateException ise) { /* Success case */
}
});
assertTrue(exception.getMessage().contains("remove() can not be called before calling next()"));
}

/**
Expand All @@ -239,11 +237,10 @@ public void testRemoveCalledTwice() {
assertEquals("b", iter.next());
iter.remove();

try {
Exception exception = assertThrows(IllegalStateException.class, () -> {
iter.remove();
fail("Expected IllegalStateException.");
} catch (final IllegalStateException ise) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand Down Expand Up @@ -271,11 +268,10 @@ public void testRemoveFirst() {
assertEquals("f", iter.next());

assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand Down Expand Up @@ -303,11 +299,10 @@ public void testRemoveMiddle() {
assertEquals("f", iter.next());

assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand All @@ -331,21 +326,19 @@ public void testRemoveLast() {
assertEquals("f", iter.next());

assertFalse(iter.hasNext());
try {
Exception exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());

iter.remove();
assertFalse(testListCopy.contains("f"));

assertFalse(iter.hasNext());
try {
exception = assertThrows(NoSuchElementException.class, () -> {
iter.next();
fail("Expected NoSuchElementException.");
} catch (final NoSuchElementException nsee) { /* Success case */
}
});
assertNull(exception.getMessage());
}

/**
Expand All @@ -364,10 +357,9 @@ public void remove() {
final Iterator<E> iter = new BoundedIterator<>(mockIterator, 1, 5);
assertTrue(iter.hasNext());
assertEquals("b", iter.next());
try {
Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
iter.remove();
fail("Expected UnsupportedOperationException.");
} catch (final UnsupportedOperationException usoe) { /* Success case */
}
});
assertNull(exception.getMessage());
}
}
Loading

0 comments on commit 4e8ca01

Please sign in to comment.