001/*
002 * Copyright (C) 2013 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.assertContains;
020import static com.google.common.collect.testing.Helpers.mapEntry;
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 org.junit.Assert.assertThrows;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.Multimap;
028import com.google.common.collect.testing.features.MapFeature;
029import java.util.Collection;
030
031/**
032 * Tester for {@link Multimap#putAll(Multimap)}.
033 *
034 * @author Louis Wasserman
035 */
036@GwtCompatible
037public class MultimapPutAllMultimapTester<K, V>
038    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
039  @MapFeature.Require(absent = SUPPORTS_PUT)
040  public void testPutUnsupported() {
041    assertThrows(
042        UnsupportedOperationException.class,
043        () -> multimap().putAll(getSubjectGenerator().create(mapEntry(k3(), v3()))));
044  }
045
046  @MapFeature.Require(SUPPORTS_PUT)
047  // Empty multimaps *do* have defined equals semantics.
048  @SuppressWarnings("UndefinedEquals")
049  public void testPutAllIntoEmpty() {
050    Multimap<K, V> target = getSubjectGenerator().create();
051    assertEquals(!multimap().isEmpty(), target.putAll(multimap()));
052    assertEquals(multimap(), target);
053  }
054
055  @MapFeature.Require(SUPPORTS_PUT)
056  public void testPutAll() {
057    Multimap<K, V> source =
058        getSubjectGenerator().create(mapEntry(k0(), v3()), mapEntry(k3(), v3()));
059    assertTrue(multimap().putAll(source));
060    assertTrue(multimap().containsEntry(k0(), v3()));
061    assertTrue(multimap().containsEntry(k3(), v3()));
062  }
063
064  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
065  public void testPutAllWithNullValue() {
066    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(k0(), null));
067    assertTrue(multimap().putAll(source));
068    assertTrue(multimap().containsEntry(k0(), null));
069  }
070
071  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
072  public void testPutAllWithNullKey() {
073    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(null, v0()));
074    assertTrue(multimap().putAll(source));
075    assertTrue(multimap().containsEntry(null, v0()));
076  }
077
078  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
079  public void testPutAllRejectsNullValue() {
080    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(k0(), null));
081    assertThrows(NullPointerException.class, () -> multimap().putAll(source));
082    expectUnchanged();
083  }
084
085  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
086  public void testPutAllRejectsNullKey() {
087    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(null, v0()));
088    assertThrows(NullPointerException.class, () -> multimap().putAll(source));
089    expectUnchanged();
090  }
091
092  @MapFeature.Require(SUPPORTS_PUT)
093  public void testPutAllPropagatesToGet() {
094    Multimap<K, V> source =
095        getSubjectGenerator().create(mapEntry(k0(), v3()), mapEntry(k3(), v3()));
096    Collection<V> getCollection = multimap().get(k0());
097    int getCollectionSize = getCollection.size();
098    assertTrue(multimap().putAll(source));
099    assertEquals(getCollectionSize + 1, getCollection.size());
100    assertContains(getCollection, v3());
101  }
102}