View Javadoc
1   /*
2    * Copyright (C) 2012 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.Helpers.copyToList;
18  import static com.google.common.collect.testing.Helpers.copyToSet;
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
21  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.SetMultimap;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  import java.util.List;
28  import java.util.Map.Entry;
29  import java.util.Set;
30  
31  /**
32   * Tests for {@link SetMultimap#replaceValues}.
33   *
34   * @author Louis Wasserman
35   */
36  @GwtCompatible
37  public class SetMultimapPutTester<K, V> extends AbstractMultimapTester<K, V, SetMultimap<K, V>> {
38    // Tests for non-duplicate values are in MultimapPutTester
39  
40    @MapFeature.Require(SUPPORTS_PUT)
41    @CollectionSize.Require(absent = ZERO)
42    public void testPutDuplicateValuePreservesSize() {
43      assertFalse(multimap().put(k0(), v0()));
44      assertEquals(getNumElements(), multimap().size());
45    }
46  
47    @MapFeature.Require(SUPPORTS_PUT)
48    public void testPutDuplicateValue() {
49      List<Entry<K, V>> entries = copyToList(multimap().entries());
50  
51      for (Entry<K, V> entry : entries) {
52        resetContainer();
53        K k = entry.getKey();
54        V v = entry.getValue();
55  
56        Set<V> values = multimap().get(k);
57        Set<V> expectedValues = copyToSet(values);
58  
59        assertFalse(multimap().put(k, v));
60        assertEquals(expectedValues, values);
61        assertGet(k, expectedValues);
62      }
63    }
64  
65    @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
66    @CollectionSize.Require(absent = ZERO)
67    public void testPutDuplicateValue_null() {
68      initMultimapWithNullValue();
69      assertFalse(multimap().put(getKeyForNullValue(), null));
70      expectContents(createArrayWithNullValue());
71    }
72  }