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