Use generics to parameterize Map tests

- More precise typing for Map tests
- Helpful for testing BeanMap in Apache Commons BeanUtils
- Javadoc
This commit is contained in:
Gary Gregory 2024-10-03 08:55:40 -04:00
parent 7bc8ae8650
commit 7521b9ce29
29 changed files with 162 additions and 84 deletions

View File

@ -26,8 +26,11 @@ import org.junit.jupiter.api.Test;
/**
* Tests TreeMap.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public abstract class AbstractTreeMapTest<K, V> extends AbstractMapTest<K, V> {
public abstract class AbstractTreeMapTest<K, V> extends AbstractMapTest<TreeMap<K, V>, K, V> {
public AbstractTreeMapTest(final String testName) {
super(testName);

View File

@ -77,15 +77,15 @@ public abstract class AbstractSortedBidiMapTest<K extends Comparable<K>, V exten
// }
public BulkTest bulkTestHeadMap() {
return new AbstractSortedMapTest.TestHeadMap<>(this);
return new AbstractSortedMapTest.TestHeadMap<K, V>((AbstractBidiMapTest) this);
}
public BulkTest bulkTestSubMap() {
return new AbstractSortedMapTest.TestSubMap<>(this);
return new AbstractSortedMapTest.TestSubMap<>((AbstractBidiMapTest) this);
}
public BulkTest bulkTestTailMap() {
return new AbstractSortedMapTest.TestTailMap<>(this);
return new AbstractSortedMapTest.TestTailMap<>((AbstractBidiMapTest) this);
}
@Override

View File

@ -27,7 +27,7 @@ import org.apache.commons.collections4.map.ConcurrentReferenceHashMap.Option;
* @param <K> the key type.
* @param <V> the value type.
*/
public abstract class AbstractConcurrentReferenceHashMapTest<K, V> extends AbstractMapTest<K, V> {
public abstract class AbstractConcurrentReferenceHashMapTest<K, V> extends AbstractMapTest<ConcurrentReferenceHashMap<K, V>, K, V> {
protected static final EnumSet<Option> IDENTITY_COMPARISONS = EnumSet.of(Option.IDENTITY_COMPARISONS);

View File

@ -34,7 +34,7 @@ import org.junit.jupiter.api.Test;
* @param <K> the key type.
* @param <V> the value type.
*/
public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<K, V> {
public abstract class AbstractIterableMapTest<K, V> extends AbstractMapTest<IterableMap<K, V>, K, V> {
public class InnerTestMapIterator extends AbstractMapIteratorTest<K, V> {
public InnerTestMapIterator() {

View File

@ -132,10 +132,11 @@ import org.junit.jupiter.api.Test;
* {@link #isAllowDuplicateValues()} and have it return {@code false}
* </p>
*
* @param <M> the Map type.
* @param <K> the key type.
* @param <V> the value type.
*/
public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
public abstract class AbstractMapTest<M extends Map<K, V>, K, V> extends AbstractObjectTest {
public class TestMapEntrySet extends AbstractSetTest<Map.Entry<K, V>> {
public TestMapEntrySet() {
@ -514,7 +515,7 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
}
/** Map created by reset(). */
protected Map<K, V> map;
protected M map;
/** Entry set of map created by reset(). */
protected Set<Map.Entry<K, V>> entrySet;
@ -639,7 +640,7 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
*
* @return Map<K, V>
*/
public Map<K, V> getMap() {
public M getMap() {
return map;
}
@ -877,8 +878,8 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
*
* @return the map to be tested
*/
public Map<K, V> makeFullMap() {
final Map<K, V> m = makeObject();
public M makeFullMap() {
final M m = makeObject();
addSampleMappings(m);
return m;
}
@ -889,7 +890,7 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
* @return the map to be tested
*/
@Override
public abstract Map<K, V> makeObject();
public abstract M makeObject();
/**
* Resets the {@link #map}, {@link #entrySet}, {@link #keySet}, {@link #values} and {@link #confirmed} fields to empty.

View File

@ -36,13 +36,13 @@ import org.junit.jupiter.api.Test;
* @param <K> the key type.
* @param <V> the value type.
*/
public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V> {
public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<SortedMap<K, V>, K, V> {
public static class TestHeadMap<K, V> extends TestViewMap<K, V> {
static final int SUBSIZE = 6;
final K toKey;
public TestHeadMap(final AbstractMapTest<K, V> main) {
public TestHeadMap(final AbstractMapTest<SortedMap<K, V>, K, V> main) {
super("SortedMap.HeadMap", main);
final Map<K, V> sm = main.makeFullMap();
for (final Entry<K, V> entry : sm.entrySet()) {
@ -98,7 +98,7 @@ public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V>
final K fromKey;
final K toKey;
public TestSubMap(final AbstractMapTest<K, V> main) {
public TestSubMap(final AbstractMapTest<SortedMap<K, V>, K, V> main) {
super("SortedMap.SubMap", main);
final Map<K, V> sm = main.makeFullMap();
for (final Entry<K, V> entry : sm.entrySet()) {
@ -161,7 +161,7 @@ public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V>
final K fromKey;
final K invalidKey;
public TestTailMap(final AbstractMapTest<K, V> main) {
public TestTailMap(final AbstractMapTest<SortedMap<K, V>, K, V> main) {
super("SortedMap.TailMap", main);
final Map<K, V> sm = main.makeFullMap();
for (final Entry<K, V> entry : sm.entrySet()) {
@ -214,12 +214,12 @@ public abstract class AbstractSortedMapTest<K, V> extends AbstractMapTest<K, V>
}
public abstract static class TestViewMap<K, V> extends AbstractSortedMapTest<K, V> {
protected final AbstractMapTest<K, V> main;
protected final AbstractMapTest<SortedMap<K, V>, K, V> main;
protected final List<K> subSortedKeys = new ArrayList<>();
protected final List<V> subSortedValues = new ArrayList<>();
protected final List<V> subSortedNewValues = new ArrayList<>();
public TestViewMap(final String name, final AbstractMapTest<K, V> main) {
public TestViewMap(final String name, final AbstractMapTest<SortedMap<K, V>, K, V> main) {
super(name);
this.main = main;
}

View File

@ -17,7 +17,6 @@
package org.apache.commons.collections4.map;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -26,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentHashMapSanityTest<K, V> extends AbstractMapTest<K, V> {
public class ConcurrentHashMapSanityTest<K, V> extends AbstractMapTest<ConcurrentHashMap<K, V>, K, V> {
public ConcurrentHashMapSanityTest() {
super(ConcurrentHashMapSanityTest.class.getSimpleName());
@ -56,7 +55,7 @@ public class ConcurrentHashMapSanityTest<K, V> extends AbstractMapTest<K, V> {
}
@Override
public Map<K, V> makeObject() {
public ConcurrentHashMap<K, V> makeObject() {
return new ConcurrentHashMap<>();
}

View File

@ -18,12 +18,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapDefaultsTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// The default behavior
return ConcurrentReferenceHashMap.<K, V>builder().get();
}

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKSoftVSoftIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.softKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKSoftVSoftTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.softKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKSoftVStrongIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.softKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKSoftVStrongTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.softKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKSoftVWeakIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.softKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKSoftVWeakTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.softKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKStrongVSoftIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.strongKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKStrongVSoftTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.strongKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKStrongVStrongIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.strongKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKStrongVStrongTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.strongKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKStrongVWeakIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.strongKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKStrongVWeakTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.strongKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKWeakVSoftIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.weakKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKWeakVSoftTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.weakKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKWeakVStrongIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.weakKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKWeakVStrongTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.weakKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKWeakVWeakIdCTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.weakKeys()

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections4.map;
import java.util.Map;
/**
* Tests {@link ConcurrentReferenceHashMap}.
*
* @param <K> the key type.
* @param <V> the value type.
*/
public class ConcurrentReferenceHashMapKWeakVWeakTest<K, V> extends AbstractConcurrentReferenceHashMapTest<K, V> {
@Override
public Map<K, V> makeObject() {
public ConcurrentReferenceHashMap<K, V> makeObject() {
// @formatter:off
return ConcurrentReferenceHashMap.<K, V>builder()
.weakKeys()

View File

@ -18,7 +18,6 @@
package org.apache.commons.collections4.map;
import java.util.HashMap;
import java.util.Map;
/**
* A sanity test for the test framework.
@ -26,7 +25,7 @@ import java.util.Map;
* @param <K> the key type.
* @param <V> the value type.
*/
public class HashMapSanityTest<K, V> extends AbstractMapTest<K, V> {
public class HashMapSanityTest<K, V> extends AbstractMapTest<HashMap<K, V>, K, V> {
public HashMapSanityTest() {
super(HashMapSanityTest.class.getSimpleName());
@ -41,7 +40,7 @@ public class HashMapSanityTest<K, V> extends AbstractMapTest<K, V> {
}
@Override
public Map<K, V> makeObject() {
public HashMap<K, V> makeObject() {
return new HashMap<>();
}

View File

@ -39,7 +39,7 @@ import org.junit.jupiter.api.Test;
* @param <K> the key type.
* @param <V> the value type.
*/
public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<K, V> {
public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<PassiveExpiringMap<K, V>, K, V> {
private static final class TestExpirationPolicy
implements ExpirationPolicy<Integer, String> {
@ -95,7 +95,7 @@ public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<K, V> {
}
@Override
public Map<K, V> makeObject() {
public PassiveExpiringMap<K, V> makeObject() {
return new PassiveExpiringMap<>();
}

View File

@ -62,7 +62,7 @@ import org.junit.jupiter.api.Test;
*/
public abstract class AbstractMultiValuedMapTest<K, V> extends AbstractObjectTest {
public class TestMultiValuedMapAsMap extends AbstractMapTest<K, Collection<V>> {
public class TestMultiValuedMapAsMap extends AbstractMapTest<Map<K, Collection<V>>, K, Collection<V>> {
public TestMultiValuedMapAsMap() {
super(StringUtils.EMPTY);