001/*
002 * Copyright (C) 2012 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.copyToList;
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_VALUES;
024import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
025import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
026import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
027import static java.util.Arrays.asList;
028import static java.util.Collections.emptyList;
029import static java.util.Collections.singletonList;
030
031import com.google.common.annotations.GwtCompatible;
032import com.google.common.collect.Multimap;
033import com.google.common.collect.testing.features.CollectionSize;
034import com.google.common.collect.testing.features.MapFeature;
035import java.util.ArrayList;
036import java.util.Collection;
037import java.util.List;
038import org.junit.Ignore;
039
040/**
041 * Tests for {@link Multimap#replaceValues(Object, Iterable)}.
042 *
043 * @author Louis Wasserman
044 */
045@GwtCompatible
046@Ignore("test runners must not instantiate and run this directly, only via suites we build")
047// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
048@SuppressWarnings("JUnit4ClassUsedInJUnit3")
049public class MultimapReplaceValuesTester<K, V>
050    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
051
052  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
053  public void testReplaceValuesWithNullValue() {
054    List<V> values = asList(v0(), null, v3());
055    multimap().replaceValues(k0(), values);
056    assertGet(k0(), values);
057  }
058
059  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
060  public void testReplaceValuesWithNullKey() {
061    List<V> values = asList(v0(), v2(), v3());
062    multimap().replaceValues(null, values);
063    assertGet(null, values);
064  }
065
066  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
067  public void testReplaceEmptyValues() {
068    int size = multimap().size();
069    List<V> values = asList(v0(), v2(), v3());
070    multimap().replaceValues(k3(), values);
071    assertGet(k3(), values);
072    assertEquals(size + values.size(), multimap().size());
073  }
074
075  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
076  public void testReplaceValuesWithEmpty() {
077    int size = multimap().size();
078    List<V> oldValues = new ArrayList<>(multimap().get(k0()));
079    List<V> values = emptyList();
080    assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
081    assertGet(k0());
082    assertEquals(size - oldValues.size(), multimap().size());
083  }
084
085  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
086  public void testReplaceValuesWithDuplicates() {
087    int size = multimap().size();
088    List<V> oldValues = new ArrayList<>(multimap().get(k0()));
089    List<V> values = asList(v0(), v3(), v0());
090    assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
091    assertEquals(size - oldValues.size() + multimap().get(k0()).size(), multimap().size());
092    assertTrue(multimap().get(k0()).containsAll(values));
093  }
094
095  @CollectionSize.Require(absent = ZERO)
096  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
097  public void testReplaceNonEmptyValues() {
098    List<K> keys = copyToList(multimap().keySet());
099    List<V> values = asList(v0(), v2(), v3());
100
101    for (K k : keys) {
102      resetContainer();
103
104      int size = multimap().size();
105      Collection<V> oldKeyValues = copyToList(multimap().get(k));
106      multimap().replaceValues(k, values);
107      assertGet(k, values);
108      assertEquals(size + values.size() - oldKeyValues.size(), multimap().size());
109    }
110  }
111
112  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
113  public void testReplaceValuesPropagatesToGet() {
114    Collection<V> getCollection = multimap().get(k0());
115    List<V> values = asList(v0(), v2(), v3());
116    multimap().replaceValues(k0(), values);
117    assertContentsAnyOrder(getCollection, v0(), v2(), v3());
118  }
119
120  @MapFeature.Require(absent = SUPPORTS_REMOVE)
121  @CollectionSize.Require(absent = ZERO)
122  public void testReplaceValuesRemoveNotSupported() {
123    List<V> values = singletonList(v3());
124    assertThrows(UnsupportedOperationException.class, () -> multimap().replaceValues(k0(), values));
125  }
126
127  @MapFeature.Require(absent = SUPPORTS_PUT)
128  public void testReplaceValuesPutNotSupported() {
129    List<V> values = singletonList(v3());
130    assertThrows(UnsupportedOperationException.class, () -> multimap().replaceValues(k0(), values));
131  }
132}