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.features.CollectionSize.ZERO;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
024import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.Multimap;
028import com.google.common.collect.testing.Helpers;
029import com.google.common.collect.testing.features.CollectionSize;
030import com.google.common.collect.testing.features.MapFeature;
031import java.util.ArrayList;
032import java.util.Arrays;
033import java.util.Collection;
034import java.util.Collections;
035import java.util.List;
036
037/**
038 * Tests for {@link Multimap#replaceValues(Object, Iterable)}.
039 *
040 * @author Louis Wasserman
041 */
042@GwtCompatible
043public class MultimapReplaceValuesTester<K, V>
044    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
045
046  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
047  public void testReplaceValuesWithNullValue() {
048    @SuppressWarnings("unchecked")
049    List<V> values = Arrays.asList(v0(), null, v3());
050    multimap().replaceValues(k0(), values);
051    assertGet(k0(), values);
052  }
053
054  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
055  public void testReplaceValuesWithNullKey() {
056    @SuppressWarnings("unchecked")
057    List<V> values = Arrays.asList(v0(), v2(), v3());
058    multimap().replaceValues(null, values);
059    assertGet(null, values);
060  }
061
062  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
063  public void testReplaceEmptyValues() {
064    int size = multimap().size();
065    @SuppressWarnings("unchecked")
066    List<V> values = Arrays.asList(v0(), v2(), v3());
067    multimap().replaceValues(k3(), values);
068    assertGet(k3(), values);
069    assertEquals(size + values.size(), multimap().size());
070  }
071
072  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
073  public void testReplaceValuesWithEmpty() {
074    int size = multimap().size();
075    List<V> oldValues = new ArrayList<>(multimap().get(k0()));
076    @SuppressWarnings("unchecked")
077    List<V> values = Collections.emptyList();
078    assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
079    assertGet(k0());
080    assertEquals(size - oldValues.size(), multimap().size());
081  }
082
083  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
084  public void testReplaceValuesWithDuplicates() {
085    int size = multimap().size();
086    List<V> oldValues = new ArrayList<>(multimap().get(k0()));
087    List<V> values = Arrays.asList(v0(), v3(), v0());
088    assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
089    assertEquals(size - oldValues.size() + multimap().get(k0()).size(), multimap().size());
090    assertTrue(multimap().get(k0()).containsAll(values));
091  }
092
093  @CollectionSize.Require(absent = ZERO)
094  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
095  public void testReplaceNonEmptyValues() {
096    List<K> keys = Helpers.copyToList(multimap().keySet());
097    @SuppressWarnings("unchecked")
098    List<V> values = Arrays.asList(v0(), v2(), v3());
099
100    for (K k : keys) {
101      resetContainer();
102
103      int size = multimap().size();
104      Collection<V> oldKeyValues = Helpers.copyToList(multimap().get(k));
105      multimap().replaceValues(k, values);
106      assertGet(k, values);
107      assertEquals(size + values.size() - oldKeyValues.size(), multimap().size());
108    }
109  }
110
111  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
112  public void testReplaceValuesPropagatesToGet() {
113    Collection<V> getCollection = multimap().get(k0());
114    @SuppressWarnings("unchecked")
115    List<V> values = Arrays.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 = Collections.singletonList(v3());
124    try {
125      multimap().replaceValues(k0(), values);
126      fail("Expected UnsupportedOperationException");
127    } catch (UnsupportedOperationException expected) {
128      // success
129    }
130  }
131
132  @MapFeature.Require(absent = SUPPORTS_PUT)
133  public void testReplaceValuesPutNotSupported() {
134    List<V> values = Collections.singletonList(v3());
135    try {
136      multimap().replaceValues(k0(), values);
137      fail("Expected UnsupportedOperationException");
138    } catch (UnsupportedOperationException expected) {
139      // success
140    }
141  }
142}