001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.collect.testing.google;
016
017import static com.google.common.collect.Iterables.getOnlyElement;
018import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder;
019import static com.google.common.collect.testing.Helpers.assertContentsInOrder;
020import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
021import static com.google.common.collect.testing.Helpers.mapEntry;
022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
023import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
024import static com.google.common.collect.testing.features.CollectionSize.ZERO;
025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
026import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
027import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
028import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
029import static org.junit.Assert.assertThrows;
030
031import com.google.common.annotations.GwtCompatible;
032import com.google.common.collect.Multimap;
033import com.google.common.collect.testing.features.CollectionFeature;
034import com.google.common.collect.testing.features.CollectionSize;
035import com.google.common.collect.testing.features.MapFeature;
036import java.util.ArrayList;
037import java.util.Collection;
038import java.util.Iterator;
039import java.util.List;
040import java.util.Map.Entry;
041import java.util.Set;
042
043/**
044 * Tests for {@link Multimap#asMap}.
045 *
046 * @author Louis Wasserman
047 */
048@GwtCompatible
049public class MultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
050  public void testAsMapGet() {
051    for (K key : sampleKeys()) {
052      List<V> expectedValues = new ArrayList<>();
053      for (Entry<K, V> entry : getSampleElements()) {
054        if (entry.getKey().equals(key)) {
055          expectedValues.add(entry.getValue());
056        }
057      }
058
059      Collection<V> collection = multimap().asMap().get(key);
060      if (expectedValues.isEmpty()) {
061        assertNull(collection);
062      } else {
063        assertEqualIgnoringOrder(expectedValues, collection);
064      }
065    }
066  }
067
068  @CollectionSize.Require(absent = ZERO)
069  @MapFeature.Require(ALLOWS_NULL_KEYS)
070  public void testAsMapGetNullKeyPresent() {
071    initMultimapWithNullKey();
072    assertContentsAnyOrder(multimap().asMap().get(null), getValueForNullKey());
073  }
074
075  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
076  public void testAsMapGetNullKeyAbsent() {
077    assertNull(multimap().asMap().get(null));
078  }
079
080  @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
081  public void testAsMapGetNullKeyUnsupported() {
082    assertThrows(NullPointerException.class, () -> multimap().asMap().get(null));
083  }
084
085  @CollectionSize.Require(absent = ZERO)
086  @MapFeature.Require(SUPPORTS_REMOVE)
087  public void testAsMapRemove() {
088    assertContentsInOrder(multimap().asMap().remove(k0()), v0());
089    assertGet(k0());
090    assertEquals(getNumElements() - 1, multimap().size());
091  }
092
093  @CollectionSize.Require(SEVERAL)
094  @MapFeature.Require(SUPPORTS_PUT)
095  public void testAsMapEntrySetReflectsPutSameKey() {
096    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3()));
097
098    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
099    Collection<V> valueCollection = getOnlyElement(asMapEntrySet).getValue();
100    assertContentsAnyOrder(valueCollection, v0(), v3());
101    assertTrue(multimap().put(k0(), v4()));
102    assertContentsAnyOrder(valueCollection, v0(), v3(), v4());
103  }
104
105  @CollectionSize.Require(SEVERAL)
106  @MapFeature.Require(SUPPORTS_PUT)
107  public void testAsMapEntrySetReflectsPutDifferentKey() {
108    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3()));
109
110    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
111    assertTrue(multimap().put(k1(), v4()));
112    assertEquals(2, asMapEntrySet.size());
113  }
114
115  @CollectionSize.Require(SEVERAL)
116  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
117  public void testAsMapEntrySetRemovePropagatesToMultimap() {
118    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3()));
119    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
120    Entry<K, Collection<V>> asMapEntry0 = getOnlyElement(asMapEntrySet);
121    assertTrue(multimap().put(k1(), v4()));
122    assertTrue(asMapEntrySet.remove(asMapEntry0));
123    assertEquals(1, multimap().size());
124    assertContentsInOrder(multimap().keySet(), k1());
125  }
126
127  @CollectionSize.Require(SEVERAL)
128  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
129  public void testAsMapEntrySetIteratorRemovePropagatesToMultimap() {
130    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3()));
131    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
132    Iterator<Entry<K, Collection<V>>> asMapEntryItr = asMapEntrySet.iterator();
133    asMapEntryItr.next();
134    asMapEntryItr.remove();
135    assertTrue(multimap().isEmpty());
136  }
137}