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 0ea3686 commit 2ceab70
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 196 deletions.
12 changes: 4 additions & 8 deletions src/test/java/org/apache/commons/collections4/BulkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,13 @@ public BulkTest(final String name) {
}

/**
* Creates a clone of this {@code BulkTest}.<P>
* Creates a clone of this {@code BulkTest}.
*
* @return a clone of this {@code BulkTest}
* @return a clone of this {@code BulkTest}
*/
@Override
public Object clone() {
try {
return super.clone();
} catch (final CloneNotSupportedException e) {
throw new Error(); // should never happen
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -189,7 +190,7 @@ public void testNopClosure() {
* serialization/deserialization process.
*/
@Test
public void testSingletonPatternInSerialization() {
public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException {
final Object[] singletons = {
ExceptionClosure.INSTANCE,
NOPClosure.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void testPrototypeFactoryPublicSerializationError() {
* serialization/deserialization process.
*/
@Test
public void testSingletonPatternInSerialization() {
public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException {
final Object[] singletons = {
ExceptionFactory.INSTANCE,
};
Expand Down
10 changes: 1 addition & 9 deletions src/test/java/org/apache/commons/collections4/MapUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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 static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
Expand Down Expand Up @@ -129,19 +128,12 @@ public void testDebugAndVerbosePrintCasting() {
final Map<Integer, String> inner = new HashMap<>(2, 1);
inner.put(2, "B");
inner.put(3, "C");

final Map<Integer, Object> outer = new HashMap<>(2, 1);
outer.put(0, inner);
outer.put(1, "A");

final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);

try {
MapUtils.debugPrint(outPrint, "Print Map", outer);
} catch (final ClassCastException e) {
fail("No Casting should be occurring!");
}
MapUtils.debugPrint(outPrint, "Print Map", outer);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -565,7 +566,7 @@ public void testOrPredicateEx() {
* serialization/deserialization process.
*/
@Test
public void testSingletonPatternInSerialization() {
public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException {
final Object[] singletons = {
ExceptionPredicate.INSTANCE,
FalsePredicate.INSTANCE,
Expand Down
39 changes: 20 additions & 19 deletions src/test/java/org/apache/commons/collections4/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ public final class TestUtils {
* <p>
* Effect of method call is the same as:
* {@code assertSameAfterSerialization(null, o)}.
* </p>
*
* @param o object that will be tested.
* @throws IOException Thrown on test failure.
* @throws ClassNotFoundException Thrown on test failure.
* @see #assertSameAfterSerialization(String, Object)
*/
public static void assertSameAfterSerialization(final Object o) {
public static void assertSameAfterSerialization(final Object o) throws IOException, ClassNotFoundException {
assertSameAfterSerialization(null, o);
}

Expand All @@ -49,31 +52,29 @@ public static void assertSameAfterSerialization(final Object o) {
* <p>
* This method is especially good for testing singleton pattern on classes
* that support serialization.
* </p>
*
* @param msg the identifying message for the {@code AssertionError}.
* @param o object that will be tested.
* @throws IOException Thrown on test failure.
* @throws ClassNotFoundException Thrown on test failure.
* @see #assertSameAfterSerialization(Object)
*/
public static void assertSameAfterSerialization(final String msg, final Object o) {
try {
// write object to byte buffer
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
public static void assertSameAfterSerialization(final String msg, final Object o) throws IOException, ClassNotFoundException {
// write object to byte buffer
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();

// read same object from byte buffer
final InputStream is = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(is);
final Object object = ois.readObject();
ois.close();
// read same object from byte buffer
final InputStream is = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(is);
final Object object = ois.readObject();
ois.close();

// assert that original object and deserialized objects are the same
assertSame(o, object, msg);
} catch (final IOException | ClassNotFoundException e) {
// should never happen
throw new RuntimeException(e);
}
// assert that original object and deserialized objects are the same
assertSame(o, object, msg);
}

private TestUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -257,7 +258,7 @@ public void testPredicateTransformer() {
* serialization/deserialization process.
*/
@Test
public void testSingletonPatternInSerialization() {
public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException {
final Object[] singletons = {
ExceptionTransformer.INSTANCE,
NOPTransformer.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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 java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -120,27 +121,12 @@ public ListOrderedMap<K, V> makeObject() {
@Test
public void testGetByIndex() {
resetEmpty();
ListOrderedMap<K, V> lom = getMap();
try {
lom.get(0);
} catch (final IndexOutOfBoundsException ex) {
}
try {
lom.get(-1);
} catch (final IndexOutOfBoundsException ex) {
}

assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(0));
assertThrows(IndexOutOfBoundsException.class, () -> getMap().get(-1));
resetFull();
lom = getMap();
try {
lom.get(-1);
} catch (final IndexOutOfBoundsException ex) {
}
try {
lom.get(lom.size());
} catch (final IndexOutOfBoundsException ex) {
}

final ListOrderedMap<K, V> lom = getMap();
assertThrows(IndexOutOfBoundsException.class, () -> lom.get(-1));
assertThrows(IndexOutOfBoundsException.class, () -> lom.get(lom.size()));
int i = 0;
for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
assertSame(it.next(), lom.get(i));
Expand All @@ -150,27 +136,12 @@ public void testGetByIndex() {
@Test
public void testGetValueByIndex() {
resetEmpty();
ListOrderedMap<K, V> lom = getMap();
try {
lom.getValue(0);
} catch (final IndexOutOfBoundsException ex) {
}
try {
lom.getValue(-1);
} catch (final IndexOutOfBoundsException ex) {
}

assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(0));
assertThrows(IndexOutOfBoundsException.class, () -> getMap().getValue(-1));
resetFull();
lom = getMap();
try {
lom.getValue(-1);
} catch (final IndexOutOfBoundsException ex) {
}
try {
lom.getValue(lom.size());
} catch (final IndexOutOfBoundsException ex) {
}

final ListOrderedMap<K, V> lom = getMap();
assertThrows(IndexOutOfBoundsException.class, () -> lom.getValue(-1));
assertThrows(IndexOutOfBoundsException.class, () -> lom.getValue(lom.size()));
int i = 0;
for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext(); i++) {
it.next();
Expand Down Expand Up @@ -209,27 +180,12 @@ public void testIndexOf() {
@Test
public void testRemoveByIndex() {
resetEmpty();
ListOrderedMap<K, V> lom = getMap();
try {
lom.remove(0);
} catch (final IndexOutOfBoundsException ex) {
}
try {
lom.remove(-1);
} catch (final IndexOutOfBoundsException ex) {
}

assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(0));
assertThrows(IndexOutOfBoundsException.class, () -> getMap().remove(-1));
resetFull();
lom = getMap();
try {
lom.remove(-1);
} catch (final IndexOutOfBoundsException ex) {
}
try {
lom.remove(lom.size());
} catch (final IndexOutOfBoundsException ex) {
}

final ListOrderedMap<K, V> lom = getMap();
assertThrows(IndexOutOfBoundsException.class, () -> lom.remove(-1));
assertThrows(IndexOutOfBoundsException.class, () -> lom.remove(lom.size()));
final List<K> list = new ArrayList<>();
for (final MapIterator<K, V> it = lom.mapIterator(); it.hasNext();) {
list.add(it.next());
Expand Down
Loading

0 comments on commit 2ceab70

Please sign in to comment.