A quick sanity test of this map using Guava's testlib found some simple violations. For example entrySet().iterator()
does not throw an exception for the sequence [hasNext, hasNext, next, remove, remove]. In this case Iterator.remove()
failed to null out the last
property after the first call, so a subsequent call does not throw an IllegalStateException
. As the keySet
and values
views delegate to entrySet
, this error is found multiple times in Guava's suite. You might consider using this suite on other custom collections.
Unit Tests
import java.util.Map;
import org.springframework.util.ConcurrentReferenceHashMap;
import com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder;
import com.google.common.collect.testing.TestStringMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import junit.framework.Test;
import junit.framework.TestCase;
/** Guava testlib map tests. */
public final class ConcurrentReferenceHashMapTests extends TestCase {
public static Test suite() {
var suite = ConcurrentMapTestSuiteBuilder
.using(new TestStringMapGenerator() {
@Override protected Map<String, String> create(Map.Entry<String, String>[] entries) {
var map = new ConcurrentReferenceHashMap<String, String>();
for (var entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
})
.named("ConcurrentReferenceHashMap")
.withFeatures(
MapFeature.GENERAL_PURPOSE,
MapFeature.ALLOWS_ANY_NULL_QUERIES,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
CollectionSize.ANY);
return suite.createTestSuite();
}
}
Comment From: ben-manes
@jhoeller Thanks for the quick fix. I checked with Guava's tests and it looks like the other failures (like Map.contains) were resolved thanks to this and perhaps other fixes on 6.0-SNAPSHOT (I had only checked the last 5.x release). You might consider adding Guava's testlib dependency (their testing utilities, com.google.guava:guava-testlib
) and the above suite to yours, and verify other collections like LinkedCaseInsensitiveMap
(passes below test).