This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push: new 2ceab701e [COLLECTIONS-777] Migrate to JUnit 5 2ceab701e is described below commit 2ceab701ebeaf4ced807d8423f438eee84cbb8e0 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Oct 20 09:52:45 2024 -0400 [COLLECTIONS-777] Migrate to JUnit 5 [COLLECTIONS-809] Update try/catch/fail logic to assertThrows --- .../org/apache/commons/collections4/BulkTest.java | 12 +-- .../commons/collections4/ClosureUtilsTest.java | 3 +- .../commons/collections4/FactoryUtilsTest.java | 2 +- .../apache/commons/collections4/MapUtilsTest.java | 10 +- .../commons/collections4/PredicateUtilsTest.java | 3 +- .../org/apache/commons/collections4/TestUtils.java | 39 ++++---- .../commons/collections4/TransformerUtilsTest.java | 3 +- .../collections4/map/ListOrderedMap2Test.java | 76 ++++------------ .../collections4/map/ListOrderedMapTest.java | 101 ++++----------------- .../collections4/map/PassiveExpiringMapTest.java | 20 +--- 10 files changed, 73 insertions(+), 196 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/BulkTest.java b/src/test/java/org/apache/commons/collections4/BulkTest.java index ddf5172a5..5c5a823ac 100644 --- a/src/test/java/org/apache/commons/collections4/BulkTest.java +++ b/src/test/java/org/apache/commons/collections4/BulkTest.java @@ -169,17 +169,13 @@ public class BulkTest implements Cloneable { } /** - * 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(); } /** diff --git a/src/test/java/org/apache/commons/collections4/ClosureUtilsTest.java b/src/test/java/org/apache/commons/collections4/ClosureUtilsTest.java index 40613717a..e5822c805 100644 --- a/src/test/java/org/apache/commons/collections4/ClosureUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/ClosureUtilsTest.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; 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; @@ -189,7 +190,7 @@ public class ClosureUtilsTest { * serialization/deserialization process. */ @Test - public void testSingletonPatternInSerialization() { + public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException { final Object[] singletons = { ExceptionClosure.INSTANCE, NOPClosure.INSTANCE, diff --git a/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java b/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java index e05d3f353..80abbcb0c 100644 --- a/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/FactoryUtilsTest.java @@ -219,7 +219,7 @@ public class FactoryUtilsTest { * serialization/deserialization process. */ @Test - public void testSingletonPatternInSerialization() { + public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException { final Object[] singletons = { ExceptionFactory.INSTANCE, }; diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java index d292d7040..5a33af9ef 100644 --- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java @@ -25,7 +25,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; 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; @@ -129,19 +128,12 @@ public class MapUtilsTest { 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 diff --git a/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java b/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java index f6f1d1d4d..eab191325 100644 --- a/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/PredicateUtilsTest.java @@ -24,6 +24,7 @@ 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.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -565,7 +566,7 @@ public class PredicateUtilsTest extends AbstractPredicateTest { * serialization/deserialization process. */ @Test - public void testSingletonPatternInSerialization() { + public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException { final Object[] singletons = { ExceptionPredicate.INSTANCE, FalsePredicate.INSTANCE, diff --git a/src/test/java/org/apache/commons/collections4/TestUtils.java b/src/test/java/org/apache/commons/collections4/TestUtils.java index c87e14ac7..10d5ec4b5 100644 --- a/src/test/java/org/apache/commons/collections4/TestUtils.java +++ b/src/test/java/org/apache/commons/collections4/TestUtils.java @@ -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); } @@ -49,31 +52,29 @@ public final class TestUtils { * <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() { diff --git a/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java b/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java index c758b2611..7891f169e 100644 --- a/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java @@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; 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; @@ -257,7 +258,7 @@ public class TransformerUtilsTest { * serialization/deserialization process. */ @Test - public void testSingletonPatternInSerialization() { + public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException { final Object[] singletons = { ExceptionTransformer.INSTANCE, NOPTransformer.INSTANCE, diff --git a/src/test/java/org/apache/commons/collections4/map/ListOrderedMap2Test.java b/src/test/java/org/apache/commons/collections4/map/ListOrderedMap2Test.java index 0c05682af..758732c68 100644 --- a/src/test/java/org/apache/commons/collections4/map/ListOrderedMap2Test.java +++ b/src/test/java/org/apache/commons/collections4/map/ListOrderedMap2Test.java @@ -19,6 +19,7 @@ package org.apache.commons.collections4.map; 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; @@ -120,27 +121,12 @@ public class ListOrderedMap2Test<K, V> extends AbstractOrderedMapTest<K, V> { @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)); @@ -150,27 +136,12 @@ public class ListOrderedMap2Test<K, V> extends AbstractOrderedMapTest<K, V> { @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(); @@ -209,27 +180,12 @@ public class ListOrderedMap2Test<K, V> extends AbstractOrderedMapTest<K, V> { @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()); diff --git a/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java b/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java index ea7926dce..d8c9fb085 100644 --- a/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java @@ -209,27 +209,12 @@ public class ListOrderedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { @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, () -> getMap().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)); @@ -239,27 +224,12 @@ public class ListOrderedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { @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, () -> getMap().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(); @@ -272,7 +242,6 @@ public class ListOrderedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { resetEmpty(); ListOrderedMap<K, V> lom = getMap(); assertEquals(-1, lom.indexOf(getOtherKeys())); - resetFull(); lom = getMap(); final List<K> list = new ArrayList<>(); @@ -456,27 +425,12 @@ public class ListOrderedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { @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, () -> getMap().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()); @@ -494,27 +448,12 @@ public class ListOrderedMapTest<K, V> extends AbstractOrderedMapTest<K, V> { @SuppressWarnings("unchecked") public void testSetValueByIndex() { resetEmpty(); - ListOrderedMap<K, V> lom = getMap(); - try { - lom.setValue(0, (V) StringUtils.EMPTY); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lom.setValue(-1, (V) StringUtils.EMPTY); - } catch (final IndexOutOfBoundsException ex) { - } - + assertThrows(IndexOutOfBoundsException.class, () -> getMap().setValue(0, (V) StringUtils.EMPTY)); + assertThrows(IndexOutOfBoundsException.class, () -> getMap().setValue(-1, (V) StringUtils.EMPTY)); resetFull(); - lom = getMap(); - try { - lom.setValue(-1, (V) StringUtils.EMPTY); - } catch (final IndexOutOfBoundsException ex) { - } - try { - lom.setValue(lom.size(), (V) StringUtils.EMPTY); - } catch (final IndexOutOfBoundsException ex) { - } - + final ListOrderedMap<K, V> lom = getMap(); + assertThrows(IndexOutOfBoundsException.class, () -> lom.setValue(-1, (V) StringUtils.EMPTY)); + assertThrows(IndexOutOfBoundsException.class, () -> lom.setValue(lom.size(), (V) StringUtils.EMPTY)); for (int i = 0; i < lom.size(); i++) { final V value = lom.getValue(i); final Object input = Integer.valueOf(i); diff --git a/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java b/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java index 70fcafd86..37ecafdb5 100644 --- a/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java @@ -23,7 +23,6 @@ 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 static org.junit.jupiter.api.Assertions.fail; import java.util.HashMap; import java.util.Map; @@ -191,13 +190,11 @@ public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<PassiveExpirin } @Test - public void testExpiration() { + public void testExpiration() throws InterruptedException { validateExpiration(new PassiveExpiringMap<>(500), 500); validateExpiration(new PassiveExpiringMap<>(1000), 1000); - validateExpiration(new PassiveExpiringMap<>( - new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<>(500)), 500); - validateExpiration(new PassiveExpiringMap<>( - new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<>(1, TimeUnit.SECONDS)), 1000); + validateExpiration(new PassiveExpiringMap<>(new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<>(500)), 500); + validateExpiration(new PassiveExpiringMap<>(new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<>(1, TimeUnit.SECONDS)), 1000); } @Test @@ -261,17 +258,10 @@ public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<PassiveExpirin assertNull(m.get("a")); } - private void validateExpiration(final Map<String, String> map, final long timeout) { + private void validateExpiration(final Map<String, String> map, final long timeout) throws InterruptedException { map.put("a", "b"); - assertNotNull(map.get("a")); - - try { - Thread.sleep(2 * timeout); - } catch (final InterruptedException e) { - fail(); - } - + Thread.sleep(2 * timeout); assertNull(map.get("a")); }