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