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 com.google.common.collect.testing.google.ReflectionFreeAssertThrows.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;
030import org.junit.Ignore;
031
032/**
033 * Tester for {@link Multimap#putAll(Multimap)}.
034 *
035 * @author Louis Wasserman
036 */
037@GwtCompatible
038@Ignore("test runners must not instantiate and run this directly, only via suites we build")
039// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
040@SuppressWarnings("JUnit4ClassUsedInJUnit3")
041public class MultimapPutAllMultimapTester<K, V>
042    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
043  @MapFeature.Require(absent = SUPPORTS_PUT)
044  public void testPutUnsupported() {
045    assertThrows(
046        UnsupportedOperationException.class,
047        () -> multimap().putAll(getSubjectGenerator().create(mapEntry(k3(), v3()))));
048  }
049
050  @MapFeature.Require(SUPPORTS_PUT)
051  // Empty multimaps *do* have defined equals semantics.
052  @SuppressWarnings("UndefinedEquals")
053  public void testPutAllIntoEmpty() {
054    Multimap<K, V> target = getSubjectGenerator().create();
055    assertEquals(!multimap().isEmpty(), target.putAll(multimap()));
056    assertEquals(multimap(), target);
057  }
058
059  @MapFeature.Require(SUPPORTS_PUT)
060  public void testPutAll() {
061    Multimap<K, V> source =
062        getSubjectGenerator().create(mapEntry(k0(), v3()), mapEntry(k3(), v3()));
063    assertTrue(multimap().putAll(source));
064    assertTrue(multimap().containsEntry(k0(), v3()));
065    assertTrue(multimap().containsEntry(k3(), v3()));
066  }
067
068  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
069  public void testPutAllWithNullValue() {
070    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(k0(), null));
071    assertTrue(multimap().putAll(source));
072    assertTrue(multimap().containsEntry(k0(), null));
073  }
074
075  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
076  public void testPutAllWithNullKey() {
077    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(null, v0()));
078    assertTrue(multimap().putAll(source));
079    assertTrue(multimap().containsEntry(null, v0()));
080  }
081
082  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
083  public void testPutAllRejectsNullValue() {
084    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(k0(), null));
085    assertThrows(NullPointerException.class, () -> multimap().putAll(source));
086    expectUnchanged();
087  }
088
089  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
090  public void testPutAllRejectsNullKey() {
091    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(null, v0()));
092    assertThrows(NullPointerException.class, () -> multimap().putAll(source));
093    expectUnchanged();
094  }
095
096  @MapFeature.Require(SUPPORTS_PUT)
097  public void testPutAllPropagatesToGet() {
098    Multimap<K, V> source =
099        getSubjectGenerator().create(mapEntry(k0(), v3()), mapEntry(k3(), v3()));
100    Collection<V> getCollection = multimap().get(k0());
101    int getCollectionSize = getCollection.size();
102    assertTrue(multimap().putAll(source));
103    assertEquals(getCollectionSize + 1, getCollection.size());
104    assertContains(getCollection, v3());
105  }
106}