View Javadoc
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  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
18  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
19  
20  import com.google.common.annotations.GwtCompatible;
21  import com.google.common.collect.Maps;
22  import com.google.common.collect.SetMultimap;
23  import com.google.common.collect.Sets;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  import com.google.common.testing.EqualsTester;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Map.Entry;
34  import java.util.Set;
35  
36  /**
37   * Testers for {@link SetMultimap#asMap}.
38   *
39   * @author Louis Wasserman
40   * @param <K> The key type of the tested multimap.
41   * @param <V> The value type of the tested multimap.
42   */
43  @GwtCompatible
44  public class SetMultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, SetMultimap<K, V>> {
45    public void testAsMapValuesImplementSet() {
46      for (Collection<V> valueCollection : multimap().asMap().values()) {
47        assertTrue(valueCollection instanceof Set);
48      }
49    }
50  
51    public void testAsMapGetImplementsSet() {
52      for (K key : multimap().keySet()) {
53        assertTrue(multimap().asMap().get(key) instanceof Set);
54      }
55    }
56  
57    @MapFeature.Require(SUPPORTS_REMOVE)
58    public void testAsMapRemoveImplementsSet() {
59      List<K> keys = new ArrayList<K>(multimap().keySet());
60      for (K key : keys) {
61        resetCollection();
62        assertTrue(multimap().asMap().remove(key) instanceof Set);
63      }
64    }
65  
66    @CollectionSize.Require(SEVERAL)
67    public void testEquals() {
68      resetContainer(
69          Helpers.mapEntry(k0(), v0()),
70          Helpers.mapEntry(k1(), v0()),
71          Helpers.mapEntry(k0(), v3()));
72      Map<K, Collection<V>> expected = Maps.newHashMap();
73      expected.put(k0(), Sets.newHashSet(v0(), v3()));
74      expected.put(k1(), Sets.newHashSet(v0()));
75      new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals();
76    }
77  
78    @CollectionSize.Require(SEVERAL)
79    public void testEntrySetEquals() {
80      resetContainer(
81          Helpers.mapEntry(k0(), v0()),
82          Helpers.mapEntry(k1(), v0()),
83          Helpers.mapEntry(k0(), v3()));
84      Set<Entry<K, Collection<V>>> expected = Sets.newHashSet();
85      expected.add(Helpers.mapEntry(k0(), (Collection<V>) Sets.newHashSet(v0(), v3())));
86      expected.add(Helpers.mapEntry(k1(), (Collection<V>) Sets.newHashSet(v0())));
87      new EqualsTester().addEqualityGroup(expected, multimap().asMap().entrySet()).testEquals();
88    }
89  
90    @CollectionSize.Require(SEVERAL)
91    @MapFeature.Require(SUPPORTS_REMOVE)
92    public void testValuesRemove() {
93      resetContainer(
94          Helpers.mapEntry(k0(), v0()),
95          Helpers.mapEntry(k1(), v0()),
96          Helpers.mapEntry(k0(), v3()));
97      assertTrue(multimap().asMap().values().remove(Collections.singleton(v0())));
98      assertEquals(2, multimap().size());
99      assertEquals(Collections.singletonMap(k0(), Sets.newHashSet(v0(), v3())), multimap().asMap());
100   }
101 }