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.
038@SuppressWarnings("JUnit4ClassUsedInJUnit3")
039public class MultimapPutAllMultimapTester<K, V>
040    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
041  @MapFeature.Require(absent = SUPPORTS_PUT)
042  public void testPutUnsupported() {
043    try {
044      multimap().putAll(getSubjectGenerator().create(Helpers.mapEntry(k3(), v3())));
045      fail("Expected UnsupportedOperationException");
046    } catch (UnsupportedOperationException expected) {
047    }
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(Helpers.mapEntry(k0(), v3()), Helpers.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(Helpers.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(Helpers.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(Helpers.mapEntry(k0(), null));
083    try {
084      multimap().putAll(source);
085      fail("Expected NullPointerException");
086    } catch (NullPointerException expected) {
087    }
088    expectUnchanged();
089  }
090
091  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
092  public void testPutAllRejectsNullKey() {
093    Multimap<K, V> source = getSubjectGenerator().create(Helpers.mapEntry(null, v0()));
094    try {
095      multimap().putAll(source);
096      fail("Expected NullPointerException");
097    } catch (NullPointerException expected) {
098    }
099    expectUnchanged();
100  }
101
102  @MapFeature.Require(SUPPORTS_PUT)
103  public void testPutAllPropagatesToGet() {
104    Multimap<K, V> source =
105        getSubjectGenerator().create(Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k3(), v3()));
106    Collection<V> getCollection = multimap().get(k0());
107    int getCollectionSize = getCollection.size();
108    assertTrue(multimap().putAll(source));
109    assertEquals(getCollectionSize + 1, getCollection.size());
110    assertContains(getCollection, v3());
111  }
112}