001/*
002 * Copyright (C) 2012 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.assertContentsInOrder;
018import static com.google.common.collect.testing.Helpers.copyToList;
019import static com.google.common.collect.testing.Helpers.mapEntry;
020import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
021import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
022import static java.util.Arrays.asList;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.ListMultimap;
026import com.google.common.collect.testing.features.CollectionSize;
027import com.google.common.collect.testing.features.MapFeature;
028import java.util.Collection;
029import java.util.List;
030import java.util.Map.Entry;
031import org.junit.Ignore;
032
033/**
034 * Testers for {@link ListMultimap#remove(Object, Object)}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039@Ignore("test runners must not instantiate and run this directly, only via suites we build")
040// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041@SuppressWarnings("JUnit4ClassUsedInJUnit3")
042public class ListMultimapRemoveTester<K, V> extends AbstractListMultimapTester<K, V> {
043  @MapFeature.Require(SUPPORTS_REMOVE)
044  @CollectionSize.Require(SEVERAL)
045  public void testMultimapRemoveDeletesFirstOccurrence() {
046    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
047
048    List<V> list = multimap().get(k0());
049    multimap().remove(k0(), v0());
050    assertContentsInOrder(list, v1(), v0());
051  }
052
053  @MapFeature.Require(SUPPORTS_REMOVE)
054  @CollectionSize.Require(SEVERAL)
055  public void testRemoveAtIndexFromGetPropagates() {
056    List<V> values = asList(v0(), v1(), v0());
057
058    for (int i = 0; i < 3; i++) {
059      resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
060      List<V> expectedValues = copyToList(values);
061
062      multimap().get(k0()).remove(i);
063      expectedValues.remove(i);
064
065      assertGet(k0(), expectedValues);
066    }
067  }
068
069  @MapFeature.Require(SUPPORTS_REMOVE)
070  @CollectionSize.Require(SEVERAL)
071  public void testRemoveAtIndexFromAsMapPropagates() {
072    List<V> values = asList(v0(), v1(), v0());
073
074    for (int i = 0; i < 3; i++) {
075      resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
076      List<V> expectedValues = copyToList(values);
077
078      List<V> asMapValue = (List<V>) multimap().asMap().get(k0());
079      asMapValue.remove(i);
080      expectedValues.remove(i);
081
082      assertGet(k0(), expectedValues);
083    }
084  }
085
086  @MapFeature.Require(SUPPORTS_REMOVE)
087  @CollectionSize.Require(SEVERAL)
088  public void testRemoveAtIndexFromAsMapEntrySetPropagates() {
089    List<V> values = asList(v0(), v1(), v0());
090
091    for (int i = 0; i < 3; i++) {
092      resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
093      List<V> expectedValues = copyToList(values);
094
095      Entry<K, Collection<V>> asMapEntry = multimap().asMap().entrySet().iterator().next();
096      List<V> asMapValue = (List<V>) asMapEntry.getValue();
097      asMapValue.remove(i);
098      expectedValues.remove(i);
099
100      assertGet(k0(), expectedValues);
101    }
102  }
103}