001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.collect.testing.google;
016
017import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
018import static com.google.common.collect.testing.features.CollectionSize.ZERO;
019import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
021import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.collect.Multimap;
025import com.google.common.collect.testing.features.CollectionFeature;
026import com.google.common.collect.testing.features.CollectionSize;
027import com.google.common.collect.testing.features.MapFeature;
028import java.util.Iterator;
029import java.util.Map.Entry;
030import org.junit.Ignore;
031
032/**
033 * Tester for {@code Multimap.keySet}.
034 *
035 * @author Louis Wasserman
036 */
037@GwtCompatible
038@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
039public class MultimapKeySetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
040  public void testKeySet() {
041    for (Entry<K, V> entry : getSampleElements()) {
042      assertTrue(multimap().keySet().contains(entry.getKey()));
043    }
044  }
045
046  @CollectionSize.Require(absent = ZERO)
047  @MapFeature.Require(ALLOWS_NULL_KEYS)
048  public void testKeySetContainsNullKeyPresent() {
049    initMultimapWithNullKey();
050    assertTrue(multimap().keySet().contains(null));
051  }
052
053  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
054  public void testKeySetContainsNullKeyAbsent() {
055    assertFalse(multimap().keySet().contains(null));
056  }
057
058  @MapFeature.Require(SUPPORTS_REMOVE)
059  public void testKeySetRemovePropagatesToMultimap() {
060    int key0Count = multimap().get(k0()).size();
061    assertEquals(key0Count > 0, multimap().keySet().remove(k0()));
062    assertEquals(getNumElements() - key0Count, multimap().size());
063    assertGet(k0());
064  }
065
066  @CollectionSize.Require(absent = ZERO)
067  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
068  public void testKeySetIteratorRemove() {
069    int key0Count = multimap().get(k0()).size();
070    Iterator<K> keyItr = multimap().keySet().iterator();
071    while (keyItr.hasNext()) {
072      if (keyItr.next().equals(k0())) {
073        keyItr.remove();
074      }
075    }
076    assertEquals(getNumElements() - key0Count, multimap().size());
077    assertGet(k0());
078  }
079}