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 
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36 
37 /**
38  * Testers for {@link SetMultimap#asMap}.
39  *
40  * @author Louis Wasserman
41  * @param <K> The key type of the tested multimap.
42  * @param <V> The value type of the tested multimap.
43  */
44 @GwtCompatible
45 public class SetMultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, SetMultimap<K, V>> {
testAsMapValuesImplementSet()46   public void testAsMapValuesImplementSet() {
47     for (Collection<V> valueCollection : multimap().asMap().values()) {
48       assertTrue(valueCollection instanceof Set);
49     }
50   }
51 
testAsMapGetImplementsSet()52   public void testAsMapGetImplementsSet() {
53     for (K key : multimap().keySet()) {
54       assertTrue(multimap().asMap().get(key) instanceof Set);
55     }
56   }
57 
58   @MapFeature.Require(SUPPORTS_REMOVE)
testAsMapRemoveImplementsSet()59   public void testAsMapRemoveImplementsSet() {
60     List<K> keys = new ArrayList<K>(multimap().keySet());
61     for (K key : keys) {
62       resetCollection();
63       assertTrue(multimap().asMap().remove(key) instanceof Set);
64     }
65   }
66 
67   @CollectionSize.Require(SEVERAL)
testEquals()68   public void testEquals() {
69     resetContainer(
70         Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
71         Helpers.mapEntry(sampleKeys().e1, sampleValues().e0),
72         Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
73     Map<K, Collection<V>> expected = Maps.newHashMap();
74     expected.put(sampleKeys().e0, Sets.newHashSet(sampleValues().e0, sampleValues().e3));
75     expected.put(sampleKeys().e1, Sets.newHashSet(sampleValues().e0));
76     new EqualsTester()
77         .addEqualityGroup(expected, multimap().asMap())
78         .testEquals();
79   }
80 
81   @CollectionSize.Require(SEVERAL)
testEntrySetEquals()82   public void testEntrySetEquals() {
83     resetContainer(
84         Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
85         Helpers.mapEntry(sampleKeys().e1, sampleValues().e0),
86         Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
87     Set<Entry<K, Collection<V>>> expected = Sets.newHashSet();
88     expected.add(Helpers.mapEntry(
89         sampleKeys().e0,
90         (Collection<V>) Sets.newHashSet(sampleValues().e0, sampleValues().e3)));
91     expected.add(Helpers.mapEntry(
92         sampleKeys().e1,
93         (Collection<V>) Sets.newHashSet(sampleValues().e0)));
94     new EqualsTester()
95         .addEqualityGroup(expected, multimap().asMap().entrySet())
96         .testEquals();
97   }
98 
99   @CollectionSize.Require(SEVERAL)
100   @MapFeature.Require(SUPPORTS_REMOVE)
testValuesRemove()101   public void testValuesRemove() {
102     resetContainer(
103         Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
104         Helpers.mapEntry(sampleKeys().e1, sampleValues().e0),
105         Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
106     assertTrue(multimap().asMap().values().remove(Collections.singleton(sampleValues().e0)));
107     assertEquals(2, multimap().size());
108     assertEquals(
109         Collections.singletonMap(
110             sampleKeys().e0, Sets.newHashSet(sampleValues().e0, sampleValues().e3)),
111         multimap().asMap());
112   }
113 }
114