1 /* 2 * Copyright (C) 2013 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect.testing.google; 18 19 import static com.google.common.collect.testing.Helpers.assertContains; 20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 22 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 23 24 import com.google.common.annotations.GwtCompatible; 25 import com.google.common.collect.Multimap; 26 import com.google.common.collect.testing.Helpers; 27 import com.google.common.collect.testing.features.MapFeature; 28 import java.util.Collection; 29 import org.junit.Ignore; 30 31 /** 32 * Tester for {@link Multimap#putAll(Multimap)}. 33 * 34 * @author Louis Wasserman 35 */ 36 @GwtCompatible 37 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 38 public class MultimapPutAllMultimapTester<K, V> 39 extends AbstractMultimapTester<K, V, Multimap<K, V>> { 40 @MapFeature.Require(absent = SUPPORTS_PUT) testPutUnsupported()41 public void testPutUnsupported() { 42 try { 43 multimap().putAll(getSubjectGenerator().create(Helpers.mapEntry(k3(), v3()))); 44 fail("Expected UnsupportedOperationException"); 45 } catch (UnsupportedOperationException expected) { 46 } 47 } 48 49 @MapFeature.Require(SUPPORTS_PUT) testPutAllIntoEmpty()50 public void testPutAllIntoEmpty() { 51 Multimap<K, V> target = getSubjectGenerator().create(); 52 assertEquals(!multimap().isEmpty(), target.putAll(multimap())); 53 assertEquals(multimap(), target); 54 } 55 56 @MapFeature.Require(SUPPORTS_PUT) testPutAll()57 public void testPutAll() { 58 Multimap<K, V> source = 59 getSubjectGenerator().create(Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k3(), v3())); 60 assertTrue(multimap().putAll(source)); 61 assertTrue(multimap().containsEntry(k0(), v3())); 62 assertTrue(multimap().containsEntry(k3(), v3())); 63 } 64 65 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) testPutAllWithNullValue()66 public void testPutAllWithNullValue() { 67 Multimap<K, V> source = getSubjectGenerator().create(Helpers.mapEntry(k0(), null)); 68 assertTrue(multimap().putAll(source)); 69 assertTrue(multimap().containsEntry(k0(), null)); 70 } 71 72 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) testPutAllWithNullKey()73 public void testPutAllWithNullKey() { 74 Multimap<K, V> source = getSubjectGenerator().create(Helpers.mapEntry(null, v0())); 75 assertTrue(multimap().putAll(source)); 76 assertTrue(multimap().containsEntry(null, v0())); 77 } 78 79 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) testPutAllRejectsNullValue()80 public void testPutAllRejectsNullValue() { 81 Multimap<K, V> source = getSubjectGenerator().create(Helpers.mapEntry(k0(), null)); 82 try { 83 multimap().putAll(source); 84 fail("Expected NullPointerException"); 85 } catch (NullPointerException expected) { 86 } 87 expectUnchanged(); 88 } 89 90 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS) testPutAllRejectsNullKey()91 public void testPutAllRejectsNullKey() { 92 Multimap<K, V> source = getSubjectGenerator().create(Helpers.mapEntry(null, v0())); 93 try { 94 multimap().putAll(source); 95 fail("Expected NullPointerException"); 96 } catch (NullPointerException expected) { 97 } 98 expectUnchanged(); 99 } 100 101 @MapFeature.Require(SUPPORTS_PUT) testPutAllPropagatesToGet()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 } 112