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  public void testPutAllIntoEmpty() {
052    Multimap<K, V> target = getSubjectGenerator().create();
053    assertEquals(!multimap().isEmpty(), target.putAll(multimap()));
054    assertEquals(multimap(), target);
055  }
056
057  @MapFeature.Require(SUPPORTS_PUT)
058  public void testPutAll() {
059    Multimap<K, V> source =
060        getSubjectGenerator().create(mapEntry(k0(), v3()), mapEntry(k3(), v3()));
061    assertTrue(multimap().putAll(source));
062    assertTrue(multimap().containsEntry(k0(), v3()));
063    assertTrue(multimap().containsEntry(k3(), v3()));
064  }
065
066  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
067  public void testPutAllWithNullValue() {
068    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(k0(), null));
069    assertTrue(multimap().putAll(source));
070    assertTrue(multimap().containsEntry(k0(), null));
071  }
072
073  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
074  public void testPutAllWithNullKey() {
075    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(null, v0()));
076    assertTrue(multimap().putAll(source));
077    assertTrue(multimap().containsEntry(null, v0()));
078  }
079
080  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
081  public void testPutAllRejectsNullValue() {
082    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(k0(), null));
083    assertThrows(NullPointerException.class, () -> multimap().putAll(source));
084    expectUnchanged();
085  }
086
087  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
088  public void testPutAllRejectsNullKey() {
089    Multimap<K, V> source = getSubjectGenerator().create(mapEntry(null, v0()));
090    assertThrows(NullPointerException.class, () -> multimap().putAll(source));
091    expectUnchanged();
092  }
093
094  @MapFeature.Require(SUPPORTS_PUT)
095  public void testPutAllPropagatesToGet() {
096    Multimap<K, V> source =
097        getSubjectGenerator().create(mapEntry(k0(), v3()), mapEntry(k3(), v3()));
098    Collection<V> getCollection = multimap().get(k0());
099    int getCollectionSize = getCollection.size();
100    assertTrue(multimap().putAll(source));
101    assertEquals(getCollectionSize + 1, getCollection.size());
102    assertContains(getCollection, v3());
103  }
104}