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;
031
032/**
033 * Testers for {@link ListMultimap#remove(Object, Object)}.
034 *
035 * @author Louis Wasserman
036 */
037@GwtCompatible
038public class ListMultimapRemoveTester<K, V> extends AbstractListMultimapTester<K, V> {
039  @MapFeature.Require(SUPPORTS_REMOVE)
040  @CollectionSize.Require(SEVERAL)
041  public void testMultimapRemoveDeletesFirstOccurrence() {
042    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
043
044    List<V> list = multimap().get(k0());
045    multimap().remove(k0(), v0());
046    assertContentsInOrder(list, v1(), v0());
047  }
048
049  @MapFeature.Require(SUPPORTS_REMOVE)
050  @CollectionSize.Require(SEVERAL)
051  public void testRemoveAtIndexFromGetPropagates() {
052    List<V> values = asList(v0(), v1(), v0());
053
054    for (int i = 0; i < 3; i++) {
055      resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
056      List<V> expectedValues = copyToList(values);
057
058      multimap().get(k0()).remove(i);
059      expectedValues.remove(i);
060
061      assertGet(k0(), expectedValues);
062    }
063  }
064
065  @MapFeature.Require(SUPPORTS_REMOVE)
066  @CollectionSize.Require(SEVERAL)
067  public void testRemoveAtIndexFromAsMapPropagates() {
068    List<V> values = asList(v0(), v1(), v0());
069
070    for (int i = 0; i < 3; i++) {
071      resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
072      List<V> expectedValues = copyToList(values);
073
074      List<V> asMapValue = (List<V>) multimap().asMap().get(k0());
075      asMapValue.remove(i);
076      expectedValues.remove(i);
077
078      assertGet(k0(), expectedValues);
079    }
080  }
081
082  @MapFeature.Require(SUPPORTS_REMOVE)
083  @CollectionSize.Require(SEVERAL)
084  public void testRemoveAtIndexFromAsMapEntrySetPropagates() {
085    List<V> values = asList(v0(), v1(), v0());
086
087    for (int i = 0; i < 3; i++) {
088      resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0()));
089      List<V> expectedValues = copyToList(values);
090
091      Entry<K, Collection<V>> asMapEntry = multimap().asMap().entrySet().iterator().next();
092      List<V> asMapValue = (List<V>) asMapEntry.getValue();
093      asMapValue.remove(i);
094      expectedValues.remove(i);
095
096      assertGet(k0(), expectedValues);
097    }
098  }
099}