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.Helpers.assertContains;
018import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
019import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
020import static com.google.common.collect.testing.features.CollectionSize.ONE;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
026import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
027
028import com.google.common.annotations.GwtCompatible;
029import com.google.common.collect.Multimap;
030import com.google.common.collect.testing.Helpers;
031import com.google.common.collect.testing.features.CollectionFeature;
032import com.google.common.collect.testing.features.CollectionSize;
033import com.google.common.collect.testing.features.MapFeature;
034import java.util.Collections;
035import java.util.Iterator;
036import java.util.Map.Entry;
037import org.junit.Ignore;
038
039/**
040 * Tester for {@code Multimap.entries}.
041 *
042 * @author Louis Wasserman
043 */
044@GwtCompatible
045@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
046public class MultimapEntriesTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
047  public void testEntries() {
048    assertEqualIgnoringOrder(getSampleElements(), multimap().entries());
049  }
050
051  @CollectionSize.Require(absent = ZERO)
052  @MapFeature.Require(ALLOWS_NULL_KEYS)
053  public void testContainsEntryWithNullKeyPresent() {
054    initMultimapWithNullKey();
055    assertContains(multimap().entries(), Helpers.mapEntry((K) null, getValueForNullKey()));
056  }
057
058  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
059  public void testContainsEntryWithNullKeyAbsent() {
060    assertFalse(multimap().entries().contains(Helpers.mapEntry(null, v0())));
061  }
062
063  @CollectionSize.Require(absent = ZERO)
064  @MapFeature.Require(ALLOWS_NULL_VALUES)
065  public void testContainsEntryWithNullValuePresent() {
066    initMultimapWithNullValue();
067    assertContains(multimap().entries(), Helpers.mapEntry(getKeyForNullValue(), (V) null));
068  }
069
070  @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
071  public void testContainsEntryWithNullValueAbsent() {
072    assertFalse(multimap().entries().contains(Helpers.mapEntry(k0(), null)));
073  }
074
075  @CollectionSize.Require(absent = ZERO)
076  @MapFeature.Require(SUPPORTS_REMOVE)
077  public void testRemovePropagatesToMultimap() {
078    assertTrue(multimap().entries().remove(Helpers.mapEntry(k0(), v0())));
079    expectMissing(Helpers.mapEntry(k0(), v0()));
080    assertEquals(getNumElements() - 1, multimap().size());
081    assertFalse(multimap().containsEntry(k0(), v0()));
082  }
083
084  @CollectionSize.Require(absent = ZERO)
085  @MapFeature.Require(SUPPORTS_REMOVE)
086  public void testRemoveAllPropagatesToMultimap() {
087    assertTrue(multimap().entries().removeAll(Collections.singleton(Helpers.mapEntry(k0(), v0()))));
088    expectMissing(Helpers.mapEntry(k0(), v0()));
089    assertEquals(getNumElements() - 1, multimap().size());
090    assertFalse(multimap().containsEntry(k0(), v0()));
091  }
092
093  @CollectionSize.Require(absent = ZERO)
094  @MapFeature.Require(SUPPORTS_REMOVE)
095  public void testRetainAllPropagatesToMultimap() {
096    multimap().entries().retainAll(Collections.singleton(Helpers.mapEntry(k0(), v0())));
097    assertEquals(getSubjectGenerator().create(Helpers.mapEntry(k0(), v0())), multimap());
098    assertEquals(1, multimap().size());
099    assertTrue(multimap().containsEntry(k0(), v0()));
100  }
101
102  @CollectionSize.Require(ONE)
103  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
104  public void testIteratorRemovePropagatesToMultimap() {
105    Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
106    assertEquals(Helpers.mapEntry(k0(), v0()), iterator.next());
107    iterator.remove();
108    assertTrue(multimap().isEmpty());
109  }
110
111  @CollectionSize.Require(absent = ZERO)
112  @MapFeature.Require(SUPPORTS_REMOVE)
113  public void testEntriesRemainValidAfterRemove() {
114    Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
115    Entry<K, V> entry = iterator.next();
116    K key = entry.getKey();
117    V value = entry.getValue();
118    multimap().removeAll(key);
119    assertEquals(key, entry.getKey());
120    assertEquals(value, entry.getValue());
121  }
122}