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.Helpers.assertEmpty;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
22  import static com.google.common.collect.testing.google.GoogleHelpers.assertEmpty;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.Multimap;
26  import com.google.common.collect.testing.features.CollectionSize;
27  import com.google.common.collect.testing.features.MapFeature;
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  
49    private void assertCleared() {
50      assertEquals(0, multimap().size());
51      assertEmpty(multimap());
52      assertEquals(multimap(), getSubjectGenerator().create());
53      assertEmpty(multimap().entries());
54      assertEmpty(multimap().asMap());
55      assertEmpty(multimap().keySet());
56      assertEmpty(multimap().keys());
57      assertEmpty(multimap().values());
58      for (K key : sampleKeys()) {
59        assertGet(key);
60      }
61    }
62  
63    @MapFeature.Require(SUPPORTS_REMOVE)
64    public void testClear() {
65      multimap().clear();
66      assertCleared();
67    }
68  
69    @MapFeature.Require(SUPPORTS_REMOVE)
70    public void testClearThroughEntries() {
71      multimap().entries().clear();
72      assertCleared();
73    }
74  
75    @MapFeature.Require(SUPPORTS_REMOVE)
76    public void testClearThroughAsMap() {
77      multimap().asMap().clear();
78      assertCleared();
79    }
80  
81    @MapFeature.Require(SUPPORTS_REMOVE)
82    public void testClearThroughKeySet() {
83      multimap().keySet().clear();
84      assertCleared();
85    }
86  
87    @MapFeature.Require(SUPPORTS_REMOVE)
88    public void testClearThroughKeys() {
89      multimap().keys().clear();
90      assertCleared();
91    }
92  
93    @MapFeature.Require(SUPPORTS_REMOVE)
94    public void testClearThroughValues() {
95      multimap().values().clear();
96      assertCleared();
97    }
98  
99    @MapFeature.Require(SUPPORTS_REMOVE)
100   @CollectionSize.Require(absent = ZERO)
101   public void testClearPropagatesToGet() {
102     for (K key : sampleKeys()) {
103       resetContainer();
104       Collection<V> collection = multimap().get(key);
105       multimap().clear();
106       assertEmpty(collection);
107     }
108   }
109 
110   @MapFeature.Require(SUPPORTS_REMOVE)
111   @CollectionSize.Require(absent = ZERO)
112   public void testClearPropagatesToAsMapGet() {
113     for (K key : sampleKeys()) {
114       resetContainer();
115       Collection<V> collection = multimap().asMap().get(key);
116       if (collection != null) {
117         multimap().clear();
118         assertEmpty(collection);
119       }
120     }
121   }
122 
123   @MapFeature.Require(SUPPORTS_REMOVE)
124   public void testClearPropagatesToAsMap() {
125     Map<K, Collection<V>> asMap = multimap().asMap();
126     multimap().clear();
127     assertEmpty(asMap);
128   }
129 
130   @MapFeature.Require(SUPPORTS_REMOVE)
131   public void testClearPropagatesToEntries() {
132     Collection<Entry<K, V>> entries = multimap().entries();
133     multimap().clear();
134     assertEmpty(entries);
135   }
136 }