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.
039@SuppressWarnings("JUnit4ClassUsedInJUnit3")
040public class MultimapKeySetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
041  public void testKeySet() {
042    for (Entry<K, V> entry : getSampleElements()) {
043      assertTrue(multimap().keySet().contains(entry.getKey()));
044    }
045  }
046
047  @CollectionSize.Require(absent = ZERO)
048  @MapFeature.Require(ALLOWS_NULL_KEYS)
049  public void testKeySetContainsNullKeyPresent() {
050    initMultimapWithNullKey();
051    assertTrue(multimap().keySet().contains(null));
052  }
053
054  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
055  public void testKeySetContainsNullKeyAbsent() {
056    assertFalse(multimap().keySet().contains(null));
057  }
058
059  @MapFeature.Require(SUPPORTS_REMOVE)
060  public void testKeySetRemovePropagatesToMultimap() {
061    int key0Count = multimap().get(k0()).size();
062    assertEquals(key0Count > 0, multimap().keySet().remove(k0()));
063    assertEquals(getNumElements() - key0Count, multimap().size());
064    assertGet(k0());
065  }
066
067  @CollectionSize.Require(absent = ZERO)
068  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
069  public void testKeySetIteratorRemove() {
070    int key0Count = multimap().get(k0()).size();
071    Iterator<K> keyItr = multimap().keySet().iterator();
072    while (keyItr.hasNext()) {
073      if (keyItr.next().equals(k0())) {
074        keyItr.remove();
075      }
076    }
077    assertEquals(getNumElements() - key0Count, multimap().size());
078    assertGet(k0());
079  }
080}