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;
036import org.junit.Ignore;
037
038/**
039 * Tests for {@link Multimap#replaceValues(Object, Iterable)}.
040 *
041 * @author Louis Wasserman
042 */
043@GwtCompatible
044@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045@SuppressWarnings("JUnit4ClassUsedInJUnit3")
046public class MultimapReplaceValuesTester<K, V>
047    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
048
049  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
050  public void testReplaceValuesWithNullValue() {
051    List<V> values = Arrays.asList(v0(), null, v3());
052    multimap().replaceValues(k0(), values);
053    assertGet(k0(), values);
054  }
055
056  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
057  public void testReplaceValuesWithNullKey() {
058    List<V> values = Arrays.asList(v0(), v2(), v3());
059    multimap().replaceValues(null, values);
060    assertGet(null, values);
061  }
062
063  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
064  public void testReplaceEmptyValues() {
065    int size = multimap().size();
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    List<V> values = Collections.emptyList();
077    assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
078    assertGet(k0());
079    assertEquals(size - oldValues.size(), multimap().size());
080  }
081
082  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
083  public void testReplaceValuesWithDuplicates() {
084    int size = multimap().size();
085    List<V> oldValues = new ArrayList<>(multimap().get(k0()));
086    List<V> values = Arrays.asList(v0(), v3(), v0());
087    assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values)));
088    assertEquals(size - oldValues.size() + multimap().get(k0()).size(), multimap().size());
089    assertTrue(multimap().get(k0()).containsAll(values));
090  }
091
092  @CollectionSize.Require(absent = ZERO)
093  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
094  public void testReplaceNonEmptyValues() {
095    List<K> keys = Helpers.copyToList(multimap().keySet());
096    List<V> values = Arrays.asList(v0(), v2(), v3());
097
098    for (K k : keys) {
099      resetContainer();
100
101      int size = multimap().size();
102      Collection<V> oldKeyValues = Helpers.copyToList(multimap().get(k));
103      multimap().replaceValues(k, values);
104      assertGet(k, values);
105      assertEquals(size + values.size() - oldKeyValues.size(), multimap().size());
106    }
107  }
108
109  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
110  public void testReplaceValuesPropagatesToGet() {
111    Collection<V> getCollection = multimap().get(k0());
112    List<V> values = Arrays.asList(v0(), v2(), v3());
113    multimap().replaceValues(k0(), values);
114    assertContentsAnyOrder(getCollection, v0(), v2(), v3());
115  }
116
117  @MapFeature.Require(absent = SUPPORTS_REMOVE)
118  @CollectionSize.Require(absent = ZERO)
119  public void testReplaceValuesRemoveNotSupported() {
120    List<V> values = Collections.singletonList(v3());
121    try {
122      multimap().replaceValues(k0(), values);
123      fail("Expected UnsupportedOperationException");
124    } catch (UnsupportedOperationException expected) {
125      // success
126    }
127  }
128
129  @MapFeature.Require(absent = SUPPORTS_PUT)
130  public void testReplaceValuesPutNotSupported() {
131    List<V> values = Collections.singletonList(v3());
132    try {
133      multimap().replaceValues(k0(), values);
134      fail("Expected UnsupportedOperationException");
135    } catch (UnsupportedOperationException expected) {
136      // success
137    }
138  }
139}