001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.google;
018
019import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder;
020import static com.google.common.collect.testing.Helpers.assertEmpty;
021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
022import static com.google.common.collect.testing.features.CollectionSize.ZERO;
023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
025import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
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.CollectionSize;
032import com.google.common.collect.testing.features.MapFeature;
033import java.util.Collection;
034import org.junit.Ignore;
035
036/**
037 * Tests for {@code Multimap.asMap().get(Object)}.
038 *
039 * @author Louis Wasserman
040 */
041@GwtCompatible
042@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
043public class MultimapAsMapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
044
045  @CollectionSize.Require(SEVERAL)
046  @MapFeature.Require(SUPPORTS_REMOVE)
047  public void testPropagatesRemoveToMultimap() {
048    resetContainer(
049        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k0(), v2()));
050    Collection<V> result = multimap().asMap().get(k0());
051    assertTrue(result.remove(v0()));
052    assertFalse(multimap().containsEntry(k0(), v0()));
053    assertEquals(2, multimap().size());
054  }
055
056  @CollectionSize.Require(absent = ZERO)
057  @MapFeature.Require(SUPPORTS_REMOVE)
058  public void testPropagatesRemoveLastElementToMultimap() {
059    Collection<V> result = multimap().asMap().get(k0());
060    assertTrue(result.remove(v0()));
061    assertGet(k0());
062  }
063
064  @CollectionSize.Require(absent = ZERO)
065  @MapFeature.Require(SUPPORTS_REMOVE)
066  public void testPropagatesClearToMultimap() {
067    Collection<V> result = multimap().asMap().get(k0());
068    result.clear();
069    assertGet(k0());
070    assertEmpty(result);
071  }
072
073  @CollectionSize.Require(absent = ZERO)
074  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
075  public void testAddNullValue() {
076    Collection<V> result = multimap().asMap().get(k0());
077    assertTrue(result.add(null));
078    assertTrue(multimap().containsEntry(k0(), null));
079  }
080
081  @CollectionSize.Require(absent = ZERO)
082  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES})
083  public void testRemoveNullValue() {
084    Collection<V> result = multimap().asMap().get(k0());
085    assertFalse(result.remove(null));
086  }
087
088  @CollectionSize.Require(absent = ZERO)
089  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
090  public void testAddNullValueUnsupported() {
091    Collection<V> result = multimap().asMap().get(k0());
092    try {
093      result.add(null);
094      fail("Expected NullPointerException");
095    } catch (NullPointerException expected) {
096    }
097  }
098
099  @CollectionSize.Require(absent = ZERO)
100  @MapFeature.Require(SUPPORTS_PUT)
101  public void testPropagatesAddToMultimap() {
102    Collection<V> result = multimap().asMap().get(k0());
103    result.add(v3());
104    assertContentsAnyOrder(multimap().get(k0()), v0(), v3());
105  }
106
107  @CollectionSize.Require(absent = ZERO)
108  @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT})
109  public void testPropagatesRemoveThenAddToMultimap() {
110    int oldSize = getNumElements();
111
112    Collection<V> result = multimap().asMap().get(k0());
113    assertTrue(result.remove(v0()));
114
115    assertFalse(multimap().containsKey(k0()));
116    assertFalse(multimap().containsEntry(k0(), v0()));
117    assertEmpty(result);
118
119    assertTrue(result.add(v1()));
120    assertTrue(result.add(v2()));
121
122    assertContentsAnyOrder(result, v1(), v2());
123    assertContentsAnyOrder(multimap().get(k0()), v1(), v2());
124    assertTrue(multimap().containsKey(k0()));
125    assertFalse(multimap().containsEntry(k0(), v0()));
126    assertTrue(multimap().containsEntry(k0(), v2()));
127    assertEquals(oldSize + 1, multimap().size());
128  }
129
130  @CollectionSize.Require(absent = ZERO)
131  @MapFeature.Require(SUPPORTS_REMOVE)
132  public void testReflectsMultimapRemove() {
133    Collection<V> result = multimap().asMap().get(k0());
134    multimap().removeAll(k0());
135    assertEmpty(result);
136  }
137}