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.testing.Helpers.assertContentsAnyOrder;
018import static com.google.common.collect.testing.Helpers.assertContentsInOrder;
019import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
020import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
022import static com.google.common.collect.testing.features.CollectionSize.ZERO;
023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
025import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
026import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
027
028import com.google.common.annotations.GwtCompatible;
029import com.google.common.collect.Iterables;
030import com.google.common.collect.Multimap;
031import com.google.common.collect.testing.Helpers;
032import com.google.common.collect.testing.features.CollectionFeature;
033import com.google.common.collect.testing.features.CollectionSize;
034import com.google.common.collect.testing.features.MapFeature;
035import java.util.ArrayList;
036import java.util.Collection;
037import java.util.Iterator;
038import java.util.List;
039import java.util.Map.Entry;
040import java.util.Set;
041import org.junit.Ignore;
042
043/**
044 * Tests for {@link Multimap#asMap}.
045 *
046 * @author Louis Wasserman
047 */
048@GwtCompatible
049@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
050public class MultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
051  public void testAsMapGet() {
052    for (K key : sampleKeys()) {
053      List<V> expectedValues = new ArrayList<>();
054      for (Entry<K, V> entry : getSampleElements()) {
055        if (entry.getKey().equals(key)) {
056          expectedValues.add(entry.getValue());
057        }
058      }
059
060      Collection<V> collection = multimap().asMap().get(key);
061      if (expectedValues.isEmpty()) {
062        assertNull(collection);
063      } else {
064        assertEqualIgnoringOrder(expectedValues, collection);
065      }
066    }
067  }
068
069  @CollectionSize.Require(absent = ZERO)
070  @MapFeature.Require(ALLOWS_NULL_KEYS)
071  public void testAsMapGetNullKeyPresent() {
072    initMultimapWithNullKey();
073    assertContentsAnyOrder(multimap().asMap().get(null), getValueForNullKey());
074  }
075
076  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
077  public void testAsMapGetNullKeyAbsent() {
078    assertNull(multimap().asMap().get(null));
079  }
080
081  @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
082  public void testAsMapGetNullKeyUnsupported() {
083    try {
084      multimap().asMap().get(null);
085      fail("Expected NullPointerException");
086    } catch (NullPointerException expected) {
087    }
088  }
089
090  @CollectionSize.Require(absent = ZERO)
091  @MapFeature.Require(SUPPORTS_REMOVE)
092  public void testAsMapRemove() {
093    assertContentsInOrder(multimap().asMap().remove(k0()), v0());
094    assertGet(k0());
095    assertEquals(getNumElements() - 1, multimap().size());
096  }
097
098  @CollectionSize.Require(SEVERAL)
099  @MapFeature.Require(SUPPORTS_PUT)
100  public void testAsMapEntrySetReflectsPutSameKey() {
101    resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
102
103    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
104    Collection<V> valueCollection = Iterables.getOnlyElement(asMapEntrySet).getValue();
105    assertContentsAnyOrder(valueCollection, v0(), v3());
106    assertTrue(multimap().put(k0(), v4()));
107    assertContentsAnyOrder(valueCollection, v0(), v3(), v4());
108  }
109
110  @CollectionSize.Require(SEVERAL)
111  @MapFeature.Require(SUPPORTS_PUT)
112  public void testAsMapEntrySetReflectsPutDifferentKey() {
113    resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
114
115    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
116    assertTrue(multimap().put(k1(), v4()));
117    assertEquals(2, asMapEntrySet.size());
118  }
119
120  @CollectionSize.Require(SEVERAL)
121  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
122  public void testAsMapEntrySetRemovePropagatesToMultimap() {
123    resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
124    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
125    Entry<K, Collection<V>> asMapEntry0 = Iterables.getOnlyElement(asMapEntrySet);
126    assertTrue(multimap().put(k1(), v4()));
127    assertTrue(asMapEntrySet.remove(asMapEntry0));
128    assertEquals(1, multimap().size());
129    assertContentsInOrder(multimap().keySet(), k1());
130  }
131
132  @CollectionSize.Require(SEVERAL)
133  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
134  public void testAsMapEntrySetIteratorRemovePropagatesToMultimap() {
135    resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
136    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
137    Iterator<Entry<K, Collection<V>>> asMapEntryItr = asMapEntrySet.iterator();
138    asMapEntryItr.next();
139    asMapEntryItr.remove();
140    assertTrue(multimap().isEmpty());
141  }
142}