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.copyToList;
018import static com.google.common.collect.testing.Helpers.copyToSet;
019import static com.google.common.collect.testing.features.CollectionSize.ZERO;
020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
021import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.collect.SetMultimap;
025import com.google.common.collect.testing.features.CollectionSize;
026import com.google.common.collect.testing.features.MapFeature;
027import java.util.List;
028import java.util.Map.Entry;
029import java.util.Set;
030
031/**
032 * Tests for {@link SetMultimap#replaceValues}.
033 *
034 * @author Louis Wasserman
035 */
036@GwtCompatible
037public class SetMultimapPutTester<K, V> extends AbstractMultimapTester<K, V, SetMultimap<K, V>> {
038  // Tests for non-duplicate values are in MultimapPutTester
039
040  @MapFeature.Require(SUPPORTS_PUT)
041  @CollectionSize.Require(absent = ZERO)
042  public void testPutDuplicateValuePreservesSize() {
043    assertFalse(multimap().put(k0(), v0()));
044    assertEquals(getNumElements(), multimap().size());
045  }
046
047  @MapFeature.Require(SUPPORTS_PUT)
048  public void testPutDuplicateValue() {
049    List<Entry<K, V>> entries = copyToList(multimap().entries());
050
051    for (Entry<K, V> entry : entries) {
052      resetContainer();
053      K k = entry.getKey();
054      V v = entry.getValue();
055
056      Set<V> values = multimap().get(k);
057      Set<V> expectedValues = copyToSet(values);
058
059      assertFalse(multimap().put(k, v));
060      assertEquals(expectedValues, values);
061      assertGet(k, expectedValues);
062    }
063  }
064
065  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
066  @CollectionSize.Require(absent = ZERO)
067  public void testPutDuplicateValue_null() {
068    initMultimapWithNullValue();
069    assertFalse(multimap().put(getKeyForNullValue(), null));
070    expectContents(createArrayWithNullValue());
071  }
072}