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
17package com.google.common.collect.testing.google;
18
19import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
21import static org.truth0.Truth.ASSERT;
22
23import com.google.common.annotations.GwtCompatible;
24import com.google.common.collect.Multimap;
25import com.google.common.collect.testing.features.CollectionSize;
26import com.google.common.collect.testing.features.MapFeature;
27
28import java.util.Collection;
29import java.util.Map;
30import java.util.Map.Entry;
31
32/**
33 * Tests for {@link Multimap#clear()}.
34 *
35 * @author Louis Wasserman
36 */
37@GwtCompatible
38public 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    ASSERT.that(multimap().entries()).isEmpty();
53    ASSERT.that(multimap().asMap()).isEmpty();
54    ASSERT.that(multimap().keySet()).isEmpty();
55    ASSERT.that(multimap().keys()).isEmpty();
56    ASSERT.that(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      ASSERT.that(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        ASSERT.that(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    ASSERT.that(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    ASSERT.that(entries).isEmpty();
134  }
135}
136