View Javadoc
1   /*
2    * Copyright (C) 2013 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.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
21  import static com.google.common.truth.Truth.assertThat;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.Multimap;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  
28  import java.util.Collection;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  
32  /**
33   * Tests for {@link Multimap#clear()}.
34   *
35   * @author Louis Wasserman
36   */
37  @GwtCompatible
38  public class MultimapClearTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
39    @CollectionSize.Require(absent = ZERO)
40    @MapFeature.Require(absent = SUPPORTS_REMOVE)
41    public void testClearUnsupported() {
42      try {
43        multimap().clear();
44        fail("Expected UnsupportedOperationException");
45      } catch (UnsupportedOperationException expected) {}
46    }
47  
48    private void assertCleared() {
49      assertEquals(0, multimap().size());
50      assertTrue(multimap().isEmpty());
51      assertEquals(multimap(), getSubjectGenerator().create());
52      assertThat(multimap().entries()).isEmpty();
53      assertThat(multimap().asMap()).isEmpty();
54      assertThat(multimap().keySet()).isEmpty();
55      assertThat(multimap().keys()).isEmpty();
56      assertThat(multimap().values()).isEmpty();
57      for (K key : sampleKeys()) {
58        assertGet(key);
59      }
60    }
61    
62    @MapFeature.Require(SUPPORTS_REMOVE)
63    public void testClear() {
64      multimap().clear();
65      assertCleared();
66    }
67    
68    @MapFeature.Require(SUPPORTS_REMOVE)
69    public void testClearThroughEntries() {
70      multimap().entries().clear();
71      assertCleared();
72    }
73    
74    @MapFeature.Require(SUPPORTS_REMOVE)
75    public void testClearThroughAsMap() {
76      multimap().asMap().clear();
77      assertCleared();
78    }
79    
80    @MapFeature.Require(SUPPORTS_REMOVE)
81    public void testClearThroughKeySet() {
82      multimap().keySet().clear();
83      assertCleared();
84    }
85    
86    @MapFeature.Require(SUPPORTS_REMOVE)
87    public void testClearThroughKeys() {
88      multimap().keys().clear();
89      assertCleared();
90    }
91    
92    @MapFeature.Require(SUPPORTS_REMOVE)
93    public void testClearThroughValues() {
94      multimap().values().clear();
95      assertCleared();
96    }
97    
98    @MapFeature.Require(SUPPORTS_REMOVE)
99    @CollectionSize.Require(absent = ZERO)
100   public void testClearPropagatesToGet() {
101     for (K key : sampleKeys()) {
102       resetContainer();
103       Collection<V> collection = multimap().get(key);
104       multimap().clear();
105       assertThat(collection).isEmpty();
106     }
107   }
108 
109   @MapFeature.Require(SUPPORTS_REMOVE)
110   @CollectionSize.Require(absent = ZERO)
111   public void testClearPropagatesToAsMapGet() {
112     for (K key : sampleKeys()) {
113       resetContainer();
114       Collection<V> collection = multimap().asMap().get(key);
115       if (collection != null) {
116         multimap().clear();
117         assertThat(collection).isEmpty();
118       }
119     }
120   }
121 
122   @MapFeature.Require(SUPPORTS_REMOVE)
123   public void testClearPropagatesToAsMap() {
124     Map<K, Collection<V>> asMap = multimap().asMap();
125     multimap().clear();
126     assertThat(asMap).isEmpty();
127   }
128 
129   @MapFeature.Require(SUPPORTS_REMOVE)
130   public void testClearPropagatesToEntries() {
131     Collection<Entry<K, V>> entries = multimap().entries();
132     multimap().clear();
133     assertThat(entries).isEmpty();
134   }
135 }