001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.google;
018
019import static com.google.common.collect.testing.Helpers.assertEmpty;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
022import static com.google.common.collect.testing.google.GoogleHelpers.assertEmpty;
023import static org.junit.Assert.assertThrows;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.Multimap;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.MapFeature;
029import java.util.Collection;
030import java.util.Map;
031import java.util.Map.Entry;
032
033/**
034 * Tests for {@link Multimap#clear()}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039public class MultimapClearTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
040  @CollectionSize.Require(absent = ZERO)
041  @MapFeature.Require(absent = SUPPORTS_REMOVE)
042  public void testClearUnsupported() {
043    assertThrows(UnsupportedOperationException.class, () -> multimap().clear());
044  }
045
046  // Empty multimaps *do* have defined equals semantics.
047  @SuppressWarnings("UndefinedEquals")
048  private void assertCleared() {
049    assertEquals(0, multimap().size());
050    assertEmpty(multimap());
051    assertEquals(multimap(), getSubjectGenerator().create());
052    assertEmpty(multimap().entries());
053    assertEmpty(multimap().asMap());
054    assertEmpty(multimap().keySet());
055    assertEmpty(multimap().keys());
056    assertEmpty(multimap().values());
057    for (K key : sampleKeys()) {
058      assertGet(key);
059    }
060  }
061
062  @MapFeature.Require(SUPPORTS_REMOVE)
063  public void testClear() {
064    multimap().clear();
065    assertCleared();
066  }
067
068  @MapFeature.Require(SUPPORTS_REMOVE)
069  public void testClearThroughEntries() {
070    multimap().entries().clear();
071    assertCleared();
072  }
073
074  @MapFeature.Require(SUPPORTS_REMOVE)
075  public void testClearThroughAsMap() {
076    multimap().asMap().clear();
077    assertCleared();
078  }
079
080  @MapFeature.Require(SUPPORTS_REMOVE)
081  public void testClearThroughKeySet() {
082    multimap().keySet().clear();
083    assertCleared();
084  }
085
086  @MapFeature.Require(SUPPORTS_REMOVE)
087  public void testClearThroughKeys() {
088    multimap().keys().clear();
089    assertCleared();
090  }
091
092  @MapFeature.Require(SUPPORTS_REMOVE)
093  public void testClearThroughValues() {
094    multimap().values().clear();
095    assertCleared();
096  }
097
098  @MapFeature.Require(SUPPORTS_REMOVE)
099  @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      assertEmpty(collection);
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        assertEmpty(collection);
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    assertEmpty(asMap);
127  }
128
129  @MapFeature.Require(SUPPORTS_REMOVE)
130  public void testClearPropagatesToEntries() {
131    Collection<Entry<K, V>> entries = multimap().entries();
132    multimap().clear();
133    assertEmpty(entries);
134  }
135}