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;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.Multimap;
026import com.google.common.collect.testing.features.CollectionSize;
027import com.google.common.collect.testing.features.MapFeature;
028import java.util.Collection;
029import java.util.Map;
030import java.util.Map.Entry;
031import org.junit.Ignore;
032
033/**
034 * Tests for {@link Multimap#clear()}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
040public class MultimapClearTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
041  @CollectionSize.Require(absent = ZERO)
042  @MapFeature.Require(absent = SUPPORTS_REMOVE)
043  public void testClearUnsupported() {
044    try {
045      multimap().clear();
046      fail("Expected UnsupportedOperationException");
047    } catch (UnsupportedOperationException expected) {
048    }
049  }
050
051  private void assertCleared() {
052    assertEquals(0, multimap().size());
053    assertEmpty(multimap());
054    assertEquals(multimap(), getSubjectGenerator().create());
055    assertEmpty(multimap().entries());
056    assertEmpty(multimap().asMap());
057    assertEmpty(multimap().keySet());
058    assertEmpty(multimap().keys());
059    assertEmpty(multimap().values());
060    for (K key : sampleKeys()) {
061      assertGet(key);
062    }
063  }
064
065  @MapFeature.Require(SUPPORTS_REMOVE)
066  public void testClear() {
067    multimap().clear();
068    assertCleared();
069  }
070
071  @MapFeature.Require(SUPPORTS_REMOVE)
072  public void testClearThroughEntries() {
073    multimap().entries().clear();
074    assertCleared();
075  }
076
077  @MapFeature.Require(SUPPORTS_REMOVE)
078  public void testClearThroughAsMap() {
079    multimap().asMap().clear();
080    assertCleared();
081  }
082
083  @MapFeature.Require(SUPPORTS_REMOVE)
084  public void testClearThroughKeySet() {
085    multimap().keySet().clear();
086    assertCleared();
087  }
088
089  @MapFeature.Require(SUPPORTS_REMOVE)
090  public void testClearThroughKeys() {
091    multimap().keys().clear();
092    assertCleared();
093  }
094
095  @MapFeature.Require(SUPPORTS_REMOVE)
096  public void testClearThroughValues() {
097    multimap().values().clear();
098    assertCleared();
099  }
100
101  @MapFeature.Require(SUPPORTS_REMOVE)
102  @CollectionSize.Require(absent = ZERO)
103  public void testClearPropagatesToGet() {
104    for (K key : sampleKeys()) {
105      resetContainer();
106      Collection<V> collection = multimap().get(key);
107      multimap().clear();
108      assertEmpty(collection);
109    }
110  }
111
112  @MapFeature.Require(SUPPORTS_REMOVE)
113  @CollectionSize.Require(absent = ZERO)
114  public void testClearPropagatesToAsMapGet() {
115    for (K key : sampleKeys()) {
116      resetContainer();
117      Collection<V> collection = multimap().asMap().get(key);
118      if (collection != null) {
119        multimap().clear();
120        assertEmpty(collection);
121      }
122    }
123  }
124
125  @MapFeature.Require(SUPPORTS_REMOVE)
126  public void testClearPropagatesToAsMap() {
127    Map<K, Collection<V>> asMap = multimap().asMap();
128    multimap().clear();
129    assertEmpty(asMap);
130  }
131
132  @MapFeature.Require(SUPPORTS_REMOVE)
133  public void testClearPropagatesToEntries() {
134    Collection<Entry<K, V>> entries = multimap().entries();
135    multimap().clear();
136    assertEmpty(entries);
137  }
138}