@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) public void testPutAllNullValueNullLast_unsupported() { int size = getNumElements(); try { multimap().putAll(k3(), Lists.newArrayList(v3(), null)); fail(); } catch (NullPointerException expected) { } Collection<V> values = multimap().get(k3()); if (values.size() == 0) { expectUnchanged(); // Be extra thorough in case internal state was corrupted by the expected null. assertEquals(Lists.newArrayList(), Lists.newArrayList(values)); assertEquals(size, multimap().size()); } else { assertEquals(Lists.newArrayList(v3()), Lists.newArrayList(values)); assertEquals(size + 1, multimap().size()); } }
public Test testsForTreeMapNatural() { return NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { /* * TODO(cpovirk): it would be nice to create an input Map and use * the copy constructor here and in the other tests */ return populate(new TreeMap<String, String>(), entries); } }) .named("TreeMap, natural") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_VALUES, MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForTreeMapNatural()) .createTestSuite(); }
public Test testsForUnmodifiableNavigableMap() { return MapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected NavigableMap<String, String> create(Entry<String, String>[] entries) { return Collections.unmodifiableNavigableMap(populate(new TreeMap<>(), entries)); } }) .named("unmodifiableNavigableMap/TreeMap, natural") .withFeatures( MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForUnmodifiableNavigableMap()) .createTestSuite(); }
public Test testsForConcurrentSkipListMapNatural() { return NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { return populate(new ConcurrentSkipListMap<String, String>(), entries); } }) .named("ConcurrentSkipListMap, natural") .withFeatures( MapFeature.GENERAL_PURPOSE, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForConcurrentSkipListMap()) .createTestSuite(); }
public Test testsForConcurrentSkipListMapWithComparator() { return NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { return populate( new ConcurrentSkipListMap<String, String>(arbitraryNullFriendlyComparator()), entries); } }) .named("ConcurrentSkipListMap, with comparator") .withFeatures( MapFeature.GENERAL_PURPOSE, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForConcurrentSkipListMap()) .createTestSuite(); }
public Test testsForEnumMap() { return MapTestSuiteBuilder.using( new TestEnumMapGenerator() { @Override protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) { return populate(new EnumMap<AnEnum, String>(AnEnum.class), entries); } }) .named("EnumMap") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_VALUES, MapFeature.RESTRICTS_KEYS, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForEnumMap()) .createTestSuite(); }
@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) @CollectionSize.Require(absent = ZERO) public void testCompute_presentToAbsent() { assertNull( "Map.compute(present, functionReturningNull) should return null", getMap() .compute( k0(), (k, v) -> { assertEquals(k0(), k); assertEquals(v0(), v); return null; })); expectMissing(e0()); expectMissingKeys(k0()); assertEquals(getNumElements() - 1, getMap().size()); }
@MapFeature.Require(SUPPORTS_PUT) public void testPutDuplicateValue() { List<Entry<K, V>> entries = copyToList(multimap().entries()); for (Entry<K, V> entry : entries) { resetContainer(); K k = entry.getKey(); V v = entry.getValue(); Set<V> values = multimap().get(k); Set<V> expectedValues = copyToSet(values); assertFalse(multimap().put(k, v)); assertEquals(expectedValues, values); assertGet(k, expectedValues); } }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT}) public void testPropagatesRemoveThenAddToMultimap() { int oldSize = getNumElements(); Collection<V> result = multimap().asMap().get(k0()); assertTrue(result.remove(v0())); assertFalse(multimap().containsKey(k0())); assertFalse(multimap().containsEntry(k0(), v0())); assertEmpty(result); assertTrue(result.add(v1())); assertTrue(result.add(v2())); assertContentsAnyOrder(result, v1(), v2()); assertContentsAnyOrder(multimap().get(k0()), v1(), v2()); assertTrue(multimap().containsKey(k0())); assertFalse(multimap().containsEntry(k0(), v0())); assertTrue(multimap().containsEntry(k0(), v2())); assertEquals(oldSize + 1, multimap().size()); }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testMergePresentToNull() { assertNull( "Map.merge(present, value, functionReturningNull) should return null", getMap() .merge( k0(), v3(), (oldV, newV) -> { assertEquals(v0(), oldV); assertEquals(v3(), newV); return null; })); expectMissing(e0()); }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemovePropagatesToGet() { List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries()); for (Entry<K, V> entry : entries) { resetContainer(); K key = entry.getKey(); V value = entry.getValue(); Collection<V> collection = multimap().get(key); assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().remove(key, value); expectedCollection.remove(value); assertEqualIgnoringOrder(expectedCollection, collection); assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); } }
public Test testsForUnmodifiableSortedMap() { return MapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { SortedMap<String, String> map = populate(new TreeMap<String, String>(), entries); return Collections.unmodifiableSortedMap(map); } }) .named("unmodifiableSortedMap/TreeMap, natural") .withFeatures( MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForUnmodifiableSortedMap()) .createTestSuite(); }
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) @CollectionSize.Require(absent = ZERO) public void testMappedToNull() { initMapWithNullValue(); assertEquals( "Map.merge(keyMappedToNull, value, function) should return value", v3(), getMap() .merge( getKeyForNullValue(), v3(), (oldV, newV) -> { throw new AssertionFailedError( "Should not call merge function if key was mapped to null"); })); expectReplacement(entry(getKeyForNullValue(), v3())); }
@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) @CollectionSize.Require(absent = ZERO) public void testCompute_presentNullToPresentNonnull() { initMapWithNullValue(); V value = getValueForNullKey(); assertEquals( "Map.compute(presentMappedToNull, functionReturningValue) should return new value", value, getMap() .compute( getKeyForNullValue(), (k, v) -> { assertEquals(getKeyForNullValue(), k); assertNull(v); return value; })); expectReplacement(entry(getKeyForNullValue(), value)); assertEquals(getNumElements(), getMap().size()); }
@SuppressWarnings("unchecked") @MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(ZERO) public void putDistinctKeysDistinctValues() { getMap().put(k0(), v0()); getMap().put(k1(), v1()); expectAdded(e0(), e1()); }
private static Set<Feature<?>> computeValuesCollectionFeatures(Set<Feature<?>> mapFeatures) { Set<Feature<?>> valuesCollectionFeatures = computeCommonDerivedCollectionFeatures(mapFeatures); if (mapFeatures.contains(MapFeature.ALLOWS_NULL_VALUE_QUERIES)) { valuesCollectionFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES); } if (mapFeatures.contains(MapFeature.ALLOWS_NULL_VALUES)) { valuesCollectionFeatures.add(CollectionFeature.ALLOWS_NULL_VALUES); } return valuesCollectionFeatures; }
@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) public void testGet_nullNotContainedAndUnsupported() { try { assertNull("get(null) should return null or throw", get(null)); } catch (NullPointerException tolerated) { } }
static Set<Feature<?>> computeKeysFeatures(Set<Feature<?>> multimapFeatures) { Set<Feature<?>> result = computeDerivedCollectionFeatures(multimapFeatures); if (multimapFeatures.contains(MapFeature.ALLOWS_NULL_KEYS)) { result.add(CollectionFeature.ALLOWS_NULL_VALUES); } if (multimapFeatures.contains(MapFeature.ALLOWS_NULL_KEY_QUERIES)) { result.add(CollectionFeature.ALLOWS_NULL_QUERIES); } return result; }
@MapFeature.Require(absent = SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testPutIfAbsent_unsupportedPresentExistingValue() { try { assertEquals( "putIfAbsent(present, existingValue) should return present or throw", v0(), putIfAbsent(e0())); } catch (UnsupportedOperationException tolerated) { } expectUnchanged(); }
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES) public void testReplace_absentNullKeyUnsupported() { try { getMap().replace(null, v3()); } catch (NullPointerException tolerated) { // permitted not to throw because it would be a no-op } expectUnchanged(); }
@MapFeature.Require(absent = SUPPORTS_PUT) public void testComputeIfPresent_unsupportedAbsent() { try { getMap() .computeIfPresent( k3(), (k, v) -> { throw new AssertionFailedError(); }); } catch (UnsupportedOperationException tolerated) { } expectUnchanged(); }
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS) public void testPutIfAbsent_nullKeyUnsupported() { try { getMap().putIfAbsent(null, v3()); fail("putIfAbsent(null, value) should throw"); } catch (NullPointerException expected) { } expectUnchanged(); expectNullKeyMissingWhenNullKeysUnsupported( "Should not contain null key after unsupported putIfAbsent(null, value)"); }
static Set<Feature<?>> computeEntriesFeatures(Set<Feature<?>> multimapFeatures) { Set<Feature<?>> result = computeDerivedCollectionFeatures(multimapFeatures); if (multimapFeatures.contains(MapFeature.ALLOWS_NULL_ENTRY_QUERIES)) { result.add(CollectionFeature.ALLOWS_NULL_QUERIES); } return result; }
@MapFeature.Require(absent = SUPPORTS_PUT) public void testReplaceEntry_unsupportedAbsentKey() { try { getMap().replace(k3(), v3(), v4()); } catch (UnsupportedOperationException tolerated) { // the operation would be a no-op, so exceptions are allowed but not required } expectUnchanged(); }
@MapFeature.Require(SUPPORTS_REMOVE) public void testValuesClearClearsInverse() { BiMap<V, K> inv = getMap().inverse(); getMap().values().clear(); assertTrue(getMap().isEmpty()); assertTrue(inv.isEmpty()); }
@MapFeature.Require(absent = SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemove_unsupportedPresent() { try { getMap().remove(k0(), v0()); fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException expected) {} expectUnchanged(); }
public static Set<Feature<?>> computeCommonDerivedCollectionFeatures( Set<Feature<?>> mapFeatures) { mapFeatures = new HashSet<Feature<?>>(mapFeatures); Set<Feature<?>> derivedFeatures = new HashSet<Feature<?>>(); mapFeatures.remove(CollectionFeature.SERIALIZABLE); if (mapFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) { derivedFeatures.add(CollectionFeature.SERIALIZABLE); } if (mapFeatures.contains(MapFeature.SUPPORTS_REMOVE)) { derivedFeatures.add(CollectionFeature.SUPPORTS_REMOVE); } if (mapFeatures.contains(MapFeature.REJECTS_DUPLICATES_AT_CREATION)) { derivedFeatures.add(CollectionFeature.REJECTS_DUPLICATES_AT_CREATION); } if (mapFeatures.contains(MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION)) { derivedFeatures.add(CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION); } // add the intersection of CollectionFeature.values() and mapFeatures for (CollectionFeature feature : CollectionFeature.values()) { if (mapFeatures.contains(feature)) { derivedFeatures.add(feature); } } // add the intersection of CollectionSize.values() and mapFeatures for (CollectionSize size : CollectionSize.values()) { if (mapFeatures.contains(size)) { derivedFeatures.add(size); } } return derivedFeatures; }
@CollectionSize.Require(SEVERAL) @MapFeature.Require(SUPPORTS_REMOVE) public void testPropagatesRemoveToMultimap() { resetContainer( Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k0(), v2())); Collection<V> result = multimap().get(k0()); assertTrue(result.remove(v0())); assertFalse(multimap().containsEntry(k0(), v0())); assertEquals(2, multimap().size()); }
@MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES) public void testRemove_nullKeyQueriesUnsupported() { try { assertFalse(getMap().remove(null, v3())); } catch (NullPointerException tolerated) { // since the operation would be a no-op, the exception is not required } expectUnchanged(); }
@MapFeature.Require(SUPPORTS_PUT) public void testComputeIfPresent_supportedAbsent() { assertNull( "computeIfPresent(notPresent, function) should return null", getMap() .computeIfPresent( k3(), (k, v) -> { throw new AssertionFailedError(); })); expectUnchanged(); }
@SuppressWarnings("unchecked") @MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(ZERO) public void testForcePutOverwritesOldValueEntry() { getMap().put(k0(), v0()); getMap().forcePut(k1(), v0()); // verify that the bimap is unchanged expectAdded(Helpers.mapEntry(k1(), v0())); }
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) public void testMergeAbsentNullKey() { assertEquals( "Map.merge(null, value, function) should return value", v3(), getMap() .merge( null, v3(), (oldV, newV) -> { throw new AssertionFailedError( "Should not call merge function if key was absent"); })); expectAdded(entry(null, v3())); }
@MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testPutPresentKeyPropagatesToAsMapEntrySet() { List<K> keys = Helpers.copyToList(multimap().keySet()); for (K key : keys) { resetContainer(); int size = getNumElements(); Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator(); Collection<V> collection = null; while (asMapItr.hasNext()) { Entry<K, Collection<V>> asMapEntry = asMapItr.next(); if (key.equals(asMapEntry.getKey())) { collection = asMapEntry.getValue(); break; } } assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().put(key, v3()); expectedCollection.add(v3()); assertEqualIgnoringOrder(expectedCollection, collection); assertEquals(size + 1, multimap().size()); } }
@MapFeature.Require(absent = SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemove_unsupported() { try { getMap().remove(k0()); fail("remove(present) should throw UnsupportedOperationException"); } catch (UnsupportedOperationException expected) { } expectUnchanged(); assertEquals("remove(present) should not remove the element", v0(), get(k0())); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(ALLOWS_NULL_KEYS) public void testEqualsMultimapWithNullKey() { Multimap<K, V> original = multimap(); initMultimapWithNullKey(); Multimap<K, V> withNull = multimap(); new EqualsTester() .addEqualityGroup(original) .addEqualityGroup( withNull, getSubjectGenerator().create((Object[]) createArrayWithNullKey())) .testEquals(); }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemove_present() { int initialSize = getMap().size(); assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0())); assertEquals( "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size()); expectMissing(e0()); }
/** * Copy of the {@link #testContainsEntryNullDisallowed} test. Needed because * "optional" feature requirements are not supported. */ @MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES) public void testContainsEntryNullDisallowedBecauseValueQueriesDisallowed() { try { multimap().containsEntry(k3(), null); fail("Expected NullPointerException"); } catch (NullPointerException expected) { // success } }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(SEVERAL) public void testPollLast() { assertEquals(c, navigableMap.pollLastEntry()); assertEquals( entries.subList(0, entries.size() - 1), Helpers.copyToList(navigableMap.entrySet())); }