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