MultimapAsMapTester.java revision 0888a09821a98ac0680fad765217302858e70fa4
1/*
2 * Copyright (C) 2013 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.google.common.collect.testing.google;
16
17import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
18import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
19import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
24import static org.truth0.Truth.ASSERT;
25
26import com.google.common.annotations.GwtCompatible;
27import com.google.common.collect.Iterables;
28import com.google.common.collect.Multimap;
29import com.google.common.collect.testing.Helpers;
30import com.google.common.collect.testing.features.CollectionFeature;
31import com.google.common.collect.testing.features.CollectionSize;
32import com.google.common.collect.testing.features.MapFeature;
33
34import java.util.ArrayList;
35import java.util.Collection;
36import java.util.Iterator;
37import java.util.List;
38import java.util.Map.Entry;
39import java.util.Set;
40
41/**
42 * Tests for {@link Multimap#asMap}.
43 *
44 * @author Louis Wasserman
45 */
46@GwtCompatible
47public class MultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
48  public void testAsMapGet() {
49    for (K key : sampleKeys()) {
50      List<V> expectedValues = new ArrayList<V>();
51      for (Entry<K, V> entry : getSampleElements()) {
52        if (entry.getKey().equals(key)) {
53          expectedValues.add(entry.getValue());
54        }
55      }
56
57      Collection<V> collection = multimap().asMap().get(key);
58      if (expectedValues.isEmpty()) {
59        ASSERT.that(collection).isNull();
60      } else {
61        ASSERT.that(collection).has().exactlyAs(expectedValues);
62      }
63    }
64  }
65
66  @CollectionSize.Require(absent = ZERO)
67  @MapFeature.Require(ALLOWS_NULL_KEYS)
68  public void testAsMapGetNullKeyPresent() {
69    initMultimapWithNullKey();
70    ASSERT.that(multimap().asMap().get(null)).has().exactly(getValueForNullKey());
71  }
72
73  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
74  public void testAsMapGetNullKeyAbsent() {
75    ASSERT.that(multimap().asMap().get(null)).isNull();
76  }
77
78  @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
79  public void testAsMapGetNullKeyUnsupported() {
80    try {
81      multimap().asMap().get(null);
82      fail("Expected NullPointerException");
83    } catch (NullPointerException expected) {}
84  }
85
86  @CollectionSize.Require(absent = ZERO)
87  @MapFeature.Require(SUPPORTS_REMOVE)
88  public void testAsMapRemove() {
89    ASSERT.that(multimap().asMap().remove(sampleKeys().e0)).iteratesAs(sampleValues().e0);
90    assertGet(sampleKeys().e0);
91    assertEquals(getNumElements() - 1, multimap().size());
92  }
93
94  @CollectionSize.Require(SEVERAL)
95  @MapFeature.Require(SUPPORTS_PUT)
96  public void testAsMapEntrySetReflectsPutSameKey() {
97    resetContainer(
98        Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
99        Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
100
101    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
102    Collection<V> valueCollection = Iterables.getOnlyElement(asMapEntrySet).getValue();
103    ASSERT.that(valueCollection)
104        .has().exactly(sampleValues().e0, sampleValues().e3);
105    assertTrue(multimap().put(sampleKeys().e0, sampleValues().e4));
106    ASSERT.that(valueCollection)
107        .has().exactly(sampleValues().e0, sampleValues().e3, sampleValues().e4);
108  }
109
110  @CollectionSize.Require(SEVERAL)
111  @MapFeature.Require(SUPPORTS_PUT)
112  public void testAsMapEntrySetReflectsPutDifferentKey() {
113    resetContainer(
114        Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
115        Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
116
117    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
118    assertTrue(multimap().put(sampleKeys().e1, sampleValues().e4));
119    assertEquals(2, asMapEntrySet.size());
120  }
121
122  @CollectionSize.Require(SEVERAL)
123  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
124  public void testAsMapEntrySetRemovePropagatesToMultimap() {
125    resetContainer(
126        Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
127        Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
128    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
129    Entry<K, Collection<V>> asMapEntry0 = Iterables.getOnlyElement(asMapEntrySet);
130    assertTrue(multimap().put(sampleKeys().e1, sampleValues().e4));
131    assertTrue(asMapEntrySet.remove(asMapEntry0));
132    assertEquals(1, multimap().size());
133    ASSERT.that(multimap().keySet()).iteratesAs(sampleKeys().e1);
134  }
135
136  @CollectionSize.Require(SEVERAL)
137  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
138  public void testAsMapEntrySetIteratorRemovePropagatesToMultimap() {
139    resetContainer(
140        Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
141        Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
142    Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
143    Iterator<Entry<K, Collection<V>>> asMapEntryItr = asMapEntrySet.iterator();
144    asMapEntryItr.next();
145    asMapEntryItr.remove();
146    assertTrue(multimap().isEmpty());
147  }
148}
149