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