View Javadoc
1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
24  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.collect.Multimap;
28  import com.google.common.collect.testing.Helpers;
29  import com.google.common.collect.testing.features.CollectionSize;
30  import com.google.common.collect.testing.features.MapFeature;
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.List;
36  
37  /**
38   * Tests for {@link Multimap#replaceValues(Object, Iterable)}.
39   *
40   * @author Louis Wasserman
41   */
42  @GwtCompatible
43  public class MultimapReplaceValuesTester<K, V>
44      extends AbstractMultimapTester<K, V, Multimap<K, V>> {
45  
46    @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
47    public void testReplaceValuesWithNullValue() {
48      @SuppressWarnings("unchecked")
49      List<V> values = Arrays.asList(v0(), null, v3());
50      multimap().replaceValues(k0(), values);
51      assertGet(k0(), values);
52    }
53  
54    @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
55    public void testReplaceValuesWithNullKey() {
56      @SuppressWarnings("unchecked")
57      List<V> values = Arrays.asList(v0(), v2(), v3());
58      multimap().replaceValues(null, values);
59      assertGet(null, values);
60    }
61  
62    @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
63    public void testReplaceEmptyValues() {
64      int size = multimap().size();
65      @SuppressWarnings("unchecked")
66      List<V> values = Arrays.asList(v0(), v2(), v3());
67      multimap().replaceValues(k3(), values);
68      assertGet(k3(), values);
69      assertEquals(size + values.size(), multimap().size());
70    }
71  
72    @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
73    public void testReplaceValuesWithEmpty() {
74      int size = multimap().size();
75      List<V> oldValues = new ArrayList<>(multimap().get(k0()));
76      @SuppressWarnings("unchecked")
77      List<V> values = Collections.emptyList();
78      assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
79      assertGet(k0());
80      assertEquals(size - oldValues.size(), multimap().size());
81    }
82  
83    @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
84    public void testReplaceValuesWithDuplicates() {
85      int size = multimap().size();
86      List<V> oldValues = new ArrayList<>(multimap().get(k0()));
87      List<V> values = Arrays.asList(v0(), v3(), v0());
88      assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
89      assertEquals(size - oldValues.size() + multimap().get(k0()).size(), multimap().size());
90      assertTrue(multimap().get(k0()).containsAll(values));
91    }
92  
93    @CollectionSize.Require(absent = ZERO)
94    @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
95    public void testReplaceNonEmptyValues() {
96      List<K> keys = Helpers.copyToList(multimap().keySet());
97      @SuppressWarnings("unchecked")
98      List<V> values = Arrays.asList(v0(), v2(), v3());
99  
100     for (K k : keys) {
101       resetContainer();
102 
103       int size = multimap().size();
104       Collection<V> oldKeyValues = Helpers.copyToList(multimap().get(k));
105       multimap().replaceValues(k, values);
106       assertGet(k, values);
107       assertEquals(size + values.size() - oldKeyValues.size(), multimap().size());
108     }
109   }
110 
111   @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
112   public void testReplaceValuesPropagatesToGet() {
113     Collection<V> getCollection = multimap().get(k0());
114     @SuppressWarnings("unchecked")
115     List<V> values = Arrays.asList(v0(), v2(), v3());
116     multimap().replaceValues(k0(), values);
117     assertContentsAnyOrder(getCollection, v0(), v2(), v3());
118   }
119 
120   @MapFeature.Require(absent = SUPPORTS_REMOVE)
121   @CollectionSize.Require(absent = ZERO)
122   public void testReplaceValuesRemoveNotSupported() {
123     List<V> values = Collections.singletonList(v3());
124     try {
125       multimap().replaceValues(k0(), values);
126       fail("Expected UnsupportedOperationException");
127     } catch (UnsupportedOperationException expected) {
128       // success
129     }
130   }
131 
132   @MapFeature.Require(absent = SUPPORTS_PUT)
133   public void testReplaceValuesPutNotSupported() {
134     List<V> values = Collections.singletonList(v3());
135     try {
136       multimap().replaceValues(k0(), values);
137       fail("Expected UnsupportedOperationException");
138     } catch (UnsupportedOperationException expected) {
139       // success
140     }
141   }
142 }