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.
046@SuppressWarnings("JUnit4ClassUsedInJUnit3")
047public class MultimapEntriesTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
048  public void testEntries() {
049    assertEqualIgnoringOrder(getSampleElements(), multimap().entries());
050  }
051
052  @CollectionSize.Require(absent = ZERO)
053  @MapFeature.Require(ALLOWS_NULL_KEYS)
054  public void testContainsEntryWithNullKeyPresent() {
055    initMultimapWithNullKey();
056    assertContains(multimap().entries(), Helpers.mapEntry((K) null, getValueForNullKey()));
057  }
058
059  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
060  public void testContainsEntryWithNullKeyAbsent() {
061    assertFalse(multimap().entries().contains(Helpers.mapEntry(null, v0())));
062  }
063
064  @CollectionSize.Require(absent = ZERO)
065  @MapFeature.Require(ALLOWS_NULL_VALUES)
066  public void testContainsEntryWithNullValuePresent() {
067    initMultimapWithNullValue();
068    assertContains(multimap().entries(), Helpers.mapEntry(getKeyForNullValue(), (V) null));
069  }
070
071  @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
072  public void testContainsEntryWithNullValueAbsent() {
073    assertFalse(multimap().entries().contains(Helpers.mapEntry(k0(), null)));
074  }
075
076  @CollectionSize.Require(absent = ZERO)
077  @MapFeature.Require(SUPPORTS_REMOVE)
078  public void testRemovePropagatesToMultimap() {
079    assertTrue(multimap().entries().remove(Helpers.mapEntry(k0(), v0())));
080    expectMissing(Helpers.mapEntry(k0(), v0()));
081    assertEquals(getNumElements() - 1, multimap().size());
082    assertFalse(multimap().containsEntry(k0(), v0()));
083  }
084
085  @CollectionSize.Require(absent = ZERO)
086  @MapFeature.Require(SUPPORTS_REMOVE)
087  public void testRemoveAllPropagatesToMultimap() {
088    assertTrue(multimap().entries().removeAll(Collections.singleton(Helpers.mapEntry(k0(), v0()))));
089    expectMissing(Helpers.mapEntry(k0(), v0()));
090    assertEquals(getNumElements() - 1, multimap().size());
091    assertFalse(multimap().containsEntry(k0(), v0()));
092  }
093
094  @CollectionSize.Require(absent = ZERO)
095  @MapFeature.Require(SUPPORTS_REMOVE)
096  public void testRetainAllPropagatesToMultimap() {
097    multimap().entries().retainAll(Collections.singleton(Helpers.mapEntry(k0(), v0())));
098    assertEquals(getSubjectGenerator().create(Helpers.mapEntry(k0(), v0())), multimap());
099    assertEquals(1, multimap().size());
100    assertTrue(multimap().containsEntry(k0(), v0()));
101  }
102
103  @CollectionSize.Require(ONE)
104  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
105  public void testIteratorRemovePropagatesToMultimap() {
106    Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
107    assertEquals(Helpers.mapEntry(k0(), v0()), iterator.next());
108    iterator.remove();
109    assertTrue(multimap().isEmpty());
110  }
111
112  @CollectionSize.Require(absent = ZERO)
113  @MapFeature.Require(SUPPORTS_REMOVE)
114  public void testEntriesRemainValidAfterRemove() {
115    Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
116    Entry<K, V> entry = iterator.next();
117    K key = entry.getKey();
118    V value = entry.getValue();
119    multimap().removeAll(key);
120    assertEquals(key, entry.getKey());
121    assertEquals(value, entry.getValue());
122  }
123}