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.assertContentsInOrder;
18  import static com.google.common.collect.testing.Helpers.copyToList;
19  import static com.google.common.collect.testing.Helpers.mapEntry;
20  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.ListMultimap;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  import java.util.Arrays;
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * Testers for {@link ListMultimap#remove(Object, Object)}.
34   *
35   * @author Louis Wasserman
36   */
37  @GwtCompatible
38  public class ListMultimapRemoveTester<K, V> extends AbstractListMultimapTester<K, V> {
39    @SuppressWarnings("unchecked")
40    @MapFeature.Require(SUPPORTS_REMOVE)
41    @CollectionSize.Require(SEVERAL)
42    public void testMultimapRemoveDeletesFirstOccurrence() {
43      resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
44  
45      List<V> list = multimap().get(k0());
46      multimap().remove(k0(), v0());
47      assertContentsInOrder(list, v1(), v0());
48    }
49  
50    @SuppressWarnings("unchecked")
51    @MapFeature.Require(SUPPORTS_REMOVE)
52    @CollectionSize.Require(SEVERAL)
53    public void testRemoveAtIndexFromGetPropagates() {
54      List<V> values = Arrays.asList(v0(), v1(), v0());
55  
56      for (int i = 0; i < 3; i++) {
57        resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
58        List<V> expectedValues = copyToList(values);
59  
60        multimap().get(k0()).remove(i);
61        expectedValues.remove(i);
62  
63        assertGet(k0(), expectedValues);
64      }
65    }
66  
67    @SuppressWarnings("unchecked")
68    @MapFeature.Require(SUPPORTS_REMOVE)
69    @CollectionSize.Require(SEVERAL)
70    public void testRemoveAtIndexFromAsMapPropagates() {
71      List<V> values = Arrays.asList(v0(), v1(), v0());
72  
73      for (int i = 0; i < 3; i++) {
74        resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
75        List<V> expectedValues = copyToList(values);
76  
77        List<V> asMapValue = (List<V>) multimap().asMap().get(k0());
78        asMapValue.remove(i);
79        expectedValues.remove(i);
80  
81        assertGet(k0(), expectedValues);
82      }
83    }
84  
85    @SuppressWarnings("unchecked")
86    @MapFeature.Require(SUPPORTS_REMOVE)
87    @CollectionSize.Require(SEVERAL)
88    public void testRemoveAtIndexFromAsMapEntrySetPropagates() {
89      List<V> values = Arrays.asList(v0(), v1(), v0());
90  
91      for (int i = 0; i < 3; i++) {
92        resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
93        List<V> expectedValues = copyToList(values);
94  
95        Map.Entry<K, Collection<V>> asMapEntry = multimap().asMap().entrySet().iterator().next();
96        List<V> asMapValue = (List<V>) asMapEntry.getValue();
97        asMapValue.remove(i);
98        expectedValues.remove(i);
99  
100       assertGet(k0(), expectedValues);
101     }
102   }
103 }